|
| 1 | +<script setup lang="ts"> |
| 2 | +import { useRouter } from 'vue-router' |
| 3 | +import { useUserStore } from '@/stores' |
| 4 | +
|
| 5 | +import defaultAvatar from '@/assets/images/default-avatar.svg' |
| 6 | +
|
| 7 | +definePage({ |
| 8 | + name: 'login', |
| 9 | + meta: { |
| 10 | + level: 2, |
| 11 | + i18n: 'home.login', |
| 12 | + }, |
| 13 | +}) |
| 14 | +
|
| 15 | +const { t } = useI18n() |
| 16 | +const router = useRouter() |
| 17 | +const userStore = useUserStore() |
| 18 | +
|
| 19 | +const loading = ref(false) |
| 20 | +const postData = reactive({ |
| 21 | + username: '', |
| 22 | + password: '', |
| 23 | +}) |
| 24 | +const rules = reactive({ |
| 25 | + username: [ |
| 26 | + { required: true, message: t('login.pleaseEnterUsername') }, |
| 27 | + ], |
| 28 | + password: [ |
| 29 | + { required: true, message: t('login.pleaseEnterPassword') }, |
| 30 | + ], |
| 31 | +}) |
| 32 | +
|
| 33 | +async function asyncLogin(values: any) { |
| 34 | + try { |
| 35 | + loading.value = true |
| 36 | + await userStore.login({ ...postData, ...values }) |
| 37 | + const { redirect, ...othersQuery } = router.currentRoute.value.query |
| 38 | + router.push({ |
| 39 | + name: (redirect as string) || 'home', |
| 40 | + query: { |
| 41 | + ...othersQuery, |
| 42 | + }, |
| 43 | + }) |
| 44 | + } |
| 45 | + finally { |
| 46 | + loading.value = false |
| 47 | + } |
| 48 | +} |
| 49 | +</script> |
| 50 | + |
| 51 | +<template> |
| 52 | + <Container :padding-x="0"> |
| 53 | + <div class="m-x-a w-7xl text-center"> |
| 54 | + <div class="mb-32 mt-64"> |
| 55 | + <van-image :src="defaultAvatar" round class="h-64 w-64" /> |
| 56 | + </div> |
| 57 | + <van-form :model="postData" :rules="rules" @submit="asyncLogin"> |
| 58 | + <van-cell-group inset> |
| 59 | + <van-field v-model="postData.username" :rules="rules.username" name="username" :placeholder="t('login.username')" left-icon="contact" /> |
| 60 | + <van-field v-model="postData.password" :rules="rules.password" name="password" :placeholder="t('login.password')" left-icon="lock" type="password" /> |
| 61 | + </van-cell-group> |
| 62 | + <div class="m-16"> |
| 63 | + <van-button :loading="loading" round block type="primary" native-type="submit"> |
| 64 | + {{ t('login.logout') }} |
| 65 | + </van-button> |
| 66 | + </div> |
| 67 | + </van-form> |
| 68 | + </div> |
| 69 | + </Container> |
| 70 | +</template> |
0 commit comments