BasicInfo.vue 2.3 KB

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