login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. if (downloadTask && typeof downloadTask.onStop === 'function') {
  279. downloadTask.onStop(() => this.hideLoadingIfNeeded(loadingVisible))
  280. }
  281. if (downloadTask && typeof downloadTask.onError === 'function') {
  282. downloadTask.onError(() => {
  283. this.hideLoadingIfNeeded(loadingVisible)
  284. })
  285. }
  286. },
  287. // 隐藏loading(封装方法)
  288. hideLoadingIfNeeded(loadingVisible) {
  289. if (loadingVisible) {
  290. uni.hideLoading()
  291. }
  292. },
  293. // 处理下载错误(封装方法)
  294. handleDownloadError(errorType, reject) {
  295. this.hideLoadingIfNeeded(true)
  296. uni.showToast({
  297. title: '更新失败',
  298. icon: 'none'
  299. })
  300. if (reject) reject(errorType)
  301. },
  302. // 登录方法
  303. async submit() {
  304. // #ifdef APP-PLUS
  305. if (!this.apiUrl) {
  306. this.$refs.serverSettingRef.open()
  307. return
  308. }
  309. // 检查版本更新
  310. const updateResult = await this.checkVersionForLogin()
  311. console.log('版本检查结果:', updateResult)
  312. // 更新成功时停止登录流程(应用即将重启)
  313. if (updateResult === 'updated') return
  314. // #endif
  315. // 执行登录
  316. await this.performLogin()
  317. },
  318. // 执行登录逻辑(封装方法)
  319. async performLogin() {
  320. const params = {
  321. loginName: this.userInfo.username,
  322. loginPwd: this.userInfo.passwd
  323. }
  324. try {
  325. const res = await postJ(this.apiUrl + '/main/user/login', params)
  326. // 记住密码
  327. if (this.isMemo) {
  328. setPassword(this.userInfo)
  329. }
  330. // 保存用户信息
  331. const data = res.data
  332. uni.setStorageSync('token', data.token)
  333. uni.setStorageSync('userInfo', data)
  334. // 获取权限树
  335. await this.getTree()
  336. // 显示成功提示并跳转
  337. uni.showToast({
  338. title: '登录成功',
  339. icon: 'success',
  340. duration: 2000
  341. })
  342. setTimeout(() => {
  343. uni.reLaunch({
  344. url: '/pages/home/home'
  345. })
  346. }, 2000)
  347. } catch (error) {
  348. uni.showToast({
  349. title: error.message || '登录失败',
  350. icon: 'none',
  351. duration: 2000
  352. })
  353. }
  354. },
  355. handleServerSettings() {
  356. this.$refs.serverSettingRef.open()
  357. },
  358. checkboxChange(e) {
  359. this.isMemo = e.detail.value[0] === '1'
  360. if (this.isMemo) {
  361. setMemo()
  362. } else {
  363. removeMemo()
  364. }
  365. },
  366. formatRouter(list) {
  367. let authorities = [];
  368. const fn = (list) => {
  369. let arr = [];
  370. for (const p of list) {
  371. if (p.menuType === 2) {
  372. // p.children = [];
  373. authorities.push(p);
  374. }
  375. // else {
  376. if (p.children?.length) {
  377. p.children = fn(p.children);
  378. } else {
  379. p.children = [];
  380. }
  381. arr.push(p);
  382. // }
  383. }
  384. return arr;
  385. };
  386. fn(list);
  387. return authorities
  388. },
  389. getTree() {
  390. getResourcesTree().then(res => {
  391. console.log(res)
  392. if (res.length == 0) {
  393. uni.showToast({
  394. title: '您还未配置权限',
  395. icon: 'none'
  396. })
  397. }
  398. let List = JSON.stringify(res || [])
  399. let authorities =this.formatRouter(res[0].children)
  400. uni.setStorageSync('treeList', List)
  401. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  402. })
  403. }
  404. }
  405. }
  406. </script>
  407. <style lang="scss" scoped>
  408. @import 'login.scss';
  409. .version {
  410. font-size: 12px;
  411. color: #999;
  412. position: absolute;
  413. bottom: 10rpx;
  414. text-align: center;
  415. width: 100%;
  416. }
  417. </style>