login.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. <!-- 下载进度弹窗 -->
  42. <view class="download-modal" v-if="downloadVisible" @touchmove.stop.prevent>
  43. <view class="download-mask"></view>
  44. <view class="download-dialog">
  45. <view class="download-title">正在下载安装包</view>
  46. <view class="progress-bar-wrapper">
  47. <view class="progress-bar-bg">
  48. <view class="progress-bar-fill" :style="{ width: downloadProgress + '%' }"></view>
  49. </view>
  50. <text class="progress-text">{{ downloadProgress }}%</text>
  51. </view>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. import {
  58. postJ
  59. } from '@/utils/api.js'
  60. import {
  61. login,
  62. usName,
  63. getLatestVersion
  64. } from '@/api/common.js'
  65. import {
  66. getResourcesTree
  67. } from '@/api/pda/common.js'
  68. import {
  69. setMemo,
  70. isMemo,
  71. removeMemo,
  72. setPassword,
  73. getPassword
  74. } from '@/utils/passwordMemo.js'
  75. import ServerSetting from '@/components/ServerSetting/index'
  76. export default {
  77. components: {
  78. ServerSetting
  79. },
  80. data() {
  81. return {
  82. currentVersion: '',
  83. userInfo: {
  84. username: '',
  85. passwd: ''
  86. },
  87. isMemo: isMemo(),
  88. loginDisabled: false,
  89. companyName: null,
  90. logo: null,
  91. logoName: '',
  92. downloadVisible: false,
  93. downloadProgress: 0
  94. }
  95. },
  96. onLoad() {
  97. const {
  98. appVersion
  99. } = uni.getAppBaseInfo()
  100. this.currentVersion = appVersion
  101. if (this.isMemo) {
  102. const userInfo = getPassword()
  103. if (userInfo?.username) this.userInfo = userInfo
  104. }
  105. this.getUsName()
  106. // 进入登录页自动检测版本更新
  107. this.checkVersionForLogin()
  108. },
  109. mounted() {
  110. // #ifdef APP-PLUS
  111. this.$nextTick(() => {
  112. this.$refs.serverSettingRef && this.$refs.serverSettingRef.serverCheck()
  113. })
  114. // #endif
  115. // uni.reLaunch({
  116. // url: "/pages/home/home",
  117. // });
  118. },
  119. methods: {
  120. getUsName() {
  121. usName().then(res => {
  122. this.companyName = res.companyName
  123. this.logo = res.logo
  124. this.logoName = res.logoName
  125. })
  126. },
  127. setServerStatus(val) {
  128. this.loginDisabled = !val
  129. },
  130. //登录
  131. // submit() {
  132. // // #ifdef APP-PLUS
  133. // if (!this.apiUrl) {
  134. // this.$refs.serverSettingRef.open();
  135. // return;
  136. // }
  137. // // #endif
  138. // const params = {
  139. // loginName: this.userInfo.username,
  140. // loginPwd: this.userInfo.passwd,
  141. // };
  142. // login(params)
  143. // .then((res) => {
  144. // if (this.isMemo) {
  145. // setPassword(this.userInfo);
  146. // }
  147. // let data = res.data;
  148. // uni.setStorageSync("token", data.token);
  149. // uni.setStorageSync("userInfo", data);
  150. // uni.showToast({
  151. // title: "登录成功",
  152. // icon: "success",
  153. // duration: 2000,
  154. // });
  155. // setTimeout(() => {
  156. // uni.reLaunch({
  157. // url: "/pages/home/home",
  158. // });
  159. // }, 2000);
  160. // })
  161. // .catch((err) => {
  162. // console.log(err, 222);
  163. // uni.showToast({
  164. // title: err.message || err,
  165. // icon: "none",
  166. // duration: 2000,
  167. // });
  168. // });
  169. // },
  170. handlUpdate() {
  171. // #ifdef APP-PLUS
  172. this.checkVersionForLogin()
  173. // #endif
  174. },
  175. // 版本检查和更新逻辑(封装方法)
  176. async checkVersionForLogin() {
  177. // #ifndef APP-PLUS
  178. return 'h5_platform'; // H5场景直接返回
  179. // #endif
  180. // #ifdef APP-PLUS
  181. try {
  182. const versionInfo = await getLatestVersion()
  183. const { appVersion } = uni.getAppBaseInfo()
  184. // 已是最新版本
  185. if (appVersion === versionInfo.versionCode) {
  186. return 'latest_version'
  187. }
  188. // 发现新版本,显示更新提示
  189. const confirmUpdate = await this.showUpdateModal(versionInfo.releaseNotes, false)
  190. if (!confirmUpdate) {
  191. return 'skip_update'
  192. }
  193. // 执行下载和安装
  194. return await this.downloadAndInstall(versionInfo.fileStorePath, true)
  195. } catch (error) {
  196. console.error('版本检查失败:', error)
  197. return 'version_check_failed'
  198. }
  199. // #endif
  200. },
  201. // 显示更新确认弹窗(封装方法)
  202. showUpdateModal(releaseNotes, showCancel) {
  203. return new Promise((resolve) => {
  204. uni.showModal({
  205. title: '发现新版本',
  206. content: '更新说明:' + releaseNotes,
  207. confirmText: '立即更新',
  208. cancelText: showCancel ? '暂不更新' : '',
  209. showCancel,
  210. success: (res) => {
  211. resolve(res.confirm)
  212. },
  213. fail: () => resolve(false)
  214. })
  215. })
  216. },
  217. // 下载并安装更新(封装方法)
  218. downloadAndInstall(fileStorePath, showSuccessTip) {
  219. return new Promise((resolve, reject) => {
  220. const downloadUrl = `${Vue.prototype.webviewUrl}/kd-aiot/${fileStorePath}`
  221. const downloadTask = uni.downloadFile({
  222. url: downloadUrl,
  223. success: async (data) => {
  224. if (data.statusCode !== 200) {
  225. this.handleDownloadError('download_failed', reject)
  226. return
  227. }
  228. try {
  229. const savedFilePath = await this.saveDownloadedFile(data.tempFilePath)
  230. await this.installUpdate(savedFilePath, showSuccessTip, resolve, reject)
  231. } catch (error) {
  232. this.handleDownloadError('install_failed', reject)
  233. }
  234. }
  235. })
  236. // 显示下载进度
  237. this.showDownloadProgress(downloadTask)
  238. })
  239. },
  240. // 保存下载的文件(封装方法)
  241. saveDownloadedFile(tempFilePath) {
  242. return new Promise((resolve, reject) => {
  243. uni.saveFile({
  244. tempFilePath,
  245. success: (res) => resolve(res.savedFilePath),
  246. fail: (err) => reject(err)
  247. })
  248. })
  249. },
  250. // 安装更新(封装方法)
  251. installUpdate(filePath, showSuccessTip, resolve, reject) {
  252. plus.runtime.install(
  253. filePath,
  254. { force: true },
  255. () => {
  256. if (showSuccessTip) {
  257. uni.showToast({
  258. title: '更新成功,应用即将重启',
  259. icon: 'success',
  260. duration: 2000
  261. })
  262. }
  263. resolve('updated')
  264. },
  265. () => this.handleDownloadError('install_failed', reject)
  266. )
  267. },
  268. // 显示下载进度(封装方法)
  269. showDownloadProgress(downloadTask) {
  270. this.downloadVisible = true
  271. this.downloadProgress = 0
  272. downloadTask.onProgressUpdate((res) => {
  273. this.downloadProgress = res.progress
  274. if (res.progress >= 100) {
  275. this.downloadVisible = false
  276. }
  277. })
  278. // 确保任务异常时关闭弹窗
  279. if (downloadTask && typeof downloadTask.onStop === 'function') {
  280. downloadTask.onStop(() => { this.downloadVisible = false })
  281. }
  282. if (downloadTask && typeof downloadTask.onError === 'function') {
  283. downloadTask.onError(() => { this.downloadVisible = false })
  284. }
  285. },
  286. // 处理下载错误(封装方法)
  287. handleDownloadError(errorType, reject) {
  288. this.downloadVisible = false
  289. uni.showToast({
  290. title: '更新失败',
  291. icon: 'none'
  292. })
  293. if (reject) reject(errorType)
  294. },
  295. // 登录方法
  296. async submit() {
  297. // #ifdef APP-PLUS
  298. if (!this.apiUrl) {
  299. this.$refs.serverSettingRef.open()
  300. return
  301. }
  302. // 检查版本更新
  303. const updateResult = await this.checkVersionForLogin()
  304. console.log('版本检查结果:', updateResult)
  305. // 更新成功时停止登录流程(应用即将重启)
  306. if (updateResult === 'updated') return
  307. // #endif
  308. // 执行登录
  309. await this.performLogin()
  310. },
  311. // 执行登录逻辑(封装方法)
  312. async performLogin() {
  313. const params = {
  314. loginName: this.userInfo.username,
  315. loginPwd: this.userInfo.passwd
  316. }
  317. try {
  318. const res = await postJ(this.apiUrl + '/main/user/login', params)
  319. // 记住密码
  320. if (this.isMemo) {
  321. setPassword(this.userInfo)
  322. }
  323. // 保存用户信息
  324. const data = res.data
  325. // 与 PC setCurrentUser 对齐:登录后把「当前机构 / 当前角色」落到缓存里。
  326. // 接口未回传时取 loginChangeGroupVOList 第一条(用户所属多个机构默认第一个),
  327. // 供 utils/hr/current-org.js 的 getCurrentOrgId() 兜底使用。
  328. const firstGroup = Array.isArray(data.loginChangeGroupVOList) ? data.loginChangeGroupVOList[0] : null
  329. const firstRole = firstGroup && Array.isArray(firstGroup.loginChangeRoleVOList)
  330. ? firstGroup.loginChangeRoleVOList[0]
  331. : null
  332. if (data.currentGroupId == null && firstGroup && firstGroup.groupId != null) {
  333. data.currentGroupId = firstGroup.groupId
  334. }
  335. if (data.currentRoleId == null && firstRole && firstRole.roleId != null) {
  336. data.currentRoleId = firstRole.roleId
  337. }
  338. uni.setStorageSync('token', data.token)
  339. uni.setStorageSync('userInfo', data)
  340. // 获取权限树
  341. await this.getTree()
  342. // 显示成功提示并跳转
  343. uni.showToast({
  344. title: '登录成功',
  345. icon: 'success',
  346. duration: 2000
  347. })
  348. setTimeout(() => {
  349. uni.reLaunch({
  350. url: '/pages/home/home'
  351. })
  352. }, 2000)
  353. } catch (error) {
  354. uni.showToast({
  355. title: error.message || '登录失败',
  356. icon: 'none',
  357. duration: 2000
  358. })
  359. }
  360. },
  361. handleServerSettings() {
  362. this.$refs.serverSettingRef.open()
  363. },
  364. checkboxChange(e) {
  365. this.isMemo = e.detail.value[0] === '1'
  366. if (this.isMemo) {
  367. setMemo()
  368. } else {
  369. removeMemo()
  370. }
  371. },
  372. formatRouter(list) {
  373. let authorities = [];
  374. const fn = (list) => {
  375. let arr = [];
  376. for (const p of list) {
  377. if (p.menuType === 2) {
  378. // p.children = [];
  379. authorities.push(p);
  380. }
  381. // else {
  382. if (p.children?.length) {
  383. p.children = fn(p.children);
  384. } else {
  385. p.children = [];
  386. }
  387. arr.push(p);
  388. // }
  389. }
  390. return arr;
  391. };
  392. fn(list);
  393. return authorities
  394. },
  395. getTree() {
  396. return getResourcesTree().then(res => {
  397. console.log(res)
  398. if (!Array.isArray(res) || res.length == 0) {
  399. uni.showToast({
  400. title: '您还未配置权限',
  401. icon: 'none'
  402. })
  403. // 仍然写入空菜单缓存:否则工作中心页读不到 treeList,
  404. // JSON.parse("") 会抛 SyntaxError: Unexpected end of JSON input
  405. uni.setStorageSync('treeList', '[]')
  406. uni.setStorageSync('authorities', '[]')
  407. return
  408. }
  409. let List = JSON.stringify(res)
  410. let authorities = this.formatRouter(res[0].children || [])
  411. uni.setStorageSync('treeList', List)
  412. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  413. })
  414. }
  415. }
  416. }
  417. </script>
  418. <style lang="scss" scoped>
  419. @import 'login.scss';
  420. .version {
  421. font-size: 12px;
  422. color: #999;
  423. position: absolute;
  424. bottom: 10rpx;
  425. text-align: center;
  426. width: 100%;
  427. }
  428. .download-modal {
  429. position: fixed;
  430. top: 0;
  431. left: 0;
  432. right: 0;
  433. bottom: 0;
  434. z-index: 9999;
  435. display: flex;
  436. align-items: center;
  437. justify-content: center;
  438. .download-mask {
  439. position: absolute;
  440. top: 0;
  441. left: 0;
  442. right: 0;
  443. bottom: 0;
  444. background: rgba(0, 0, 0, 0.5);
  445. }
  446. .download-dialog {
  447. position: relative;
  448. width: 560rpx;
  449. background: #fff;
  450. border-radius: 20rpx;
  451. padding: 50rpx 40rpx 40rpx;
  452. box-sizing: border-box;
  453. .download-title {
  454. font-size: 32rpx;
  455. font-weight: bold;
  456. color: #333;
  457. text-align: center;
  458. margin-bottom: 40rpx;
  459. }
  460. .progress-bar-wrapper {
  461. display: flex;
  462. align-items: center;
  463. gap: 20rpx;
  464. .progress-bar-bg {
  465. flex: 1;
  466. height: 20rpx;
  467. background: #e8e8e8;
  468. border-radius: 10rpx;
  469. overflow: hidden;
  470. .progress-bar-fill {
  471. height: 100%;
  472. background: linear-gradient(90deg, #4a90e2, #357abd);
  473. border-radius: 10rpx;
  474. transition: width 0.3s ease;
  475. }
  476. }
  477. .progress-text {
  478. font-size: 28rpx;
  479. color: #666;
  480. min-width: 80rpx;
  481. text-align: right;
  482. }
  483. }
  484. }
  485. }
  486. </style>