index.uvue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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, nextUrl:string) : Promise<void> => {
  165. // 提取 data 部分
  166. const resultObj = result as UTSJSONObject
  167. // const data = resultObj['data'] as UTSJSONObject
  168. uni.setStorageSync("login_key", "1")
  169. // 保存登录信息
  170. saveAccessToken(resultObj['token'] as string)
  171. const userInfoJson = await getUserInfo();
  172. console.log(userInfoJson);
  173. const userInfoObj = userInfoJson as UTSJSONObject
  174. const userInfo = userInfoObj['user'] as UTSJSONObject
  175. const deptInfo = userInfo['dept'] as UTSJSONObject
  176. const permissions = userInfoObj['permissions'] as any[]
  177. saveUserInfo({
  178. userId: userInfo['userId'],
  179. userName: userInfo['userName'],
  180. nickName: userInfo['nickName'],
  181. phone: userInfo['phonenumber'],
  182. deptName: deptInfo['deptName'],
  183. roleNames: userInfoObj['roleNames'],
  184. permissions: permissions
  185. })
  186. // 保存或清除记住的账号密码
  187. if (rememberPassword.value) {
  188. saveRememberedAccount(username.value, password.value)
  189. } else {
  190. clearRememberedAccount()
  191. }
  192. // 跳转到首页
  193. setTimeout(() => {
  194. uni.redirectTo({
  195. url: '/pages/index/index'
  196. })
  197. }, 1000)
  198. }
  199. // 登录处理
  200. const handleLogin = async (): Promise<void> => {
  201. // 验证输入
  202. if (username.value.trim().length == 0) {
  203. uni.showToast({
  204. title: '请输入账号',
  205. icon: 'none',
  206. duration: 2000
  207. })
  208. return
  209. }
  210. if (password.value.trim().length == 0) {
  211. uni.showToast({
  212. title: '请输入密码',
  213. icon: 'none',
  214. duration: 2000
  215. })
  216. return
  217. }
  218. try {
  219. loading.value = true
  220. /* const resultKey = await getIsKey();
  221. const resultKeyObj = resultKey as UTSJSONObject
  222. const isKey = resultKeyObj["data"] as string |'0'
  223. saveStoreIsKey(isKey); */
  224. const result = await loginByAccount(username.value, password.value)
  225. loginSuccess(result, null);
  226. uni.showToast({
  227. title: '登录成功',
  228. icon: 'success',
  229. duration: 3000
  230. })
  231. } catch (e: any) {
  232. uni.showToast({
  233. title: e.message ?? '登录失败',
  234. icon: 'none',
  235. duration: 2000
  236. })
  237. } finally {
  238. loading.value = false
  239. }
  240. }
  241. const handleLoginSSO = async (apptoken:string, nextUrl:string): Promise<void> => {
  242. try {
  243. let result = await loginSSO(apptoken)
  244. console.log('自动登录:', result)
  245. loginSuccess(result, nextUrl);
  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. duration: 3000
  281. });
  282. },
  283. fail: (error) => {
  284. console.error('安装失败:', error);
  285. uni.showToast({
  286. title: '安装失败',
  287. icon: 'none',
  288. duration: 3000
  289. });
  290. },
  291. complete: (res) => {
  292. console.log('安装完成:', res);
  293. }
  294. });
  295. }
  296. const installApkWithProgress = (): void => {
  297. // #ifdef APP-ANDROID
  298. isDownload.value = true;
  299. downloadProgress.value = 0;
  300. let donwloadUrl = '';
  301. donwloadUrl = getBaseUrl() + '/profile/app/emcs-release-'+versionServer.value+'.apk'
  302. // 下载APK
  303. const downloadTask = uni.downloadFile({
  304. url: donwloadUrl,
  305. filePath: `${uni.env.USER_DATA_PATH}/${Date.now()}_test.apk`, // 使用时间戳防止重名
  306. success: (downloadRes) => {
  307. isDownload.value = false;
  308. if (downloadRes.statusCode == 200) {
  309. // 确认安装
  310. uni.showModal({
  311. title: '安装提示',
  312. content: '下载完成,是否立即安装?',
  313. success: (modalRes) => {
  314. if (modalRes.confirm) {
  315. installApkFile(downloadRes.tempFilePath);
  316. }
  317. }
  318. });
  319. } else {
  320. uni.showToast({
  321. title: '下载失败',
  322. icon: 'error',
  323. duration: 3000
  324. });
  325. }
  326. },
  327. fail: (error) => {
  328. isDownload.value = false;
  329. uni.showToast({
  330. title: '下载失败',
  331. icon: 'none',
  332. duration: 3000
  333. });
  334. console.error('下载失败:', error);
  335. }
  336. });
  337. // 监听下载进度(更新页面内进度条,节流 5% 减轻渲染压力)
  338. let lastProgress = -5;
  339. downloadTask.onProgressUpdate((res) => {
  340. const p = Math.round(res.progress);
  341. if (p - lastProgress >= 5 || p >= 100) {
  342. lastProgress = p;
  343. downloadProgress.value = p;
  344. }
  345. });
  346. // #endif
  347. // #ifdef APP-HARMONY
  348. uni.showToast({
  349. title: '请登录PC端,扫描二维码下载并安装',
  350. icon: 'none',
  351. duration: 3000
  352. });
  353. // #endif
  354. // #ifdef APP-IOS
  355. uni.showToast({
  356. title: '请登录PC端,扫描二维码下载并安装',
  357. icon: 'none',
  358. duration: 3000
  359. });
  360. // #endif
  361. }
  362. const checkVersion = async (): Promise<void> => {
  363. const versionJSON = await getVersion() as UTSJSONObject
  364. versionServer.value = versionJSON['msg'] as string
  365. const hasNewVersion = compareVersion(versionServer.value, version.value) // true
  366. console.log("versionServer:"+versionServer.value+",app:" + version.value)
  367. console.log("hasNewVersion:"+hasNewVersion)
  368. if(hasNewVersion){
  369. installApkWithProgress();
  370. }
  371. }
  372. onLoad((options: UTSJSONObject | null) => {
  373. console.log('URL参数:', options)
  374. // 延迟一点执行,确保App.vue已保存到缓存
  375. setTimeout(() => {
  376. // 2. 从页面参数获取
  377. if (options != null) {
  378. const ticketValue = options['ticket']
  379. const nextUrl = options['next']
  380. if (ticketValue != null) {
  381. if (typeof ticketValue == 'string') {
  382. if(ticketValue.length > 0){
  383. const ticket = ticketValue as string
  384. console.log('获取到自动登录ticket:', ticket)
  385. handleLoginSSO(ticket, nextUrl);
  386. }
  387. }
  388. }
  389. }
  390. }, 500)
  391. })
  392. // 初始化:加载记住的账号密码
  393. onMounted(() => {
  394. checkVersion()
  395. const remembered = getRememberedAccount()
  396. if (remembered != null) {
  397. username.value = remembered['username'] as string
  398. password.value = remembered['password'] as string
  399. rememberPassword.value = true
  400. // 检查是否是从退出操作进入登录页
  401. const isLogout = uni.getStorageSync('isLogout')
  402. if (isLogout == null || isLogout == false || isLogout == '') {
  403. // 自动登录
  404. handleLogin()
  405. } else {
  406. // 清除退出标志
  407. uni.removeStorageSync('isLogout')
  408. }
  409. }
  410. })
  411. </script>
  412. <style lang="scss">
  413. .login-page {
  414. position: relative;
  415. flex: 1;
  416. padding-top: env(safe-area-inset-top);
  417. }
  418. .bg-image {
  419. position: absolute;
  420. top: 0;
  421. left: 0;
  422. width: 100%;
  423. height: 100%;
  424. z-index: 0;
  425. }
  426. .login-content {
  427. position: relative;
  428. flex: 1;
  429. padding: 60rpx 40rpx;
  430. z-index: 1;
  431. }
  432. .header {
  433. align-items: center;
  434. margin-bottom: 80rpx;
  435. }
  436. .logo {
  437. height: 200rpx;
  438. margin-bottom: 40rpx;
  439. border-radius:10px;
  440. }
  441. .title {
  442. font-size: 48rpx;
  443. color: #ffffff;
  444. font-weight: bold;
  445. // #ifndef APP-HARMONY
  446. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  447. // #endif
  448. }
  449. .form-card {
  450. background: rgba(255, 255, 255, 0.95);
  451. border-radius: 24rpx;
  452. padding: 50rpx 40rpx;
  453. // #ifndef APP-HARMONY
  454. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  455. // #endif
  456. margin-bottom: 100rpx;
  457. }
  458. .form-item {
  459. margin-bottom: 40rpx;
  460. }
  461. .label {
  462. font-size: 32rpx;
  463. color: #333333;
  464. font-weight: bold;
  465. margin-bottom: 20rpx;
  466. }
  467. .input-wrapper {
  468. position: relative;
  469. width: 100%;
  470. }
  471. .input {
  472. width: 100%;
  473. height: 90rpx;
  474. padding: 0 100rpx 0 30rpx;
  475. background-color: #f5f5f5;
  476. border-radius: 12rpx;
  477. font-size: 30rpx;
  478. color: #333333;
  479. border: 1rpx solid transparent;
  480. }
  481. .input:focus {
  482. border-color: #007aff;
  483. background-color: #ffffff;
  484. }
  485. .eye-icon {
  486. position: absolute;
  487. right: 30rpx;
  488. top: 50%;
  489. transform: translateY(-50%);
  490. width: 40rpx;
  491. height: 40rpx;
  492. }
  493. .remember-box {
  494. flex-direction: row;
  495. align-items: center;
  496. margin-bottom: 50rpx;
  497. }
  498. .checkbox {
  499. transform: scale(0.8);
  500. }
  501. .remember-text {
  502. font-size: 28rpx;
  503. color: #1677ff;
  504. margin-left: 12rpx;
  505. }
  506. .login-btn {
  507. width: 100%;
  508. height: 100rpx;
  509. line-height: 100rpx;
  510. background-color: #0081ff;
  511. border-radius: 50rpx;
  512. color: #ffffff;
  513. // #ifndef APP-HARMONY
  514. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  515. // #endif
  516. }
  517. .login-btn-text {
  518. font-size: 36rpx;
  519. color: #ffffff !important;
  520. font-weight: bold;
  521. }
  522. .footer {
  523. position: fixed;
  524. bottom: 60rpx;
  525. left: 0;
  526. right: 0;
  527. align-items: center;
  528. z-index: 2;
  529. }
  530. .version {
  531. font-size: 28rpx;
  532. color: #333333;
  533. margin-bottom: 15rpx;
  534. }
  535. .company {
  536. font-size: 24rpx;
  537. color: #333333;
  538. }
  539. .picker-modal {
  540. position: fixed;
  541. top: 0;
  542. left: 0;
  543. right: 0;
  544. bottom: 0;
  545. z-index: 1000;
  546. }
  547. .modal-mask {
  548. position: absolute;
  549. top: 0;
  550. left: 0;
  551. right: 0;
  552. bottom: 0;
  553. background-color: rgba(0, 0, 0, 0.5);
  554. }
  555. .modal-content {
  556. position: absolute;
  557. bottom: 0;
  558. left: 0;
  559. right: 0;
  560. background-color: #ffffff;
  561. border-top-left-radius: 16rpx;
  562. border-top-right-radius: 16rpx;
  563. max-height: 1000rpx;
  564. }
  565. .modal-header {
  566. flex-direction: row;
  567. justify-content: space-between;
  568. align-items: center;
  569. padding: 30rpx;
  570. border-bottom: 1rpx solid #f0f0f0;
  571. }
  572. .modal-title {
  573. font-size: 32rpx;
  574. font-weight: bold;
  575. color: #333333;
  576. }
  577. .modal-close {
  578. font-size: 28rpx;
  579. color: #007aff;
  580. }
  581. .form-item-btn{
  582. flex: 1;
  583. align-items: center;
  584. margin-bottom: 10px;
  585. }
  586. .form-item-input{
  587. flex: 1;
  588. flex-direction: row;
  589. background-color: #ffffff;
  590. padding: 10px;
  591. }
  592. .btn-primary {
  593. z-index: 999;
  594. font-size: 15px;
  595. border-radius: 10rpx;
  596. width: 100px;
  597. padding: 5px;
  598. background-color: #165DFF;
  599. line-height: 45rpx;
  600. color: #ffffff;
  601. .btn-text{
  602. color: #ffffff;
  603. padding: 5px 15px;
  604. }
  605. }
  606. .label-picker{
  607. font-size: 32rpx;
  608. color: #333333;
  609. font-weight: bold;
  610. width: 180rpx;
  611. padding: 20rpx;
  612. }
  613. .input-picker {
  614. width: 70%;
  615. height: 90rpx;
  616. padding: 0 100rpx 0 30rpx;
  617. background-color: #f5f5f5;
  618. border-radius: 12rpx;
  619. font-size: 30rpx;
  620. color: #333333;
  621. border: 1rpx solid transparent;
  622. }
  623. .eye-icon-picker {
  624. position: absolute;
  625. right: 230rpx;
  626. top: 50%;
  627. transform: translateY(-50%);
  628. width: 40rpx;
  629. height: 40rpx;
  630. }
  631. .download-progress-mask {
  632. position: fixed;
  633. top: 0;
  634. left: 0;
  635. right: 0;
  636. bottom: 0;
  637. background-color: rgba(0, 0, 0, 0.5);
  638. z-index: 999;
  639. align-items: center;
  640. justify-content: center;
  641. }
  642. .download-progress-box {
  643. width: 500rpx;
  644. padding: 60rpx;
  645. background-color: #ffffff;
  646. border-radius: 24rpx;
  647. align-items: center;
  648. }
  649. .download-progress-title {
  650. font-size: 32rpx;
  651. color: #333333;
  652. margin-bottom: 30rpx;
  653. }
  654. .download-progress-bar {
  655. width: 100%;
  656. height: 16rpx;
  657. background-color: #f0f0f0;
  658. border-radius: 8rpx;
  659. overflow: hidden;
  660. margin-bottom: 20rpx;
  661. }
  662. .download-progress-fill {
  663. height: 100%;
  664. background-color: #0081ff;
  665. border-radius: 8rpx;
  666. transition: width 0.2s ease;
  667. }
  668. .download-progress-text {
  669. font-size: 28rpx;
  670. color: #666666;
  671. }
  672. </style>