operations.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. export function useOperations() {
  2. const formPropsMap = new Map([
  3. [
  4. 'coal',
  5. {
  6. schemas: schemasCoalAlarm,
  7. submitFunc: (res) => (res.id ? updateCoalSeamAlarmRule(res) : addCoalSeamAlarmRule(res)),
  8. fetchRecord({ id, mineCode }) {
  9. return getCoalSeamAlarmRule({ coalSeamId: id, mineCode: mineCode }).then((r) => r[r.length - 1]);
  10. },
  11. },
  12. ],
  13. [
  14. 'goaf',
  15. {
  16. schemas: schemasGoafLimit,
  17. submitFunc: (res) => (res.id ? updateGoafDataLimit(res) : addGoafDataLimit(res)),
  18. fetchRecord({ id }) {
  19. return getGoafDataLimit({ goafId: id }).then((r) => r[r.length - 1]);
  20. },
  21. },
  22. ],
  23. ]);
  24. const modalPropsMap = new Map<string, Partial<ModalProps>>([
  25. ['coal', { title: '预警参数设置', visible: true, loading: true }],
  26. ['goaf', { title: '超限预警设置', visible: true, loading: true }],
  27. ]);
  28. const submitResolver = ref<(res: any) => Promise<void>>();
  29. // 点击编辑后,获取对应的表单和弹窗配置
  30. async function handleEdit(record, sign: string) {
  31. if (!modalPropsMap.has(sign)) return;
  32. if (!formPropsMap.has(sign)) return;
  33. setModalProps(modalPropsMap.get(sign) as ModalProps);
  34. const { schemas, fetchRecord, submitFunc } = formPropsMap.get(sign)!;
  35. const res = await fetchRecord(record);
  36. console.log('debug before resetSchema');
  37. await resetSchema(schemas);
  38. console.log('debug after resetSchema');
  39. await nextTick();
  40. console.log('debug before setFieldsValue');
  41. if (res.id) {
  42. await setFieldsValue(res);
  43. } else {
  44. await resetFields();
  45. }
  46. console.log('debug after setFieldsValue');
  47. await nextTick();
  48. console.log('debug before setFormProps');
  49. // 不要使用setFormProps因为它会错误的触发submit方法
  50. // await setFormProps({ submitFunc });
  51. submitResolver.value = submitFunc;
  52. console.log('debug after setFormProps');
  53. setModalProps({ loading: false });
  54. // await setFormProps({ model: res });
  55. }
  56. async function handleAdd(record, sign: string) {
  57. if (!modalPropsMap.has(sign)) return;
  58. if (!formPropsMap.has(sign)) return;
  59. setModalProps(modalPropsMap.get(sign) as ModalProps);
  60. const { schemas, submitFunc } = formPropsMap.get(sign)!;
  61. await resetSchema(schemas);
  62. await nextTick();
  63. await resetFields();
  64. await nextTick();
  65. // 不要使用setFormProps因为它会错误的触发submit方法
  66. // await setFormProps({ submitFunc });
  67. submitResolver.value = submitFunc;
  68. setModalProps({ loading: false });
  69. // await setFormProps({ model: res });
  70. }
  71. const deletionPropsMap = new Map<string, { api: (id: string) => Promise<void> }>([
  72. ['coal', { api: (id) => deleteCoalSeamAlarmRule({ id }) }],
  73. ['goaf', { api: (id) => deleteGoafDataLimit({ id }) }],
  74. ]);
  75. function handleDelete(record, sign: string) {
  76. if (!deletionPropsMap.has(sign)) return;
  77. deletionPropsMap.get(sign)?.api(record.id);
  78. }
  79. const [registerModal, { setModalProps, closeModal }] = useModal();
  80. const [registerForm, { setProps: setFormProps, resetFields, setFieldsValue, validate, submit, resetSchema }] = useForm({
  81. model: {},
  82. schemas: schemasCoalAlarm,
  83. showResetButton: false,
  84. showSubmitButton: false,
  85. colon: false,
  86. compact: true,
  87. });
  88. function handleSubmit() {
  89. return validate().then((res) => {
  90. submitResolver && submitResolver(res);
  91. });
  92. submit().then(() => {
  93. closeModal();
  94. });
  95. }
  96. }