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