form.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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: 130,
  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. // { title: '备注', field: 'usersbz' },
  46. // { title: '用户微信openid', field: 'usersopenid' },
  47. // { title: '用户是否实名', field: 'usersauthstatus' },
  48. // { title: '用户创建时间', field: 'usersdate' },
  49. // { title: '用户类型', field: 'userstype' },
  50. // { title: '法人代表电话(座机)', field: 'usersfrdbdh' },
  51. // { title: '法人代表手机号', field: 'usersfrdbsjh' },
  52. // { title: '销售手机号', field: 'usersxssjh' },
  53. // { title: '销售电话(座机)', field: 'usersxsdh' },
  54. // { title: '社会统一企业代码', field: 'usersshtyxydm' },
  55. // { title: '企业注册地', field: 'userszcd' },
  56. // { title: '传真', field: 'userscz' },
  57. // { title: '备注', field: 'usersbz' },
  58. {
  59. component: 'Input',
  60. fieldName: 'usersname',
  61. label: '用户名称',
  62. componentProps: {
  63. placeholder: '请输入用户名称',
  64. allowClear: true,
  65. },
  66. rules: z.string().min(1, { message: '请输入用户名称' }),
  67. },
  68. {
  69. component: 'Select',
  70. fieldName: 'usersnature',
  71. label: '用户性质',
  72. componentProps: {
  73. placeholder: '请选择用户性质',
  74. allowClear: true,
  75. options: [
  76. { label: '购机者', value: '购机者' },
  77. { label: '经销商', value: '经销商' },
  78. ],
  79. },
  80. rules: z.string().min(1, { message: '请选择用户性质' }),
  81. },
  82. {
  83. component: 'Select',
  84. fieldName: 'userstype',
  85. label: '用户类型',
  86. defaultValue: '个人',
  87. componentProps: {
  88. placeholder: '请选择用户类型',
  89. allowClear: true,
  90. options: [
  91. { label: '个人', value: '个人' },
  92. { label: '企业', value: '企业' },
  93. ],
  94. },
  95. rules: z.string().min(1, { message: '请选择用户类型' }),
  96. },
  97. // 个人用户显示身份证号
  98. {
  99. component: 'Input',
  100. fieldName: 'usersidcardnumber',
  101. label: '身份证号码',
  102. componentProps: {
  103. placeholder: '请输入身份证号码',
  104. allowClear: true,
  105. },
  106. rules: z.string().min(1, { message: '请输入身份证号码' }),
  107. dependencies: {
  108. if(values) {
  109. return values.userstype === '个人';
  110. },
  111. triggerFields: ['userstype'],
  112. },
  113. },
  114. // 企业用户显示的字段
  115. {
  116. component: 'Input',
  117. fieldName: 'usersshtyxydm',
  118. label: '统一社会信用代码',
  119. componentProps: {
  120. placeholder: '请输入统一社会信用代码',
  121. allowClear: true,
  122. },
  123. rules: z.string().min(1, { message: '请输入统一社会信用代码' }),
  124. dependencies: {
  125. if(values) {
  126. return values.userstype === '企业';
  127. },
  128. triggerFields: ['userstype'],
  129. },
  130. },
  131. {
  132. component: 'Input',
  133. fieldName: 'userszcd',
  134. label: '企业注册地',
  135. componentProps: {
  136. placeholder: '请输入企业注册地',
  137. allowClear: true,
  138. },
  139. dependencies: {
  140. if(values) {
  141. return values.userstype === '企业';
  142. },
  143. triggerFields: ['userstype'],
  144. },
  145. },
  146. {
  147. component: 'Input',
  148. fieldName: 'usersfrdbdh',
  149. label: '法人代表电话(座机)',
  150. componentProps: {
  151. placeholder: '请输入法人代表电话',
  152. allowClear: true,
  153. },
  154. dependencies: {
  155. if(values) {
  156. return values.userstype === '企业';
  157. },
  158. triggerFields: ['userstype'],
  159. },
  160. },
  161. {
  162. component: 'Input',
  163. fieldName: 'usersfrdbsjh',
  164. label: '法人代表手机号',
  165. componentProps: {
  166. placeholder: '请输入法人代表手机号',
  167. allowClear: true,
  168. },
  169. dependencies: {
  170. if(values) {
  171. return values.userstype === '企业';
  172. },
  173. triggerFields: ['userstype'],
  174. },
  175. },
  176. // 通用字段
  177. {
  178. component: 'Input',
  179. fieldName: 'usersbankname',
  180. label: '开户银行名称',
  181. componentProps: {
  182. placeholder: '请输入开户银行名称',
  183. allowClear: true,
  184. },
  185. rules: z.string().min(1, { message: '请输入开户银行名称' }),
  186. },
  187. {
  188. component: 'Input',
  189. fieldName: 'usersbanknumber',
  190. label: '开户银行账号',
  191. componentProps: {
  192. placeholder: '请输入开户银行账号',
  193. allowClear: true,
  194. },
  195. rules: z.string().min(1, { message: '请输入开户银行账号' }),
  196. },
  197. {
  198. component: 'Input',
  199. fieldName: 'usersphone',
  200. label: '手机号',
  201. componentProps: {
  202. placeholder: '请输入手机号',
  203. allowClear: true,
  204. },
  205. rules: z.string().min(1, { message: '请输入手机号' }),
  206. },
  207. {
  208. component: 'Input',
  209. fieldName: 'usersemail',
  210. label: '邮箱',
  211. componentProps: {
  212. placeholder: '请输入邮箱',
  213. allowClear: true,
  214. },
  215. rules: z.string().email({ message: '请输入正确的邮箱格式' }),
  216. },
  217. {
  218. component: 'Input',
  219. fieldName: 'usersaddress',
  220. label: '地址',
  221. componentProps: {
  222. placeholder: '请输入地址',
  223. allowClear: true,
  224. },
  225. },
  226. {
  227. component: 'Input',
  228. fieldName: 'userscontactphone',
  229. label: '联系手机号',
  230. componentProps: {
  231. placeholder: '请输入联系手机号',
  232. allowClear: true,
  233. },
  234. },
  235. {
  236. component: 'Input',
  237. fieldName: 'userscontactemail',
  238. label: '联系邮箱',
  239. componentProps: {
  240. placeholder: '请输入联系邮箱',
  241. allowClear: true,
  242. },
  243. rules: z.string().email({ message: '请输入正确的邮箱格式' }),
  244. },
  245. {
  246. component: 'Input',
  247. fieldName: 'userscontactaddress',
  248. label: '联系地址',
  249. componentProps: {
  250. placeholder: '请输入联系地址',
  251. allowClear: true,
  252. },
  253. },
  254. {
  255. component: 'Input',
  256. fieldName: 'userscz',
  257. label: '传真',
  258. componentProps: {
  259. placeholder: '请输入传真',
  260. allowClear: true,
  261. },
  262. dependencies: {
  263. if(values) {
  264. return values.userstype === '企业';
  265. },
  266. triggerFields: ['userstype'],
  267. },
  268. },
  269. {
  270. component: 'Input',
  271. fieldName: 'usersbz',
  272. label: '备注',
  273. componentProps: {
  274. placeholder: '请输入备注',
  275. allowClear: true,
  276. type: 'textarea',
  277. rows: 4,
  278. },
  279. },
  280. ],
  281. });
  282. const [Modal, modalApi] = useVbenModal({
  283. class: 'w-7/12',
  284. onCancel() {
  285. modalApi.close();
  286. },
  287. async onConfirm() {
  288. // 校验输入的数据
  289. const formValues = await baseFormApi.getValues();
  290. const validate = await baseFormApi.validate();
  291. if (!validate.valid) {
  292. return;
  293. }
  294. try {
  295. const apiMap = {
  296. create: () => addCustomerApi(formValues as unknown as CustomerEntity),
  297. edit: () =>
  298. editCustomerApi({
  299. ...formValues,
  300. 'usersid.value': data.value.row.usersid,
  301. } as unknown as CustomerEntity),
  302. };
  303. if (formType.value === 'detail') {
  304. modalApi.close();
  305. return;
  306. }
  307. await apiMap[formType.value]();
  308. ElMessage.success('操作成功');
  309. emit('finish');
  310. modalApi.close();
  311. } catch {}
  312. },
  313. async onOpenChange(isOpen) {
  314. if (isOpen) {
  315. data.value = modalApi.getData();
  316. formType.value = data.value.formType;
  317. baseFormApi.setState({
  318. commonConfig: { disabled: formType.value === 'detail' },
  319. });
  320. if (data.value.formType === 'create') {
  321. return;
  322. }
  323. try {
  324. modalApi.setState({ loading: true });
  325. const detailData = await getCustomerDetailApi({
  326. usersid: data.value.row.usersid,
  327. });
  328. baseFormApi.setValues(detailData);
  329. } catch {
  330. // console.log(error);
  331. }
  332. modalApi.setState({ loading: false });
  333. }
  334. },
  335. });
  336. </script>
  337. <template>
  338. <Modal :title="getTitle">
  339. <BaseForm />
  340. </Modal>
  341. </template>