BasicInfo.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 :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. defineOptions({ name: 'BasicInfo' })
  26. const { t } = useI18n()
  27. const message = useMessage() // 消息弹窗
  28. const userStore = useUserStore()
  29. // 表单校验
  30. const rules = reactive<FormRules>({
  31. nickname: [{ required: true, message: t('profile.rules.nickname'), trigger: 'blur' }],
  32. email: [
  33. { required: true, message: t('profile.rules.mail'), trigger: 'blur' },
  34. {
  35. type: 'email',
  36. message: t('profile.rules.truemail'),
  37. trigger: ['blur', 'change']
  38. }
  39. ],
  40. mobile: [
  41. { required: true, message: t('profile.rules.phone'), trigger: 'blur' },
  42. {
  43. pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
  44. message: t('profile.rules.truephone'),
  45. trigger: 'blur'
  46. }
  47. ]
  48. })
  49. const schema = reactive<FormSchema[]>([
  50. {
  51. field: 'nickname',
  52. label: t('profile.user.nickname'),
  53. component: 'Input'
  54. },
  55. {
  56. field: 'mobile',
  57. label: t('profile.user.mobile'),
  58. component: 'Input'
  59. },
  60. {
  61. field: 'email',
  62. label: t('profile.user.email'),
  63. component: 'Input'
  64. },
  65. {
  66. field: 'sex',
  67. label: t('profile.user.sex'),
  68. component: 'InputNumber',
  69. value: 0
  70. }
  71. ])
  72. const formRef = ref<FormExpose>() // 表单 Ref
  73. const submit = () => {
  74. const elForm = unref(formRef)?.getElFormRef()
  75. if (!elForm) return
  76. elForm.validate(async (valid) => {
  77. if (valid) {
  78. const data = unref(formRef)?.formModel as UserProfileUpdateReqVO
  79. await updateUserProfile(data)
  80. message.success(t('common.updateSuccess'))
  81. const profile = await init()
  82. userStore.setUserNicknameAction(profile.nickname)
  83. }
  84. })
  85. }
  86. const init = async () => {
  87. const res = await getUserProfile()
  88. unref(formRef)?.setValues(res)
  89. return res
  90. }
  91. onMounted(async () => {
  92. await init()
  93. })
  94. </script>