zhou zhou
5 天以前 aaf8a50511d77dbc209ca93bbba308c21179a8bc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<!-- 登录页面 -->
<template>
  <div class="flex w-full h-screen">
    <LoginLeftView />
 
    <div class="relative flex-1">
      <AuthTopBar />
 
      <div class="auth-right-wrap">
        <div class="form">
          <h3 class="title">{{ $t('login.title') }}</h3>
          <p class="sub-title">{{ $t('login.subTitle') }}</p>
          <ElForm
            ref="formRef"
            :model="formData"
            :rules="rules"
            :key="formKey"
            @keyup.enter="handleSubmit"
            style="margin-top: 25px"
          >
            <ElFormItem prop="username">
              <ElInput
                class="custom-height"
                :placeholder="$t('login.placeholder.username')"
                v-model.trim="formData.username"
              />
            </ElFormItem>
            <ElFormItem prop="password">
              <ElInput
                class="custom-height"
                :placeholder="$t('login.placeholder.password')"
                v-model.trim="formData.password"
                type="password"
                autocomplete="off"
                show-password
              />
            </ElFormItem>
 
            <div class="flex-cb mt-2 text-sm">
              <ElCheckbox v-model="formData.rememberPassword">{{
                $t('login.rememberPwd')
              }}</ElCheckbox>
              <RouterLink class="text-theme" :to="{ name: 'ForgetPassword' }">{{
                $t('login.forgetPwd')
              }}</RouterLink>
            </div>
 
            <div style="margin-top: 30px">
              <ElButton
                class="w-full custom-height"
                type="primary"
                @click="handleSubmit"
                :loading="loading"
                v-ripple
              >
                {{ $t('login.btnText') }}
              </ElButton>
            </div>
 
            <div class="mt-5 text-sm text-gray-600">
              <span>{{ $t('login.noAccount') }}</span>
              <RouterLink class="text-theme" :to="{ name: 'Register' }">{{
                $t('login.register')
              }}</RouterLink>
            </div>
          </ElForm>
        </div>
      </div>
    </div>
  </div>
</template>
 
<script setup>
  import AppConfig from '@/config'
  import { useUserStore } from '@/store/modules/user'
  import { useI18n } from 'vue-i18n'
  import { fetchGetUserInfo, fetchLogin, normalizeLoginResponse } from '@/api/auth'
  import { ElNotification } from 'element-plus'
  defineOptions({ name: 'Login' })
  const { t, locale } = useI18n()
  const formKey = ref(0)
  watch(locale, () => {
    formKey.value++
  })
  const userStore = useUserStore()
  const router = useRouter()
  const route = useRoute()
  const systemName = AppConfig.systemInfo.name
  const formRef = ref()
  const formData = reactive({
    username: '',
    password: '',
    rememberPassword: true
  })
  const rules = computed(() => ({
    username: [{ required: true, message: t('login.placeholder.username'), trigger: 'blur' }],
    password: [{ required: true, message: t('login.placeholder.password'), trigger: 'blur' }]
  }))
  const loading = ref(false)
  const handleSubmit = async () => {
    if (!formRef.value) return
    try {
      const valid = await formRef.value.validate()
      if (!valid) return
      loading.value = true
      const payload = normalizeLoginResponse(await fetchLogin(formData))
      if (!payload.accessToken) return
      userStore.setToken(payload.accessToken, payload.refreshToken)
      userStore.setLoginStatus(true)
      const userInfo = await fetchGetUserInfo()
      if (userInfo && Object.keys(userInfo).length > 0) {
        userStore.setUserInfo(userInfo)
      } else if (payload.user && Object.keys(payload.user).length > 0) {
        userStore.setUserInfo(payload.user)
      }
      showLoginSuccessNotice()
      router.push(route.query.redirect || '/')
    } finally {
      loading.value = false
    }
  }
  const showLoginSuccessNotice = () => {
    setTimeout(() => {
      ElNotification({
        title: t('login.success.title'),
        type: 'success',
        duration: 2500,
        zIndex: 1e4,
        message: `${t('login.success.message')}, ${systemName}!`
      })
    }, 1e3)
  }
</script>
 
<style scoped>
  @import './style.css';
</style>