index.uvue 20 KB

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