index.uvue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. </view>
  43. </template>
  44. <script setup lang="uts">
  45. import { ref, computed, onMounted } from 'vue'
  46. import { loginByAccount, getUserInfo } from '../../api/auth/login'
  47. import {
  48. saveAccessToken,
  49. saveUserInfo,
  50. getRememberedAccount,
  51. saveRememberedAccount,
  52. clearRememberedAccount
  53. } from '../../utils/storage'
  54. import { validatePassword } from '../../utils/validate'
  55. import { encryptAES } from '../../utils/crypto'
  56. // @ts-ignore
  57. import manifest from '@/manifest.json'
  58. // 表单数据
  59. const username = ref<string>("")
  60. const password = ref<string>("")
  61. const rememberPassword = ref<boolean>(false)
  62. const showPassword = ref<boolean>(false)
  63. const loading = ref<boolean>(false)
  64. // 版本号
  65. const manifestVersion = manifest.versionName as string | null
  66. const version = ref<string>(manifestVersion != null ? manifestVersion : '1.0.0')
  67. // 是否可以登录
  68. const canLogin = computed((): boolean => {
  69. return username.value.length > 0 && password.value.length > 0 && !loading.value
  70. })
  71. // 记住密码切换
  72. const handleRememberChange = (): void => {
  73. rememberPassword.value = !rememberPassword.value
  74. }
  75. // 切换密码显示/隐藏
  76. const handleTogglePassword = (): void => {
  77. showPassword.value = !showPassword.value
  78. }
  79. // 登录处理
  80. const handleLogin = async (): Promise<void> => {
  81. // 验证输入
  82. if (username.value.trim().length == 0) {
  83. uni.showToast({
  84. title: '请输入账号',
  85. icon: 'none',
  86. duration: 2000
  87. })
  88. return
  89. }
  90. if (password.value.trim().length == 0) {
  91. uni.showToast({
  92. title: '请输入密码',
  93. icon: 'none',
  94. duration: 2000
  95. })
  96. return
  97. }
  98. // // 验证密码强度
  99. // if (!validatePassword(password.value)) {
  100. // uni.showToast({
  101. // title: '密码强度不足:需包含数字、大小写字母、特殊字符,至少6位',
  102. // icon: 'none',
  103. // duration: 3000
  104. // })
  105. // return
  106. // }
  107. try {
  108. loading.value = true
  109. const result = await loginByAccount(username.value, password.value)
  110. // 提取 data 部分
  111. const resultObj = result as UTSJSONObject
  112. // const data = resultObj['data'] as UTSJSONObject
  113. // 保存登录信息
  114. saveAccessToken(resultObj['token'] as string)
  115. const userInfoJson = await getUserInfo();
  116. console.log(userInfoJson);
  117. const userInfoObj = userInfoJson as UTSJSONObject
  118. const userInfo = userInfoObj['user'] as UTSJSONObject
  119. const deptInfo = userInfo['dept'] as UTSJSONObject
  120. saveUserInfo({
  121. userId: userInfo['userId'],
  122. userName: userInfo['userName'],
  123. nickName: userInfo['nickName'],
  124. phone: userInfo['phonenumber'],
  125. deptName: deptInfo['deptName'],
  126. roleNames: userInfoObj['roleNames']
  127. })
  128. // 保存或清除记住的账号密码
  129. if (rememberPassword.value) {
  130. saveRememberedAccount(username.value, password.value)
  131. } else {
  132. clearRememberedAccount()
  133. }
  134. uni.showToast({
  135. title: '登录成功',
  136. icon: 'success'
  137. })
  138. // 跳转到首页
  139. setTimeout(() => {
  140. uni.redirectTo({
  141. url: '/pages/index/index'
  142. })
  143. }, 1000)
  144. } catch (e: any) {
  145. uni.showToast({
  146. title: e.message ?? '登录失败',
  147. icon: 'none',
  148. duration: 2000
  149. })
  150. } finally {
  151. loading.value = false
  152. }
  153. }
  154. // 初始化:加载记住的账号密码
  155. onMounted(() => {
  156. const remembered = getRememberedAccount()
  157. if (remembered != null) {
  158. username.value = remembered['username'] as string
  159. password.value = remembered['password'] as string
  160. rememberPassword.value = true
  161. }
  162. })
  163. </script>
  164. <style lang="scss">
  165. .login-page {
  166. position: relative;
  167. flex: 1;
  168. padding-top: env(safe-area-inset-top);
  169. }
  170. .bg-image {
  171. position: absolute;
  172. top: 0;
  173. left: 0;
  174. width: 100%;
  175. height: 100%;
  176. z-index: 0;
  177. }
  178. .login-content {
  179. position: relative;
  180. flex: 1;
  181. padding: 60rpx 40rpx;
  182. z-index: 1;
  183. }
  184. .header {
  185. align-items: center;
  186. margin-bottom: 80rpx;
  187. }
  188. .logo {
  189. width: 200rpx;
  190. height: 200rpx;
  191. margin-bottom: 40rpx;
  192. }
  193. .title {
  194. font-size: 48rpx;
  195. color: #ffffff;
  196. font-weight: bold;
  197. // #ifndef APP-HARMONY
  198. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  199. // #endif
  200. }
  201. .form-card {
  202. background: rgba(255, 255, 255, 0.95);
  203. border-radius: 24rpx;
  204. padding: 50rpx 40rpx;
  205. // #ifndef APP-HARMONY
  206. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  207. // #endif
  208. margin-bottom: 100rpx;
  209. }
  210. .form-item {
  211. margin-bottom: 40rpx;
  212. }
  213. .label {
  214. font-size: 32rpx;
  215. color: #333333;
  216. font-weight: bold;
  217. margin-bottom: 20rpx;
  218. }
  219. .input-wrapper {
  220. position: relative;
  221. width: 100%;
  222. }
  223. .input {
  224. width: 100%;
  225. height: 90rpx;
  226. padding: 0 100rpx 0 30rpx;
  227. background-color: #f5f5f5;
  228. border-radius: 12rpx;
  229. font-size: 30rpx;
  230. color: #333333;
  231. border: 1rpx solid transparent;
  232. }
  233. .input:focus {
  234. border-color: #007aff;
  235. background-color: #ffffff;
  236. }
  237. .eye-icon {
  238. position: absolute;
  239. right: 30rpx;
  240. top: 50%;
  241. transform: translateY(-50%);
  242. width: 40rpx;
  243. height: 40rpx;
  244. }
  245. .remember-box {
  246. flex-direction: row;
  247. align-items: center;
  248. margin-bottom: 50rpx;
  249. }
  250. .checkbox {
  251. transform: scale(0.8);
  252. }
  253. .remember-text {
  254. font-size: 28rpx;
  255. color: #1677ff;
  256. margin-left: 12rpx;
  257. }
  258. .login-btn {
  259. width: 100%;
  260. height: 100rpx;
  261. background-color: #0081ff;
  262. border-radius: 50rpx;
  263. color: #ffffff;
  264. // #ifndef APP-HARMONY
  265. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  266. // #endif
  267. }
  268. .login-btn-text {
  269. font-size: 36rpx;
  270. color: #ffffff !important;
  271. font-weight: bold;
  272. }
  273. .footer {
  274. position: fixed;
  275. bottom: 60rpx;
  276. left: 0;
  277. right: 0;
  278. align-items: center;
  279. z-index: 2;
  280. }
  281. .version {
  282. font-size: 28rpx;
  283. color: #333333;
  284. margin-bottom: 15rpx;
  285. }
  286. .company {
  287. font-size: 24rpx;
  288. color: #333333;
  289. }
  290. </style>