index.uvue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. <template>
  2. <view class="login-page">
  3. <!-- 背景图 -->
  4. <image class="bg-image" src="/static/images/login/1.png" mode="aspectFill"></image>
  5. <scroll-view class="login-content">
  6. <!-- Logo 和标题 -->
  7. <view class="header">
  8. <image class="logo" src="/static/images/login/2.png" mode="aspectFit"></image>
  9. <text class="title">工效通</text>
  10. </view>
  11. <!-- 登录表单卡片 -->
  12. <view class="form-card">
  13. <!-- 账号输入 -->
  14. <view class="form-item">
  15. <text class="label">账号</text>
  16. <input class="input" type="text" placeholder="请输入您的账号" v-model="username" />
  17. </view>
  18. <!-- 密码输入 -->
  19. <view class="form-item">
  20. <text class="label">密码</text>
  21. <view class="input-wrapper">
  22. <input class="input" :type="showPassword ? 'text' : 'password'" placeholder="请输入您的密码" v-model="password" />
  23. <image class="eye-icon" :src="showPassword ? '/static/images/login/4.png' : '/static/images/login/3.png'" mode="aspectFit" @click="handleTogglePassword"></image>
  24. </view>
  25. </view>
  26. <!-- 记住密码 -->
  27. <view class="remember-box">
  28. <checkbox :checked="rememberPassword" @click="handleRememberChange" class="checkbox" />
  29. <text class="remember-text">记住密码</text>
  30. </view>
  31. <!-- 登录按钮 -->
  32. <button class="login-btn" @click="handleLogin">
  33. <text class="login-btn-text">{{ loading ? "登录中..." : "登录" }}</text>
  34. </button>
  35. </view>
  36. <!-- 底部信息 -->
  37. <view class="footer">
  38. <text class="version">v{{ version }}</text>
  39. <text class="company">宇光同行</text>
  40. </view>
  41. </scroll-view>
  42. <!-- 自定义选择器弹窗 -->
  43. <view v-if="showPasswordPicker" class="picker-modal">
  44. <view class="modal-mask" @click="showPasswordPicker = false"></view>
  45. <view class="modal-content">
  46. <view class="modal-header">
  47. <text class="modal-title">修改密码</text>
  48. <text class="modal-close" @click="showPasswordPicker = false">取消</text>
  49. </view>
  50. <view class="form-item-input">
  51. <text class="label-picker">新密码</text>
  52. <view class="view-input-picker">
  53. <input class="input-picker" :type="showNewPassword ? 'text' : 'password'" placeholder="请输入新密码" v-model="newpassword" />
  54. <image class="eye-icon-picker" :src="showNewPassword ? '/static/images/login/4.png' : '/static/images/login/3.png'" mode="aspectFit" @click="handleToggleNewPassword"></image>
  55. </view>
  56. </view>
  57. <view class="form-item-input">
  58. <text class="label-picker">确认密码</text>
  59. <view class="view-input-picker">
  60. <input class="input-picker" :type="showConfirmPassword ? 'text' : 'password'" placeholder="请输入确认密码" v-model="confirmpassword" />
  61. <image class="eye-icon-picker" :src="showConfirmPassword ? '/static/images/login/4.png' : '/static/images/login/3.png'" mode="aspectFit" @click="handleToggleConfirmPassword"></image>
  62. </view>
  63. </view>
  64. <view class="form-item-btn">
  65. <button class="btn-primary" @click="handleRestPassword">
  66. 确认
  67. </button>
  68. </view>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script setup lang="uts">
  74. import { ref, computed, onMounted } from 'vue'
  75. import { loginByAccount, loginSSO, getUserInfo, resetPassword , getIsKey} from '../../api/auth/login'
  76. // import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update'
  77. import { getVersion } from '../../api/system/config.uts'
  78. import { getBaseUrl } from '../../utils/request'
  79. import {
  80. saveAccessToken,
  81. saveUserInfo,
  82. getRememberedAccount,
  83. saveRememberedAccount,
  84. clearRememberedAccount,
  85. saveStoreIsKey,
  86. getStoreIsKey
  87. } from '../../utils/storage'
  88. import { validatePassword } from '../../utils/validate'
  89. // @ts-ignore
  90. import manifest from '@/manifest.json'
  91. // 表单数据
  92. const username = ref<string>("")
  93. const password = ref<string>("")
  94. const newpassword = ref<string>("")
  95. const confirmpassword = ref<string>("")
  96. const rememberPassword = ref<boolean>(false)
  97. const showPassword = ref<boolean>(false)
  98. const showNewPassword = ref<boolean>(false)
  99. const showConfirmPassword = ref<boolean>(false)
  100. const loading = ref<boolean>(false)
  101. const isDownload = ref<boolean>(false)
  102. const showPasswordPicker = ref<boolean>(false)
  103. // 版本号
  104. const manifestVersion = manifest.versionName as string | null
  105. const version = ref<string>(manifestVersion != null ? manifestVersion : '1.0.0')
  106. const versionServer = ref<string>('1.0.0')
  107. // 是否可以登录
  108. const canLogin = computed((): boolean => {
  109. return username.value.length > 0 && password.value.length > 0 && !loading.value
  110. })
  111. // 记住密码切换
  112. const handleRememberChange = (): void => {
  113. rememberPassword.value = !rememberPassword.value
  114. }
  115. // 切换密码显示/隐藏
  116. const handleTogglePassword = (): void => {
  117. showPassword.value = !showPassword.value
  118. }
  119. // 切换密码显示/隐藏
  120. const handleToggleNewPassword = (): void => {
  121. showNewPassword.value = !showNewPassword.value
  122. }
  123. // 切换密码显示/隐藏
  124. const handleToggleConfirmPassword = (): void => {
  125. showConfirmPassword.value = !showConfirmPassword.value
  126. }
  127. const handleRestPassword = async (): Promise<void> => {
  128. try {
  129. if(newpassword.value != confirmpassword.value){
  130. uni.showToast({
  131. title: '两次输入的密码不一致',
  132. icon: 'error'
  133. })
  134. return;
  135. }
  136. await resetPassword(username.value, password.value, newpassword.value);
  137. showPasswordPicker.value = false;
  138. uni.showToast({
  139. title: '修改成功,请重新登录',
  140. icon: 'success'
  141. })
  142. newpassword.value = ""
  143. confirmpassword.value = "";
  144. } catch (e: any) {
  145. uni.showToast({
  146. title: e.message ?? '密码修改失败',
  147. icon: 'none',
  148. duration: 2000
  149. })
  150. }
  151. };
  152. const loginSuccess = async(result: any) : Promise<void> => {
  153. // 提取 data 部分
  154. const resultObj = result as UTSJSONObject
  155. // const data = resultObj['data'] as UTSJSONObject
  156. const isInitPassword = resultObj["isInitPassword"] as boolean | null;
  157. // console.log("================"+ code)
  158. if(isInitPassword==true){
  159. showPasswordPicker.value = true;
  160. return
  161. }
  162. uni.setStorageSync("login_key", "1")
  163. // 保存登录信息
  164. saveAccessToken(resultObj['token'] as string)
  165. const userInfoJson = await getUserInfo();
  166. console.log(userInfoJson);
  167. const userInfoObj = userInfoJson as UTSJSONObject
  168. const userInfo = userInfoObj['user'] as UTSJSONObject
  169. const deptInfo = userInfo['dept'] as UTSJSONObject
  170. const permissions = userInfoObj['permissions'] as any[]
  171. saveUserInfo({
  172. userId: userInfo['userId'],
  173. userName: userInfo['userName'],
  174. nickName: userInfo['nickName'],
  175. phone: userInfo['phonenumber'],
  176. deptName: deptInfo['deptName'],
  177. roleNames: userInfoObj['roleNames'],
  178. permissions: permissions
  179. })
  180. // 保存或清除记住的账号密码
  181. if (rememberPassword.value) {
  182. saveRememberedAccount(username.value, password.value)
  183. } else {
  184. clearRememberedAccount()
  185. }
  186. // 跳转到首页
  187. setTimeout(() => {
  188. /* uni.redirectTo({
  189. url: '/pages/index/index'
  190. }) */
  191. uni.redirectTo({
  192. url: '/pages/splash/index'
  193. })
  194. }, 1000)
  195. }
  196. // 登录处理
  197. const handleLogin = async (): Promise<void> => {
  198. // 验证输入
  199. if (username.value.trim().length == 0) {
  200. uni.showToast({
  201. title: '请输入账号',
  202. icon: 'none',
  203. duration: 2000
  204. })
  205. return
  206. }
  207. if (password.value.trim().length == 0) {
  208. uni.showToast({
  209. title: '请输入密码',
  210. icon: 'none',
  211. duration: 2000
  212. })
  213. return
  214. }
  215. try {
  216. loading.value = true
  217. const resultKey = await getIsKey();
  218. const resultKeyObj = resultKey as UTSJSONObject
  219. const isKey = resultKeyObj["data"] as string |'0'
  220. saveStoreIsKey(isKey);
  221. const result = await loginByAccount(username.value, password.value)
  222. loginSuccess(result);
  223. uni.showToast({
  224. title: '登录成功',
  225. icon: 'success'
  226. })
  227. } catch (e: any) {
  228. uni.showToast({
  229. title: e.message ?? '登录失败',
  230. icon: 'none',
  231. duration: 2000
  232. })
  233. } finally {
  234. loading.value = false
  235. }
  236. }
  237. const handleLoginSSO = async (apptoken:string): Promise<void> => {
  238. try {
  239. const resultKey = await getIsKey();
  240. const resultKeyObj = resultKey as UTSJSONObject
  241. const isKey = resultKeyObj["data"] as string |'0'
  242. saveStoreIsKey(isKey);
  243. let result = await loginSSO(apptoken)
  244. console.log('自动登录:', result)
  245. loginSuccess(result);
  246. } catch (e: any) {
  247. uni.showToast({
  248. title: e.message ?? '登录失败',
  249. icon: 'none',
  250. duration: 2000
  251. })
  252. } finally {
  253. loading.value = false
  254. }
  255. }
  256. const compareVersion = (newVersion: string, currentVersion: string): boolean => {
  257. const newVer = newVersion.split('.').map(item => parseInt(item))
  258. const currentVer = currentVersion.split('.').map(item => parseInt(item))
  259. const maxLength = Math.max(newVer.length, currentVer.length)
  260. for (let i = 0; i < maxLength; i++) {
  261. const newNum = i < newVer.length ? newVer[i] : 0
  262. const currentNum = i < currentVer.length ? currentVer[i] : 0
  263. if (newNum > currentNum) {
  264. return true
  265. } else if (newNum < currentNum) {
  266. return false
  267. }
  268. }
  269. return false
  270. }
  271. // 安装APK的单独函数
  272. const installApkFile = (filePath: string): void => {
  273. uni.installApk({
  274. filePath: filePath,
  275. success: () => {
  276. console.log('安装成功');
  277. uni.showToast({
  278. title: '安装成功',
  279. icon: 'success'
  280. });
  281. },
  282. fail: (error) => {
  283. console.error('安装失败:', error);
  284. uni.showToast({
  285. title: '安装失败',
  286. icon: 'none'
  287. });
  288. },
  289. complete: (res) => {
  290. console.log('安装完成:', res);
  291. }
  292. });
  293. }
  294. const installApkWithProgress = (): void => {
  295. // #ifdef APP-ANDROID
  296. // 显示下载进度
  297. uni.showLoading({
  298. title: '下载中...',
  299. mask: true
  300. });
  301. isDownload.value = true;
  302. let donwloadUrl = '';
  303. donwloadUrl = getBaseUrl() + '/profile/app/gxt-release-'+versionServer.value+'.apk'
  304. // 下载APK
  305. const downloadTask = uni.downloadFile({
  306. url: donwloadUrl,
  307. filePath: `${uni.env.USER_DATA_PATH}/${Date.now()}_test.apk`, // 使用时间戳防止重名
  308. success: (downloadRes) => {
  309. if (downloadRes.statusCode == 200) {
  310. // 确认安装
  311. uni.showModal({
  312. title: '安装提示',
  313. content: '下载完成,是否立即安装?',
  314. success: (modalRes) => {
  315. if (modalRes.confirm) {
  316. installApkFile(downloadRes.tempFilePath);
  317. }
  318. }
  319. });
  320. } else {
  321. uni.showToast({
  322. title: '下载失败',
  323. icon: 'error'
  324. });
  325. }
  326. uni.hideLoading();
  327. },
  328. fail: (error) => {
  329. uni.hideLoading();
  330. uni.showToast({
  331. title: '下载失败',
  332. icon: 'none'
  333. });
  334. console.error('下载失败:', error);
  335. }
  336. });
  337. // 监听下载进度
  338. downloadTask.onProgressUpdate((res) => {
  339. console.log('下载进度:', res.progress + '%');
  340. // 如果需要,可以更新UI显示进度
  341. // uni.showLoading({
  342. // title: `下载中...`,
  343. // mask: true
  344. // });
  345. });
  346. // #endif
  347. // #ifdef APP-HARMONY
  348. uni.showToast({
  349. title: '请登录PC端,扫描二维码下载并安装',
  350. icon: 'none'
  351. });
  352. // #endif
  353. // #ifdef APP-IOS
  354. uni.showToast({
  355. title: '请登录PC端,扫描二维码下载并安装',
  356. icon: 'none'
  357. });
  358. // #endif
  359. }
  360. const checkVersion = async (): Promise<void> => {
  361. const versionJSON = await getVersion() as UTSJSONObject
  362. versionServer.value = versionJSON['msg'] as string
  363. const hasNewVersion = compareVersion(versionServer.value, version.value) // true
  364. console.log("versionServer:"+versionServer.value)
  365. console.log("hasNewVersion:"+hasNewVersion)
  366. if(hasNewVersion){
  367. installApkWithProgress();
  368. }
  369. }
  370. onLoad((options: UTSJSONObject | null) => {
  371. console.log('URL参数:', options)
  372. // 延迟一点执行,确保App.vue已保存到缓存
  373. setTimeout(() => {
  374. // 1. 从缓存获取ICE的apptoken
  375. const iceToken = uni.getStorageSync('ice_apptoken')
  376. if (iceToken != null) {
  377. if (typeof iceToken == 'string') {
  378. if(iceToken.length > 0){
  379. console.log('从缓存获取ICE apptoken:', iceToken)
  380. handleLoginSSO(iceToken)
  381. }
  382. // 清除缓存,避免重复登录
  383. uni.removeStorageSync('ice_apptoken')
  384. return
  385. }
  386. }
  387. // 2. 从页面参数获取
  388. if (options != null) {
  389. const apptokenValue = options['apptoken']
  390. if (apptokenValue != null) {
  391. if (typeof apptokenValue == 'string') {
  392. if(apptokenValue.length > 0){
  393. const apptoken = apptokenValue as string
  394. console.log('获取到自动登录apptoken:', apptoken)
  395. handleLoginSSO(apptoken);
  396. }
  397. }
  398. }
  399. }
  400. console.log('未检测到ICE登录,显示普通登录界面')
  401. }, 500)
  402. })
  403. // 初始化:加载记住的账号密码
  404. onMounted(() => {
  405. // checkUpdate()
  406. checkVersion()
  407. const remembered = getRememberedAccount()
  408. if (remembered != null) {
  409. username.value = remembered['username'] as string
  410. password.value = remembered['password'] as string
  411. rememberPassword.value = true
  412. }
  413. })
  414. </script>
  415. <style lang="scss">
  416. .login-page {
  417. position: relative;
  418. flex: 1;
  419. padding-top: env(safe-area-inset-top);
  420. }
  421. .bg-image {
  422. position: absolute;
  423. top: 0;
  424. left: 0;
  425. width: 100%;
  426. height: 100%;
  427. z-index: 0;
  428. }
  429. .login-content {
  430. position: relative;
  431. flex: 1;
  432. padding: 60rpx 40rpx;
  433. z-index: 1;
  434. }
  435. .header {
  436. align-items: center;
  437. margin-bottom: 80rpx;
  438. }
  439. .logo {
  440. width: 200rpx;
  441. height: 200rpx;
  442. margin-bottom: 40rpx;
  443. border-radius:10px;
  444. }
  445. .title {
  446. font-size: 48rpx;
  447. color: #ffffff;
  448. font-weight: bold;
  449. // #ifndef APP-HARMONY
  450. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  451. // #endif
  452. }
  453. .form-card {
  454. background: rgba(255, 255, 255, 0.95);
  455. border-radius: 24rpx;
  456. padding: 50rpx 40rpx;
  457. // #ifndef APP-HARMONY
  458. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  459. // #endif
  460. margin-bottom: 100rpx;
  461. }
  462. .form-item {
  463. margin-bottom: 40rpx;
  464. }
  465. .label {
  466. font-size: 32rpx;
  467. color: #333333;
  468. font-weight: bold;
  469. margin-bottom: 20rpx;
  470. }
  471. .input-wrapper {
  472. position: relative;
  473. width: 100%;
  474. }
  475. .input {
  476. width: 100%;
  477. height: 90rpx;
  478. padding: 0 100rpx 0 30rpx;
  479. background-color: #f5f5f5;
  480. border-radius: 12rpx;
  481. font-size: 30rpx;
  482. color: #333333;
  483. border: 1rpx solid transparent;
  484. }
  485. .input:focus {
  486. border-color: #007aff;
  487. background-color: #ffffff;
  488. }
  489. .eye-icon {
  490. position: absolute;
  491. right: 30rpx;
  492. top: 50%;
  493. transform: translateY(-50%);
  494. width: 40rpx;
  495. height: 40rpx;
  496. }
  497. .remember-box {
  498. flex-direction: row;
  499. align-items: center;
  500. margin-bottom: 50rpx;
  501. }
  502. .checkbox {
  503. transform: scale(0.8);
  504. }
  505. .remember-text {
  506. font-size: 28rpx;
  507. color: #1677ff;
  508. margin-left: 12rpx;
  509. }
  510. .login-btn {
  511. width: 100%;
  512. height: 100rpx;
  513. line-height: 100rpx;
  514. background-color: #0081ff;
  515. border-radius: 50rpx;
  516. color: #ffffff;
  517. // #ifndef APP-HARMONY
  518. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  519. // #endif
  520. }
  521. .login-btn-text {
  522. font-size: 36rpx;
  523. color: #ffffff !important;
  524. font-weight: bold;
  525. }
  526. .footer {
  527. position: fixed;
  528. bottom: 60rpx;
  529. left: 0;
  530. right: 0;
  531. align-items: center;
  532. z-index: 2;
  533. }
  534. .version {
  535. font-size: 28rpx;
  536. color: #333333;
  537. margin-bottom: 15rpx;
  538. }
  539. .company {
  540. font-size: 24rpx;
  541. color: #333333;
  542. }
  543. .picker-modal {
  544. position: fixed;
  545. top: 0;
  546. left: 0;
  547. right: 0;
  548. bottom: 0;
  549. z-index: 1000;
  550. }
  551. .modal-mask {
  552. position: absolute;
  553. top: 0;
  554. left: 0;
  555. right: 0;
  556. bottom: 0;
  557. background-color: rgba(0, 0, 0, 0.5);
  558. }
  559. .modal-content {
  560. position: absolute;
  561. bottom: 0;
  562. left: 0;
  563. right: 0;
  564. background-color: #ffffff;
  565. border-top-left-radius: 16rpx;
  566. border-top-right-radius: 16rpx;
  567. max-height: 1000rpx;
  568. }
  569. .modal-header {
  570. flex-direction: row;
  571. justify-content: space-between;
  572. align-items: center;
  573. padding: 30rpx;
  574. border-bottom: 1rpx solid #f0f0f0;
  575. }
  576. .modal-title {
  577. font-size: 32rpx;
  578. font-weight: bold;
  579. color: #333333;
  580. }
  581. .modal-close {
  582. font-size: 28rpx;
  583. color: #007aff;
  584. }
  585. .form-item-btn{
  586. flex: 1;
  587. align-items: center;
  588. margin-bottom: 10px;
  589. }
  590. .form-item-input{
  591. flex: 1;
  592. flex-direction: row;
  593. background-color: #ffffff;
  594. padding: 10px;
  595. }
  596. .btn-primary {
  597. z-index: 999;
  598. font-size: 15px;
  599. border-radius: 10rpx;
  600. width: 100px;
  601. padding: 5px;
  602. background-color: #165DFF;
  603. line-height: 45rpx;
  604. color: #ffffff;
  605. .btn-text{
  606. color: #ffffff;
  607. padding: 5px 15px;
  608. }
  609. }
  610. .label-picker{
  611. font-size: 32rpx;
  612. color: #333333;
  613. font-weight: bold;
  614. width: 180rpx;
  615. padding: 20rpx;
  616. }
  617. .input-picker {
  618. width: 70%;
  619. height: 90rpx;
  620. padding: 0 100rpx 0 30rpx;
  621. background-color: #f5f5f5;
  622. border-radius: 12rpx;
  623. font-size: 30rpx;
  624. color: #333333;
  625. border: 1rpx solid transparent;
  626. }
  627. .eye-icon-picker {
  628. position: absolute;
  629. right: 230rpx;
  630. top: 50%;
  631. transform: translateY(-50%);
  632. width: 40rpx;
  633. height: 40rpx;
  634. }
  635. </style>