login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <template>
  2. <view class="login-container">
  3. <view class="login-wrapper">
  4. <!-- Logo 区域 -->
  5. <view class="logo-section">
  6. <image class="logo" src="/static/logo.png" mode="aspectFit" />
  7. <text class="app-name">系统登录</text>
  8. </view>
  9. <!-- 登录表单 -->
  10. <view class="form-section">
  11. <uni-forms
  12. ref="loginFormRef"
  13. :model="loginForm"
  14. :rules="loginRules"
  15. label-position="top"
  16. >
  17. <!-- 用户名输入框 -->
  18. <uni-forms-item label="用户名" name="username" required>
  19. <uni-easyinput
  20. v-model="loginForm.username"
  21. placeholder="请输入用户名"
  22. :clearable="true"
  23. :focus="false"
  24. @input="clearError"
  25. >
  26. <template #left>
  27. <uni-icons type="person" size="20" color="#999" />
  28. </template>
  29. </uni-easyinput>
  30. </uni-forms-item>
  31. <!-- 密码输入框 -->
  32. <uni-forms-item label="密码" name="password" required>
  33. <uni-easyinput
  34. v-model="loginForm.password"
  35. type="password"
  36. placeholder="请输入密码"
  37. :clearable="true"
  38. @input="clearError"
  39. >
  40. <template #left>
  41. <uni-icons type="locked" size="20" color="#999" />
  42. </template>
  43. </uni-easyinput>
  44. </uni-forms-item>
  45. <!-- 验证码输入框 -->
  46. <uni-forms-item
  47. v-if="needCaptcha"
  48. label="验证码"
  49. name="code"
  50. required
  51. >
  52. <view class="captcha-container">
  53. <uni-easyinput
  54. v-model="loginForm.code"
  55. placeholder="请输入验证码"
  56. :clearable="true"
  57. class="captcha-input"
  58. @input="clearError"
  59. >
  60. <template #left>
  61. <uni-icons type="checkmarkempty" size="20" color="#999" />
  62. </template>
  63. </uni-easyinput>
  64. <image
  65. v-if="captchaImage"
  66. :src="captchaImage"
  67. class="captcha-image"
  68. @click="refreshCaptcha"
  69. />
  70. </view>
  71. </uni-forms-item>
  72. <!-- 记住我 -->
  73. <view class="remember-section">
  74. <uni-data-checkbox
  75. v-model="loginForm.rememberMe"
  76. :localdata="rememberOptions"
  77. mode="tag"
  78. />
  79. </view>
  80. <!-- 错误提示 -->
  81. <view v-if="errorMessage" class="error-message">
  82. <uni-icons type="info" color="#f56c6c" size="16" />
  83. <text class="error-text">{{ errorMessage }}</text>
  84. </view>
  85. <!-- 登录按钮 -->
  86. <button
  87. class="login-btn"
  88. :class="{ 'login-btn--loading': loading }"
  89. :disabled="loading"
  90. @click="handleLogin"
  91. >
  92. <uni-icons
  93. v-if="loading"
  94. type="spinner-cycle"
  95. class="loading-icon"
  96. size="18"
  97. color="#fff"
  98. />
  99. {{ loading ? '登录中...' : '登录' }}
  100. </button>
  101. </uni-forms>
  102. </view>
  103. <!-- 底部链接 -->
  104. <view class="footer-section">
  105. <text class="footer-link" @click="handleForgotPassword">忘记密码?</text>
  106. <text class="footer-divider">|</text>
  107. <text class="footer-link" @click="handleRegister">注册账号</text>
  108. </view>
  109. </view>
  110. </view>
  111. </template>
  112. <script setup lang="ts">
  113. import { reactive, ref, onMounted } from 'vue';
  114. import { useAuthStore } from '@/store/modules/auth';
  115. import type { LoginForm } from '@/store/modules/auth';
  116. // Store
  117. const authStore = useAuthStore();
  118. // 响应式数据
  119. const loginFormRef = ref();
  120. const loading = ref<boolean>(false);
  121. const errorMessage = ref<string>('');
  122. const needCaptcha = ref<boolean>(false);
  123. const captchaImage = ref<string>('');
  124. // 登录表单
  125. const loginForm = reactive<LoginForm>({
  126. username: '',
  127. password: '',
  128. code: '',
  129. uuid: '',
  130. rememberMe: false
  131. });
  132. // 记住我选项
  133. const rememberOptions = [
  134. {
  135. text: '记住我',
  136. value: true
  137. }
  138. ];
  139. // 表单验证规则
  140. const loginRules = {
  141. username: [
  142. {
  143. required: true,
  144. errorMessage: '请输入用户名'
  145. },
  146. {
  147. minLength: 2,
  148. maxLength: 20,
  149. errorMessage: '用户名长度在 2 到 20 个字符'
  150. }
  151. ],
  152. password: [
  153. {
  154. required: true,
  155. errorMessage: '请输入密码'
  156. },
  157. {
  158. minLength: 6,
  159. maxLength: 20,
  160. errorMessage: '密码长度在 6 到 20 个字符'
  161. }
  162. ],
  163. code: [
  164. {
  165. required: true,
  166. errorMessage: '请输入验证码'
  167. },
  168. {
  169. minLength: 4,
  170. maxLength: 6,
  171. errorMessage: '验证码长度在 4 到 6 个字符'
  172. }
  173. ]
  174. };
  175. /**
  176. * 清除错误信息
  177. */
  178. const clearError = (): void => {
  179. errorMessage.value = '';
  180. };
  181. /**
  182. * 获取验证码
  183. */
  184. const getCaptcha = async (): Promise<void> => {
  185. try {
  186. // 这里应该调用获取验证码的API
  187. // const response = await useClientRequest.get('/auth/captcha');
  188. // if (response.code === 200) {
  189. // captchaImage.value = response.data.image;
  190. // loginForm.uuid = response.data.uuid;
  191. // needCaptcha.value = true;
  192. // }
  193. // 临时模拟
  194. needCaptcha.value = false;
  195. } catch (error) {
  196. console.error('获取验证码失败:', error);
  197. }
  198. };
  199. /**
  200. * 刷新验证码
  201. */
  202. const refreshCaptcha = (): void => {
  203. getCaptcha();
  204. };
  205. /**
  206. * 处理登录
  207. */
  208. const handleLogin = async (): Promise<void> => {
  209. try {
  210. // 表单验证
  211. const valid = await loginFormRef.value?.validate();
  212. if (!valid) {
  213. return;
  214. }
  215. loading.value = true;
  216. errorMessage.value = '';
  217. // 调用登录
  218. const success = await authStore.login(loginForm);
  219. if (success) {
  220. uni.showToast({
  221. title: '登录成功',
  222. icon: 'success'
  223. });
  224. // 登录成功后跳转
  225. setTimeout(() => {
  226. const pages = getCurrentPages();
  227. const currentPage = pages[pages.length - 1];
  228. const options = (currentPage as any).options || {};
  229. const redirectUrl = options.redirect || '/pages/index/index';
  230. uni.reLaunch({
  231. url: redirectUrl
  232. });
  233. }, 1500);
  234. } else {
  235. errorMessage.value = authStore.loginError || '登录失败,请重试';
  236. // 如果是验证码错误,刷新验证码
  237. if (needCaptcha.value) {
  238. refreshCaptcha();
  239. loginForm.code = '';
  240. }
  241. }
  242. } catch (error: any) {
  243. console.error('登录错误:', error);
  244. errorMessage.value = error.message || '网络错误,请稍后重试';
  245. } finally {
  246. loading.value = false;
  247. }
  248. };
  249. /**
  250. * 忘记密码
  251. */
  252. const handleForgotPassword = (): void => {
  253. uni.showToast({
  254. title: '功能开发中',
  255. icon: 'none'
  256. });
  257. };
  258. /**
  259. * 注册账号
  260. */
  261. const handleRegister = (): void => {
  262. uni.showToast({
  263. title: '功能开发中',
  264. icon: 'none'
  265. });
  266. };
  267. /**
  268. * 页面加载
  269. */
  270. onMounted(() => {
  271. // 检查是否需要验证码
  272. getCaptcha();
  273. // 如果已经登录,直接跳转
  274. if (authStore.isLoggedIn) {
  275. uni.reLaunch({
  276. url: '/pages/index/index'
  277. });
  278. }
  279. });
  280. </script>
  281. <style lang="scss" scoped>
  282. .login-container {
  283. min-height: 100vh;
  284. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  285. display: flex;
  286. align-items: center;
  287. justify-content: center;
  288. padding: 40rpx;
  289. }
  290. .login-wrapper {
  291. width: 100%;
  292. max-width: 600rpx;
  293. background: #fff;
  294. border-radius: 20rpx;
  295. padding: 60rpx 40rpx;
  296. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.1);
  297. }
  298. .logo-section {
  299. text-align: center;
  300. margin-bottom: 60rpx;
  301. .logo {
  302. width: 120rpx;
  303. height: 120rpx;
  304. margin-bottom: 20rpx;
  305. }
  306. .app-name {
  307. font-size: 32rpx;
  308. font-weight: 600;
  309. color: #333;
  310. }
  311. }
  312. .form-section {
  313. .captcha-container {
  314. display: flex;
  315. align-items: center;
  316. gap: 20rpx;
  317. .captcha-input {
  318. flex: 1;
  319. }
  320. .captcha-image {
  321. width: 160rpx;
  322. height: 80rpx;
  323. border: 1rpx solid #ddd;
  324. border-radius: 8rpx;
  325. cursor: pointer;
  326. }
  327. }
  328. .remember-section {
  329. margin: 30rpx 0;
  330. }
  331. .error-message {
  332. display: flex;
  333. align-items: center;
  334. gap: 10rpx;
  335. margin-bottom: 30rpx;
  336. padding: 20rpx;
  337. background: #fef0f0;
  338. border: 1rpx solid #fbc4c4;
  339. border-radius: 8rpx;
  340. .error-text {
  341. font-size: 28rpx;
  342. color: #f56c6c;
  343. }
  344. }
  345. .login-btn {
  346. width: 100%;
  347. height: 88rpx;
  348. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  349. color: #fff;
  350. border: none;
  351. border-radius: 12rpx;
  352. font-size: 32rpx;
  353. font-weight: 600;
  354. display: flex;
  355. align-items: center;
  356. justify-content: center;
  357. gap: 10rpx;
  358. transition: all 0.3s ease;
  359. &:not([disabled]):active {
  360. transform: translateY(2rpx);
  361. opacity: 0.9;
  362. }
  363. &[disabled] {
  364. opacity: 0.7;
  365. }
  366. &--loading {
  367. .loading-icon {
  368. animation: spin 1s linear infinite;
  369. }
  370. }
  371. }
  372. }
  373. .footer-section {
  374. text-align: center;
  375. margin-top: 40rpx;
  376. display: flex;
  377. align-items: center;
  378. justify-content: center;
  379. gap: 20rpx;
  380. .footer-link {
  381. font-size: 28rpx;
  382. color: #666;
  383. cursor: pointer;
  384. &:hover {
  385. color: #667eea;
  386. }
  387. }
  388. .footer-divider {
  389. font-size: 28rpx;
  390. color: #ddd;
  391. }
  392. }
  393. @keyframes spin {
  394. from {
  395. transform: rotate(0deg);
  396. }
  397. to {
  398. transform: rotate(360deg);
  399. }
  400. }
  401. // uni-app 组件样式重写
  402. ::v-deep .uni-forms-item {
  403. margin-bottom: 30rpx;
  404. }
  405. ::v-deep .uni-forms-item__label {
  406. font-size: 28rpx;
  407. color: #666;
  408. margin-bottom: 10rpx;
  409. }
  410. ::v-deep .uni-easyinput {
  411. .uni-easyinput__content {
  412. height: 88rpx;
  413. border: 1rpx solid #ddd;
  414. border-radius: 12rpx;
  415. background: #fafafa;
  416. &.uni-easyinput__content-focus {
  417. border-color: #667eea;
  418. background: #fff;
  419. }
  420. }
  421. .uni-easyinput__content-input {
  422. font-size: 30rpx;
  423. color: #333;
  424. padding-left: 20rpx;
  425. }
  426. .uni-easyinput__placeholder-class {
  427. font-size: 30rpx;
  428. color: #999;
  429. }
  430. }
  431. ::v-deep .uni-data-checklist {
  432. .checklist-group {
  433. .checklist-box {
  434. border: none;
  435. padding: 0;
  436. margin: 0;
  437. .checklist-content {
  438. font-size: 28rpx;
  439. color: #666;
  440. }
  441. .checkbox__inner {
  442. border-color: #667eea;
  443. &.checkbox__inner--checked {
  444. background-color: #667eea;
  445. border-color: #667eea;
  446. }
  447. }
  448. }
  449. }
  450. }
  451. </style>