login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. uni.setStorageSync('token', data.token)
  326. uni.setStorageSync('userInfo', data)
  327. // 获取权限树
  328. await this.getTree()
  329. // 显示成功提示并跳转
  330. uni.showToast({
  331. title: '登录成功',
  332. icon: 'success',
  333. duration: 2000
  334. })
  335. setTimeout(() => {
  336. uni.reLaunch({
  337. url: '/pages/home/home'
  338. })
  339. }, 2000)
  340. } catch (error) {
  341. uni.showToast({
  342. title: error.message || '登录失败',
  343. icon: 'none',
  344. duration: 2000
  345. })
  346. }
  347. },
  348. handleServerSettings() {
  349. this.$refs.serverSettingRef.open()
  350. },
  351. checkboxChange(e) {
  352. this.isMemo = e.detail.value[0] === '1'
  353. if (this.isMemo) {
  354. setMemo()
  355. } else {
  356. removeMemo()
  357. }
  358. },
  359. formatRouter(list) {
  360. let authorities = [];
  361. const fn = (list) => {
  362. let arr = [];
  363. for (const p of list) {
  364. if (p.menuType === 2) {
  365. // p.children = [];
  366. authorities.push(p);
  367. }
  368. // else {
  369. if (p.children?.length) {
  370. p.children = fn(p.children);
  371. } else {
  372. p.children = [];
  373. }
  374. arr.push(p);
  375. // }
  376. }
  377. return arr;
  378. };
  379. fn(list);
  380. return authorities
  381. },
  382. getTree() {
  383. getResourcesTree().then(res => {
  384. console.log(res)
  385. if (res.length == 0) {
  386. uni.showToast({
  387. title: '您还未配置权限',
  388. icon: 'none'
  389. })
  390. }
  391. let List = JSON.stringify(res || [])
  392. let authorities =this.formatRouter(res[0].children)
  393. uni.setStorageSync('treeList', List)
  394. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  395. })
  396. }
  397. }
  398. }
  399. </script>
  400. <style lang="scss" scoped>
  401. @import 'login.scss';
  402. .version {
  403. font-size: 12px;
  404. color: #999;
  405. position: absolute;
  406. bottom: 10rpx;
  407. text-align: center;
  408. width: 100%;
  409. }
  410. .download-modal {
  411. position: fixed;
  412. top: 0;
  413. left: 0;
  414. right: 0;
  415. bottom: 0;
  416. z-index: 9999;
  417. display: flex;
  418. align-items: center;
  419. justify-content: center;
  420. .download-mask {
  421. position: absolute;
  422. top: 0;
  423. left: 0;
  424. right: 0;
  425. bottom: 0;
  426. background: rgba(0, 0, 0, 0.5);
  427. }
  428. .download-dialog {
  429. position: relative;
  430. width: 560rpx;
  431. background: #fff;
  432. border-radius: 20rpx;
  433. padding: 50rpx 40rpx 40rpx;
  434. box-sizing: border-box;
  435. .download-title {
  436. font-size: 32rpx;
  437. font-weight: bold;
  438. color: #333;
  439. text-align: center;
  440. margin-bottom: 40rpx;
  441. }
  442. .progress-bar-wrapper {
  443. display: flex;
  444. align-items: center;
  445. gap: 20rpx;
  446. .progress-bar-bg {
  447. flex: 1;
  448. height: 20rpx;
  449. background: #e8e8e8;
  450. border-radius: 10rpx;
  451. overflow: hidden;
  452. .progress-bar-fill {
  453. height: 100%;
  454. background: linear-gradient(90deg, #4a90e2, #357abd);
  455. border-radius: 10rpx;
  456. transition: width 0.3s ease;
  457. }
  458. }
  459. .progress-text {
  460. font-size: 28rpx;
  461. color: #666;
  462. min-width: 80rpx;
  463. text-align: right;
  464. }
  465. }
  466. }
  467. }
  468. </style>