BasicInfo.vue 2.4 KB

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