login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <template>
  2. <view class="content">
  3. <uni-icons class="setting" type="gear" @click="handleServerSettings" size="50" color="#fff"></uni-icons>
  4. <view class="login_logo">
  5. <!-- <image :src="logo || 'https://img.picgo.net/2024/04/18/login_logo45cb6a5eb95c8c1c.png'"></image> -->
  6. <view class="logo_bj">{{ logoName || '中赢' }}</view>
  7. </view>
  8. <view class="login-title">
  9. <label>工业互联网平台</label>
  10. <text>{{ companyName }}</text>
  11. </view>
  12. <view>
  13. <view class="login-content">
  14. <view>
  15. <view class="form">
  16. <view class="login-input">
  17. <label class="icon-lable"></label>
  18. <input name="input" v-model="userInfo.username" placeholder="请输入账号" />
  19. </view>
  20. <view class="login-input">
  21. <label class="icon-lable2"></label>
  22. <input class="" name="input" password v-model="userInfo.passwd" placeholder="请输入密码" />
  23. </view>
  24. <button @click="submit">登录</button>
  25. <view class="password-memo">
  26. <checkbox-group @change="checkboxChange">
  27. <label>
  28. <checkbox value="1" :checked="isMemo" />
  29. 记住密码
  30. </label>
  31. </checkbox-group>
  32. </view>
  33. </view>
  34. </view>
  35. </view>
  36. </view>
  37. <view class="version">
  38. <text>当前版本:{{ currentVersion }}</text>
  39. </view>
  40. <ServerSetting ref="serverSettingRef" @setServerStatus="setServerStatus" />
  41. </view>
  42. </template>
  43. <script>
  44. import {
  45. postJ
  46. } from '@/utils/api.js'
  47. import {
  48. login,
  49. usName,
  50. getLatestVersion
  51. } from '@/api/common.js'
  52. import {
  53. getResourcesTree
  54. } from '@/api/pda/common.js'
  55. import {
  56. setMemo,
  57. isMemo,
  58. removeMemo,
  59. setPassword,
  60. getPassword
  61. } from '@/utils/passwordMemo.js'
  62. import ServerSetting from '@/components/ServerSetting/index'
  63. export default {
  64. components: {
  65. ServerSetting
  66. },
  67. data() {
  68. return {
  69. currentVersion: '',
  70. userInfo: {
  71. username: '',
  72. passwd: ''
  73. },
  74. isMemo: isMemo(),
  75. loginDisabled: false,
  76. companyName: null,
  77. logo: null,
  78. logoName: ''
  79. }
  80. },
  81. onLoad() {
  82. const {
  83. appVersion
  84. } = uni.getAppBaseInfo()
  85. this.currentVersion = appVersion
  86. if (this.isMemo) {
  87. const userInfo = getPassword()
  88. if (userInfo?.username) this.userInfo = userInfo
  89. }
  90. this.getUsName()
  91. },
  92. mounted() {
  93. // #ifdef APP-PLUS
  94. this.$nextTick(() => {
  95. this.$refs.serverSettingRef && this.$refs.serverSettingRef.serverCheck()
  96. })
  97. // #endif
  98. // uni.reLaunch({
  99. // url: "/pages/home/home",
  100. // });
  101. },
  102. methods: {
  103. getUsName() {
  104. usName().then(res => {
  105. this.companyName = res.companyName
  106. this.logo = res.logo
  107. this.logoName = res.logoName
  108. })
  109. },
  110. setServerStatus(val) {
  111. this.loginDisabled = !val
  112. },
  113. //登录
  114. // submit() {
  115. // // #ifdef APP-PLUS
  116. // if (!this.apiUrl) {
  117. // this.$refs.serverSettingRef.open();
  118. // return;
  119. // }
  120. // // #endif
  121. // const params = {
  122. // loginName: this.userInfo.username,
  123. // loginPwd: this.userInfo.passwd,
  124. // };
  125. // login(params)
  126. // .then((res) => {
  127. // if (this.isMemo) {
  128. // setPassword(this.userInfo);
  129. // }
  130. // let data = res.data;
  131. // uni.setStorageSync("token", data.token);
  132. // uni.setStorageSync("userInfo", data);
  133. // uni.showToast({
  134. // title: "登录成功",
  135. // icon: "success",
  136. // duration: 2000,
  137. // });
  138. // setTimeout(() => {
  139. // uni.reLaunch({
  140. // url: "/pages/home/home",
  141. // });
  142. // }, 2000);
  143. // })
  144. // .catch((err) => {
  145. // console.log(err, 222);
  146. // uni.showToast({
  147. // title: err.message || err,
  148. // icon: "none",
  149. // duration: 2000,
  150. // });
  151. // });
  152. // },
  153. handlUpdate() {
  154. // #ifdef APP-PLUS
  155. this.checkVersionForLogin()
  156. // #endif
  157. },
  158. // 版本检查和更新逻辑(封装方法)
  159. async checkVersionForLogin() {
  160. // #ifndef APP-PLUS
  161. return 'h5_platform'; // H5场景直接返回
  162. // #endif
  163. // #ifdef APP-PLUS
  164. try {
  165. const versionInfo = await getLatestVersion()
  166. const { appVersion } = uni.getAppBaseInfo()
  167. // 已是最新版本
  168. if (appVersion === versionInfo.versionCode) {
  169. return 'latest_version'
  170. }
  171. // 发现新版本,显示更新提示
  172. const confirmUpdate = await this.showUpdateModal(versionInfo.releaseNotes, false)
  173. if (!confirmUpdate) {
  174. return 'skip_update'
  175. }
  176. // 执行下载和安装
  177. return await this.downloadAndInstall(versionInfo.fileStorePath, true)
  178. } catch (error) {
  179. console.error('版本检查失败:', error)
  180. return 'version_check_failed'
  181. }
  182. // #endif
  183. },
  184. // 显示更新确认弹窗(封装方法)
  185. showUpdateModal(releaseNotes, showCancel) {
  186. return new Promise((resolve) => {
  187. uni.showModal({
  188. title: '发现新版本',
  189. content: '更新说明:' + releaseNotes,
  190. confirmText: '立即更新',
  191. cancelText: showCancel ? '暂不更新' : '',
  192. showCancel,
  193. success: (res) => {
  194. resolve(res.confirm)
  195. },
  196. fail: () => resolve(false)
  197. })
  198. })
  199. },
  200. // 下载并安装更新(封装方法)
  201. downloadAndInstall(fileStorePath, showSuccessTip) {
  202. return new Promise((resolve, reject) => {
  203. const downloadUrl = `${Vue.prototype.webviewUrl}/kd-aiot/${fileStorePath}`
  204. const downloadTask = uni.downloadFile({
  205. url: downloadUrl,
  206. success: async (data) => {
  207. if (data.statusCode !== 200) {
  208. this.handleDownloadError('download_failed', reject)
  209. return
  210. }
  211. try {
  212. const savedFilePath = await this.saveDownloadedFile(data.tempFilePath)
  213. await this.installUpdate(savedFilePath, showSuccessTip, resolve, reject)
  214. } catch (error) {
  215. this.handleDownloadError('install_failed', reject)
  216. }
  217. }
  218. })
  219. // 显示下载进度
  220. this.showDownloadProgress(downloadTask)
  221. })
  222. },
  223. // 保存下载的文件(封装方法)
  224. saveDownloadedFile(tempFilePath) {
  225. return new Promise((resolve, reject) => {
  226. uni.saveFile({
  227. tempFilePath,
  228. success: (res) => resolve(res.savedFilePath),
  229. fail: (err) => reject(err)
  230. })
  231. })
  232. },
  233. // 安装更新(封装方法)
  234. installUpdate(filePath, showSuccessTip, resolve, reject) {
  235. plus.runtime.install(
  236. filePath,
  237. { force: true },
  238. () => {
  239. if (showSuccessTip) {
  240. uni.showToast({
  241. title: '更新成功,应用即将重启',
  242. icon: 'success',
  243. duration: 2000
  244. })
  245. }
  246. resolve('updated')
  247. },
  248. () => this.handleDownloadError('install_failed', reject)
  249. )
  250. },
  251. // 显示下载进度(封装方法)
  252. showDownloadProgress(downloadTask) {
  253. let lastProgress = 0
  254. let loadingVisible = true
  255. uni.showLoading({
  256. title: '正在下载安装包: 0%',
  257. mask: true
  258. })
  259. downloadTask.onProgressUpdate((res) => {
  260. // 只在进度有明显变化(每10%)或达到100%时更新提示
  261. if ((res.progress > lastProgress && res.progress % 10 === 0) || res.progress === 100) {
  262. if (loadingVisible) {
  263. uni.hideLoading()
  264. }
  265. if (res.progress < 100) {
  266. uni.showLoading({
  267. title: `正在下载安装包: ${(res.progress > lastProgress ? res.progress : lastProgress)}%`,
  268. mask: true
  269. })
  270. loadingVisible = true
  271. } else {
  272. loadingVisible = false
  273. }
  274. lastProgress = res.progress > lastProgress ? res.progress : lastProgress
  275. }
  276. })
  277. // 确保任务完成时隐藏loading
  278. downloadTask.onStop(() => this.hideLoadingIfNeeded(loadingVisible))
  279. downloadTask.onError(() => {
  280. this.hideLoadingIfNeeded(loadingVisible)
  281. })
  282. },
  283. // 隐藏loading(封装方法)
  284. hideLoadingIfNeeded(loadingVisible) {
  285. if (loadingVisible) {
  286. uni.hideLoading()
  287. }
  288. },
  289. // 处理下载错误(封装方法)
  290. handleDownloadError(errorType, reject) {
  291. this.hideLoadingIfNeeded(true)
  292. uni.showToast({
  293. title: '更新失败',
  294. icon: 'none'
  295. })
  296. if (reject) reject(errorType)
  297. },
  298. // 登录方法
  299. async submit() {
  300. // #ifdef APP-PLUS
  301. if (!this.apiUrl) {
  302. this.$refs.serverSettingRef.open()
  303. return
  304. }
  305. // 检查版本更新
  306. const updateResult = await this.checkVersionForLogin()
  307. console.log('版本检查结果:', updateResult)
  308. // 更新成功时停止登录流程(应用即将重启)
  309. if (updateResult === 'updated') return
  310. // #endif
  311. // 执行登录
  312. await this.performLogin()
  313. },
  314. // 执行登录逻辑(封装方法)
  315. async performLogin() {
  316. const params = {
  317. loginName: this.userInfo.username,
  318. loginPwd: this.userInfo.passwd
  319. }
  320. try {
  321. const res = await postJ(this.apiUrl + '/main/user/login', params)
  322. // 记住密码
  323. if (this.isMemo) {
  324. setPassword(this.userInfo)
  325. }
  326. // 保存用户信息
  327. const data = res.data
  328. uni.setStorageSync('token', data.token)
  329. uni.setStorageSync('userInfo', data)
  330. // 获取权限树
  331. await this.getTree()
  332. // 显示成功提示并跳转
  333. uni.showToast({
  334. title: '登录成功',
  335. icon: 'success',
  336. duration: 2000
  337. })
  338. setTimeout(() => {
  339. uni.reLaunch({
  340. url: '/pages/home/home'
  341. })
  342. }, 2000)
  343. } catch (error) {
  344. uni.showToast({
  345. title: error.message || '登录失败',
  346. icon: 'none',
  347. duration: 2000
  348. })
  349. }
  350. },
  351. handleServerSettings() {
  352. this.$refs.serverSettingRef.open()
  353. },
  354. checkboxChange(e) {
  355. this.isMemo = e.detail.value[0] === '1'
  356. if (this.isMemo) {
  357. setMemo()
  358. } else {
  359. removeMemo()
  360. }
  361. },
  362. formatRouter(list) {
  363. let authorities = [];
  364. const fn = (list) => {
  365. let arr = [];
  366. for (const p of list) {
  367. if (p.menuType === 2) {
  368. // p.children = [];
  369. authorities.push(p);
  370. }
  371. // else {
  372. if (p.children?.length) {
  373. p.children = fn(p.children);
  374. } else {
  375. p.children = [];
  376. }
  377. arr.push(p);
  378. // }
  379. }
  380. return arr;
  381. };
  382. fn(list);
  383. return authorities
  384. },
  385. getTree() {
  386. getResourcesTree().then(res => {
  387. console.log(res)
  388. if (res.length == 0) {
  389. uni.showToast({
  390. title: '您还未配置权限',
  391. icon: 'none'
  392. })
  393. }
  394. let List = JSON.stringify(res || [])
  395. let authorities =this.formatRouter(res[0].children)
  396. uni.setStorageSync('treeList', List)
  397. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  398. })
  399. }
  400. }
  401. }
  402. </script>
  403. <style lang="scss" scoped>
  404. @import 'login.scss';
  405. .version {
  406. font-size: 12px;
  407. color: #999;
  408. position: absolute;
  409. bottom: 10rpx;
  410. text-align: center;
  411. width: 100%;
  412. }
  413. </style>