|
| 1 | +<template> |
| 2 | + <a-modal v-model:visible="visible" title="修改基本信息" @before-ok="save" @close="reset"> |
| 3 | + <GiForm ref="formRef" v-model="form" :options="options" :columns="columns" /> |
| 4 | + </a-modal> |
| 5 | +</template> |
| 6 | + |
| 7 | +<script setup lang="ts"> |
| 8 | +import { updateUserBaseInfo } from '@/apis' |
| 9 | +import { Message } from '@arco-design/web-vue' |
| 10 | +import { GiForm, type Columns } from '@/components/GiForm' |
| 11 | +import { useForm } from '@/hooks' |
| 12 | +import { useUserStore } from '@/stores' |
| 13 | +
|
| 14 | +const options: Options = { |
| 15 | + form: {}, |
| 16 | + col: { xs: 24, sm: 24, md: 24, lg: 24, xl: 24, xxl: 24 }, |
| 17 | + btns: { hide: true } |
| 18 | +} |
| 19 | +
|
| 20 | +const columns: Columns = [ |
| 21 | + { |
| 22 | + label: '昵称', |
| 23 | + field: 'nickname', |
| 24 | + type: 'input', |
| 25 | + rules: [{ required: true, message: '请输入昵称' }] |
| 26 | + }, |
| 27 | + { |
| 28 | + label: '性别', |
| 29 | + field: 'gender', |
| 30 | + type: 'radio-group', |
| 31 | + options: [ |
| 32 | + { label: '男', value: 1 }, |
| 33 | + { label: '女', value: 2 }, |
| 34 | + { label: '未知', value: 0, disabled: true } |
| 35 | + ], |
| 36 | + rules: [{ required: true, message: '请选择性别' }] |
| 37 | + } |
| 38 | +] |
| 39 | +
|
| 40 | +const userStore = useUserStore() |
| 41 | +const userInfo = computed(() => userStore.userInfo) |
| 42 | +const { form, resetForm } = useForm({ |
| 43 | + nickname: userInfo.value.nickname, |
| 44 | + gender: userInfo.value.gender |
| 45 | +}) |
| 46 | +
|
| 47 | +const formRef = ref<InstanceType<typeof GiForm>>() |
| 48 | +// 重置 |
| 49 | +const reset = () => { |
| 50 | + formRef.value?.formRef?.resetFields() |
| 51 | + resetForm() |
| 52 | +} |
| 53 | +
|
| 54 | +const visible = ref(false) |
| 55 | +// 修改 |
| 56 | +const onUpdate = async () => { |
| 57 | + reset() |
| 58 | + visible.value = true |
| 59 | +} |
| 60 | +
|
| 61 | +// 保存 |
| 62 | +const save = async () => { |
| 63 | + const isInvalid = await formRef.value?.formRef?.validate() |
| 64 | + if (isInvalid) return false |
| 65 | + try { |
| 66 | + await updateUserBaseInfo(form) |
| 67 | + Message.success('修改成功') |
| 68 | + // 修改成功后,重新获取用户信息 |
| 69 | + await userStore.getInfo() |
| 70 | + return true |
| 71 | + } catch (error) { |
| 72 | + return false |
| 73 | + } |
| 74 | +} |
| 75 | +
|
| 76 | +defineExpose({ onUpdate }) |
| 77 | +</script> |
0 commit comments