懒羊羊
2024-01-31 e57a8990ae56f657a59c435a0613c5f7a8728003
提交 | 用户 | 时间
e57a89 1 import { exportDefault, titleCase } from '@/utils/index'
2 import { trigger } from './config'
3
4 const units = {
5   KB: '1024',
6   MB: '1024 / 1024',
7   GB: '1024 / 1024 / 1024'
8 }
9 let confGlobal
10 const inheritAttrs = {
11   file: '',
12   dialog: 'inheritAttrs: false,'
13 }
14
15
16 export function makeUpJs(conf, type) {
17   confGlobal = conf = JSON.parse(JSON.stringify(conf))
18   const dataList = []
19   const ruleList = []
20   const optionsList = []
21   const propsList = []
22   const methodList = mixinMethod(type)
23   const uploadVarList = []
24
25   conf.fields.forEach(el => {
26     buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
27   })
28
29   const script = buildexport(
30     conf,
31     type,
32     dataList.join('\n'),
33     ruleList.join('\n'),
34     optionsList.join('\n'),
35     uploadVarList.join('\n'),
36     propsList.join('\n'),
37     methodList.join('\n')
38   )
39   confGlobal = null
40   return script
41 }
42
43 function buildAttributes(el, dataList, ruleList, optionsList, methodList, propsList, uploadVarList) {
44   buildData(el, dataList)
45   buildRules(el, ruleList)
46
47   if (el.options && el.options.length) {
48     buildOptions(el, optionsList)
49     if (el.dataType === 'dynamic') {
50       const model = `${el.vModel}Options`
51       const options = titleCase(model)
52       buildOptionMethod(`get${options}`, model, methodList)
53     }
54   }
55
56   if (el.props && el.props.props) {
57     buildProps(el, propsList)
58   }
59
60   if (el.action && el.tag === 'el-upload') {
61     uploadVarList.push(
62       `${el.vModel}Action: '${el.action}',
63       ${el.vModel}fileList: [],`
64     )
65     methodList.push(buildBeforeUpload(el))
66     if (!el['auto-upload']) {
67       methodList.push(buildSubmitUpload(el))
68     }
69   }
70
71   if (el.children) {
72     el.children.forEach(el2 => {
73       buildAttributes(el2, dataList, ruleList, optionsList, methodList, propsList, uploadVarList)
74     })
75   }
76 }
77
78 function mixinMethod(type) {
79   const list = []; const
80     minxins = {
81       file: confGlobal.formBtns ? {
82         submitForm: `submitForm() {
83         this.$refs['${confGlobal.formRef}'].validate(valid => {
84           if(!valid) return
85           // TODO 提交表单
86         })
87       },`,
88         resetForm: `resetForm() {
89         this.$refs['${confGlobal.formRef}'].resetFields()
90       },`
91       } : null,
92       dialog: {
93         onOpen: 'onOpen() {},',
94         onClose: `onClose() {
95         this.$refs['${confGlobal.formRef}'].resetFields()
96       },`,
97         close: `close() {
98         this.$emit('update:visible', false)
99       },`,
100         handleConfirm: `handleConfirm() {
101         this.$refs['${confGlobal.formRef}'].validate(valid => {
102           if(!valid) return
103           this.close()
104         })
105       },`
106       }
107     }
108
109   const methods = minxins[type]
110   if (methods) {
111     Object.keys(methods).forEach(key => {
112       list.push(methods[key])
113     })
114   }
115
116   return list
117 }
118
119 function buildData(conf, dataList) {
120   if (conf.vModel === undefined) return
121   let defaultValue
122   if (typeof (conf.defaultValue) === 'string' && !conf.multiple) {
123     defaultValue = `'${conf.defaultValue}'`
124   } else {
125     defaultValue = `${JSON.stringify(conf.defaultValue)}`
126   }
127   dataList.push(`${conf.vModel}: ${defaultValue},`)
128 }
129
130 function buildRules(conf, ruleList) {
131   if (conf.vModel === undefined) return
132   const rules = []
133   if (trigger[conf.tag]) {
134     if (conf.required) {
135       const type = Array.isArray(conf.defaultValue) ? 'type: \'array\',' : ''
136       let message = Array.isArray(conf.defaultValue) ? `请至少选择一个${conf.vModel}` : conf.placeholder
137       if (message === undefined) message = `${conf.label}不能为空`
138       rules.push(`{ required: true, ${type} message: '${message}', trigger: '${trigger[conf.tag]}' }`)
139     }
140     if (conf.regList && Array.isArray(conf.regList)) {
141       conf.regList.forEach(item => {
142         if (item.pattern) {
143           rules.push(`{ pattern: ${eval(item.pattern)}, message: '${item.message}', trigger: '${trigger[conf.tag]}' }`)
144         }
145       })
146     }
147     ruleList.push(`${conf.vModel}: [${rules.join(',')}],`)
148   }
149 }
150
151 function buildOptions(conf, optionsList) {
152   if (conf.vModel === undefined) return
153   if (conf.dataType === 'dynamic') { conf.options = [] }
154   const str = `${conf.vModel}Options: ${JSON.stringify(conf.options)},`
155   optionsList.push(str)
156 }
157
158 function buildProps(conf, propsList) {
159   if (conf.dataType === 'dynamic') {
160     conf.valueKey !== 'value' && (conf.props.props.value = conf.valueKey)
161     conf.labelKey !== 'label' && (conf.props.props.label = conf.labelKey)
162     conf.childrenKey !== 'children' && (conf.props.props.children = conf.childrenKey)
163   }
164   const str = `${conf.vModel}Props: ${JSON.stringify(conf.props.props)},`
165   propsList.push(str)
166 }
167
168 function buildBeforeUpload(conf) {
169   const unitNum = units[conf.sizeUnit]; let rightSizeCode = ''; let acceptCode = ''; const
170     returnList = []
171   if (conf.fileSize) {
172     rightSizeCode = `let isRightSize = file.size / ${unitNum} < ${conf.fileSize}
173     if(!isRightSize){
174       this.$message.error('文件大小超过 ${conf.fileSize}${conf.sizeUnit}')
175     }`
176     returnList.push('isRightSize')
177   }
178   if (conf.accept) {
179     acceptCode = `let isAccept = new RegExp('${conf.accept}').test(file.type)
180     if(!isAccept){
181       this.$message.error('应该选择${conf.accept}类型的文件')
182     }`
183     returnList.push('isAccept')
184   }
185   const str = `${conf.vModel}BeforeUpload(file) {
186     ${rightSizeCode}
187     ${acceptCode}
188     return ${returnList.join('&&')}
189   },`
190   return returnList.length ? str : ''
191 }
192
193 function buildSubmitUpload(conf) {
194   const str = `submitUpload() {
195     this.$refs['${conf.vModel}'].submit()
196   },`
197   return str
198 }
199
200 function buildOptionMethod(methodName, model, methodList) {
201   const str = `${methodName}() {
202     // TODO 发起请求获取数据
203     this.${model}
204   },`
205   methodList.push(str)
206 }
207
208 function buildexport(conf, type, data, rules, selectOptions, uploadVar, props, methods) {
209   const str = `${exportDefault}{
210   ${inheritAttrs[type]}
211   components: {},
212   props: [],
213   data () {
214     return {
215       ${conf.formModel}: {
216         ${data}
217       },
218       ${conf.formRules}: {
219         ${rules}
220       },
221       ${uploadVar}
222       ${selectOptions}
223       ${props}
224     }
225   },
226   computed: {},
227   watch: {},
228   created () {},
229   mounted () {},
230   methods: {
231     ${methods}
232   }
233 }`
234   return str
235 }