login.vue 2.2 KB

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