Blame view

components/ncc/ncc-parser/index_1.vue 4.32 KB
290144e9   易尊强   第一次
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
  <template>
  	<u-form :model="formData" :rules="rules" ref="dataForm" :errorType="['toast']"
  		:label-position="formConf.labelPosition==='top'?'top':'left'" :label-width="formConf.labelWidth*1.5"
  		:label-align="formConf.labelPosition==='right'?'right':'left'" class="ncc-form">
  		<template v-for="(item, index) in formConfCopy.fields">
  			<Item :key="index" :item="item" :formConf="formConf" @input="setValue"
  				:ref="item.__vModel__?item.__vModel__: undefined" v-if="!item.__config__.noShow" />
  		</template>
  	</u-form>
  </template>
  <script>
  	import Item from './Item'
  	import {
  		getDictionaryDataSelector,
  		previewDataInterface
  	} from '@/api/common'
  	const dyOptionsList = ['radio', 'checkbox', 'select', 'cascader', 'treeSelect']
  
  	export default {
  		components: {
  			Item
  		},
  		props: {
  			formConf: {
  				type: Object,
  				required: true
  			},
  			loading: {
  				type: Boolean,
  				default: false
  			}
  		},
  		data() {
  			const data = {
  				formConfCopy: this.$u.deepClone(this.formConf),
  				formData: {},
  				rules: {},
  				tableRefs: {}
  			}
  			this.initFormData(data.formConfCopy.fields, data.formData, data.tableRefs)
  			this.buildRules(data.formConfCopy.fields, data.rules)
  			this.buildOptions(data.formConfCopy.fields)
  			return data
  		},
  		mounted() {
  			this.$refs.dataForm.setRules(this.rules);
  		},
  		methods: {
  			initFormData(componentList, formData, tableRefs) {
  				componentList.forEach(cur => {
  					const config = cur.__config__
  					if (cur.__vModel__) formData[cur.__vModel__] = config.defaultValue
  					if (config.children && cur.__config__.nccKey !== 'table') {
  						this.initFormData(config.children, formData)
  					}
  					if (cur.__config__.nccKey == 'table' && !cur.__config__.noShow) {
  						tableRefs[cur.__vModel__] = cur
  					}
  				})
  			},
  			buildOptions(componentList) {
  				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
  							isTreeSelect ? cur.options = [] : cur.__slot__.options = []
  							getDictionaryDataSelector(config.dictionaryType).then(res => {
  								isTreeSelect ? cur.options = res.data.list : cur.__slot__.options = res
  									.data.list
  							})
  						}
  						if (config.dataType === 'dynamic') {
  							if (!config.propsUrl) return
  							isTreeSelect ? cur.options = [] : cur.__slot__.options = []
  							previewDataInterface(config.propsUrl).then(res => {
  								isTreeSelect ? cur.options = res.data : cur.__slot__.options = res.data
  							})
  						}
  					}
  					if (config.children && config.nccKey !== 'table') this.buildOptions(config.children)
  				})
  			},
  			buildRules(componentList, rules) {
  				componentList.forEach(cur => {
  					const config = cur.__config__
  					const nccKey = config.nccKey
  					const useNumList = ['numInput', 'switch', 'date', 'rate', 'slider']
  					if (!Array.isArray(config.regList)) config.regList = []
  					if (config.required) {
  						config.regList.push({
  							required: config.required,
  							message: `${config.label}不能为空`
  						})
  					}
  					const rule = config.regList.map(item => {
  						item.pattern && (item.pattern = eval(item.pattern))
  						item.trigger = config.trigger || 'blur'
  						if (useNumList.includes(nccKey)) item.type = 'number'
  						if (Array.isArray(config.defaultValue)) item.type = 'array'
  						return item
  					})
  					if (rule.length) rules[cur.__vModel__] = rule
  					if (config.children && nccKey !== 'table') this.buildRules(config.children, rules)
  				})
  			},
  			setValue(item) {
  				if (!item.__vModel__) return
  				this.$set(this.formData, item.__vModel__, item.__config__.defaultValue)
  			},
  			checkTableData() {
  				let valid = true
  				Object.keys(this.tableRefs).forEach(vModel => {
  					const res = this.$refs[vModel][0].$refs[vModel].submit()
  					res ? (this.formData[vModel] = res) : (valid = false)
  				})
  				return valid
  			},
  			submitForm() {
  				if (!this.checkTableData()) return
  				this.$refs.dataForm.validate(valid => {
  					if (!valid) return
  					this.$emit('submit', this.formData)
  				});
  			},
  			resetForm() {
  				this.$refs.dataForm.resetFields()
  			},
  			setFormValue(vModel, value) {
  				this.formData[vModel] = value
  				this.$refs[vModel][0].setDefaultValue(value)
  			}
  		}
  	}
  </script>