BasicInfo.vue 2.3 KB

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