login.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. return
  178. // #ifndef APP-PLUS
  179. return 'h5_platform'; // H5场景直接返回
  180. // #endif
  181. // #ifdef APP-PLUS
  182. try {
  183. const versionInfo = await getLatestVersion()
  184. const { appVersion } = uni.getAppBaseInfo()
  185. // 已是最新版本
  186. if (appVersion === versionInfo.versionCode) {
  187. return 'latest_version'
  188. }
  189. // 发现新版本,显示更新提示
  190. const confirmUpdate = await this.showUpdateModal(versionInfo.releaseNotes, false)
  191. if (!confirmUpdate) {
  192. return 'skip_update'
  193. }
  194. // 执行下载和安装
  195. return await this.downloadAndInstall(versionInfo.fileStorePath, true)
  196. } catch (error) {
  197. console.error('版本检查失败:', error)
  198. return 'version_check_failed'
  199. }
  200. // #endif
  201. },
  202. // 显示更新确认弹窗(封装方法)
  203. showUpdateModal(releaseNotes, showCancel) {
  204. return new Promise((resolve) => {
  205. uni.showModal({
  206. title: '发现新版本',
  207. content: '更新说明:' + releaseNotes,
  208. confirmText: '立即更新',
  209. cancelText: showCancel ? '暂不更新' : '',
  210. showCancel,
  211. success: (res) => {
  212. resolve(res.confirm)
  213. },
  214. fail: () => resolve(false)
  215. })
  216. })
  217. },
  218. // 下载并安装更新(封装方法)
  219. downloadAndInstall(fileStorePath, showSuccessTip) {
  220. return new Promise((resolve, reject) => {
  221. const downloadUrl = `${Vue.prototype.webviewUrl}/kd-aiot/${fileStorePath}`
  222. const downloadTask = uni.downloadFile({
  223. url: downloadUrl,
  224. success: async (data) => {
  225. if (data.statusCode !== 200) {
  226. this.handleDownloadError('download_failed', reject)
  227. return
  228. }
  229. try {
  230. const savedFilePath = await this.saveDownloadedFile(data.tempFilePath)
  231. await this.installUpdate(savedFilePath, showSuccessTip, resolve, reject)
  232. } catch (error) {
  233. this.handleDownloadError('install_failed', reject)
  234. }
  235. }
  236. })
  237. // 显示下载进度
  238. this.showDownloadProgress(downloadTask)
  239. })
  240. },
  241. // 保存下载的文件(封装方法)
  242. saveDownloadedFile(tempFilePath) {
  243. return new Promise((resolve, reject) => {
  244. uni.saveFile({
  245. tempFilePath,
  246. success: (res) => resolve(res.savedFilePath),
  247. fail: (err) => reject(err)
  248. })
  249. })
  250. },
  251. // 安装更新(封装方法)
  252. installUpdate(filePath, showSuccessTip, resolve, reject) {
  253. plus.runtime.install(
  254. filePath,
  255. { force: true },
  256. () => {
  257. if (showSuccessTip) {
  258. uni.showToast({
  259. title: '更新成功,应用即将重启',
  260. icon: 'success',
  261. duration: 2000
  262. })
  263. }
  264. resolve('updated')
  265. },
  266. () => this.handleDownloadError('install_failed', reject)
  267. )
  268. },
  269. // 显示下载进度(封装方法)
  270. showDownloadProgress(downloadTask) {
  271. this.downloadVisible = true
  272. this.downloadProgress = 0
  273. downloadTask.onProgressUpdate((res) => {
  274. this.downloadProgress = res.progress
  275. if (res.progress >= 100) {
  276. this.downloadVisible = false
  277. }
  278. })
  279. // 确保任务异常时关闭弹窗
  280. if (downloadTask && typeof downloadTask.onStop === 'function') {
  281. downloadTask.onStop(() => { this.downloadVisible = false })
  282. }
  283. if (downloadTask && typeof downloadTask.onError === 'function') {
  284. downloadTask.onError(() => { this.downloadVisible = false })
  285. }
  286. },
  287. // 处理下载错误(封装方法)
  288. handleDownloadError(errorType, reject) {
  289. this.downloadVisible = false
  290. uni.showToast({
  291. title: '更新失败',
  292. icon: 'none'
  293. })
  294. if (reject) reject(errorType)
  295. },
  296. // 登录方法
  297. async submit() {
  298. // #ifdef APP-PLUS
  299. if (!this.apiUrl) {
  300. this.$refs.serverSettingRef.open()
  301. return
  302. }
  303. // 检查版本更新
  304. const updateResult = await this.checkVersionForLogin()
  305. console.log('版本检查结果:', updateResult)
  306. // 更新成功时停止登录流程(应用即将重启)
  307. if (updateResult === 'updated') return
  308. // #endif
  309. // 执行登录
  310. await this.performLogin()
  311. },
  312. // 执行登录逻辑(封装方法)
  313. async performLogin() {
  314. const params = {
  315. loginName: this.userInfo.username,
  316. loginPwd: this.userInfo.passwd
  317. }
  318. try {
  319. const res = await postJ(this.apiUrl + '/main/user/login', params)
  320. // 记住密码
  321. if (this.isMemo) {
  322. setPassword(this.userInfo)
  323. }
  324. // 保存用户信息
  325. const data = res.data
  326. uni.setStorageSync('token', data.token)
  327. uni.setStorageSync('userInfo', data)
  328. // 获取权限树
  329. await this.getTree()
  330. // 显示成功提示并跳转
  331. uni.showToast({
  332. title: '登录成功',
  333. icon: 'success',
  334. duration: 2000
  335. })
  336. setTimeout(() => {
  337. uni.reLaunch({
  338. url: '/pages/home/home'
  339. })
  340. }, 2000)
  341. } catch (error) {
  342. uni.showToast({
  343. title: error.message || '登录失败',
  344. icon: 'none',
  345. duration: 2000
  346. })
  347. }
  348. },
  349. handleServerSettings() {
  350. this.$refs.serverSettingRef.open()
  351. },
  352. checkboxChange(e) {
  353. this.isMemo = e.detail.value[0] === '1'
  354. if (this.isMemo) {
  355. setMemo()
  356. } else {
  357. removeMemo()
  358. }
  359. },
  360. formatRouter(list) {
  361. let authorities = [];
  362. const fn = (list) => {
  363. let arr = [];
  364. for (const p of list) {
  365. if (p.menuType === 2) {
  366. // p.children = [];
  367. authorities.push(p);
  368. }
  369. // else {
  370. if (p.children?.length) {
  371. p.children = fn(p.children);
  372. } else {
  373. p.children = [];
  374. }
  375. arr.push(p);
  376. // }
  377. }
  378. return arr;
  379. };
  380. fn(list);
  381. return authorities
  382. },
  383. getTree() {
  384. getResourcesTree().then(res => {
  385. console.log(res)
  386. if (res.length == 0) {
  387. uni.showToast({
  388. title: '您还未配置权限',
  389. icon: 'none'
  390. })
  391. }
  392. let List = JSON.stringify(res || [])
  393. let authorities =this.formatRouter(res[0].children)
  394. uni.setStorageSync('treeList', List)
  395. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  396. })
  397. }
  398. }
  399. }
  400. </script>
  401. <style lang="scss" scoped>
  402. @import 'login.scss';
  403. .version {
  404. font-size: 12px;
  405. color: #999;
  406. position: absolute;
  407. bottom: 10rpx;
  408. text-align: center;
  409. width: 100%;
  410. }
  411. .download-modal {
  412. position: fixed;
  413. top: 0;
  414. left: 0;
  415. right: 0;
  416. bottom: 0;
  417. z-index: 9999;
  418. display: flex;
  419. align-items: center;
  420. justify-content: center;
  421. .download-mask {
  422. position: absolute;
  423. top: 0;
  424. left: 0;
  425. right: 0;
  426. bottom: 0;
  427. background: rgba(0, 0, 0, 0.5);
  428. }
  429. .download-dialog {
  430. position: relative;
  431. width: 560rpx;
  432. background: #fff;
  433. border-radius: 20rpx;
  434. padding: 50rpx 40rpx 40rpx;
  435. box-sizing: border-box;
  436. .download-title {
  437. font-size: 32rpx;
  438. font-weight: bold;
  439. color: #333;
  440. text-align: center;
  441. margin-bottom: 40rpx;
  442. }
  443. .progress-bar-wrapper {
  444. display: flex;
  445. align-items: center;
  446. gap: 20rpx;
  447. .progress-bar-bg {
  448. flex: 1;
  449. height: 20rpx;
  450. background: #e8e8e8;
  451. border-radius: 10rpx;
  452. overflow: hidden;
  453. .progress-bar-fill {
  454. height: 100%;
  455. background: linear-gradient(90deg, #4a90e2, #357abd);
  456. border-radius: 10rpx;
  457. transition: width 0.3s ease;
  458. }
  459. }
  460. .progress-text {
  461. font-size: 28rpx;
  462. color: #666;
  463. min-width: 80rpx;
  464. text-align: right;
  465. }
  466. }
  467. }
  468. }
  469. </style>