login.vue 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <script lang="ts" setup>
  2. import type { LoginAndRegisterParams } from '@vben/common-ui';
  3. import { useRequest } from '@vben-core/request';
  4. import { useAccessStore } from '@vben-core/stores';
  5. import { getUserInfo, userLogin } from '@/apis';
  6. import { AuthenticationLogin } from '@vben/common-ui';
  7. import { $t } from '@vben/locales';
  8. import { notification } from 'ant-design-vue';
  9. import { computed } from 'vue';
  10. import { useRouter } from 'vue-router';
  11. defineOptions({ name: 'Login' });
  12. const router = useRouter();
  13. const accessStore = useAccessStore();
  14. const { loading, runAsync: runUserLogin } = useRequest(userLogin, {
  15. manual: true,
  16. });
  17. const { loading: userInfoLoading, runAsync: runGetUserInfo } = useRequest(
  18. getUserInfo,
  19. {
  20. manual: true,
  21. },
  22. );
  23. /**
  24. * 异步处理登录操作
  25. * Asynchronously handle the login process
  26. * @param values 登录表单数据
  27. */
  28. async function handleLogin(values: LoginAndRegisterParams) {
  29. // 异步处理用户登录操作并获取 accessToken
  30. // Asynchronously handle the user login operation and obtain the accessToken
  31. const { accessToken } = await runUserLogin(values);
  32. // 如果成功获取到 accessToken
  33. // If accessToken is successfully obtained
  34. if (accessToken) {
  35. // 将 accessToken 存储到 accessStore 中
  36. // Store the accessToken in accessStore
  37. accessStore.setAccessToken(accessToken);
  38. // 获取用户信息并存储到 accessStore 中
  39. // Get user information and store it in accessStore
  40. const userInfo = await runGetUserInfo();
  41. accessStore.setUserInfo(userInfo);
  42. // 跳转到用户信息中定义的 homePath 路径
  43. // Redirect to the homePath defined in the user information
  44. await router.push(userInfo.homePath);
  45. notification.success({
  46. description: `${$t('authentication.login-success-desc')}:${userInfo.realName}`,
  47. duration: 3,
  48. message: $t('authentication.login-success'),
  49. });
  50. }
  51. }
  52. const loginLoading = computed(() => {
  53. return loading.value || userInfoLoading.value;
  54. });
  55. </script>
  56. <template>
  57. <AuthenticationLogin
  58. username-placeholder="vben"
  59. password-placeholder="123456"
  60. :loading="loginLoading"
  61. @submit="handleLogin"
  62. />
  63. </template>