index.uvue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. const permissions = userInfoObj['permissions'] as any[]
  121. saveUserInfo({
  122. userId: userInfo['userId'],
  123. userName: userInfo['userName'],
  124. nickName: userInfo['nickName'],
  125. phone: userInfo['phonenumber'],
  126. deptName: deptInfo['deptName'],
  127. roleNames: userInfoObj['roleNames'],
  128. permissions: permissions
  129. })
  130. // 保存或清除记住的账号密码
  131. if (rememberPassword.value) {
  132. saveRememberedAccount(username.value, password.value)
  133. } else {
  134. clearRememberedAccount()
  135. }
  136. uni.showToast({
  137. title: '登录成功',
  138. icon: 'success'
  139. })
  140. // 跳转到首页
  141. setTimeout(() => {
  142. uni.redirectTo({
  143. url: '/pages/index/index'
  144. })
  145. }, 1000)
  146. } catch (e: any) {
  147. uni.showToast({
  148. title: e.message ?? '登录失败',
  149. icon: 'none',
  150. duration: 2000
  151. })
  152. } finally {
  153. loading.value = false
  154. }
  155. }
  156. // 初始化:加载记住的账号密码
  157. onMounted(() => {
  158. const remembered = getRememberedAccount()
  159. if (remembered != null) {
  160. username.value = remembered['username'] as string
  161. password.value = remembered['password'] as string
  162. rememberPassword.value = true
  163. }
  164. })
  165. </script>
  166. <style lang="scss">
  167. .login-page {
  168. position: relative;
  169. flex: 1;
  170. padding-top: env(safe-area-inset-top);
  171. }
  172. .bg-image {
  173. position: absolute;
  174. top: 0;
  175. left: 0;
  176. width: 100%;
  177. height: 100%;
  178. z-index: 0;
  179. }
  180. .login-content {
  181. position: relative;
  182. flex: 1;
  183. padding: 60rpx 40rpx;
  184. z-index: 1;
  185. }
  186. .header {
  187. align-items: center;
  188. margin-bottom: 80rpx;
  189. }
  190. .logo {
  191. width: 200rpx;
  192. height: 200rpx;
  193. margin-bottom: 40rpx;
  194. }
  195. .title {
  196. font-size: 48rpx;
  197. color: #ffffff;
  198. font-weight: bold;
  199. // #ifndef APP-HARMONY
  200. text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
  201. // #endif
  202. }
  203. .form-card {
  204. background: rgba(255, 255, 255, 0.95);
  205. border-radius: 24rpx;
  206. padding: 50rpx 40rpx;
  207. // #ifndef APP-HARMONY
  208. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.1);
  209. // #endif
  210. margin-bottom: 100rpx;
  211. }
  212. .form-item {
  213. margin-bottom: 40rpx;
  214. }
  215. .label {
  216. font-size: 32rpx;
  217. color: #333333;
  218. font-weight: bold;
  219. margin-bottom: 20rpx;
  220. }
  221. .input-wrapper {
  222. position: relative;
  223. width: 100%;
  224. }
  225. .input {
  226. width: 100%;
  227. height: 90rpx;
  228. padding: 0 100rpx 0 30rpx;
  229. background-color: #f5f5f5;
  230. border-radius: 12rpx;
  231. font-size: 30rpx;
  232. color: #333333;
  233. border: 1rpx solid transparent;
  234. }
  235. .input:focus {
  236. border-color: #007aff;
  237. background-color: #ffffff;
  238. }
  239. .eye-icon {
  240. position: absolute;
  241. right: 30rpx;
  242. top: 50%;
  243. transform: translateY(-50%);
  244. width: 40rpx;
  245. height: 40rpx;
  246. }
  247. .remember-box {
  248. flex-direction: row;
  249. align-items: center;
  250. margin-bottom: 50rpx;
  251. }
  252. .checkbox {
  253. transform: scale(0.8);
  254. }
  255. .remember-text {
  256. font-size: 28rpx;
  257. color: #1677ff;
  258. margin-left: 12rpx;
  259. }
  260. .login-btn {
  261. width: 100%;
  262. height: 100rpx;
  263. background-color: #0081ff;
  264. border-radius: 50rpx;
  265. color: #ffffff;
  266. // #ifndef APP-HARMONY
  267. box-shadow: 0 8rpx 16rpx rgba(0, 122, 255, 0.3);
  268. // #endif
  269. }
  270. .login-btn-text {
  271. font-size: 36rpx;
  272. color: #ffffff !important;
  273. font-weight: bold;
  274. }
  275. .footer {
  276. position: fixed;
  277. bottom: 60rpx;
  278. left: 0;
  279. right: 0;
  280. align-items: center;
  281. z-index: 2;
  282. }
  283. .version {
  284. font-size: 28rpx;
  285. color: #333333;
  286. margin-bottom: 15rpx;
  287. }
  288. .company {
  289. font-size: 24rpx;
  290. color: #333333;
  291. }
  292. </style>