提交 | 用户 | 时间
|
314507
|
1 |
<template> |
H |
2 |
<Form ref="formRef" :labelWidth="200" :rules="rules" :schema="schema"> |
|
3 |
<template #sex="form"> |
|
4 |
<el-radio-group v-model="form['sex']"> |
|
5 |
<el-radio :label="1">{{ t('profile.user.man') }}</el-radio> |
|
6 |
<el-radio :label="2">{{ t('profile.user.woman') }}</el-radio> |
|
7 |
</el-radio-group> |
|
8 |
</template> |
|
9 |
</Form> |
|
10 |
<div style="text-align: center"> |
|
11 |
<XButton :title="t('common.save')" type="primary" @click="submit()" /> |
|
12 |
<XButton :title="t('common.reset')" type="danger" @click="init()" /> |
|
13 |
</div> |
|
14 |
</template> |
|
15 |
<script lang="ts" setup> |
|
16 |
import type { FormRules } from 'element-plus' |
|
17 |
import { FormSchema } from '@/types/form' |
|
18 |
import type { FormExpose } from '@/components/Form' |
|
19 |
import { |
|
20 |
getUserProfile, |
|
21 |
updateUserProfile, |
|
22 |
UserProfileUpdateReqVO |
|
23 |
} from '@/api/system/user/profile' |
|
24 |
import { useUserStore } from '@/store/modules/user' |
|
25 |
|
|
26 |
defineOptions({ name: 'BasicInfo' }) |
|
27 |
|
|
28 |
const { t } = useI18n() |
|
29 |
const message = useMessage() // 消息弹窗 |
|
30 |
const userStore = useUserStore() |
|
31 |
// 表单校验 |
|
32 |
const rules = reactive<FormRules>({ |
|
33 |
nickname: [{ required: true, message: t('profile.rules.nickname'), trigger: 'blur' }], |
|
34 |
email: [ |
|
35 |
{ required: true, message: t('profile.rules.mail'), trigger: 'blur' }, |
|
36 |
{ |
|
37 |
type: 'email', |
|
38 |
message: t('profile.rules.truemail'), |
|
39 |
trigger: ['blur', 'change'] |
|
40 |
} |
|
41 |
], |
|
42 |
mobile: [ |
|
43 |
{ required: true, message: t('profile.rules.phone'), trigger: 'blur' }, |
|
44 |
{ |
|
45 |
pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, |
|
46 |
message: t('profile.rules.truephone'), |
|
47 |
trigger: 'blur' |
|
48 |
} |
|
49 |
] |
|
50 |
}) |
|
51 |
const schema = reactive<FormSchema[]>([ |
|
52 |
{ |
|
53 |
field: 'nickname', |
|
54 |
label: t('profile.user.nickname'), |
|
55 |
component: 'Input' |
|
56 |
}, |
|
57 |
{ |
|
58 |
field: 'mobile', |
|
59 |
label: t('profile.user.mobile'), |
|
60 |
component: 'Input' |
|
61 |
}, |
|
62 |
{ |
|
63 |
field: 'email', |
|
64 |
label: t('profile.user.email'), |
|
65 |
component: 'Input' |
|
66 |
}, |
|
67 |
{ |
|
68 |
field: 'sex', |
|
69 |
label: t('profile.user.sex'), |
|
70 |
component: 'InputNumber', |
|
71 |
value: 0 |
|
72 |
} |
|
73 |
]) |
|
74 |
const formRef = ref<FormExpose>() // 表单 Ref |
|
75 |
const submit = () => { |
|
76 |
const elForm = unref(formRef)?.getElFormRef() |
|
77 |
if (!elForm) return |
|
78 |
elForm.validate(async (valid) => { |
|
79 |
if (valid) { |
|
80 |
const data = unref(formRef)?.formModel as UserProfileUpdateReqVO |
|
81 |
await updateUserProfile(data) |
|
82 |
message.success(t('common.updateSuccess')) |
|
83 |
const profile = await init() |
|
84 |
userStore.setUserNicknameAction(profile.nickname) |
|
85 |
} |
|
86 |
}) |
|
87 |
} |
|
88 |
const init = async () => { |
|
89 |
const res = await getUserProfile() |
|
90 |
unref(formRef)?.setValues(res) |
|
91 |
return res |
|
92 |
} |
|
93 |
onMounted(async () => { |
|
94 |
await init() |
|
95 |
}) |
|
96 |
</script> |