BasicInfo.vue 2.5 KB

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