|
| 1 | +<script setup lang="ts"> |
| 2 | +import useUserStore from '@/store/modules/user' |
| 3 | +import { FormControl, FormField, FormItem, FormMessage } from '@/ui/shadcn/ui/form' |
| 4 | +import { toTypedSchema } from '@vee-validate/zod' |
| 5 | +import { useForm } from 'vee-validate' |
| 6 | +import { toast } from 'vue-sonner' |
| 7 | +import * as z from 'zod' |
| 8 | +
|
| 9 | +defineOptions({ |
| 10 | + name: 'EditPasswordForm', |
| 11 | +}) |
| 12 | +
|
| 13 | +const userStore = useUserStore() |
| 14 | +
|
| 15 | +const loading = ref(false) |
| 16 | +
|
| 17 | +const form = useForm({ |
| 18 | + validationSchema: toTypedSchema( |
| 19 | + z.object({ |
| 20 | + password: z.string().min(1, '请输入原密码'), |
| 21 | + newPassword: z.string().min(1, '请输入新密码').min(6, '密码长度为6到18位').max(18, '密码长度为6到18位'), |
| 22 | + checkPassword: z.string().min(1, '请确认新密码'), |
| 23 | + }).refine(data => data.newPassword === data.checkPassword, { |
| 24 | + message: '两次输入的密码不一致', |
| 25 | + path: ['checkPassword'], |
| 26 | + }), |
| 27 | + ), |
| 28 | + initialValues: { |
| 29 | + password: '', |
| 30 | + newPassword: '', |
| 31 | + checkPassword: '', |
| 32 | + }, |
| 33 | +}) |
| 34 | +const onSubmit = form.handleSubmit((values) => { |
| 35 | + loading.value = true |
| 36 | + userStore.editPassword(values).then(async () => { |
| 37 | + toast.success('模拟修改成功,请重新登录') |
| 38 | + userStore.logout() |
| 39 | + }).finally(() => { |
| 40 | + loading.value = false |
| 41 | + }) |
| 42 | +}) |
| 43 | +</script> |
| 44 | + |
| 45 | +<template> |
| 46 | + <div class="w-full flex-col-stretch-center"> |
| 47 | + <div class="mb-6 space-y-2"> |
| 48 | + <h3 class="text-4xl color-[var(--el-text-color-primary)] font-bold"> |
| 49 | + 修改密码 |
| 50 | + </h3> |
| 51 | + <p class="text-sm text-muted-foreground lg:text-base"> |
| 52 | + 请输入原密码、新密码和确认密码 |
| 53 | + </p> |
| 54 | + </div> |
| 55 | + <form @submit="onSubmit"> |
| 56 | + <FormField v-slot="{ componentField, errors }" name="password"> |
| 57 | + <FormItem class="relative pb-6 space-y-0"> |
| 58 | + <FormControl> |
| 59 | + <FaInput type="password" placeholder="请输入原密码" class="w-full" :class="errors.length && 'border-destructive'" v-bind="componentField" /> |
| 60 | + </FormControl> |
| 61 | + <Transition enter-active-class="transition-opacity" enter-from-class="opacity-0" leave-active-class="transition-opacity" leave-to-class="opacity-0"> |
| 62 | + <FormMessage class="absolute bottom-1 text-xs" /> |
| 63 | + </Transition> |
| 64 | + </FormItem> |
| 65 | + </FormField> |
| 66 | + <FormField v-slot="{ componentField, errors }" name="newPassword"> |
| 67 | + <FormItem class="relative pb-6 space-y-0"> |
| 68 | + <FormControl> |
| 69 | + <FaInput type="password" placeholder="请输入新密码" class="w-full" :class="errors.length && 'border-destructive'" v-bind="componentField" /> |
| 70 | + </FormControl> |
| 71 | + <Transition enter-active-class="transition-opacity" enter-from-class="opacity-0" leave-active-class="transition-opacity" leave-to-class="opacity-0"> |
| 72 | + <FormMessage class="absolute bottom-1 text-xs" /> |
| 73 | + </Transition> |
| 74 | + </FormItem> |
| 75 | + </FormField> |
| 76 | + <FormField v-slot="{ componentField, errors }" name="checkPassword"> |
| 77 | + <FormItem class="relative pb-6 space-y-0"> |
| 78 | + <FormControl> |
| 79 | + <FaInput type="password" placeholder="请确认新密码" class="w-full" :class="errors.length && 'border-destructive'" v-bind="componentField" /> |
| 80 | + </FormControl> |
| 81 | + <Transition enter-active-class="transition-opacity" enter-from-class="opacity-0" leave-active-class="transition-opacity" leave-to-class="opacity-0"> |
| 82 | + <FormMessage class="absolute bottom-1 text-xs" /> |
| 83 | + </Transition> |
| 84 | + </FormItem> |
| 85 | + </FormField> |
| 86 | + <FaButton :loading="loading" size="lg" class="mt-8 w-full" type="submit"> |
| 87 | + 保存 |
| 88 | + </FaButton> |
| 89 | + </form> |
| 90 | + </div> |
| 91 | +</template> |
0 commit comments