| ¶Ô±ÈÐÂÎļþ |
| | |
| | | import { titleCase } from '@/utils/index' |
| | | import { trigger } from './config' |
| | | // æä»¶å¤§å°è®¾ç½® |
| | | const units = { |
| | | KB: '1024', |
| | | MB: '1024 / 1024', |
| | | GB: '1024 / 1024 / 1024', |
| | | } |
| | | /** |
| | | * @name: çæjséè¦çæ°æ® |
| | | * @description: çæjséè¦çæ°æ® |
| | | * @param {*} conf |
| | | * @param {*} type å¼¹çªæè¡¨å |
| | | * @return {*} |
| | | */ |
| | | export function makeUpJs(conf, type) { |
| | | conf = JSON.parse(JSON.stringify(conf)) |
| | | const dataList = [] |
| | | const ruleList = [] |
| | | const optionsList = [] |
| | | const propsList = [] |
| | | const methodList = [] |
| | | const uploadVarList = [] |
| | | |
| | | conf.fields.forEach((el) => { |
| | | buildAttributes( |
| | | el, |
| | | dataList, |
| | | ruleList, |
| | | optionsList, |
| | | methodList, |
| | | propsList, |
| | | uploadVarList |
| | | ) |
| | | }) |
| | | |
| | | const script = buildexport( |
| | | conf, |
| | | type, |
| | | dataList.join('\n'), |
| | | ruleList.join('\n'), |
| | | optionsList.join('\n'), |
| | | uploadVarList.join('\n'), |
| | | propsList.join('\n'), |
| | | methodList.join('\n') |
| | | ) |
| | | |
| | | return script |
| | | } |
| | | /** |
| | | * @name: çæåæ° |
| | | * @description: çæåæ°ï¼å
æ¬è¡¨åæ°æ®è¡¨åéªè¯æ°æ®ï¼å¤ééé¡¹æ°æ®ï¼ä¸ä¼ æ°æ®ç |
| | | * @return {*} |
| | | */ |
| | | function buildAttributes( |
| | | el, |
| | | dataList, |
| | | ruleList, |
| | | optionsList, |
| | | methodList, |
| | | propsList, |
| | | uploadVarList |
| | | ){ |
| | | buildData(el, dataList) |
| | | buildRules(el, ruleList) |
| | | |
| | | if (el.options && el.options.length) { |
| | | buildOptions(el, optionsList) |
| | | if (el.dataType === 'dynamic') { |
| | | const model = `${el.vModel}Options` |
| | | const options = titleCase(model) |
| | | buildOptionMethod(`get${options}`, model, methodList) |
| | | } |
| | | } |
| | | |
| | | if (el.props && el.props.props) { |
| | | buildProps(el, propsList) |
| | | } |
| | | |
| | | if (el.action && el.tag === 'el-upload') { |
| | | uploadVarList.push( |
| | | ` |
| | | // ä¸ä¼ 请æ±è·¯å¾ |
| | | const ${el.vModel}Action = ref('${el.action}') |
| | | // ä¸ä¼ æä»¶å表 |
| | | const ${el.vModel}fileList = ref([])` |
| | | ) |
| | | methodList.push(buildBeforeUpload(el)) |
| | | if (!el['auto-upload']) { |
| | | methodList.push(buildSubmitUpload(el)) |
| | | } |
| | | } |
| | | |
| | | if (el.children) { |
| | | el.children.forEach((el2) => { |
| | | buildAttributes( |
| | | el2, |
| | | dataList, |
| | | ruleList, |
| | | optionsList, |
| | | methodList, |
| | | propsList, |
| | | uploadVarList |
| | | ) |
| | | }) |
| | | } |
| | | } |
| | | /** |
| | | * @name: çæè¡¨åæ°æ®formData |
| | | * @description: çæè¡¨åæ°æ®formData |
| | | * @param {*} conf |
| | | * @param {*} dataList æ°æ®å表 |
| | | * @return {*} |
| | | */ |
| | | function buildData(conf, dataList) { |
| | | if (conf.vModel === undefined) return |
| | | let defaultValue |
| | | if (typeof conf.defaultValue === 'string' && !conf.multiple) { |
| | | defaultValue = `'${conf.defaultValue}'` |
| | | } else { |
| | | defaultValue = `${JSON.stringify(conf.defaultValue)}` |
| | | } |
| | | dataList.push(`${conf.vModel}: ${defaultValue},`) |
| | | } |
| | | /** |
| | | * @name: çæè¡¨åéªè¯æ°æ®rule |
| | | * @description: çæè¡¨åéªè¯æ°æ®rule |
| | | * @param {*} conf |
| | | * @param {*} ruleList éªè¯æ°æ®å表 |
| | | * @return {*} |
| | | */ |
| | | function buildRules(conf, ruleList) { |
| | | if (conf.vModel === undefined) return |
| | | const rules = [] |
| | | if (trigger[conf.tag]) { |
| | | if (conf.required) { |
| | | const type = Array.isArray(conf.defaultValue) ? "type: 'array'," : '' |
| | | let message = Array.isArray(conf.defaultValue) |
| | | ? `请è³å°éæ©ä¸ä¸ª${conf.vModel}` |
| | | : conf.placeholder |
| | | if (message === undefined) message = `${conf.label}ä¸è½ä¸ºç©º` |
| | | rules.push( |
| | | `{ required: true, ${type} message: '${message}', trigger: '${ |
| | | trigger[conf.tag] |
| | | }' }` |
| | | ) |
| | | } |
| | | if (conf.regList && Array.isArray(conf.regList)) { |
| | | conf.regList.forEach((item) => { |
| | | if (item.pattern) { |
| | | rules.push( |
| | | `{ pattern: new RegExp(${item.pattern}), message: '${ |
| | | item.message |
| | | }', trigger: '${trigger[conf.tag]}' }` |
| | | ) |
| | | } |
| | | }) |
| | | } |
| | | ruleList.push(`${conf.vModel}: [${rules.join(',')}],`) |
| | | } |
| | | } |
| | | /** |
| | | * @name: çæéé¡¹æ°æ® |
| | | * @description: çæéé¡¹æ°æ®ï¼åéå¤é䏿ç |
| | | * @param {*} conf |
| | | * @param {*} optionsList éé¡¹æ°æ®å表 |
| | | * @return {*} |
| | | */ |
| | | function buildOptions(conf, optionsList) { |
| | | if (conf.vModel === undefined) return |
| | | if (conf.dataType === 'dynamic') { |
| | | conf.options = [] |
| | | } |
| | | const str = `const ${conf.vModel}Options = ref(${JSON.stringify(conf.options)})` |
| | | optionsList.push(str) |
| | | } |
| | | /** |
| | | * @name: çææ¹æ³ |
| | | * @description: çææ¹æ³ |
| | | * @param {*} methodName æ¹æ³å |
| | | * @param {*} model |
| | | * @param {*} methodList æ¹æ³å表 |
| | | * @return {*} |
| | | */ |
| | | function buildOptionMethod(methodName, model, methodList) { |
| | | const str = `function ${methodName}() { |
| | | // TODO å起请æ±è·åæ°æ® |
| | | ${model}.value |
| | | }` |
| | | methodList.push(str) |
| | | } |
| | | /** |
| | | * @name: çæè¡¨åç»ä»¶éè¦çprops设置 |
| | | * @description: çæè¡¨åç»ä»¶éè¦çprops设置ï¼å¦ï¼çº§èç»ä»¶ |
| | | * @param {*} conf |
| | | * @param {*} propsList |
| | | * @return {*} |
| | | */ |
| | | function buildProps(conf, propsList) { |
| | | if (conf.dataType === 'dynamic') { |
| | | conf.valueKey !== 'value' && (conf.props.props.value = conf.valueKey) |
| | | conf.labelKey !== 'label' && (conf.props.props.label = conf.labelKey) |
| | | conf.childrenKey !== 'children' && |
| | | (conf.props.props.children = conf.childrenKey) |
| | | } |
| | | const str = ` |
| | | // props设置 |
| | | const ${conf.vModel}Props = ref(${JSON.stringify(conf.props.props)})` |
| | | propsList.push(str) |
| | | } |
| | | /** |
| | | * @name: çæä¸ä¼ ç»ä»¶çç¸å
³å
容 |
| | | * @description: çæä¸ä¼ ç»ä»¶çç¸å
³å
容 |
| | | * @param {*} conf |
| | | * @return {*} |
| | | */ |
| | | function buildBeforeUpload(conf) { |
| | | const unitNum = units[conf.sizeUnit] |
| | | let rightSizeCode = '' |
| | | let acceptCode = '' |
| | | const returnList = [] |
| | | if (conf.fileSize) { |
| | | rightSizeCode = `let isRightSize = file.size / ${unitNum} < ${conf.fileSize} |
| | | if(!isRightSize){ |
| | | proxy.$modal.msgError('æä»¶å¤§å°è¶
è¿ ${conf.fileSize}${conf.sizeUnit}') |
| | | }` |
| | | returnList.push('isRightSize') |
| | | } |
| | | if (conf.accept) { |
| | | acceptCode = `let isAccept = new RegExp('${conf.accept}').test(file.type) |
| | | if(!isAccept){ |
| | | proxy.$modal.msgError('åºè¯¥éæ©${conf.accept}ç±»åçæä»¶') |
| | | }` |
| | | returnList.push('isAccept') |
| | | } |
| | | const str = ` |
| | | /** |
| | | * @name: ä¸ä¼ ä¹åçæä»¶å¤æ |
| | | * @description: ä¸ä¼ ä¹åçæä»¶å¤æï¼å¤ææä»¶å¤§å°æä»¶ç±»åç |
| | | * @param {*} file |
| | | * @return {*} |
| | | */ |
| | | function ${conf.vModel}BeforeUpload(file) { |
| | | ${rightSizeCode} |
| | | ${acceptCode} |
| | | return ${returnList.join('&&')} |
| | | }` |
| | | return returnList.length ? str : '' |
| | | } |
| | | /** |
| | | * @name: çææäº¤è¡¨åæ¹æ³ |
| | | * @description: çææäº¤è¡¨åæ¹æ³ |
| | | * @param {Object} conf vModel 表åref |
| | | * @return {*} |
| | | */ |
| | | function buildSubmitUpload(conf) { |
| | | const str = `function submitUpload() { |
| | | this.$refs['${conf.vModel}'].submit() |
| | | }` |
| | | return str |
| | | } |
| | | /** |
| | | * @name: ç»è£
js代ç |
| | | * @description: ç»è£
jsä»£ç æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function buildexport( |
| | | conf, |
| | | type, |
| | | data, |
| | | rules, |
| | | selectOptions, |
| | | uploadVar, |
| | | props, |
| | | methods |
| | | ) { |
| | | let str = ` |
| | | const { proxy } = getCurrentInstance() |
| | | const ${conf.formRef} = ref() |
| | | const data = reactive({ |
| | | ${conf.formModel}: { |
| | | ${data} |
| | | }, |
| | | ${conf.formRules}: { |
| | | ${rules} |
| | | } |
| | | }) |
| | | |
| | | const {${conf.formModel}, ${conf.formRules}} = toRefs(data) |
| | | |
| | | ${selectOptions} |
| | | |
| | | ${uploadVar} |
| | | |
| | | ${props} |
| | | |
| | | ${methods} |
| | | ` |
| | | |
| | | if(type === 'dialog') { |
| | | str += ` |
| | | // å¼¹çªè®¾ç½® |
| | | const dialogVisible = defineModel() |
| | | // å¼¹çªç¡®è®¤åè° |
| | | const emit = defineEmits(['confirm']) |
| | | /** |
| | | * @name: å¼¹çªæå¼åæ§è¡ |
| | | * @description: å¼¹çªæå¼åæ§è¡æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function onOpen(){ |
| | | |
| | | } |
| | | /** |
| | | * @name: å¼¹çªå
³éæ¶æ§è¡ |
| | | * @description: å¼¹çªå
³éæ¹æ³ï¼é置表å |
| | | * @return {*} |
| | | */ |
| | | function onClose(){ |
| | | ${conf.formRef}.value.resetFields() |
| | | } |
| | | /** |
| | | * @name: å¼¹çªåæ¶ |
| | | * @description: å¼¹çªåæ¶æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function close(){ |
| | | dialogVisible.value = false |
| | | } |
| | | /** |
| | | * @name: å¼¹çªè¡¨åæäº¤ |
| | | * @description: å¼¹çªè¡¨åæäº¤æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function handelConfirm(){ |
| | | ${conf.formRef}.value.validate((valid) => { |
| | | if (!valid) return |
| | | // TODO æäº¤è¡¨å |
| | | |
| | | close() |
| | | // åè°ç¶çº§ç»ä»¶ |
| | | emit('confirm') |
| | | }) |
| | | } |
| | | ` |
| | | } else { |
| | | str += ` |
| | | /** |
| | | * @name: 表åæäº¤ |
| | | * @description: 表åæäº¤æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function submitForm() { |
| | | ${conf.formRef}.value.validate((valid) => { |
| | | if (!valid) return |
| | | // TODO æäº¤è¡¨å |
| | | }) |
| | | } |
| | | /** |
| | | * @name: 表åéç½® |
| | | * @description: 表åéç½®æ¹æ³ |
| | | * @return {*} |
| | | */ |
| | | function resetForm() { |
| | | ${conf.formRef}.value.resetFields() |
| | | } |
| | | ` |
| | | } |
| | | return str |
| | | } |