login copy.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. getLatestVersion().then(res => {
  156. console.log('res:', res)
  157. const {
  158. appVersion
  159. } = uni.getAppBaseInfo()
  160. if (appVersion != res.versionCode) {
  161. uni.showModal({
  162. title: '发现新版本',
  163. content: '更新说明:' + res.releaseNotes,
  164. confirmText: '立即更新',
  165. showCancel: false,
  166. success: (val) => {
  167. if (val.confirm) {
  168. const downloadTask = uni.downloadFile({
  169. url: `${Vue.prototype.webviewUrl}/kd-aiot/${res.fileStorePath}`,
  170. success: (data) => {
  171. if (data.statusCode === 200) {
  172. uni.saveFile({
  173. tempFilePath: data
  174. .tempFilePath,
  175. success: (saveRes) => {
  176. // uni.showToast({
  177. // title: '下载成功',
  178. // icon: 'success'
  179. // });
  180. // uni.hideLoading();
  181. // uni.showLoading({
  182. // title: "正在更新...",
  183. // mask: true,
  184. // });
  185. // 调用安装逻辑
  186. plus.runtime.install(
  187. saveRes
  188. .savedFilePath, {
  189. force: true
  190. },
  191. function() {
  192. // uni
  193. // .hideLoading();
  194. },
  195. function() {
  196. // uni
  197. // .hideLoading();
  198. uni.showToast({
  199. title: '更新失败',
  200. icon: 'error'
  201. });
  202. });
  203. }
  204. });
  205. }
  206. }
  207. });
  208. // 先显示初始的loading提示
  209. uni.showLoading({
  210. title: "正在下载安装包: 0%",
  211. mask: true,
  212. });
  213. // 记录上次显示的进度,避免频繁更新
  214. let lastProgress = 0;
  215. let loadingVisible = true;
  216. downloadTask.onProgressUpdate((ress) => {
  217. console.log('下载进度:', ress)
  218. // 只在进度有明显变化(每10%)或者达到100%时更新提示
  219. if ((ress.progress > lastProgress && ress.progress %
  220. 10 === 0) || ress.progress === 100) {
  221. // 先隐藏再显示以更新内容
  222. if (loadingVisible) {
  223. uni.hideLoading();
  224. }
  225. if (ress.progress < 100) {
  226. uni.showLoading({
  227. title: "正在下载安装包: " + (ress
  228. .progress > lastProgress ? ress.progress : lastProgress) + "%",
  229. mask: true,
  230. });
  231. loadingVisible = true;
  232. } else {
  233. loadingVisible = false;
  234. }
  235. lastProgress = ress.progress > lastProgress ? ress.progress : lastProgress;
  236. }
  237. })
  238. // 确保任务完成时隐藏loading
  239. downloadTask.onStop(() => {
  240. if (loadingVisible) {
  241. uni.hideLoading();
  242. loadingVisible = false;
  243. }
  244. })
  245. downloadTask.onError(() => {
  246. if (loadingVisible) {
  247. uni.hideLoading();
  248. loadingVisible = false;
  249. }
  250. })
  251. }
  252. }
  253. });
  254. }
  255. }).catch(err => {
  256. console.log('err:', err)
  257. })
  258. // #endif
  259. },
  260. // 登录流程中的版本检测
  261. checkVersionForLogin() {
  262. return new Promise((resolve, reject) => {
  263. // #ifdef APP-PLUS
  264. getLatestVersion().then(res => {
  265. console.log('res:', res)
  266. const {
  267. appVersion
  268. } = uni.getAppBaseInfo()
  269. if (appVersion != res.versionCode) {
  270. uni.showModal({
  271. title: '发现新版本',
  272. content: '更新说明:' + res.releaseNotes,
  273. confirmText: '立即更新',
  274. cancelText: '暂不更新',
  275. showCancel: false,
  276. success: (val) => {
  277. if (val.confirm) {
  278. const downloadTask = uni.downloadFile({
  279. url: `${Vue.prototype.webviewUrl}/kd-aiot/${res.fileStorePath}`,
  280. success: (data) => {
  281. if (data.statusCode === 200) {
  282. uni.saveFile({
  283. tempFilePath: data
  284. .tempFilePath,
  285. success: (saveRes) => {
  286. // 调用安装逻辑
  287. plus.runtime.install(
  288. saveRes
  289. .savedFilePath, {
  290. force: true
  291. },
  292. function() {
  293. // 安装成功
  294. uni.showToast({
  295. title: '更新成功,应用即将重启',
  296. icon: 'success',
  297. duration: 2000
  298. });
  299. resolve('updated'); // 返回更新成功状态
  300. },
  301. function() {
  302. uni.showToast({
  303. title: '更新失败',
  304. icon: 'error'
  305. });
  306. reject('update_failed'); // 返回更新失败状态
  307. });
  308. }
  309. });
  310. }
  311. }
  312. });
  313. // 先显示初始的loading提示
  314. uni.showLoading({
  315. title: "正在下载安装包: 0%",
  316. mask: true,
  317. });
  318. // 记录上次显示的进度,避免频繁更新
  319. let lastProgress = 0;
  320. let loadingVisible = true;
  321. downloadTask.onProgressUpdate((ress) => {
  322. console.log('下载进度:', ress)
  323. // 只在进度有明显变化(每10%)或者达到100%时更新提示
  324. if ((ress.progress > lastProgress && ress.progress %
  325. 10 === 0) || ress.progress === 100) {
  326. // 先隐藏再显示以更新内容
  327. if (loadingVisible) {
  328. uni.hideLoading();
  329. }
  330. if (ress.progress < 100) {
  331. uni.showLoading({
  332. title: "正在下载安装包: " + (ress
  333. .progress > lastProgress ? ress.progress : lastProgress) + "%",
  334. mask: true,
  335. });
  336. loadingVisible = true;
  337. } else {
  338. loadingVisible = false;
  339. }
  340. lastProgress = ress.progress > lastProgress ? ress.progress : lastProgress;
  341. }
  342. })
  343. // 确保任务完成时隐藏loading
  344. downloadTask.onStop(() => {
  345. if (loadingVisible) {
  346. uni.hideLoading();
  347. loadingVisible = false;
  348. }
  349. })
  350. downloadTask.onError(() => {
  351. if (loadingVisible) {
  352. uni.hideLoading();
  353. loadingVisible = false;
  354. }
  355. reject('download_failed'); // 返回下载失败状态
  356. })
  357. } else {
  358. // 用户选择暂不更新
  359. resolve('skip_update'); // 返回跳过更新状态
  360. }
  361. }
  362. });
  363. } else {
  364. // 已是最新版本
  365. resolve('latest_version'); // 返回已是最新版本状态
  366. }
  367. }).catch(err => {
  368. console.log('err:', err)
  369. reject('version_check_failed'); // 返回版本检查失败状态
  370. })
  371. // #endif
  372. // #ifndef APP-PLUS
  373. // H5场景直接返回最新版本状态
  374. resolve('h5_platform');
  375. // #endif
  376. });
  377. },
  378. //登录
  379. async submit() {
  380. // #ifdef APP-PLUS
  381. if (!this.apiUrl) {
  382. this.$refs.serverSettingRef.open()
  383. return
  384. }
  385. // #endif
  386. // APP场景:先检查版本
  387. try {
  388. const updateResult = await this.checkVersionForLogin()
  389. console.log('版本检查结果:', updateResult)
  390. // 如果是更新成功状态,停止登录流程(应用即将重启)
  391. if (updateResult === 'updated') {
  392. return
  393. }
  394. // 其他情况(latest_version, skip_update, h5_platform)都可以继续登录
  395. } catch (error) {
  396. console.log('版本检查失败:', error)
  397. // 版本检查失败时,可以选择继续登录或提示用户
  398. // 这里选择继续登录,不阻碍用户使用
  399. }
  400. // H5场景直接登录,无需版本检查
  401. let param = {
  402. loginName: this.userInfo.username,
  403. loginPwd: this.userInfo.passwd
  404. }
  405. postJ(this.apiUrl + '/main/user/login', param)
  406. .then(res => {
  407. if (this.isMemo) {
  408. setPassword(this.userInfo)
  409. }
  410. let data = res.data
  411. uni.setStorageSync('token', data.token)
  412. uni.setStorageSync("userInfo", data);
  413. uni.showToast({
  414. title: '登录成功',
  415. icon: 'success',
  416. duration: 2000
  417. })
  418. this.getTree()
  419. setTimeout(() => {
  420. uni.reLaunch({
  421. url: '/pages/home/home'
  422. })
  423. }, 2000)
  424. })
  425. .catch(err => {
  426. uni.showToast({
  427. title: err.message || '登录失败',
  428. icon: 'none',
  429. duration: 2000
  430. })
  431. })
  432. },
  433. handleServerSettings() {
  434. this.$refs.serverSettingRef.open()
  435. },
  436. checkboxChange(e) {
  437. this.isMemo = e.detail.value[0] === '1'
  438. if (this.isMemo) {
  439. setMemo()
  440. } else {
  441. removeMemo()
  442. }
  443. },
  444. formatRouter(list) {
  445. let authorities = [];
  446. const fn = (list) => {
  447. let arr = [];
  448. for (const p of list) {
  449. if (p.menuType === 2) {
  450. // p.children = [];
  451. authorities.push(p);
  452. }
  453. // else {
  454. if (p.children?.length) {
  455. p.children = fn(p.children);
  456. } else {
  457. p.children = [];
  458. }
  459. arr.push(p);
  460. // }
  461. }
  462. return arr;
  463. };
  464. fn(list);
  465. return authorities
  466. },
  467. getTree() {
  468. getResourcesTree().then(res => {
  469. console.log(res)
  470. if (res.length == 0) {
  471. uni.showToast({
  472. title: '您还未配置权限',
  473. icon: 'none'
  474. })
  475. }
  476. let List = JSON.stringify(res || [])
  477. let authorities =this.formatRouter(res[0].children)
  478. uni.setStorageSync('treeList', List)
  479. uni.setStorageSync('authorities', JSON.stringify(authorities)) //按钮
  480. })
  481. }
  482. }
  483. }
  484. </script>
  485. <style lang="scss" scoped>
  486. @import 'login.scss';
  487. .version {
  488. font-size: 12px;
  489. color: #999;
  490. position: absolute;
  491. bottom: 10rpx;
  492. text-align: center;
  493. width: 100%;
  494. }
  495. </style>