index.uvue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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">工效通</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, loginSSO, 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. const loginSuccess = async(result: any) : Promise<void> => {
  149. // 提取 data 部分
  150. const resultObj = result as UTSJSONObject
  151. // const data = resultObj['data'] as UTSJSONObject
  152. const isInitPassword = resultObj["isInitPassword"] as boolean | null;
  153. // console.log("================"+ code)
  154. if(isInitPassword==true){
  155. showPasswordPicker.value = true;
  156. return
  157. }
  158. uni.setStorageSync("login_key", "1")
  159. // 保存登录信息
  160. saveAccessToken(resultObj['token'] as string)
  161. const userInfoJson = await getUserInfo();
  162. console.log(userInfoJson);
  163. const userInfoObj = userInfoJson as UTSJSONObject
  164. const userInfo = userInfoObj['user'] as UTSJSONObject
  165. const deptInfo = userInfo['dept'] as UTSJSONObject
  166. const permissions = userInfoObj['permissions'] as any[]
  167. saveUserInfo({
  168. userId: userInfo['userId'],
  169. userName: userInfo['userName'],
  170. nickName: userInfo['nickName'],
  171. phone: userInfo['phonenumber'],
  172. deptName: deptInfo['deptName'],
  173. roleNames: userInfoObj['roleNames'],
  174. permissions: permissions
  175. })
  176. // 保存或清除记住的账号密码
  177. if (rememberPassword.value) {
  178. saveRememberedAccount(username.value, password.value)
  179. } else {
  180. clearRememberedAccount()
  181. }
  182. // 跳转到首页
  183. setTimeout(() => {
  184. /* uni.redirectTo({
  185. url: '/pages/index/index'
  186. }) */
  187. uni.redirectTo({
  188. url: '/pages/splash/index'
  189. })
  190. }, 1000)
  191. }
  192. // 登录处理
  193. const handleLogin = async (): Promise<void> => {
  194. // 验证输入
  195. if (username.value.trim().length == 0) {
  196. uni.showToast({
  197. title: '请输入账号',
  198. icon: 'none',
  199. duration: 2000
  200. })
  201. return
  202. }
  203. if (password.value.trim().length == 0) {
  204. uni.showToast({
  205. title: '请输入密码',
  206. icon: 'none',
  207. duration: 2000
  208. })
  209. return
  210. }
  211. try {
  212. loading.value = true
  213. const resultKey = await getIsKey();
  214. const resultKeyObj = resultKey as UTSJSONObject
  215. const isKey = resultKeyObj["data"] as string |'0'
  216. saveStoreIsKey(isKey);
  217. const result = await loginByAccount(username.value, password.value)
  218. loginSuccess(result);
  219. uni.showToast({
  220. title: '登录成功',
  221. icon: 'success'
  222. })
  223. } catch (e: any) {
  224. uni.showToast({
  225. title: e.message ?? '登录失败',
  226. icon: 'none',
  227. duration: 2000
  228. })
  229. } finally {
  230. loading.value = false
  231. }
  232. }
  233. const handleLoginSSO = async (apptoken:string): Promise<void> => {
  234. try {
  235. const resultKey = await getIsKey();
  236. const resultKeyObj = resultKey as UTSJSONObject
  237. const isKey = resultKeyObj["data"] as string |'0'
  238. saveStoreIsKey(isKey);
  239. let result = await loginSSO(apptoken)
  240. console.log('自动登录:', result)
  241. loginSuccess(result);
  242. } catch (e: any) {
  243. uni.showToast({
  244. title: e.message ?? '登录失败',
  245. icon: 'none',
  246. duration: 2000
  247. })
  248. } finally {
  249. loading.value = false
  250. }
  251. }
  252. onLoad((options: UTSJSONObject | null) => {
  253. console.log('URL参数:', options)
  254. if(options != null){
  255. // 获取apptoken参数
  256. const apptokenValue = options['apptoken']
  257. if (apptokenValue != null) {
  258. if (typeof apptokenValue == 'string') {
  259. const apptoken = apptokenValue as string
  260. console.log('获取到自动登录apptoken:', apptoken)
  261. handleLoginSSO(apptoken);
  262. }
  263. }
  264. }
  265. })
  266. // 初始化:加载记住的账号密码
  267. onMounted(() => {
  268. checkUpdate()
  269. const remembered = getRememberedAccount()
  270. if (remembered != null) {
  271. username.value = remembered['username'] as string
  272. password.value = remembered['password'] as string
  273. rememberPassword.value = true
  274. }
  275. })
  276. </script>
  277. <style lang="scss">
  278. .login-page {
  279. position: relative;
  280. flex: 1;
  281. padding-top: env(safe-area-inset-top);
  282. }
  283. .bg-image {
  284. position: absolute;
  285. top: 0;
  286. left: 0;
  287. width: 100%;
  288. height: 100%;
  289. z-index: 0;
  290. }
  291. .login-content {
  292. position: relative;
  293. flex: 1;
  294. padding: 60rpx 40rpx;
  295. z-index: 1;
  296. }
  297. .header {
  298. align-items: center;
  299. margin-bottom: 80rpx;
  300. }
  301. .logo {
  302. width: 200rpx;
  303. height: 200rpx;
  304. margin-bottom: 40rpx;
  305. border-radius:10px;
  306. }
  307. .title {
  308. font-size: 48rpx;
  309. color: #ffffff;
  310. font-weight: bold;
  311. // #ifndef APP-HARMONY
  312. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  313. // #endif
  314. }
  315. .form-card {
  316. background: rgba(255, 255, 255, 0.95);
  317. border-radius: 24rpx;
  318. padding: 50rpx 40rpx;
  319. // #ifndef APP-HARMONY
  320. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  321. // #endif
  322. margin-bottom: 100rpx;
  323. }
  324. .form-item {
  325. margin-bottom: 40rpx;
  326. }
  327. .label {
  328. font-size: 32rpx;
  329. color: #333333;
  330. font-weight: bold;
  331. margin-bottom: 20rpx;
  332. }
  333. .input-wrapper {
  334. position: relative;
  335. width: 100%;
  336. }
  337. .input {
  338. width: 100%;
  339. height: 90rpx;
  340. padding: 0 100rpx 0 30rpx;
  341. background-color: #f5f5f5;
  342. border-radius: 12rpx;
  343. font-size: 30rpx;
  344. color: #333333;
  345. border: 1rpx solid transparent;
  346. }
  347. .input:focus {
  348. border-color: #007aff;
  349. background-color: #ffffff;
  350. }
  351. .eye-icon {
  352. position: absolute;
  353. right: 30rpx;
  354. top: 50%;
  355. transform: translateY(-50%);
  356. width: 40rpx;
  357. height: 40rpx;
  358. }
  359. .remember-box {
  360. flex-direction: row;
  361. align-items: center;
  362. margin-bottom: 50rpx;
  363. }
  364. .checkbox {
  365. transform: scale(0.8);
  366. }
  367. .remember-text {
  368. font-size: 28rpx;
  369. color: #1677ff;
  370. margin-left: 12rpx;
  371. }
  372. .login-btn {
  373. width: 100%;
  374. height: 100rpx;
  375. line-height: 100rpx;
  376. background-color: #0081ff;
  377. border-radius: 50rpx;
  378. color: #ffffff;
  379. // #ifndef APP-HARMONY
  380. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  381. // #endif
  382. }
  383. .login-btn-text {
  384. font-size: 36rpx;
  385. color: #ffffff !important;
  386. font-weight: bold;
  387. }
  388. .footer {
  389. position: fixed;
  390. bottom: 60rpx;
  391. left: 0;
  392. right: 0;
  393. align-items: center;
  394. z-index: 2;
  395. }
  396. .version {
  397. font-size: 28rpx;
  398. color: #333333;
  399. margin-bottom: 15rpx;
  400. }
  401. .company {
  402. font-size: 24rpx;
  403. color: #333333;
  404. }
  405. .picker-modal {
  406. position: fixed;
  407. top: 0;
  408. left: 0;
  409. right: 0;
  410. bottom: 0;
  411. z-index: 1000;
  412. }
  413. .modal-mask {
  414. position: absolute;
  415. top: 0;
  416. left: 0;
  417. right: 0;
  418. bottom: 0;
  419. background-color: rgba(0, 0, 0, 0.5);
  420. }
  421. .modal-content {
  422. position: absolute;
  423. bottom: 0;
  424. left: 0;
  425. right: 0;
  426. background-color: #ffffff;
  427. border-top-left-radius: 16rpx;
  428. border-top-right-radius: 16rpx;
  429. max-height: 1000rpx;
  430. }
  431. .modal-header {
  432. flex-direction: row;
  433. justify-content: space-between;
  434. align-items: center;
  435. padding: 30rpx;
  436. border-bottom: 1rpx solid #f0f0f0;
  437. }
  438. .modal-title {
  439. font-size: 32rpx;
  440. font-weight: bold;
  441. color: #333333;
  442. }
  443. .modal-close {
  444. font-size: 28rpx;
  445. color: #007aff;
  446. }
  447. .form-item-btn{
  448. flex: 1;
  449. align-items: center;
  450. margin-bottom: 10px;
  451. }
  452. .form-item-input{
  453. flex: 1;
  454. flex-direction: row;
  455. background-color: #ffffff;
  456. padding: 10px;
  457. }
  458. .btn-primary {
  459. z-index: 999;
  460. font-size: 15px;
  461. border-radius: 10rpx;
  462. width: 100px;
  463. padding: 5px;
  464. background-color: #165DFF;
  465. line-height: 45rpx;
  466. color: #ffffff;
  467. .btn-text{
  468. color: #ffffff;
  469. padding: 5px 15px;
  470. }
  471. }
  472. .label-picker{
  473. font-size: 32rpx;
  474. color: #333333;
  475. font-weight: bold;
  476. width: 180rpx;
  477. padding: 20rpx;
  478. }
  479. .input-picker {
  480. width: 70%;
  481. height: 90rpx;
  482. padding: 0 100rpx 0 30rpx;
  483. background-color: #f5f5f5;
  484. border-radius: 12rpx;
  485. font-size: 30rpx;
  486. color: #333333;
  487. border: 1rpx solid transparent;
  488. }
  489. .eye-icon-picker {
  490. position: absolute;
  491. right: 230rpx;
  492. top: 50%;
  493. transform: translateY(-50%);
  494. width: 40rpx;
  495. height: 40rpx;
  496. }
  497. </style>