form.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <script lang="ts" setup>
  2. import type { WorkerEntity } from '@vben/types';
  3. import { computed, ref } from 'vue';
  4. import { useVbenModal } from '@vben/common-ui';
  5. import { ElMessage } from 'element-plus';
  6. import { useVbenForm, z } from '#/adapter/form';
  7. import { addWorkerApi, editWorkerApi, getWorkerDetailApi } from '#/api/worker';
  8. const emit = defineEmits(['finish']);
  9. const data = ref();
  10. const formType = ref<'create' | 'detail' | 'edit'>('create');
  11. const titleMap = {
  12. create: '新增职员',
  13. detail: '职员详情',
  14. edit: '编辑职员',
  15. } as const;
  16. const getTitle = computed(() => titleMap[formType.value]);
  17. const [BaseForm, baseFormApi] = useVbenForm({
  18. showDefaultActions: false,
  19. // 所有表单项共用,可单独在表单内覆盖
  20. commonConfig: {
  21. labelWidth: 130,
  22. // 所有表单项
  23. componentProps: {
  24. class: 'w-full',
  25. },
  26. },
  27. wrapperClass: 'grid-cols-1 lg:grid-cols-2',
  28. schema: [
  29. // { title: '姓名', field: 'worker_xm' },
  30. // { title: '工号', field: 'worker_gh' },
  31. // { title: '类别', field: 'worker_lb' },
  32. // { title: '状态', field: 'worker_zt' },
  33. // { title: '所属部门', field: 'worker_ssbm' },
  34. // { title: '二级部门', field: 'worker_ssbm2' },
  35. // { title: '岗位', field: 'worker_gw' },
  36. // { title: '联系地址', field: 'worker_lxdz' },
  37. // { title: '联系电话', field: 'worker_dh' },
  38. // { title: '邮箱', field: 'worker_email' },
  39. // { title: '紧急联系人', field: 'worker_jjlxr' },
  40. // { title: '紧急联系电话', field: 'worker_jjlxdh' },
  41. // { title: '创建时间', field: 'worker_createdate' },
  42. // { title: '创建者', field: 'worker_create' },
  43. // { title: '是否绑定微信', field: 'worker_iswxbind' },
  44. {
  45. component: 'Input',
  46. fieldName: 'worker_xm',
  47. label: '姓名',
  48. rules: z.string().min(1, '请输入姓名'),
  49. },
  50. {
  51. component: 'Input',
  52. fieldName: 'worker_gh',
  53. label: '工号',
  54. rules: z.string().min(1, '请输入工号'),
  55. },
  56. {
  57. component: 'Select',
  58. fieldName: 'worker_lb',
  59. label: '类别',
  60. componentProps: {
  61. options: [
  62. { label: '合同工', value: '合同工' },
  63. { label: '实习工', value: '实习工' },
  64. { label: '临时工', value: '临时工' },
  65. ],
  66. },
  67. rules: z.string().min(1, '请选择类别'),
  68. },
  69. {
  70. component: 'Select',
  71. fieldName: 'worker_zt',
  72. label: '状态',
  73. componentProps: {
  74. options: [
  75. { label: '在职', value: '在职' },
  76. { label: '离职', value: '离职' },
  77. { label: '退假', value: '退假' },
  78. ],
  79. },
  80. rules: z.string().min(1, '请选择状态'),
  81. },
  82. // {
  83. // component: 'Input',
  84. // fieldName: 'worker_ssbm',
  85. // label: '所属部门',
  86. // rules: z.string().min(1, '请输入所属部门'),
  87. // },
  88. // {
  89. // component: 'Input',
  90. // fieldName: 'worker_ssbm2',
  91. // label: '二级部门',
  92. // },
  93. {
  94. component: 'Input',
  95. fieldName: 'worker_gw',
  96. label: '岗位',
  97. },
  98. {
  99. component: 'Input',
  100. fieldName: 'worker_lxdz',
  101. label: '联系地址',
  102. },
  103. {
  104. component: 'Input',
  105. fieldName: 'worker_dh',
  106. label: '联系电话',
  107. rules: z.string().min(1, '请输入联系电话'),
  108. },
  109. {
  110. component: 'Input',
  111. fieldName: 'worker_email',
  112. label: '邮箱',
  113. },
  114. {
  115. component: 'Input',
  116. fieldName: 'worker_jjlxr',
  117. label: '紧急联系人',
  118. },
  119. {
  120. component: 'Input',
  121. fieldName: 'worker_jjlxdh',
  122. label: '紧急联系电话',
  123. },
  124. {
  125. component: 'Input',
  126. fieldName: 'worker_userid',
  127. label: '关联渠道id',
  128. },
  129. ],
  130. });
  131. const [Modal, modalApi] = useVbenModal({
  132. class: 'w-7/12',
  133. onCancel() {
  134. modalApi.close();
  135. },
  136. async onConfirm() {
  137. // 校验输入的数据
  138. const formValues = await baseFormApi.getValues();
  139. const validate = await baseFormApi.validate();
  140. if (!validate.valid) {
  141. return;
  142. }
  143. try {
  144. const apiMap = {
  145. create: () => addWorkerApi(formValues as unknown as WorkerEntity),
  146. edit: () =>
  147. editWorkerApi({
  148. ...formValues,
  149. 'worker_id.value': data.value.row.worker_id,
  150. } as unknown as WorkerEntity),
  151. };
  152. if (formType.value === 'detail') {
  153. modalApi.close();
  154. return;
  155. }
  156. await apiMap[formType.value]();
  157. ElMessage.success('操作成功');
  158. emit('finish');
  159. modalApi.close();
  160. } catch {}
  161. },
  162. async onOpenChange(isOpen) {
  163. if (isOpen) {
  164. data.value = modalApi.getData();
  165. formType.value = data.value.formType;
  166. baseFormApi.setState({
  167. commonConfig: { disabled: formType.value === 'detail' },
  168. });
  169. if (data.value.formType === 'create') {
  170. return;
  171. }
  172. try {
  173. modalApi.setState({ loading: true });
  174. const detailData = await getWorkerDetailApi({
  175. worker_id: data.value.row.worker_id,
  176. });
  177. baseFormApi.setValues(detailData);
  178. } catch {
  179. // console.log(error);
  180. }
  181. modalApi.setState({ loading: false });
  182. }
  183. },
  184. });
  185. </script>
  186. <template>
  187. <Modal :title="getTitle">
  188. <BaseForm />
  189. </Modal>
  190. </template>