Ben Lin
2024-07-22 323e576c64129723df20fd18effb20d96d8d18b3
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<!--
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *                               神兽保佑            永无BUG
 -->
 
<!--
 * @Description: 通用侧边框
 * @Author: Ben Lin
 * @version: 
 * @Date: 2024-05-30 13:28:20
 * @LastEditors: Ben Lin
 * @LastEditTime: 2024-06-14 09:14:35
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    showFooter
    :title="getTitle"
    width="600px"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm">
      <template #[item]="{ field }" v-for="item in crudColSlots" :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>
  </BasicDrawer>
</template>
<script lang="ts" setup>
  import { ref, computed, unref, onMounted } from 'vue';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form/index';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import { useGlobSetting } from '/@/hooks/setting';
  import GeneralModal from '/@/views/components/GeneralModal.vue';
  import { SaveEntity, fetchJson, formatValues } from '/@/api/tigerapi/system';
  import { useModal } from '/@/components/Modal';
  import { isNullOrUnDef } from '/@/utils/is';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { isFunction } from 'xe-utils';
 
  const emit = defineEmits(['success', 'register']);
  const { t } = useI18n();
  const { createErrorModal } = useMessage();
  const globSetting = useGlobSetting();
  const isUpdate = ref(true);
  const ifSave = ref(true);
  const entityName = ref('');
  const formSchema = ref([]);
  const crudColSlots = ref<any>([]);
  const isExistSql = ref('');
  const [registerItemAdd, { openModal: openItemModal }] = useModal();
  const [registerForm, { resetFields, setFieldsValue, getFieldsValue, validate }] = useForm({
    labelWidth: 140,
    schemas: formSchema as unknown as FormSchema[],
    actionColOptions: {
      span: 24,
    },
    showActionButtonGroup: false,
  });
 
  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    /* 动态import实体名.ts的自定义方法 */
    let m = {} as any;
    try {
      m = await import(`./entityts/${data?.keyName}.ts`);
    } catch (e) {}
    isUpdate.value = !!data?.isUpdate;
    ifSave.value = !!data?.ifSave;
    entityName.value = data?.entityName;
    formSchema.value = !isNullOrUnDef(m.default)
      ? m.default()[0].GetCrudForm(data?.name, data?.data, data.record)
      : data?.formJson;
    crudColSlots.value = data?.crudColSlots;
    isExistSql.value = data?.isExistSql;
    resetFields();
    setDrawerProps({ confirmLoading: false });
 
    if (unref(isUpdate)) {
      setFieldsValue({
        ...data.record,
      });
    } else {
      if (!isNullOrUnDef(data?.keyFieldValues)) {
        setFieldsValue(data?.keyFieldValues);
      }
    }
  });
 
  const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));
 
  onMounted(async () => {});
  async function handleSubmit() {
    try {
      let values = await validate();
      //判断保存的值如果是[]数组的,就直接取[0]第一个值,一般针对上传模板的地址
      values = formatValues(values);
      setDrawerProps({ confirmLoading: true });
      // TODO custom api
      if (!unref(ifSave)) {
        //保存
        SaveEntity(
          values,
          unref(isUpdate),
          unref(entityName),
          `${isExistSql.value}='${values[isExistSql.value]}'`,
        ).then((action) => {
          if (action.IsSuccessed) {
            closeDrawer();
            emit('success');
          } else {
            createErrorModal({ title: t('sys.api.errorTip'), content: action.Message });
          }
        });
      } else {
        closeDrawer();
        emit('success', values, { isUpdate: isUpdate.value });
      }
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
 
  /**
   * @description: 弹出选择框选择成功后事件
   * @param {*} d
   * @param {*} u
   * @param {*} item
   * @return {*}
   */
  function handleItemSuccess(d, u, item) {
    /* 动态import实体名.ts的自定义方法 */
    try {
      import(`./entityts/${getFieldsValue()[`${item.replace(/add/, '')}PSelect_0`]}.ts`)
        .then((m) => {
          const [{ GetSelectSuccess }] = m.default();
          setFieldsValue(GetSelectSuccess(d, u));
        })
        .catch(() => {
          setFieldsValue({
            ITEM_CODE: d.values['val'],
          });
        });
    } catch (e) {}
  }
 
  /**
   * @description: 弹出选择框
   * @param {*} item
   * @return {*}
   */
  function handleSelectItem(item) {
    /* 动态import实体名.ts的自定义方法 */
    try {
      import(`./entityts/${getFieldsValue()[`${item.replace(/add/, '')}PSelect_0`]}.ts`)
        .then((m) => {
          const [{ OpenSelectItem }] = m.default();
          OpenSelectItem(openItemModal);
        })
        .catch(() => {
          openItemModal(true, {
            title: '物料列表',
            schemas: [
              {
                field: 'ITEM_CODE',
                component: 'Input',
                label: '物料编码',
                colProps: {
                  span: 12,
                },
              },
            ],
            ItemColumns: [
              {
                title: t('物料编码'),
                dataIndex: 'ITEM_CODE',
                resizable: true,
                sorter: true,
                width: 200,
              },
              {
                title: t('物料名称'),
                dataIndex: 'ITEM_NAME',
                resizable: true,
                sorter: true,
                width: 180,
              },
            ],
            tableName: 'BAS_ITEM',
            rowKey: 'ITEM_CODE',
            searchInfo: { TABLE_NAME: 'BAS_ITEM' },
          });
        });
    } catch (e) {}
  }
</script>