Parser.vue
11.9 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<script>
import { deepClone } from '@/utils'
import render from '@/components/Generator/render/render.js'
import { ruleTrigger, dyOptionsList } from '@/components/Generator/generator/comConfig'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { previewDataInterface } from '@/api/systemData/dataInterface'
import request from '@/utils/request'
const hasOptionsList = [...dyOptionsList, 'popupSelect']
const layouts = {
colFormItem(h, scheme) {
const config = scheme.__config__
const listeners = buildListeners.call(this, scheme)
let labelWidth = config.labelWidth ? `${config.labelWidth}px` : null
if (config.showLabel === false) labelWidth = '0'
if (!config.noShow) {
return (
<el-col span={config.span}>
<el-form-item label-width={labelWidth} prop={scheme.__vModel__}
label={config.showLabel ? config.label : ''}>
<render formData={this[this.formConf.formModel]} conf={scheme} {...{ on: listeners }} ref={config.rowType === 'table' ? scheme.__vModel__ : undefined} />
</el-form-item>
</el-col>
)
}
},
rowFormItem(h, scheme) {
const listeners = buildListeners.call(this, scheme)
if (scheme.__config__.nccKey === 'tab') {
return (
<el-col span={scheme.__config__.span} class="mb-10">
<el-tabs type={scheme.type} tab-position={scheme['tab-position']} vModel={scheme.__config__.active} {...{ on: listeners }}>
{
scheme.__config__.children.map((item, i) => {
let child = renderChildren.call(this, h, item)
return (
<el-tab-pane key={item.name} label={item.title} >
<el-row>
{child}
</el-row>
</el-tab-pane>
)
})
}
</el-tabs>
</el-col>
)
}
if (scheme.__config__.nccKey === 'collapse') {
return (
<el-col span={scheme.__config__.span} class="mb-20">
<el-collapse vModel={scheme.__config__.active} accordion={scheme.accordion} {...{ on: listeners }}>
{
scheme.__config__.children.map((item, i) => {
let child = renderChildren.call(this, h, item)
return (
<el-collapse-item key={item.name} title={item.title} name={item.name} >
<el-row>
{child}
</el-row>
</el-collapse-item>
)
})
}
</el-collapse>
</el-col>
)
}
let child = renderChildren.apply(this, arguments)
if (scheme.__config__.nccKey === 'table') {
if (!scheme.__config__.noShow) this.tableRefs[scheme.__vModel__] = scheme
const param = { ...scheme, config: scheme }
return layouts.colFormItem.call(this, h, param)
}
if (scheme.__config__.nccKey === 'card') {
return (
<el-col span={scheme.__config__.span} class="item-card">
<el-card shadow={scheme.shadow} header={scheme.header} class="mb-20">
{child}
</el-card>
</el-col>
)
}
if (scheme.__config__.nccKey === 'row') {
if (scheme.type === 'flex') {
child = <el-row type={scheme.type} justify={scheme.justify} align={scheme.align}>
{child}
</el-row>
}
return (
<el-col span={scheme.__config__.span}>
<el-row gutter={scheme.gutter}>
{child}
</el-row>
</el-col>
)
}
}
}
function renderFrom(h) {
const { formConfCopy } = this
return (
<el-row gutter={formConfCopy.gutter} class={formConfCopy.formStyle}>
<el-form
size={formConfCopy.size}
label-position={formConfCopy.labelPosition}
disabled={formConfCopy.disabled}
label-width={`${formConfCopy.labelWidth}px`}
ref={formConfCopy.formRef}
// model不能直接赋值 https://github.com/vuejs/jsx/issues/49#issuecomment-472013664
props={{ model: this[formConfCopy.formModel] }}
rules={this[formConfCopy.formRules]}
>
{renderFormItem.call(this, h, formConfCopy.fields)}
{formConfCopy.formBtns && formBtns.call(this, h)}
</el-form>
</el-row>
)
}
function formBtns(h) {
return <el-col>
<el-form-item size="large">
<el-button type="primary" onClick={this.submitForm}>提交</el-button>
<el-button onClick={this.resetForm}>重置</el-button>
</el-form-item>
</el-col>
}
function renderFormItem(h, elementList) {
return elementList.map(scheme => {
const config = scheme.__config__
const layout = layouts[config.layout]
if (layout) {
return layout.call(this, h, scheme)
}
throw new Error(`没有与${config.layout}匹配的layout`)
})
}
function renderChildren(h, scheme) {
const config = scheme.__config__
if (!Array.isArray(config.children)) return null
return renderFormItem.call(this, h, config.children)
}
function setValue(event, config, scheme) {
this.$set(config, 'defaultValue', event)
this.$set(this[this.formConf.formModel], scheme.__vModel__, event)
}
function getFunc(str) {
let func = null
try {
func = eval(str)
return func
} catch (error) {
console.log(error);
return false
}
}
function buildListeners(scheme) {
const config = scheme.__config__
const listeners = {}
if (scheme.on) {
// 响应 组件事件
Object.keys(scheme.on).forEach(key => {
const str = scheme.on[key];
const func = getFunc(str);
if (!func) return
listeners[key] = event => func.call(this, { data: event, ...this.parameter })
})
}
// 响应 render.js 中的 vModel $emit('input', val)
listeners.input = event => setValue.call(this, event, config, scheme)
return listeners
}
export default {
components: {
render
},
props: {
setFormData: Function,
setShowOrHide: Function,
setRequired: Function,
setDisabled: Function,
setFieldOptions: Function,
formConf: {
type: Object,
required: true
}
},
data() {
const data = {
formConfCopy: deepClone(this.formConf),
[this.formConf.formModel]: {},
[this.formConf.formRules]: {},
options: {},
tableRefs: {}
}
this.initFormData(data.formConfCopy.fields, data[this.formConf.formModel])
this.buildRules(data.formConfCopy.fields, data[this.formConf.formRules])
this.buildOptions(data.formConfCopy.fields, data.options)
this.$nextTick(() => {
this.onLoad(data.formConfCopy, data[this.formConf.formModel])
})
return data
},
computed: {
parameter() {
return {
formData: this[this.formConf.formModel],
setFormData: this.setFormData,
setShowOrHide: this.setShowOrHide,
setRequired: this.setRequired,
setDisabled: this.setDisabled,
request: this.request,
getFieldOptions: this.getFieldOptions,
setFieldOptions: this.setFieldOptions
}
}
},
methods: {
initFormData(componentList, formData) {
this.$store.commit('generator/UPDATE_RELATION_DATA', {})
componentList.forEach(cur => {
const config = cur.__config__
if (cur.__vModel__) formData[cur.__vModel__] = config.defaultValue
if (cur.__config__.nccKey == 'table') return
if (config.children) this.initFormData(config.children, formData)
})
},
buildOptions(componentList, data) {
componentList.forEach(cur => {
const config = cur.__config__
if (dyOptionsList.indexOf(config.nccKey) > -1) {
let isTreeSelect = config.nccKey === 'treeSelect' || config.nccKey === 'cascader'
if (config.dataType === 'dictionary') {
if (!config.dictionaryType) return
getDictionaryDataSelector(config.dictionaryType).then(res => {
isTreeSelect ? cur.options = res.data.list : cur.__slot__.options = res.data.list
isTreeSelect ? data[cur.__vModel__ + 'Options'] = cur.options : data[cur.__vModel__ + 'Options'] = cur.__slot__.options
})
}
if (config.dataType === 'dynamic') {
if (!config.propsUrl) return
previewDataInterface(config.propsUrl).then(res => {
isTreeSelect ? cur.options = res.data : cur.__slot__.options = res.data
isTreeSelect ? data[cur.__vModel__ + 'Options'] = cur.options : data[cur.__vModel__ + 'Options'] = cur.__slot__.options
})
}
}
if (config.nccKey === 'popupSelect') {
if (!cur.interfaceId) return
previewDataInterface(cur.interfaceId).then(res => {
cur.options = res.data
data[cur.__vModel__ + 'Options'] = res.data
})
}
if (config.children && config.nccKey !== 'table') this.buildOptions(config.children, data)
})
},
buildRules(componentList, rules) {
componentList.forEach(cur => {
const config = cur.__config__
if (Array.isArray(config.regList)) {
if (config.required) {
const required = { required: config.required, message: cur.placeholder }
if (Array.isArray(config.defaultValue)) {
required.type = 'array'
required.message = `请至少选择一个${config.label}`
}
required.message === undefined && (required.message = `${config.label}不能为空`)
config.regList.push(required)
}
rules[cur.__vModel__] = config.regList.map(item => {
item.pattern && (item.pattern = eval(item.pattern))
item.trigger = config.trigger || 'blur'
return item
})
}
if (config.children) this.buildRules(config.children, rules)
})
},
onLoad(formConfCopy, formData) {
if (!formConfCopy || !formConfCopy.funcs || !formConfCopy.funcs.onLoad) return
const onLoadFunc = getFunc(formConfCopy.funcs.onLoad)
if (!onLoadFunc) return
onLoadFunc({ formData, request: this.request })
},
resetForm() {
this.$store.commit('generator/UPDATE_RELATION_DATA', {})
this.formConfCopy = deepClone(this.formConf)
this.$refs[this.formConf.formRef].resetFields()
Object.keys(this.tableRefs).forEach(vModel => {
this.$refs[vModel].$children[0].resetTable()
})
},
checkTableData() {
let valid = true
Object.keys(this.tableRefs).forEach(vModel => {
const res = this.$refs[vModel].$children[0].submit() // 返回false或表单数据
res ? (this[this.formConf.formModel][vModel] = res) : (valid = false)
})
return valid
},
request(url, method, data) {
if (!url) return
return request({
url: url,
method: method || 'GET',
data: data || {}
})
},
getFieldOptions(prop) {
if (!prop) return []
return this.options[prop + 'Options'] || []
},
beforeSubmit() {
let valid = true
if (!this.formConfCopy || !this.formConfCopy.funcs || !this.formConfCopy.funcs.beforeSubmit) return valid
const func = getFunc(this.formConfCopy.funcs.beforeSubmit)
if (!func) return valid
valid = func(this.parameter)
return valid
},
afterSubmit() {
if (!this.formConfCopy || !this.formConfCopy.funcs || !this.formConfCopy.funcs.afterSubmit) return
const func = getFunc(this.formConfCopy.funcs.afterSubmit)
if (!func) return
func(this.parameter)
},
submitForm() {
const beforeSubmitValid = this.beforeSubmit()
if (!beforeSubmitValid) return false
const isTableValid = this.checkTableData()
this.$refs[this.formConf.formRef].validate(valid => {
if (!valid) return false
if (!isTableValid) return false
// 触发sumit事件
this.$emit('submit', this[this.formConf.formModel], this.afterSubmit)
return true
})
}
},
render(h) {
return renderFrom.call(this, h)
}
}
</script>