login.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <script lang="ts" setup>
  2. import type { LoginAndRegisterParams } from '@vben/common-ui';
  3. import { getUserInfo, userLogin } from '@/services';
  4. import { AuthenticationLogin } from '@vben/common-ui';
  5. import { useRequest } from '@vben/hooks';
  6. import { $t } from '@vben/locales';
  7. import { useAccessStore } from '@vben/stores';
  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 :loading="loginLoading" @submit="handleLogin" />
  58. </template>