Write.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <script setup lang="tsx">
  2. import { Form, FormSchema } from '@/components/Form'
  3. import { useForm } from '@/hooks/web/useForm'
  4. import { PropType, reactive, watch, ref } from 'vue'
  5. import { useValidator } from '@/hooks/web/useValidator'
  6. // import { useI18n } from '@/hooks/web/useI18n'
  7. import { getInfoApi } from '@/api/user'
  8. import { getRoleOptionsApi } from '@/api/role'
  9. // const { t } = useI18n()
  10. const { required, isMobilePhone } = useValidator()
  11. const props = defineProps({
  12. currentRow: {
  13. type: Object as PropType<any>,
  14. default: () => null
  15. }
  16. })
  17. const formSchema = ref<FormSchema[]>([
  18. {
  19. field: 'name',
  20. label: '用户名',
  21. component: 'Input',
  22. colProps: {
  23. span: 24
  24. }
  25. },
  26. {
  27. field: 'gender',
  28. label: '性别',
  29. component: 'RadioGroup',
  30. colProps: {
  31. span: 24
  32. },
  33. componentProps: {
  34. options: [
  35. {
  36. label: '男',
  37. value: 1
  38. },
  39. {
  40. label: '女',
  41. value: 0
  42. }
  43. ]
  44. }
  45. },
  46. {
  47. field: 'email',
  48. label: '邮箱',
  49. component: 'Input',
  50. colProps: {
  51. span: 24
  52. }
  53. },
  54. {
  55. field: 'mobile',
  56. label: '手机号',
  57. component: 'Input',
  58. colProps: {
  59. span: 24
  60. }
  61. },
  62. {
  63. field: 'password',
  64. label: '密码',
  65. component: 'Input',
  66. componentProps: {
  67. type: 'password'
  68. },
  69. colProps: {
  70. span: 24
  71. }
  72. },
  73. {
  74. field: 'avatar',
  75. label: '密码',
  76. component: 'Input',
  77. componentProps: {
  78. type: 'password'
  79. },
  80. hidden: true,
  81. value: '1',
  82. colProps: {
  83. span: 24
  84. }
  85. },
  86. {
  87. field: 'roles',
  88. label: '角色配置',
  89. component: 'Select',
  90. colProps: {
  91. span: 24
  92. },
  93. componentProps: {
  94. options: [],
  95. multiple: true
  96. },
  97. // 远程加载option
  98. optionApi: async () => {
  99. const res = await getRoleOptionsApi()
  100. return res.data.map((e: any) => {
  101. return {
  102. label: e.name,
  103. value: e.id
  104. }
  105. })
  106. }
  107. }
  108. ])
  109. const rules = reactive({
  110. name: [required()],
  111. password: [required()],
  112. mobile: [required(), isMobilePhone()],
  113. roles: [required()]
  114. })
  115. const { formRegister, formMethods } = useForm()
  116. const { setValues, getFormData, getElFormExpose } = formMethods
  117. const submit = async () => {
  118. const elForm = await getElFormExpose()
  119. const valid = await elForm?.validate().catch((err) => {
  120. console.log(err)
  121. })
  122. if (valid) {
  123. const formData = await getFormData()
  124. return formData
  125. }
  126. }
  127. const initValue = () => {
  128. getInfoApi({
  129. id: props.currentRow.id
  130. }).then((res) => {
  131. setValues(res.data)
  132. })
  133. }
  134. watch(
  135. () => props.currentRow,
  136. (currentRow) => {
  137. if (!currentRow) {
  138. return
  139. }
  140. initValue()
  141. },
  142. {
  143. deep: true,
  144. immediate: true
  145. }
  146. )
  147. defineExpose({
  148. submit
  149. })
  150. </script>
  151. <template>
  152. <Form :rules="rules" @register="formRegister" :schema="formSchema" />
  153. </template>