Ben Lin
2024-07-27 a82003f4cf9ae6b69f70212374b3a71090f1770f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!--
 * @Description: file content
 * @Author: Ben Lin
 * @version: 
 * @Date: 2024-06-24 23:44:31
 * @LastEditors: Ben Lin
 * @LastEditTime: 2024-07-23 20:48:29
-->
<template>
  <Card :title="GetTitle(props.configType)['configTitle']">
    <BasicForm @register="registerForm">
      <template #[item]="{ field }" v-for="item in GetCrudColSlots()" :key="item">
        <a-button
          v-if="field"
          class="mt-1 ml-1"
          size="small"
          @click="handleSelectItem(item)"
          preIcon="search|svg"
        />
        <GeneralModal
          @register="registerItemAdd"
          @success="(d, u) => handleItemSuccess(d, u, item)"
        />
      </template>
    </BasicForm>
  </Card>
</template>
<script lang="ts" setup>
  import { Ref, inject, nextTick, onMounted, ref, unref, watch } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { Card } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import GeneralModal from '/@/views/components/GeneralModal.vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { isNullOrUnDef } from '/@/utils/is';
  import { EntityCustFunctionType } from '/@/api/tigerapi/model/basModel';
 
  const { t } = useI18n();
  const emit = defineEmits(['success', 'register']);
  const props = defineProps({
    configType: { type: String },
  });
  const [registerItemAdd, { openModal: openItemModal }] = useModal();
  const objParams = inject('objParams') as Ref<any>;
  const custImport = ref<any[]>([]);
  const EntityCustFunction = ref([
    {
      GetCrudForm(type: string | undefined) {},
      GetCrudColSlots(val, id) {},
      OpenSelectItem(openItemModal: Fn, ...args) {},
      GetSelectSuccess(d, u, ...args) {},
      GetTitle(type: string | undefined) {},
      nodeChange(params: {
        resetFields: any;
        setFieldsValue: any;
        objParams: Ref<any>;
        selectedNodes: Ref<any[]>;
      }) {},
      SubmitFunc(values: Recordable<any>, type: string, emit) {},
    } as EntityCustFunctionType,
  ]);
  /* 动态import实体名.ts的自定义方法 */
  try {
    custImport.value = await import(`../entityts/${objParams.value['Name']}.ts`);
  } catch (e) {}
  const [
    {
      GetCrudForm,
      GetCrudColSlots,
      nodeChange,
      OpenSelectItem,
      GetSelectSuccess,
      GetTitle,
      SubmitFunc,
    },
  ] = isNullOrUnDef(custImport.value['default'])
    ? EntityCustFunction.value
    : custImport.value['default']();
 
  let formSchema: FormSchema[] = GetCrudForm(props.configType);
  const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
    labelWidth: 120,
    schemas: formSchema,
    actionColOptions: {
      offset: 8,
      span: 24,
    },
    wrapperCol: {
      span: 15,
    },
    showActionButtonGroup: true,
    submitButtonOptions: {
      text: '保存',
    },
    submitFunc: customSubmitFunc,
  });
 
  /* 注入选中节点数据 */
  const selectedNodes = inject('selectedNodes') as Ref<any>;
  watch(
    () => selectedNodes.value,
    (newVal, oldVal) => {
      nextTick(() => {
        /* 节点切换事件 */
        nodeChange({ resetFields, setFieldsValue, objParams, selectedNodes });
        formSchema = GetCrudForm(selectedNodes.value[0].type);
        setProps({
          schemas: []
        });
        setProps({
          schemas: formSchema
        });
      });
    },
    { deep: true, immediate: true },
  );
 
  onMounted(() => {
    /* 节点切换事件 */
    nodeChange({ resetFields, setFieldsValue, objParams, selectedNodes });
  });
 
  async function customSubmitFunc() {
    try {
      const values = await validate();
      SubmitFunc(values,props.configType, emit);
      // values.AUTH_PROD = useUserStore().getUserInfo.prodCode;
      // values.FACTORY = useUserStore().getUserInfo.prodCode;
      // const apiAction = SaveEntity(values, true, 'MES_PROD_ACTION');
      // apiAction.then((action) => {
      //   if (action.IsSuccessed) {
      //     emit('success');
      //   }
      // });
    } catch (e) {}
  }
 
  /**
   * @description: 点击打开弹出选择列表框
   * @param {*} item
   * @return {*}
   */
  function handleSelectItem(item) {
    OpenSelectItem(openItemModal, item);
  }
 
  /**
   * @description: 弹出选择框选择成功后返回
   * @param {*} d
   * @param {*} u
   * @param {*} item
   * @return {*}
   */
  function handleItemSuccess(d, u, item) {
    setFieldsValue(GetSelectSuccess(d, u, item));
  }
</script>