From 69b917fa605be8ccd0984e5c095f24d6476dce95 Mon Sep 17 00:00:00 2001
From: yyb <995253665@qq.com>
Date: 星期五, 05 六月 2026 00:55:46 +0800
Subject: [PATCH] 1
---
src/utils/generator/js.js | 370 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 370 insertions(+), 0 deletions(-)
diff --git a/src/utils/generator/js.js b/src/utils/generator/js.js
new file mode 100644
index 0000000..dc38bfe
--- /dev/null
+++ b/src/utils/generator/js.js
@@ -0,0 +1,370 @@
+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浠g爜
+ * @description: 缁勮js浠g爜鏂规硶
+ * @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
+}
--
Gitblit v1.9.3