login.vue 13 KB

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