index.uvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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">工效通APP</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, getUserInfo, resetPassword } from '../../api/auth/login'
  76. import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update'
  77. import {
  78. saveAccessToken,
  79. saveUserInfo,
  80. getRememberedAccount,
  81. saveRememberedAccount,
  82. clearRememberedAccount
  83. } from '../../utils/storage'
  84. import { validatePassword } from '../../utils/validate'
  85. import { encryptAES } from '../../utils/crypto'
  86. // @ts-ignore
  87. import manifest from '@/manifest.json'
  88. // 表单数据
  89. const username = ref<string>("")
  90. const password = ref<string>("")
  91. const newpassword = ref<string>("")
  92. const confirmpassword = ref<string>("")
  93. const rememberPassword = ref<boolean>(false)
  94. const showPassword = ref<boolean>(false)
  95. const showNewPassword = ref<boolean>(false)
  96. const showConfirmPassword = ref<boolean>(false)
  97. const loading = ref<boolean>(false)
  98. const showPasswordPicker = ref<boolean>(false)
  99. // 版本号
  100. const manifestVersion = manifest.versionName as string | null
  101. const version = ref<string>(manifestVersion != null ? manifestVersion : '1.0.0')
  102. // 是否可以登录
  103. const canLogin = computed((): boolean => {
  104. return username.value.length > 0 && password.value.length > 0 && !loading.value
  105. })
  106. // 记住密码切换
  107. const handleRememberChange = (): void => {
  108. rememberPassword.value = !rememberPassword.value
  109. }
  110. // 切换密码显示/隐藏
  111. const handleTogglePassword = (): void => {
  112. showPassword.value = !showPassword.value
  113. }
  114. // 切换密码显示/隐藏
  115. const handleToggleNewPassword = (): void => {
  116. showNewPassword.value = !showNewPassword.value
  117. }
  118. // 切换密码显示/隐藏
  119. const handleToggleConfirmPassword = (): void => {
  120. showConfirmPassword.value = !showConfirmPassword.value
  121. }
  122. const handleRestPassword = async (): Promise<void> => {
  123. try {
  124. if(newpassword.value != confirmpassword.value){
  125. uni.showToast({
  126. title: '两次输入的密码不一致',
  127. icon: 'error'
  128. })
  129. return;
  130. }
  131. await resetPassword(username.value, password.value, newpassword.value);
  132. showPasswordPicker.value = false;
  133. uni.showToast({
  134. title: '修改成功,请重新登录',
  135. icon: 'success'
  136. })
  137. newpassword.value = ""
  138. confirmpassword.value = "";
  139. } catch (e: any) {
  140. uni.showToast({
  141. title: e.message ?? '密码修改失败',
  142. icon: 'none',
  143. duration: 2000
  144. })
  145. }
  146. };
  147. // 登录处理
  148. const handleLogin = async (): Promise<void> => {
  149. // 验证输入
  150. if (username.value.trim().length == 0) {
  151. uni.showToast({
  152. title: '请输入账号',
  153. icon: 'none',
  154. duration: 2000
  155. })
  156. return
  157. }
  158. if (password.value.trim().length == 0) {
  159. uni.showToast({
  160. title: '请输入密码',
  161. icon: 'none',
  162. duration: 2000
  163. })
  164. return
  165. }
  166. // // 验证密码强度
  167. // if (!validatePassword(password.value)) {
  168. // uni.showToast({
  169. // title: '密码强度不足:需包含数字、大小写字母、特殊字符,至少6位',
  170. // icon: 'none',
  171. // duration: 3000
  172. // })
  173. // return
  174. // }
  175. try {
  176. loading.value = true
  177. const result = await loginByAccount(username.value, password.value)
  178. // 提取 data 部分
  179. const resultObj = result as UTSJSONObject
  180. // const data = resultObj['data'] as UTSJSONObject
  181. const isInitPassword = resultObj["isInitPassword"] as boolean | null;
  182. // console.log("================"+ code)
  183. if(isInitPassword==true){
  184. showPasswordPicker.value = true;
  185. return
  186. }
  187. // 保存登录信息
  188. saveAccessToken(resultObj['token'] as string)
  189. const userInfoJson = await getUserInfo();
  190. console.log(userInfoJson);
  191. const userInfoObj = userInfoJson as UTSJSONObject
  192. const userInfo = userInfoObj['user'] as UTSJSONObject
  193. const deptInfo = userInfo['dept'] as UTSJSONObject
  194. const permissions = userInfoObj['permissions'] as any[]
  195. saveUserInfo({
  196. userId: userInfo['userId'],
  197. userName: userInfo['userName'],
  198. nickName: userInfo['nickName'],
  199. phone: userInfo['phonenumber'],
  200. deptName: deptInfo['deptName'],
  201. roleNames: userInfoObj['roleNames'],
  202. permissions: permissions
  203. })
  204. // 保存或清除记住的账号密码
  205. if (rememberPassword.value) {
  206. saveRememberedAccount(username.value, password.value)
  207. } else {
  208. clearRememberedAccount()
  209. }
  210. uni.showToast({
  211. title: '登录成功',
  212. icon: 'success'
  213. })
  214. // 跳转到首页
  215. setTimeout(() => {
  216. uni.redirectTo({
  217. url: '/pages/index/index'
  218. })
  219. }, 1000)
  220. } catch (e: any) {
  221. uni.showToast({
  222. title: e.message ?? '登录失败',
  223. icon: 'none',
  224. duration: 2000
  225. })
  226. } finally {
  227. loading.value = false
  228. }
  229. }
  230. // 初始化:加载记住的账号密码
  231. onMounted(() => {
  232. checkUpdate()
  233. const remembered = getRememberedAccount()
  234. if (remembered != null) {
  235. username.value = remembered['username'] as string
  236. password.value = remembered['password'] as string
  237. rememberPassword.value = true
  238. }
  239. })
  240. </script>
  241. <style lang="scss">
  242. .login-page {
  243. position: relative;
  244. flex: 1;
  245. padding-top: env(safe-area-inset-top);
  246. }
  247. .bg-image {
  248. position: absolute;
  249. top: 0;
  250. left: 0;
  251. width: 100%;
  252. height: 100%;
  253. z-index: 0;
  254. }
  255. .login-content {
  256. position: relative;
  257. flex: 1;
  258. padding: 60rpx 40rpx;
  259. z-index: 1;
  260. }
  261. .header {
  262. align-items: center;
  263. margin-bottom: 80rpx;
  264. }
  265. .logo {
  266. width: 200rpx;
  267. height: 200rpx;
  268. margin-bottom: 40rpx;
  269. }
  270. .title {
  271. font-size: 48rpx;
  272. color: #ffffff;
  273. font-weight: bold;
  274. // #ifndef APP-HARMONY
  275. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  276. // #endif
  277. }
  278. .form-card {
  279. background: rgba(255, 255, 255, 0.95);
  280. border-radius: 24rpx;
  281. padding: 50rpx 40rpx;
  282. // #ifndef APP-HARMONY
  283. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  284. // #endif
  285. margin-bottom: 100rpx;
  286. }
  287. .form-item {
  288. margin-bottom: 40rpx;
  289. }
  290. .label {
  291. font-size: 32rpx;
  292. color: #333333;
  293. font-weight: bold;
  294. margin-bottom: 20rpx;
  295. }
  296. .input-wrapper {
  297. position: relative;
  298. width: 100%;
  299. }
  300. .input {
  301. width: 100%;
  302. height: 90rpx;
  303. padding: 0 100rpx 0 30rpx;
  304. background-color: #f5f5f5;
  305. border-radius: 12rpx;
  306. font-size: 30rpx;
  307. color: #333333;
  308. border: 1rpx solid transparent;
  309. }
  310. .input:focus {
  311. border-color: #007aff;
  312. background-color: #ffffff;
  313. }
  314. .eye-icon {
  315. position: absolute;
  316. right: 30rpx;
  317. top: 50%;
  318. transform: translateY(-50%);
  319. width: 40rpx;
  320. height: 40rpx;
  321. }
  322. .remember-box {
  323. flex-direction: row;
  324. align-items: center;
  325. margin-bottom: 50rpx;
  326. }
  327. .checkbox {
  328. transform: scale(0.8);
  329. }
  330. .remember-text {
  331. font-size: 28rpx;
  332. color: #1677ff;
  333. margin-left: 12rpx;
  334. }
  335. .login-btn {
  336. width: 100%;
  337. height: 100rpx;
  338. background-color: #0081ff;
  339. border-radius: 50rpx;
  340. color: #ffffff;
  341. // #ifndef APP-HARMONY
  342. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  343. // #endif
  344. }
  345. .login-btn-text {
  346. font-size: 36rpx;
  347. color: #ffffff !important;
  348. font-weight: bold;
  349. }
  350. .footer {
  351. position: fixed;
  352. bottom: 60rpx;
  353. left: 0;
  354. right: 0;
  355. align-items: center;
  356. z-index: 2;
  357. }
  358. .version {
  359. font-size: 28rpx;
  360. color: #333333;
  361. margin-bottom: 15rpx;
  362. }
  363. .company {
  364. font-size: 24rpx;
  365. color: #333333;
  366. }
  367. .picker-modal {
  368. position: fixed;
  369. top: 0;
  370. left: 0;
  371. right: 0;
  372. bottom: 0;
  373. z-index: 1000;
  374. }
  375. .modal-mask {
  376. position: absolute;
  377. top: 0;
  378. left: 0;
  379. right: 0;
  380. bottom: 0;
  381. background-color: rgba(0, 0, 0, 0.5);
  382. }
  383. .modal-content {
  384. position: absolute;
  385. bottom: 0;
  386. left: 0;
  387. right: 0;
  388. background-color: #ffffff;
  389. border-top-left-radius: 16rpx;
  390. border-top-right-radius: 16rpx;
  391. max-height: 1000rpx;
  392. }
  393. .modal-header {
  394. flex-direction: row;
  395. justify-content: space-between;
  396. align-items: center;
  397. padding: 30rpx;
  398. border-bottom: 1rpx solid #f0f0f0;
  399. }
  400. .modal-title {
  401. font-size: 32rpx;
  402. font-weight: bold;
  403. color: #333333;
  404. }
  405. .modal-close {
  406. font-size: 28rpx;
  407. color: #007aff;
  408. }
  409. .form-item-btn{
  410. flex: 1;
  411. align-items: center;
  412. margin-bottom: 10px;
  413. }
  414. .form-item-input{
  415. flex: 1;
  416. flex-direction: row;
  417. background-color: #ffffff;
  418. padding: 10px;
  419. }
  420. .btn-primary {
  421. z-index: 999;
  422. font-size: 15px;
  423. border-radius: 10rpx;
  424. width: 100px;
  425. padding: 5px;
  426. background-color: #165DFF;
  427. line-height: 45rpx;
  428. color: #ffffff;
  429. .btn-text{
  430. color: #ffffff;
  431. padding: 5px 15px;
  432. }
  433. }
  434. .label-picker{
  435. font-size: 32rpx;
  436. color: #333333;
  437. font-weight: bold;
  438. width: 180rpx;
  439. padding: 20rpx;
  440. }
  441. .input-picker {
  442. width: 70%;
  443. height: 90rpx;
  444. padding: 0 100rpx 0 30rpx;
  445. background-color: #f5f5f5;
  446. border-radius: 12rpx;
  447. font-size: 30rpx;
  448. color: #333333;
  449. border: 1rpx solid transparent;
  450. }
  451. .eye-icon-picker {
  452. position: absolute;
  453. right: 230rpx;
  454. top: 50%;
  455. transform: translateY(-50%);
  456. width: 40rpx;
  457. height: 40rpx;
  458. }
  459. </style>