index.uvue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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. 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);
  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): Promise<void> => {
  242. try {
  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. 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. if (ticketValue != null) {
  380. if (typeof ticketValue == 'string') {
  381. if(ticketValue.length > 0){
  382. const ticket = ticketValue as string
  383. console.log('获取到自动登录ticket:', ticket)
  384. handleLoginSSO(ticket);
  385. }
  386. }
  387. }
  388. }
  389. }, 500)
  390. })
  391. // 初始化:加载记住的账号密码
  392. onMounted(() => {
  393. checkVersion()
  394. const remembered = getRememberedAccount()
  395. if (remembered != null) {
  396. username.value = remembered['username'] as string
  397. password.value = remembered['password'] as string
  398. rememberPassword.value = true
  399. // 检查是否是从退出操作进入登录页
  400. const isLogout = uni.getStorageSync('isLogout')
  401. if (isLogout == null || isLogout == false || isLogout == '') {
  402. // 自动登录
  403. handleLogin()
  404. } else {
  405. // 清除退出标志
  406. uni.removeStorageSync('isLogout')
  407. }
  408. }
  409. })
  410. </script>
  411. <style lang="scss">
  412. .login-page {
  413. position: relative;
  414. flex: 1;
  415. padding-top: env(safe-area-inset-top);
  416. }
  417. .bg-image {
  418. position: absolute;
  419. top: 0;
  420. left: 0;
  421. width: 100%;
  422. height: 100%;
  423. z-index: 0;
  424. }
  425. .login-content {
  426. position: relative;
  427. flex: 1;
  428. padding: 60rpx 40rpx;
  429. z-index: 1;
  430. }
  431. .header {
  432. align-items: center;
  433. margin-bottom: 80rpx;
  434. }
  435. .logo {
  436. height: 200rpx;
  437. margin-bottom: 40rpx;
  438. border-radius:10px;
  439. }
  440. .title {
  441. font-size: 48rpx;
  442. color: #ffffff;
  443. font-weight: bold;
  444. // #ifndef APP-HARMONY
  445. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  446. // #endif
  447. }
  448. .form-card {
  449. background: rgba(255, 255, 255, 0.95);
  450. border-radius: 24rpx;
  451. padding: 50rpx 40rpx;
  452. // #ifndef APP-HARMONY
  453. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  454. // #endif
  455. margin-bottom: 100rpx;
  456. }
  457. .form-item {
  458. margin-bottom: 40rpx;
  459. }
  460. .label {
  461. font-size: 32rpx;
  462. color: #333333;
  463. font-weight: bold;
  464. margin-bottom: 20rpx;
  465. }
  466. .input-wrapper {
  467. position: relative;
  468. width: 100%;
  469. }
  470. .input {
  471. width: 100%;
  472. height: 90rpx;
  473. padding: 0 100rpx 0 30rpx;
  474. background-color: #f5f5f5;
  475. border-radius: 12rpx;
  476. font-size: 30rpx;
  477. color: #333333;
  478. border: 1rpx solid transparent;
  479. }
  480. .input:focus {
  481. border-color: #007aff;
  482. background-color: #ffffff;
  483. }
  484. .eye-icon {
  485. position: absolute;
  486. right: 30rpx;
  487. top: 50%;
  488. transform: translateY(-50%);
  489. width: 40rpx;
  490. height: 40rpx;
  491. }
  492. .remember-box {
  493. flex-direction: row;
  494. align-items: center;
  495. margin-bottom: 50rpx;
  496. }
  497. .checkbox {
  498. transform: scale(0.8);
  499. }
  500. .remember-text {
  501. font-size: 28rpx;
  502. color: #1677ff;
  503. margin-left: 12rpx;
  504. }
  505. .login-btn {
  506. width: 100%;
  507. height: 100rpx;
  508. line-height: 100rpx;
  509. background-color: #0081ff;
  510. border-radius: 50rpx;
  511. color: #ffffff;
  512. // #ifndef APP-HARMONY
  513. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  514. // #endif
  515. }
  516. .login-btn-text {
  517. font-size: 36rpx;
  518. color: #ffffff !important;
  519. font-weight: bold;
  520. }
  521. .footer {
  522. position: fixed;
  523. bottom: 60rpx;
  524. left: 0;
  525. right: 0;
  526. align-items: center;
  527. z-index: 2;
  528. }
  529. .version {
  530. font-size: 28rpx;
  531. color: #333333;
  532. margin-bottom: 15rpx;
  533. }
  534. .company {
  535. font-size: 24rpx;
  536. color: #333333;
  537. }
  538. .picker-modal {
  539. position: fixed;
  540. top: 0;
  541. left: 0;
  542. right: 0;
  543. bottom: 0;
  544. z-index: 1000;
  545. }
  546. .modal-mask {
  547. position: absolute;
  548. top: 0;
  549. left: 0;
  550. right: 0;
  551. bottom: 0;
  552. background-color: rgba(0, 0, 0, 0.5);
  553. }
  554. .modal-content {
  555. position: absolute;
  556. bottom: 0;
  557. left: 0;
  558. right: 0;
  559. background-color: #ffffff;
  560. border-top-left-radius: 16rpx;
  561. border-top-right-radius: 16rpx;
  562. max-height: 1000rpx;
  563. }
  564. .modal-header {
  565. flex-direction: row;
  566. justify-content: space-between;
  567. align-items: center;
  568. padding: 30rpx;
  569. border-bottom: 1rpx solid #f0f0f0;
  570. }
  571. .modal-title {
  572. font-size: 32rpx;
  573. font-weight: bold;
  574. color: #333333;
  575. }
  576. .modal-close {
  577. font-size: 28rpx;
  578. color: #007aff;
  579. }
  580. .form-item-btn{
  581. flex: 1;
  582. align-items: center;
  583. margin-bottom: 10px;
  584. }
  585. .form-item-input{
  586. flex: 1;
  587. flex-direction: row;
  588. background-color: #ffffff;
  589. padding: 10px;
  590. }
  591. .btn-primary {
  592. z-index: 999;
  593. font-size: 15px;
  594. border-radius: 10rpx;
  595. width: 100px;
  596. padding: 5px;
  597. background-color: #165DFF;
  598. line-height: 45rpx;
  599. color: #ffffff;
  600. .btn-text{
  601. color: #ffffff;
  602. padding: 5px 15px;
  603. }
  604. }
  605. .label-picker{
  606. font-size: 32rpx;
  607. color: #333333;
  608. font-weight: bold;
  609. width: 180rpx;
  610. padding: 20rpx;
  611. }
  612. .input-picker {
  613. width: 70%;
  614. height: 90rpx;
  615. padding: 0 100rpx 0 30rpx;
  616. background-color: #f5f5f5;
  617. border-radius: 12rpx;
  618. font-size: 30rpx;
  619. color: #333333;
  620. border: 1rpx solid transparent;
  621. }
  622. .eye-icon-picker {
  623. position: absolute;
  624. right: 230rpx;
  625. top: 50%;
  626. transform: translateY(-50%);
  627. width: 40rpx;
  628. height: 40rpx;
  629. }
  630. .download-progress-mask {
  631. position: fixed;
  632. top: 0;
  633. left: 0;
  634. right: 0;
  635. bottom: 0;
  636. background-color: rgba(0, 0, 0, 0.5);
  637. z-index: 999;
  638. align-items: center;
  639. justify-content: center;
  640. }
  641. .download-progress-box {
  642. width: 500rpx;
  643. padding: 60rpx;
  644. background-color: #ffffff;
  645. border-radius: 24rpx;
  646. align-items: center;
  647. }
  648. .download-progress-title {
  649. font-size: 32rpx;
  650. color: #333333;
  651. margin-bottom: 30rpx;
  652. }
  653. .download-progress-bar {
  654. width: 100%;
  655. height: 16rpx;
  656. background-color: #f0f0f0;
  657. border-radius: 8rpx;
  658. overflow: hidden;
  659. margin-bottom: 20rpx;
  660. }
  661. .download-progress-fill {
  662. height: 100%;
  663. background-color: #0081ff;
  664. border-radius: 8rpx;
  665. transition: width 0.2s ease;
  666. }
  667. .download-progress-text {
  668. font-size: 28rpx;
  669. color: #666666;
  670. }
  671. </style>