BasicInfo.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  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 :value="1">{{ t('profile.user.man') }}</el-radio>
  6. <el-radio :value="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. defineOptions({ name: 'BasicInfo' })
  26. const { t } = useI18n()
  27. const message = useMessage() // 消息弹窗
  28. const userStore = useUserStore()
  29. // 定义事件
  30. const emit = defineEmits<{
  31. (e: 'success'): void
  32. }>()
  33. // 表单校验
  34. const rules = reactive<FormRules>({
  35. nickname: [{ required: true, message: t('profile.rules.nickname'), trigger: 'blur' }],
  36. email: [
  37. { required: true, message: t('profile.rules.mail'), trigger: 'blur' },
  38. {
  39. type: 'email',
  40. message: t('profile.rules.truemail'),
  41. trigger: ['blur', 'change']
  42. }
  43. ],
  44. mobile: [
  45. { required: true, message: t('profile.rules.phone'), trigger: 'blur' },
  46. {
  47. pattern: /^1[3-9]\d{9}$/,
  48. message: t('profile.rules.truephone'),
  49. trigger: 'blur'
  50. }
  51. ]
  52. })
  53. const schema = reactive<FormSchema[]>([
  54. {
  55. field: 'nickname',
  56. label: t('profile.user.nickname'),
  57. component: 'Input'
  58. },
  59. {
  60. field: 'mobile',
  61. label: t('profile.user.mobile'),
  62. component: 'Input'
  63. },
  64. {
  65. field: 'email',
  66. label: t('profile.user.email'),
  67. component: 'Input'
  68. },
  69. {
  70. field: 'sex',
  71. label: t('profile.user.sex'),
  72. component: 'InputNumber',
  73. value: 0
  74. }
  75. ])
  76. const formRef = ref<FormExpose>() // 表单 Ref
  77. // 监听 userStore 中头像的变化,同步更新表单数据
  78. watch(
  79. () => userStore.getUser.avatar,
  80. (newAvatar) => {
  81. if (newAvatar && formRef.value) {
  82. // 直接更新表单模型中的头像字段
  83. const formModel = formRef.value.formModel
  84. if (formModel) {
  85. formModel.avatar = newAvatar
  86. }
  87. }
  88. }
  89. )
  90. const submit = () => {
  91. const elForm = unref(formRef)?.getElFormRef()
  92. if (!elForm) return
  93. elForm.validate(async (valid) => {
  94. if (valid) {
  95. const data = unref(formRef)?.formModel as UserProfileUpdateReqVO
  96. await updateUserProfile(data)
  97. message.success(t('common.updateSuccess'))
  98. const profile = await init()
  99. await userStore.setUserNicknameAction(profile.nickname)
  100. // 发送成功事件
  101. emit('success')
  102. }
  103. })
  104. }
  105. const init = async () => {
  106. const res = await getUserProfile()
  107. unref(formRef)?.setValues(res)
  108. return res
  109. }
  110. onMounted(async () => {
  111. await init()
  112. })
  113. </script>