diff --git a/src/rest/aicode/detail.js b/src/rest/aicode/detail.js index 36b89ae..3b29436 100644 --- a/src/rest/aicode/detail.js +++ b/src/rest/aicode/detail.js @@ -1,15 +1,6 @@ const dbUtils = require("../../utils/dbUitls"); -const checkJson = str => { - if (typeof str == 'string') { - try { - let obj = JSON.parse(str); - return !!(typeof obj == 'object' && obj); - } catch (e) { - return false; - } - } - return false; -} +const {checkJson} = require("../../tools"); + module.exports = { action: "/node/aicode/detail", method: "post", diff --git a/src/rest/aicode/getCode.js b/src/rest/aicode/getCode.js new file mode 100644 index 0000000..bb170c4 --- /dev/null +++ b/src/rest/aicode/getCode.js @@ -0,0 +1,34 @@ +const dbUtils = require("../../utils/dbUitls"); +const fse = require("fs-extra"); +const execute = require("../../tools/exec"); +const generate = require("../../tools/generate"); +module.exports = { + action: "/node/aicode/getCode", + method: "post", + execute: (request, response) => { + let id = request.query?.id, sql = `select * from node_aicode where id='${id}'` + dbUtils.query(sql).then(res => { + let info = res?.[0] + if (info?.id) { + let path = `/home/deploy/node-service/aicode/${info.id}`, zipPath = `${path}/${info.id}.zip` + generate(info, path).then(() => { + fse.pathExists(path, (err, exists) => { + console.log(`${path}=========>${exists}`) + if (exists) { + execute(`cd ${path}&&zip -r ${info.id}.zip .`) + .then(() => { + console.log('压缩完成!') + setTimeout(() => { + response.download(zipPath) + }, 1000) + }) + } else response.send({code: 1, err: "没有打包文件!"}) + }) + }) + } else response.send({code: 1, err: "无法找到应用信息"}) + }).catch(err => { + console.log(err) + response.send({code: 1, err: err.sqlMessage}) + }) + } +} diff --git a/src/tools/generate.js b/src/tools/generate.js new file mode 100644 index 0000000..f0b92df --- /dev/null +++ b/src/tools/generate.js @@ -0,0 +1,157 @@ +const fse = require("fs-extra"); +const {readFile} = require("../utils/fsUtils"); +const {checkJson} = require("./index"); +/** + * 生成入口页 + */ +const genHome = app => { + return readFile('./tpl/AppEntry.vue').then(data => { + let file = data.toString(), + content = file.replace(/@appName/g, app.appName) + .replace(/@name/g, app.name) + let dicts = `'yesOrNo'` + if (checkJson(app.props)) { + let props = JSON.parse(app.props)?.map(e => e.dict).filter(e => !!e) + props.length > 0 && (dicts = props.map(e => `'${e}'`).toString()) + } + content = content.replace(/@dicts/g, dicts) + return fse.outputFileSync(`./zips/${app.id}/${app.appName}.vue`, content) + }) +} +/** + * 生成列表页 + */ +const genList = app => { + return readFile('./tpl/list.vue').then(data => { + let file = data.toString(), + content = file.replace(/@rightCode/g, app.rightCode) + .replace(/@name/g, app.name), + listProps = "", searchProps = "", btns = "", tableBtns = "" + if (checkJson(app.props)) { + let props = JSON.parse(app.props) + listProps = JSON.stringify(props.filter(e => e.isTable)) + props.filter(e => e.isSearch && e.dict).map(e => { + searchProps += `` + }) + } + if (checkJson(app.btns)) { + let buttons = JSON.parse(app.btns) + buttons.map(e => { + if (e == "insertEnable") { + btns += `添加` + } else if (e == "importEnable") { + btns += `` + } else if (e == "exportEnalbe") { + btns += `` + } else if (e == "editEnable") { + tableBtns += `编辑` + } else if (e == "deleteEnable") { + tableBtns += `删除` + } else if (e == "batchDelEnable") { + btns += `删除` + } + }) + if (!searchProps) { + //当没有筛选条件时,按钮替换原筛选条件位置 + searchProps = btns + btns = "" + } else { + btns = `` + } + } + content = content.replace(/@listProps/g, listProps) + .replace(/@searchProps/g, searchProps) + .replace(/@btns/g, btns) + .replace(/@tableBtns/g, tableBtns) + return fse.outputFileSync(`./zips/${app.id}/list.vue`, content) + }) +} +/** + * 生成新增/编辑页 + */ +const genAdd = app => { + return readFile('./tpl/add.vue').then(data => { + let file = data.toString(), + content = file.replace(/@rightCode/g, app.rightCode) + .replace(/@name/g, app.name), + rules = "", domain = "" + if (checkJson(app.detailConfig)) { + let detail = JSON.parse(app.detailConfig), props = detail?.map(e => e.column).flat() + props.filter(e => e.mustFill == 1).map(e => { + rules += `${e.prop}: {required: true, message: "${e.fieldTips}"},` + }) + detail.map(group => { + let fields = `` + group.column?.map(e => { + fields += `${getComp(e)}` + }) + domain += `` + }) + } + content = content.replace(/@content/g, domain) + .replace(/@rules/g, rules) + return fse.outputFileSync(`./zips/${app.id}/add.vue`, content) + }) +} +const getComp = e => { + //字典下拉选择 + if (e.type == 'dict') { + return `` + //单选radio + } else if (e.type == 'radio') { + return ` + {{ item.dictName }} + ` + //开关onOff + } else if (e.type == 'onOff') { + return `` + //多选checkbox + } else if (e.type == 'checkbox') { + return ` + {{ item.dictName }} + ` + } else if (e.type == 'idNumber') { + return `` + //input输入框 + } else if (['input', 'name', 'phone'].includes(e.type)) { + return `` + //number 输入框 + } else if (e.type == 'number') { + return `` + //textarea输入框 + } else if (e.type == 'textarea' || e.type == 'text') { + return `` + //日期选择 + } else if (e.type == 'date') { + return `` + //日期带时分秒选择 + } else if (e.type == 'datetime') { + return `` + //时间-时分秒选择 + } else if (e.type == 'time') { + return `` + //附件 + } else if (e.type == 'upload') { + return ` ` + //富文本 + } else if (e.type == 'rtf') { + return `` + //地区选择 + } else if (e.type == 'area') { + return ` ` + } else if (e.type == 'user') { + //人员选择 + return `` + } +} +const generate = (app, dest) => { + fse.emptydirSync(dest) + fse.emptydirSync(`./zips/${app.id}`) + let tasks = [genHome(app), genList(app)] + app.detailType == 0 && tasks.push(genAdd(app)) + return Promise.all(tasks) +} +module.exports = generate diff --git a/src/tools/index.js b/src/tools/index.js new file mode 100644 index 0000000..f6dd143 --- /dev/null +++ b/src/tools/index.js @@ -0,0 +1,12 @@ +const checkJson = str => { + if (typeof str == 'string') { + try { + let obj = JSON.parse(str); + return !!(typeof obj == 'object' && obj); + } catch (e) { + return false; + } + } + return false; +} +module.exports = {checkJson} diff --git a/src/utils/fsUtils.js b/src/utils/fsUtils.js index ccd3df4..19b6e5c 100644 --- a/src/utils/fsUtils.js +++ b/src/utils/fsUtils.js @@ -34,4 +34,5 @@ const findFile = (dir = '.', cb) => { })) }) } -module.exports = {readdir, stat, findFile} +const readFile = promisify(fs.readFile) +module.exports = {readdir, stat, findFile, readFile} diff --git a/tpl/AppEntry.vue b/tpl/AppEntry.vue new file mode 100644 index 0000000..b62669a --- /dev/null +++ b/tpl/AppEntry.vue @@ -0,0 +1,36 @@ + + + + + diff --git a/tpl/add.vue b/tpl/add.vue new file mode 100644 index 0000000..955d985 --- /dev/null +++ b/tpl/add.vue @@ -0,0 +1,77 @@ + + + + + diff --git a/tpl/list.vue b/tpl/list.vue new file mode 100644 index 0000000..167f625 --- /dev/null +++ b/tpl/list.vue @@ -0,0 +1,82 @@ + + + + +