1
This commit is contained in:
159
api/controller_front/model.js
Normal file
159
api/controller_front/model.js
Normal file
@@ -0,0 +1,159 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { sys_model, sys_project, querySql, sys_model_field, op } = require("../../middleware/baseModel");
|
||||
const autoFile = require("../service/autoFile");
|
||||
const funTool = require("../../tool/funTool");
|
||||
const uuid = require("node-uuid");
|
||||
|
||||
module.exports = {
|
||||
"POST /model/all": async (ctx, next) => {
|
||||
let { projectKey } = ctx.getBody();
|
||||
|
||||
let projectRow = await sys_project.findOne({ where: { key: projectKey } });
|
||||
if (projectRow) {
|
||||
let project_id = projectRow.id;
|
||||
let resList = await sys_model.findAll({ where: { project_id } });
|
||||
|
||||
let resPromise = resList.map(async (row) => {
|
||||
let newRow = row.toJSON();
|
||||
|
||||
let isExistTableName = await querySql(`select TABLE_NAME from information_schema.TABLES where TABLE_NAME ="${newRow.key}"`);
|
||||
if (isExistTableName && isExistTableName.length > 0) {
|
||||
newRow.isTableExist = true;
|
||||
} else {
|
||||
newRow.isTableExist = false;
|
||||
}
|
||||
|
||||
let modelPath = await autoFile.outBuildPath(`api/model/${newRow.key}.js`, project_id);
|
||||
let isExistModel = await funTool.isExist(modelPath);
|
||||
newRow.isExistModel = isExistModel;
|
||||
|
||||
// 查询模型下所有字段
|
||||
let dbColKeys = [];
|
||||
let modelRow = await sys_model.findOne({ where: { id: newRow.id } });
|
||||
if (modelRow && newRow.isTableExist) {
|
||||
let colRows = await querySql(`show columns from ${modelRow.key}`);
|
||||
dbColKeys = colRows.map((p) => p.Field);
|
||||
}
|
||||
|
||||
let isDbFielExist = true;
|
||||
let model_fields = await sys_model_field.findAll({ where: { model_id: newRow.id } });
|
||||
model_fields.forEach((newFielRow) => {
|
||||
let = newFielRow.toJSON();
|
||||
if (dbColKeys.indexOf(newFielRow.key) === -1) {
|
||||
isDbFielExist = false;
|
||||
}
|
||||
});
|
||||
|
||||
newRow.isDbFielExist = isDbFielExist;
|
||||
|
||||
return newRow;
|
||||
});
|
||||
|
||||
let newResList = await Promise.all(resPromise);
|
||||
|
||||
return ctx.success(newResList);
|
||||
} else {
|
||||
return ctx.fail("未找到项目key");
|
||||
}
|
||||
},
|
||||
|
||||
"POST /model/detailPath": async (ctx, next) => {
|
||||
let { id } = ctx.getBody();
|
||||
let modelRow = await sys_model.findOne({ where: { id } });
|
||||
if (modelRow) {
|
||||
let { key, project_id } = modelRow;
|
||||
let projectRow = await sys_project.findOne({ where: { id: project_id } });
|
||||
if (projectRow) {
|
||||
let controllerPath = path.normalize(`/${projectRow.key}/api/controller_admin/${key}.js`);
|
||||
let modelPath = path.normalize(`/${projectRow.key}/api/model/${key}.js`);
|
||||
return ctx.success({ controllerPath, modelPath });
|
||||
}
|
||||
}
|
||||
|
||||
return ctx.success({ controllerPath: "", modelPath: "" });
|
||||
},
|
||||
|
||||
|
||||
"POST /form/updateForm": async (ctx, next) => {
|
||||
let { id, name, model_id, api_path, component } = ctx.getBody();
|
||||
let formRow = await sys_form.findOne({ where: { id: id } });
|
||||
if (formRow) {
|
||||
let res = await sys_form.update({ name: `${name}_表单`, model_id, api_path, component }, { where: { id } });
|
||||
return ctx.success(res);
|
||||
} else {
|
||||
return ctx.fail("未找到指定项目");
|
||||
}
|
||||
},
|
||||
|
||||
"POST /form/createForm": async (ctx, next) => {
|
||||
let { name, project_key, model_id, api_path, component } = ctx.getBody();
|
||||
let projectRow = await sys_project.findOne({ where: { key: project_key } });
|
||||
if (projectRow) {
|
||||
let project_id = projectRow.id;
|
||||
let res = await sys_form.create({ name: `${name}_表单`, project_id, model_id, api_path, component });
|
||||
return ctx.success(res);
|
||||
} else {
|
||||
return ctx.fail("未找到指定项目");
|
||||
}
|
||||
},
|
||||
|
||||
"POST /form/generate": async (ctx, next) => {
|
||||
let form_id = ctx.get("id");
|
||||
if (form_id) {
|
||||
const sysFormRow = await sys_form.findOne({ where: { id: form_id } });
|
||||
if (sysFormRow) {
|
||||
await autoFile.createForm(sysFormRow);
|
||||
let frontApiUrl = await autoFile.autoFrontApi(sysFormRow);
|
||||
let frontVueUrl = await autoFile.autoFrontVue(sysFormRow);
|
||||
|
||||
frontApiUrl = path.normalize(frontApiUrl);
|
||||
frontVueUrl = path.normalize(frontVueUrl);
|
||||
|
||||
return ctx.success({ frontApiUrl, frontVueUrl });
|
||||
}
|
||||
}
|
||||
return ctx.fail("生成失败");
|
||||
},
|
||||
|
||||
// 生成model
|
||||
"POST /model/regenerate": async (ctx, next) => {
|
||||
let row = ctx.getBody();
|
||||
let { id } = row;
|
||||
if (id) {
|
||||
let sysModelFields = await sys_model_field.findAll({ where: { model_id: id } });
|
||||
if (sysModelFields && sysModelFields.length > 0) {
|
||||
let tempUUid = uuid.v4();
|
||||
// 创建model
|
||||
await autoFile.autoModel(id, tempUUid);
|
||||
|
||||
// 创建apiServer
|
||||
await autoFile.autoController(id);
|
||||
|
||||
|
||||
let modelRow = await sys_model.findOne({ where: { id } });
|
||||
if (modelRow) {
|
||||
let { key, project_id } = modelRow;
|
||||
let projectRow = await sys_project.findOne({ where: { id: project_id } });
|
||||
if (projectRow) {
|
||||
let controllerPath = path.normalize(`/${projectRow.key}/api/controller_admin/${key}.js`);
|
||||
let modelPath = path.normalize(`/${projectRow.key}/api/model/${key}.js`);
|
||||
return ctx.success({ controllerPath, modelPath });
|
||||
}
|
||||
else {
|
||||
return ctx.fail("未找到项目");
|
||||
}
|
||||
}
|
||||
else {
|
||||
return ctx.fail("未找到模型文件");
|
||||
}
|
||||
|
||||
|
||||
|
||||
} else {
|
||||
return ctx.fail("字段未添加,无效生成");
|
||||
}
|
||||
}
|
||||
return ctx.fail("参数错误 id");
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user