刘光辉
7 小时以前 0dfe84494048ce27ba8449831782128412d3eb13
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
<script lang="ts" setup>
import { inject, nextTick, onMounted, reactive, ref, toRefs, unref } from 'vue';
 
import { useMessage } from '@jnpf/hooks';
import { createAsyncComponent, getDateTimeUnit } from '@jnpf/utils';
 
import { useUserStore } from '@vben/stores';
 
import dayjs from 'dayjs';
 
import { buildDisplayOnlySubmitData } from '#/components/FormGenerator/src/helper/displayOnly';
import { useGeneratorStore } from '#/store';
 
const props = defineProps(['config']);
const emit = defineEmits(['setPageLoad', 'eventReceiver']);
defineExpose({ dataFormSubmit });
 
interface State {
  config: any;
  formConf: any;
  formData: any;
  key: number;
  loading: boolean;
  isCustomCopy: boolean;
  dataForm: any;
  eventType: string;
  flowUrgent: number;
}
 
const { createMessage } = useMessage();
const getLeftTreeActiveInfo: (() => any) | null = inject('getLeftTreeActiveInfo', null);
const Parser = createAsyncComponent(() => import('#/components/FormGenerator/src/components/Parser.vue'));
const parserRef = ref<any>(null);
const state = reactive<State>({
  config: {},
  formConf: {},
  formData: {},
  key: Date.now(),
  loading: false,
  isCustomCopy: false,
  dataForm: {
    id: '',
    formData: {},
    flowId: '',
  },
  eventType: '',
  flowUrgent: 1,
});
const { config, formConf, key, loading } = toRefs(state);
const generatorStore = useGeneratorStore();
const userStore = useUserStore();
 
function init(config) {
  state.config = config;
  state.formConf = config.formConf ? JSON.parse(config.formConf) : {};
  state.formData = {};
  state.dataForm.id = config.id || '';
  state.dataForm.flowId = config.flowId;
  state.isCustomCopy = config.flowTemplateJson && config.flowTemplateJson.properties && config.flowTemplateJson.properties.isCustomCopy;
  state.loading = true;
  let extra = {};
  if (config.id) {
    extra = {
      modelId: config.flowId,
      id: config.id,
      type: config.type,
      flowId: config.flowId,
      processId: config.id,
      taskId: config.taskId,
      opType: config.opType,
    };
    const formData = config.draftData || config.formData;
    state.formData = { id: config.id, ...formData, flowId: config.flowId };
  } else {
    state.formData = { ...(config.formData || {}), ...(config.params || {}) };
    if (getLeftTreeActiveInfo) state.formData = { ...getLeftTreeActiveInfo(), ...state.formData };
  }
  generatorStore.setDynamicModelExtra(extra);
  fillFormData(state.formConf, state.formData, !config.id);
  nextTick(() => {
    state.loading = false;
    state.key = Date.now();
    setTimeout(() => {
      emit('setPageLoad');
    }, 200);
  });
}
function fillFormData(form, data, isAdd) {
  const userInfo: any = userStore.getUserInfo;
  const currDate = new Date();
  form.disabled = state.config.disabled;
  const isEmptyValue = (value) => value === null || value === undefined || value === '' || (Array.isArray(value) && value.length === 0);
  const getFieldOperate = (item, parent?) => {
    if (!state.config.formOperates || !state.config.formOperates.length) return null;
    const id = item.__config__.isSubTable ? `${parent.__vModel__}-${item.__vModel__}` : item.__vModel__;
    const arr = state.config.formOperates.filter((o) => o.id === id) || [];
    return arr.length ? arr[0] : null;
  };
  const loop = (list, parent?) => {
    for (const item of list) {
      if (item.__vModel__) {
        const hasDataKey = Object.prototype.hasOwnProperty.call(data, item.__vModel__);
        const hasDataValue = hasDataKey && !isEmptyValue(data[item.__vModel__]);
        const fieldOperate = getFieldOperate(item, parent);
        const noWritePermission = !!fieldOperate && !fieldOperate.write;
        const shouldUseDataValue = hasDataKey && (hasDataValue || !item.__config__.defaultCurrent);
        const val = shouldUseDataValue ? data[item.__vModel__] : noWritePermission ? undefined : item.__config__.defaultValue;
        item.__config__.__skipDefaultValueInit = noWritePermission && !hasDataValue;
        if (!item.__config__.isSubTable) item.__config__.defaultValue = val;
        const canInitCurrentDefault = isAdd || item.__config__.isSubTable || (!!fieldOperate?.write && !hasDataValue);
        if (canInitCurrentDefault && item.__config__.defaultCurrent && !item.__config__.__skipDefaultValueInit) {
          if (item.__config__.jnpfKey === 'datePicker') {
            item.__config__.defaultValue = dayjs(currDate).startOf(getDateTimeUnit(item.format)).valueOf();
          }
          if (item.__config__.jnpfKey === 'timePicker') {
            item.__config__.defaultValue = dayjs(currDate).format(item.format || 'HH:mm:ss');
          }
          if (item.__config__.jnpfKey === 'organizeSelect' && userInfo.organizeIds?.length) {
            item.__config__.defaultValue = item.multiple ? userInfo.organizeIds : userInfo.organizeId;
          }
          if (item.__config__.jnpfKey === 'userSelect' && userInfo.userId) {
            item.__config__.defaultValue = item.multiple ? [userInfo.userId] : userInfo.userId;
          }
          if (item.__config__.jnpfKey === 'usersSelect' && userInfo.userId) {
            item.__config__.defaultValue = [`${userInfo.userId}--user`];
          }
          if (item.__config__.jnpfKey === 'posSelect' && userInfo.positionIds?.length) {
            item.__config__.defaultValue = item.multiple ? userInfo.positionIds : userInfo.positionId;
          }
          if (item.__config__.jnpfKey === 'sign' && userInfo.signImg) {
            item.__config__.defaultValue = userInfo.signImg;
          }
        }
        if (isAdd && !item.__config__.isSubTable && hasDataValue) item.__config__.defaultValue = data[item.__vModel__];
        let isDisabled = item.disabled || false;
        let noShow = item.__config__.noShow || false;
        let required = item.__config__.required || false;
        if (fieldOperate) {
          if (!fieldOperate.read) noShow = true;
          if (!fieldOperate.write) isDisabled = true;
          required = fieldOperate.required ? fieldOperate.required : item.__config__.required;
        }
        if (state.config.readonly || state.config.disabled) isDisabled = true;
        item.disabled = isDisabled;
        item.__config__.noShow = noShow;
        item.__config__.required = required || false;
      }
      if (['popupAttr', 'relationFormAttr'].includes(item.__config__.jnpfKey) && !item.isStorage && state.config.disabled) {
        item.disabled = true;
      }
      if (item.__config__ && item.__config__.children && Array.isArray(item.__config__.children)) {
        loop(item.__config__.children, item);
      }
    }
  };
  loop(form.fields);
  form.formData = data;
}
function getParser() {
  const parser = unref(parserRef);
  if (!parser) {
    throw new Error('parser is null!');
  }
  return parser;
}
function dataFormSubmit(eventType, flowUrgent) {
  if (state.config.isPreview) return createMessage.warning('功能预览不支持数据保存');
  state.eventType = eventType;
  state.flowUrgent = flowUrgent;
  // 暂存类操作(发起暂存/审批暂存/协办保存)不触发提交前置(beforeSubmit)
  const isSave = ['save', 'saveAudit', 'saveAssist'].includes(eventType);
  getParser().handleSubmit(isSave);
}
function submitForm(data, callback) {
  if (!data) return;
  const formData = buildDisplayOnlySubmitData(state.formConf.fields, { ...state.formData, ...data });
  state.dataForm.formData = formData;
  if (callback && typeof callback === 'function') callback();
  emit('eventReceiver', state.dataForm, state.eventType);
}
 
onMounted(() => {
  init(props.config);
});
</script>
 
<template>
  <div class="flow-form" :style="{ margin: '0 auto', width: formConf.fullScreenWidth || '100%' }">
    <Parser
      ref="parserRef"
      :form-conf="formConf"
      :model-id="state.config.formId || state.config.flowId"
      :is-online-utils-open="true"
      :params="config.params"
      :require-review="false"
      @submit="submitForm"
      :key="key"
      v-if="!loading" />
  </div>
</template>