form.vue 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <script lang="ts" setup>
  2. import type { CustomerEntity } 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 {
  8. addCustomerApi,
  9. editCustomerApi,
  10. getCustomerDetailApi,
  11. } from '#/api/user';
  12. const emit = defineEmits(['finish']);
  13. const data = ref();
  14. const formType = ref<'create' | 'detail' | 'edit'>('create');
  15. const titleMap = {
  16. create: '新增客户',
  17. detail: '客户详情',
  18. edit: '编辑客户',
  19. } as const;
  20. const getTitle = computed(() => titleMap[formType.value]);
  21. const [BaseForm, baseFormApi] = useVbenForm({
  22. showDefaultActions: false,
  23. // 所有表单项共用,可单独在表单内覆盖
  24. commonConfig: {
  25. labelWidth: 120,
  26. // 所有表单项
  27. componentProps: {
  28. class: 'w-full',
  29. },
  30. },
  31. wrapperClass: 'grid-cols-1 lg:grid-cols-2',
  32. schema: [
  33. // { title: '用户名称', field: 'usersname' },
  34. // { title: '用户性质', field: 'usersnature' },
  35. // { title: '用户类型', field: 'userstype' },
  36. // { title: '用户证件号码', field: 'usersidcardnumber' },
  37. // { title: '用户开户银行名称', field: 'usersbankname' },
  38. // { title: '用户开户银行账号', field: 'usersbanknumber' },
  39. // { title: '用户手机号', field: 'usersphone' },
  40. // { title: '用户邮箱', field: 'usersemail' },
  41. // { title: '用户地址', field: 'usersaddress' },
  42. // { title: '用户联系手机号', field: 'userscontactphone' },
  43. // { title: '用户联系邮箱', field: 'userscontactemail' },
  44. // { title: '用户联系地址', field: 'userscontactaddress' },
  45. {
  46. component: 'Input',
  47. fieldName: 'usersname',
  48. label: '用户名称',
  49. componentProps: {
  50. placeholder: '请输入用户名称',
  51. allowClear: true,
  52. },
  53. rules: z.string().min(1, { message: '请输入用户名称' }),
  54. },
  55. {
  56. component: 'Select',
  57. fieldName: 'usersnature',
  58. label: '用户性质',
  59. componentProps: {
  60. placeholder: '请输入用户性质',
  61. allowClear: true,
  62. options: [
  63. {
  64. label: '用户',
  65. value: '用户',
  66. },
  67. {
  68. label: '渠道',
  69. value: '渠道',
  70. },
  71. ],
  72. },
  73. rules: z.string().min(1, { message: '请输入用户性质' }),
  74. },
  75. {
  76. component: 'Select',
  77. fieldName: 'userstype',
  78. label: '用户类型',
  79. defaultValue: '个人',
  80. componentProps: {
  81. placeholder: '请输入用户类型',
  82. allowClear: true,
  83. options: [
  84. {
  85. label: '个人',
  86. value: '个人',
  87. },
  88. {
  89. label: '企业',
  90. value: '企业',
  91. },
  92. ],
  93. },
  94. rules: z.string().min(1, { message: '请输入用户类型' }),
  95. },
  96. {
  97. component: 'Input',
  98. fieldName: 'usersidcardnumber',
  99. label: '用户身份证号码',
  100. componentProps: {
  101. placeholder: '请输入用户身份证号码',
  102. allowClear: true,
  103. },
  104. rules: z.string().min(1, { message: '请输入用户身份证号码' }),
  105. dependencies: {
  106. if(values) {
  107. return values.userstype && values.userstype === '个人';
  108. },
  109. triggerFields: ['userstype'],
  110. },
  111. },
  112. {
  113. component: 'Input',
  114. fieldName: 'usersidcardnumber',
  115. label: '纳税人识别号',
  116. componentProps: {
  117. placeholder: '请输入纳税人识别号',
  118. allowClear: true,
  119. },
  120. rules: z.string().min(1, { message: '请输入纳税人识别号' }),
  121. dependencies: {
  122. if(values) {
  123. return values.userstype && values.userstype === '企业';
  124. },
  125. triggerFields: ['userstype'],
  126. },
  127. },
  128. {
  129. component: 'Input',
  130. fieldName: 'usersbankname',
  131. label: '用户开户银行名称',
  132. componentProps: {
  133. placeholder: '请输入用户开户银行名称',
  134. allowClear: true,
  135. },
  136. rules: z.string().min(1, { message: '请输入用户开户银行名称' }),
  137. },
  138. {
  139. component: 'Input',
  140. fieldName: 'usersbanknumber',
  141. label: '用户开户银行账号',
  142. componentProps: {
  143. placeholder: '请输入用户开户银行账号',
  144. allowClear: true,
  145. },
  146. rules: z.string().min(1, { message: '请输入用户开户银行账号' }),
  147. },
  148. {
  149. component: 'Input',
  150. fieldName: 'usersphone',
  151. label: '用户手机号',
  152. componentProps: {
  153. placeholder: '请输入用户手机号',
  154. allowClear: true,
  155. },
  156. rules: z.string().min(1, { message: '请输入用户手机号' }),
  157. },
  158. {
  159. component: 'Input',
  160. fieldName: 'usersemail',
  161. label: '用户邮箱',
  162. componentProps: {
  163. placeholder: '请输入用户邮箱',
  164. allowClear: true,
  165. },
  166. rules: z.string().min(1, { message: '请输入用户邮箱' }),
  167. },
  168. {
  169. component: 'Input',
  170. fieldName: 'usersaddress',
  171. label: '用户地址',
  172. componentProps: {
  173. placeholder: '请输入用户地址',
  174. allowClear: true,
  175. },
  176. rules: z.string().min(1, { message: '请输入用户地址' }),
  177. },
  178. {
  179. component: 'Input',
  180. fieldName: 'userscontactphone',
  181. label: '用户联系手机号',
  182. componentProps: {
  183. placeholder: '请输入用户联系手机号',
  184. allowClear: true,
  185. },
  186. rules: z.string().min(1, { message: '请输入用户联系手机号' }),
  187. },
  188. {
  189. component: 'Input',
  190. fieldName: 'userscontactemail',
  191. label: '用户联系邮箱',
  192. componentProps: {
  193. placeholder: '请输入用户联系邮箱',
  194. allowClear: true,
  195. },
  196. rules: z.string().min(1, { message: '请输入用户联系邮箱' }),
  197. },
  198. {
  199. component: 'Input',
  200. fieldName: 'userscontactaddress',
  201. label: '用户联系地址',
  202. componentProps: {
  203. placeholder: '请输入用户联系地址',
  204. allowClear: true,
  205. },
  206. rules: z.string().min(1, { message: '请输入用户联系地址' }),
  207. },
  208. ],
  209. });
  210. const [Modal, modalApi] = useVbenModal({
  211. class: 'w-7/12',
  212. onCancel() {
  213. modalApi.close();
  214. },
  215. async onConfirm() {
  216. // 校验输入的数据
  217. const validate = await baseFormApi.validate();
  218. if (!validate.valid) {
  219. return;
  220. }
  221. try {
  222. // 调用新增或编辑接口
  223. const apiMap = {
  224. create: () => addCustomerApi(validate.values as CustomerEntity),
  225. edit: () =>
  226. editCustomerApi({
  227. ...validate.values,
  228. 'usersid.value': data.value.row.usersid,
  229. } as any),
  230. };
  231. if (formType.value === 'detail') {
  232. modalApi.close();
  233. return;
  234. }
  235. await apiMap[formType.value]();
  236. ElMessage.success('操作成功');
  237. emit('finish');
  238. modalApi.close();
  239. } catch {}
  240. },
  241. async onOpenChange(isOpen) {
  242. if (isOpen) {
  243. data.value = modalApi.getData();
  244. formType.value = data.value.formType;
  245. baseFormApi.setState({
  246. commonConfig: { disabled: formType.value === 'detail' },
  247. });
  248. if (data.value.formType === 'create') {
  249. return;
  250. }
  251. try {
  252. modalApi.setState({ loading: true });
  253. const detailData = await getCustomerDetailApi({
  254. usersid: data.value.row.usersid,
  255. });
  256. baseFormApi.setValues(detailData);
  257. } catch {
  258. // console.log(error);
  259. }
  260. modalApi.setState({ loading: false });
  261. }
  262. },
  263. });
  264. </script>
  265. <template>
  266. <Modal :title="getTitle">
  267. <BaseForm />
  268. </Modal>
  269. </template>