Login.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <script lang="ts" setup>
  2. import { SvgIcon } from '@/components/common';
  3. import { reactive, ref, toRaw } from 'vue';
  4. import { useMessage } from 'naive-ui';
  5. import { useUserStore } from '@/store/modules/user';
  6. import { useRouter } from 'vue-router';
  7. import { t } from '@/locales';
  8. import { rules } from '@/views/login/data';
  9. const formRef = ref();
  10. const message = useMessage();
  11. const loading = ref(false);
  12. const userStore = useUserStore();
  13. const router = useRouter();
  14. const form = reactive({
  15. username: 'langchat@outlook.com',
  16. password: '123456',
  17. });
  18. const handleSubmit = (e: any) => {
  19. e.preventDefault();
  20. formRef.value.validate(async (errors: any) => {
  21. if (!errors) {
  22. loading.value = true;
  23. userStore
  24. .login(toRaw(form))
  25. .then(async () => {
  26. message.success(t('login.success'));
  27. await router.replace('/');
  28. })
  29. .catch(() => {
  30. loading.value = false;
  31. });
  32. }
  33. });
  34. };
  35. </script>
  36. <template>
  37. <div class="mt-4 login-content-form">
  38. <n-form ref="formRef" :model="form" :rules="rules" label-placement="left" size="large">
  39. <n-form-item class="login-animation1" path="username">
  40. <n-input v-model:value="form.username" :placeholder="t('login.namePlaceholder')">
  41. <template #prefix>
  42. <n-icon color="#808695" size="18">
  43. <SvgIcon icon="material-symbols:person-outline" />
  44. </n-icon>
  45. </template>
  46. </n-input>
  47. </n-form-item>
  48. <n-form-item class="login-animation2" path="password">
  49. <n-input
  50. v-model:value="form.password"
  51. :placeholder="t('login.passPlaceholder')"
  52. showPasswordOn="click"
  53. type="password"
  54. >
  55. <template #prefix>
  56. <n-icon color="#808695" size="18">
  57. <SvgIcon icon="mdi:lock-outline" />
  58. </n-icon>
  59. </template>
  60. </n-input>
  61. </n-form-item>
  62. <n-form-item class="login-animation3">
  63. <n-space class="w-full" vertical>
  64. <n-button :loading="loading" block secondary type="primary" @click="handleSubmit">
  65. {{ t('login.title') }}
  66. </n-button>
  67. </n-space>
  68. </n-form-item>
  69. </n-form>
  70. </div>
  71. </template>
  72. <style lang="less" scoped></style>