This commit is contained in:
张成
2025-11-21 16:53:49 +08:00
commit 8309808835
286 changed files with 32656 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
const { sys_announcement, op } = require('../../middleware/baseModel')
module.exports = {
'GET /sys_announcement/dtl': async (ctx, next) => {
const resList = await sys_announcement.findOne({ order: [['id', 'DESC']] })
return ctx.success(resList)
},
'POST /sys_announcement/add': async (ctx, next) => {
let row = ctx.getBody()
let dbRow = await sys_announcement.findOne({ order: [['id', 'DESC']] })
if (dbRow) {
await dbRow.update(row)
} else {
await sys_announcement.create(row)
}
return ctx.success()
},
'POST /sys_announcement/edit': async (ctx, next) => {
let row = ctx.getBody()
let id = ctx.get('id')
const resList = await sys_announcement.update(row, {
where: {
id: id
}
})
return ctx.success(resList)
},
}

View File

@@ -0,0 +1,9 @@
const fs = require("fs");
const path = require("path");
const { sys_model, 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 /sys_build/down": async (ctx, next) => {},
};

View File

@@ -0,0 +1,43 @@
const { sys_control_type, op } = require("../../middleware/baseModel");
module.exports = {
"GET /sys_control_type/all": async (ctx, next) => {
const resList = await sys_control_type.findAll();
return ctx.success(resList);
},
"POST /sys_control_type/page": async (ctx, next) => {
let param = ctx.getPageSize();
let row = ctx.getBody();
let { key, value } = row.seachOption;
let where = {};
if (key && value) {
where[key] = { [op.like]: "%" + value + "%" };
}
const res = await sys_control_type.findAndCountAll({
where,
order: [["id", "DESC"]],
...param,
});
return ctx.success(res);
},
"POST /sys_control_type/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_control_type.create(row);
return ctx.success(res);
},
"POST /sys_control_type/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
const resList = await sys_control_type.update(row, { where: { id: id } });
return ctx.success(resList);
},
"POST /sys_control_type/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_control_type.destroy({ where: { id: id } });
return ctx.success(res);
},
};

View File

@@ -0,0 +1,104 @@
var UUID = require("uuid");
var fs = require("fs");
var path = require("path");
const ossTool = require("../service/ossTool");
const funTool = require("../../tool/funTool");
const { sys_project, sys_project_config } = require("../../middleware/baseModel");
var ejs = require("ejs");
const AdmZip = require("adm-zip");
module.exports = {
"POST /sys_file/upload_img": async (ctx, next) => {
const files = ctx.request.files; // 获取上传文件
let fileArray = [];
let rootPath = path.join(__dirname, "../../upload/imgs");
for (var key in files) {
fileArray.push(files[key]);
}
//创建文件夹
await funTool.mkdirsSync(rootPath);
let resArray = [];
fileArray.forEach((file) => {
// 创建可读流
const reader = fs.createReadStream(file.path);
let filePath = `/${UUID.v1() + "_" + file.name}`;
// 创建可写流
const upStream = fs.createWriteStream(path.join(rootPath, filePath));
// 可读流通过管道写入可写流
reader.pipe(upStream);
resArray.push({ name: file.name, path: path.join("/imgs", filePath) });
});
ctx.success(resArray);
},
"POST /sys_file/upload_oos_img": async (ctx, next) => {
let fileArray = [];
const files = ctx.request.files; // 获取上传文件
for (var key in files) {
fileArray.push(files[key]);
}
let data = await ossTool.putImg(fileArray[0]);
if (data.path) {
return ctx.success(data);
} else {
return ctx.fail();
}
},
"POST /sys_file/get_base_project": async (ctx, next) => {
let project_id = ctx.get("project_id");
const project_row = await sys_project.findOne({ where: { id: project_id } });
const project_config_row = await sys_project_config.findOne({ where: { project_id: project_id } });
if (project_row && project_config_row) {
let tempPath = `./config/template/temp/${project_row.key}/`
const zip = new AdmZip("./config/template/systemWeb.zip");
zip.extractAllTo(tempPath, true);
let apiConfigPath = tempPath + "config/config.js"
let apiTemplateConfig = await fs.readFileSync(apiConfigPath, "utf-8");
let { db, aliyun, redis, wechat } = project_config_row
let apiConfigText = ejs.render(apiTemplateConfig, { project_key: project_row.key, db, aliyun, redis, wechat }, { rmWhitespace: true });
fs.writeFileSync(apiConfigPath, apiConfigText) // 替换api 配置文件
let adminConfigPath = tempPath + "admin/src/config/index.js"
let adminTemplateConfig = await fs.readFileSync(adminConfigPath, "utf-8");
let adminConfigText = ejs.render(adminTemplateConfig, { title: project_row.name }, { rmWhitespace: true });
fs.writeFileSync(adminConfigPath, adminConfigText) // 替换admin 配置文件
const zip2 = new AdmZip();
zip2.addLocalFolder(tempPath);
let buildPath = `./build/${project_row.key}/${project_row.key}.zip`
if (fs.existsSync(buildPath)) {
fs.unlinkSync(buildPath);
}
zip2.writeZip(buildPath); // 重新压缩文件
fs.rmSync(tempPath, { recursive: true, force: true });
return ctx.success({ path: buildPath.substring(2, buildPath.length) });
}
else {
return ctx.fail('获取错误,db 或者项目 配置不正确');
}
}
};

View File

@@ -0,0 +1,46 @@
const { sys_form_field, sys_form, op } = require("../../middleware/baseModel");
const { getMd5 } = require("../../tool/md5");
module.exports = {
"GET /sys_form_field/all": async (ctx, next) => {
let form_id = ctx.get("form_id");
const resList = await sys_form_field.findAll({
where: { form_id },
include: [
{
association: sys_form_field.associations.field,
attributes: ["key", "name"],
},
],
order: [["sort", "asc"]],
});
return ctx.success(resList);
},
"POST /sys_form_field/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_form_field.create(row);
return ctx.success(res);
},
"POST /sys_form_field/edit": async (ctx, next) => {
let row = ctx.getBody();
let { id } = row;
const res = await sys_form_field.update(row, {
where: {
id: id,
},
});
return ctx.success(res);
},
"POST /sys_form_field/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_form_field.destroy({
where: {
id: id,
},
});
return ctx.success(res);
},
};

View File

@@ -0,0 +1,75 @@
const path = require("path");
const autoFile = require("../service/autoFile");
const { sys_form, op } = require("../../middleware/baseModel");
module.exports = {
"GET /sys_form/all": async (ctx, next) => {
let project_id = ctx.getAdminProjectId();
const res = await sys_form.findAll({ where: { project_id } });
return ctx.success(res);
},
"GET /sys_form/detail": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_form.findOne({
where: { id: id },
include: [{ association: sys_form.associations.model, attributes: ["key", "name"] }],
});
return ctx.success(res);
},
"POST /sys_form/page": async (ctx, next) => {
let project_id = ctx.getAdminProjectId();
let param = ctx.getPageSize();
let row = ctx.getBody();
let { key, value } = row.seachOption;
let where = { project_id };
if (key && value) {
where[key] = { [op.like]: "%" + value + "%" };
}
const res = await sys_form.findAndCountAll({
where,
include: [{ association: sys_form.associations.model, attributes: ["key", "name"] }],
order: [["id", "DESC"]],
...param,
});
return ctx.success(res);
},
"POST /sys_form/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_form.create(row);
return ctx.success(res);
},
"POST /sys_form/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
const res = await sys_form.update(row, { where: { id: id } });
return ctx.success(res);
},
"POST /sys_form/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_form.destroy({ where: { id: id } });
return ctx.success(res);
},
"POST /sys_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("生成失败");
},
};

View File

@@ -0,0 +1,41 @@
const fs = require("fs");
const path = require("path");
const funTool = require("../../tool/funTool");
const { sys_log, op } = require("../../middleware/baseModel");
module.exports = {
"GET /sys_log/all": async (ctx, next) => {
let folderPath = path.join(__dirname, `../../logs`);
let files = fs.readdirSync(folderPath);
let newFiles = files.reverse().map((p) => {
return {
title: p,
};
});
return ctx.success(newFiles);
},
"GET /sys_log/detail": async (ctx, next) => {
let title = ctx.get("title");
let filePath = path.join(__dirname, `../../logs/${title}`);
let data = fs.readFileSync(filePath, "utf-8");
// data = data.replace(/\n/g, "</br>");
return ctx.success(data);
},
"GET /sys_log/delete": async (ctx, next) => {
let title = ctx.get("title");
let filePath = path.join(__dirname, `../../logs/${title}`);
if (await funTool.isExist(filePath)) {
fs.unlinkSync(filePath);
}
return ctx.success();
},
"GET /sys_log/operates": async (ctx, next) => {
let param = ctx.getPageSize();
let data = await sys_log.findAndCountAll({ order: [["id", "desc"]], ...param });
return ctx.success(data);
},
};

View File

@@ -0,0 +1,46 @@
const { sys_menu, op } = require("../../middleware/baseModel");
const autoFile = require("../service/autoFile");
const funTool = require("../../tool/funTool");
module.exports = {
"GET /sys_menu/index": async (ctx, next) => {
const menuRows = await sys_menu.findAll({
order: [["sort", "ASC"]],
});
let newMenuRows = menuRows.map((row) => {
let newRow = row.toJSON();
let nodeIds = funTool.getParantNode(newRow, menuRows);
newRow.parent_node_ids = nodeIds.reverse();
return newRow;
});
return ctx.success(newMenuRows);
},
"POST /sys_menu/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_menu.create(row);
return ctx.success(res);
},
"POST /sys_menu/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
const resList = await sys_menu.update(row, {
where: {
id: id,
},
});
return ctx.success(resList);
},
"POST /sys_menu/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_menu.destroy({ where: { id: id } });
return ctx.success(res);
},
};

View File

@@ -0,0 +1,229 @@
const fs = require("fs");
const path = require("path");
const { sys_model, 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 = {
"GET /sys_model/detail": async (ctx, next) => {
let id = ctx.get("id");
let res = await sys_model.findOne({ where: { id } });
return ctx.success(res);
},
"GET /sys_model/all": async (ctx, next) => {
let project_id = ctx.getAdminProjectId();
let resList = await sys_model.findAll({ where: { project_id } });
let resPromise = resList.map(async (row) => {
let newRow = row.toJSON();
let isExistTableName = await autoFile.querySql(`select TABLE_NAME from information_schema.TABLES where TABLE_NAME ="${newRow.key}"`, project_id);
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 autoFile.querySql(`show columns from ${modelRow.key}`, project_id);
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 curRow = newFielRow.toJSON();
if (dbColKeys.indexOf(curRow.key) === -1) {
isDbFielExist = false;
}
});
newRow.isDbFielExist = isDbFielExist;
return newRow;
});
let newResList = await Promise.all(resPromise);
return ctx.success(newResList);
},
// 从db中同步模型
"POST /sys_model/autoModelBydb": async (ctx, next) => {
let project_id = ctx.getAdminProjectId();
let rows = await autoFile.querySql(`select TABLE_NAME,TABLE_COMMENT from information_schema.TABLES`, project_id)
let notNeedPrefix = ["sys", "usr", "dtl"]
const checkNeedPrefix = (item) => {
let isCheck = true
for (let i = 0; i < notNeedPrefix.length; i++) {
let str = notNeedPrefix[i]
if (item.indexOf(str) > -1) {
isCheck = false
break;
}
}
return isCheck
}
let needKeys = rows.filter(item => item.TABLE_NAME.charAt(3) === '_' && /^[a-z_]+$/.test(item.TABLE_NAME) && checkNeedPrefix(item.TABLE_NAME));
if (needKeys && needKeys.length > 0) {
// 删除已存在的 model
let model_rows = await sys_model.findAll({ where: { project_id } })
if (model_rows && model_rows.length > 0) {
for (let i = 0; i < model_rows.length; i++) {
let model_row = model_rows[i]
await sys_model_field.destroy({ where: { model_id: model_row.id } })
await model_row.destroy()
}
}
for (let i = 0; i < needKeys.length; i++) {
let needKey = needKeys[i].TABLE_NAME
let needName = needKeys[i].TABLE_COMMENT || ''
let modelRow = await sys_model.findOne({ where: { key: needKey, project_id } });
// 不存在则创建model
if (!modelRow) {
let res = await sys_model.create({ key: needKey, name: needName, project_id: project_id })
let sql = `SELECT COLUMN_NAME AS Field,COLUMN_TYPE AS Type,COLUMN_COMMENT AS Comment, IS_NULLABLE AS 'NULL',COLUMN_DEFAULT AS 'Default' FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '${needKey}'`
console.warn(`\nautoModelBydb:${sql}\n`)
let colRows = await autoFile.querySql(sql, project_id);
// 过滤掉id主键
colRows = colRows.filter(p => p.Field !== "id" || p.Field !== "last_modify_time" || p.Field !== "create_time")
if (colRows && colRows.length > 0) {
for (let j = 0; j < colRows.length; j++) {
let colRow = colRows[j]
let sort = (j + 1) * 10
let default_value = colRow.Default ? colRow.Default : ""
let allow_null = colRow.Null === "NO" ? false : true
let data_length = 0
if (colRow.Type) {
let regex = /\((\d+)\)/g;
let match = regex.exec(colRow.Type)
if (match && match.length === 2 && match[1]) {
data_length = parseInt(match[1])
}
}
let data_type = autoFile.mapMySQLTypeToSequelize(colRow.Type)
// 映射前端组件
const dataTypeToComponentMap = { STRING: 'Input', TEXT: 'Editor', INTEGER: 'InputNumber', BOOLEAN: 'i-switch', DATE: 'DatePicker' };
let comType = dataTypeToComponentMap[data_type] || "Input"
let control = { "comType": comType }
await sys_model_field.create({ model_id: res.id, key: colRow.Field, name: colRow.Comment, data_type, data_length, control, allow_null, default_value: default_value, sort })
}
}
}
}
}
return ctx.success();
},
"POST /sys_model/interface": async (ctx, next) => {
let row = ctx.getBody();
const baseModel = require("../../middleware/baseModel");
let { model_key, map_option } = row;
let { key, value } = map_option;
if (baseModel[model_key]) {
let dataRows = await baseModel[model_key].findAll({ attributes: [key, value] });
dataRows = dataRows.map((p) => {
return { key: p[key], value: p[value] };
});
return ctx.success(dataRows);
}
return ctx.fail("数据模型不存在");
},
"POST /sys_model/add": async (ctx, next) => {
let row = ctx.getBody();
let project_id = ctx.getAdminProjectId();
let { key, name } = row;
key = key.trim();
const res = await sys_model.create({ key, name, project_id });
return ctx.success(res);
},
"POST /sys_model/edit": async (ctx, next) => {
let row = ctx.getBody();
let { id, key, name } = row;
key = key.trim();
const res = await sys_model.update(
{ id, key, name },
{
where: {
id: id,
},
}
);
return ctx.success(res);
},
"POST /sys_model/del": async (ctx, next) => {
let id = ctx.get("id");
await autoFile.destroyApiFile(id);
// 删除字段表
await sys_model_field.destroy({ where: { model_id: id } });
// 删除主数据
const res = await sys_model.destroy({ where: { id: id } });
return ctx.success(res);
},
// 生成model
"POST /sys_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);
// 创建db
await autoFile.autoDb(id, tempUUid);
// 创建apiServer
await autoFile.autoController(id);
} else {
return ctx.fail("字段未添加,无效生成");
}
}
return ctx.success();
},
};

View File

@@ -0,0 +1,90 @@
const { sys_model_field, sys_model, querySql, op } = require("../../middleware/baseModel");
const autoFile = require("../service/autoFile");
const handleRow = (row) => {
let { data_type, defaultValue } = row;
if (data_type === "INTEGER") {
row.defaultValue = row.defaultValue || 0;
if (row.data_length < 1 || row.data_length > 11) {
row.data_length = 11;
}
} else if (data_type === "DOUBLE") {
row.defaultValue = row.defaultValue || 0;
if (row.data_length < 1 || row.data_length > 12) {
row.data_length = 12;
}
}
};
module.exports = {
"GET /sys_model_field/all": async (ctx, next) => {
let model_id = ctx.get("model_id");
let dbColKeys = [];
let modelRow = await sys_model.findOne({ where: { id: model_id } });
if (modelRow) {
let { project_id } = modelRow;
let isExistTableName = await autoFile.querySql(`select TABLE_NAME from information_schema.TABLES where TABLE_NAME ="${modelRow.key}"`, project_id);
if (isExistTableName && isExistTableName.length > 0) {
let colRows = await autoFile.querySql(`show columns from ${modelRow.key}`, project_id);
dbColKeys = colRows.map((p) => p.Field);
}
}
let model_fields = await sys_model_field.findAll({ where: { model_id }, order: [["sort", "asc"]] });
model_fields = model_fields.map((p) => {
let newRow = p.toJSON();
if (dbColKeys.indexOf(newRow.key) > -1) {
newRow.isDbExist = true;
} else {
newRow.isDbExist = false;
}
return newRow;
});
return ctx.success(model_fields);
},
"GET /sys_model_field/allByKey": async (ctx, next) => {
let key = ctx.get("key");
let modelRow = await sys_model.findOne({ where: { key } });
if (modelRow) {
let model_id = modelRow.id;
const resList = await sys_model_field.findAll({ where: { model_id }, order: [["sort", "asc"]] });
return ctx.success(resList);
}
return ctx.fail();
},
"POST /sys_model_field/add": async (ctx, next) => {
let row = ctx.getBody();
handleRow(row);
const res = await sys_model_field.create(row);
return ctx.success(res);
},
"POST /sys_model_field/edit": async (ctx, next) => {
let row = ctx.getBody();
let { id } = row;
handleRow(row);
const res = await sys_model_field.update(row, {
where: {
id: id,
},
});
return ctx.success(res);
},
"POST /sys_model_field/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_model_field.destroy({
where: {
id: id,
},
});
return ctx.success(res);
},
};

View File

@@ -0,0 +1,46 @@
const { sys_parameter, op } = require("../../middleware/baseModel");
module.exports = {
"GET /sys_parameter/index": async (ctx, next) => {
const resList = await sys_parameter.findAll({ where: { is_modified: 0 } });
return ctx.success(resList);
},
"GET /sys_parameter/key": async (ctx, next) => {
let key = ctx.get("key");
const res = await sys_parameter.findOne({ where: { key: key } });
return ctx.success(res);
},
"POST /sys_parameter/setSysConfig": async (ctx, next) => {
let { title, logoUrl } = ctx.getBody();
await sys_parameter.update({ value: title }, { where: { key: "sys_title" } });
await sys_parameter.update({ value: logoUrl }, { where: { key: "sys_logo" } });
return ctx.success();
},
"POST /sys_parameter/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_parameter.create(row);
return ctx.success(res);
},
"POST /sys_parameter/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
const resList = await sys_parameter.update(row, {
where: {
id: id,
},
});
return ctx.success(resList);
},
"POST /sys_parameter/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_parameter.destroy({
where: {
id: id,
},
});
return ctx.success(res);
},
};

View File

@@ -0,0 +1,152 @@
const { sys_project, sys_project_config, op } = require("../../middleware/baseModel");
const autoFile = require("../service/autoFile");
module.exports = {
"GET /sys_project/all": async (ctx, next) => {
const res = await sys_project.findAll();
return ctx.success(res);
},
"GET /sys_project/detail": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_project.findOne({ where: { id: id } });
return ctx.success(res);
},
"POST /sys_project/detailConfig": async (ctx, next) => {
let project_id = ctx.get("project_id");
const res = await sys_project_config.findOne({ where: { project_id } });
return ctx.success(res);
},
"POST /sys_project/autoInitDb": async (ctx, next) => {
let project_id = ctx.get("project_id");
await autoFile.autoInitDb(project_id)
return ctx.success();
},
"POST /sys_project/editDbConfig": async (ctx, next) => {
let { project_id, username, password, database, host, port, dialect } = ctx.getBody();
const projectRow = await sys_project_config.findOne({ where: { project_id: project_id } });
if (projectRow) {
const res = await projectRow.update({ db: { username, password, database, host, port, dialect } }, { individualHooks: true });
await autoFile.autoDbConfig(project_id);
await autoFile.autoConfig(project_id);
return ctx.success(res);
} else {
return ctx.fail("未存在项目配置")
}
},
"POST /sys_project/editWxConfig": async (ctx, next) => {
let { project_id, appid, secret, mch_id, partner_key, partnerV3_key, notify_url, refund_notify_url } = ctx.getBody();
const projectRow = await sys_project_config.findOne({ where: { project_id: project_id } });
if (projectRow) {
const res = await projectRow.update({ wechat: { appid, secret, mch_id, partner_key, partnerV3_key, notify_url, refund_notify_url } }, { individualHooks: true });
await autoFile.autoConfig(project_id);
return ctx.success(res);
} else {
return ctx.fail("未存在项目配置")
}
},
"POST /sys_project/editRedisConfig": async (ctx, next) => {
let { project_id, host, pwd, port } = ctx.getBody();
const projectRow = await sys_project_config.findOne({ where: { project_id: project_id } });
if (projectRow) {
const res = await projectRow.update({ redis: { host, pwd, port } }, { individualHooks: true });
await autoFile.autoConfig(project_id);
return ctx.success(res);
}
else {
return ctx.fail("未存在项目配置")
}
},
"POST /sys_project/editAliyunConfig": async (ctx, next) => {
let { project_id, accessKeyId, accessKeySecret, ossUrl, } = ctx.getBody();
const projectRow = await sys_project_config.findOne({ where: { project_id: project_id } });
if (projectRow) {
const res = await projectRow.update({ aliyun: { accessKeyId, accessKeySecret, ossUrl } }, { individualHooks: true });
await autoFile.autoConfig(project_id);
return ctx.success(res);
} else {
return ctx.fail("未存在项目配置")
}
},
"POST /sys_project/export": async (ctx, next) => {
let rows = [];
let cols = [];
let title = "sys_project";
let tableAttributes = sys_project.tableAttributes;
let colKeys = Object.keys(tableAttributes);
colKeys.forEach((key) => {
let row = tableAttributes[key];
let caption = row.comment ? row.comment : row.fieldName;
cols.push({ caption, type: row.type.__proto__.key === "INTEGER" ? "number" : "string", key: row.field });
});
const dbRows = await sys_project.findAll({});
rows = dbRows.map((p) => {
let tempRow = p.toJSON();
let row = [];
cols.forEach((col) => {
row.push(tempRow[col.key]);
});
return row;
});
return ctx.downFile({ title, rows, cols });
},
"POST /sys_project/page": async (ctx, next) => {
let param = ctx.getPageSize();
let row = ctx.getBody();
let { key, value } = row.seachOption;
let where = {};
if (key && value) {
where[key] = { [op.like]: "%" + value + "%" };
}
const res = await sys_project.findAndCountAll({
where,
order: [["id", "DESC"]],
...param,
});
return ctx.success(res);
},
"POST /sys_project/add": async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_project.create(row);
await sys_project_config.create({ project_id: res.id });
return ctx.success(res);
},
"POST /sys_project/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
const res = await sys_project.update(row, { where: { id: id }, individualHooks: true });
return ctx.success(res);
},
"POST /sys_project/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_project.destroy({ where: { id: id }, individualHooks: true });
return ctx.success(res);
},
};

View File

@@ -0,0 +1,77 @@
const {sys_project_type, op } = require('../../middleware/baseModel');
module.exports = {
'GET /sys_project_type/all' : async (ctx, next) => {
const res = await sys_project_type.findAll();
return ctx.success(res);
},
'GET /sys_project_type/detail' : async (ctx, next) => {
let id = ctx.get('id')
const res = await sys_project_type.findOne({where:{id:id}});
return ctx.success(res);
},
"POST /sys_project_type/export": async (ctx, next) => {
let rows = [];
let cols = [];
let title='sys_project_type';
let tableAttributes = sys_project_type.tableAttributes;
let colKeys = Object.keys(tableAttributes);
colKeys.forEach((key) => {
let row = tableAttributes[key];
let caption = row.comment ? row.comment : row.fieldName;
cols.push({ caption, type: row.type.__proto__.key === "INTEGER" ? "number" : "string", key: row.field });
});
const dbRows = await sys_project_type.findAll({});
rows = dbRows.map((p) => {
let tempRow = p.toJSON();
let row = [];
cols.forEach((col) => {
row.push(tempRow[col.key]);
});
return row;
});
return ctx.downFile({title, rows, cols });
},
'POST /sys_project_type/page' : async (ctx, next) => {
let param = ctx.getPageSize()
let row = ctx.getBody();
let {key,value}=row.seachOption
let where={}
if(key&&value){
where[key]={ [op.like]:'%' +value + '%'}
}
const res = await sys_project_type.findAndCountAll({
where,
order: [["id", "DESC"]],
...param,
});
return ctx.success(res);
},
'POST /sys_project_type/add': async (ctx, next) => {
let row = ctx.getBody();
const res = await sys_project_type.create(row);
return ctx.success(res);
},
'POST /sys_project_type/edit': async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get('id')
const res = await sys_project_type.update(row, { where: { id: id }, individualHooks: true });
return ctx.success(res);
},
'POST /sys_project_type/del': async (ctx, next) => {
let id = ctx.get('id')
const res = await sys_project_type.destroy({ where: { id: id }, individualHooks: true });
return ctx.success(res);
}
};

View File

@@ -0,0 +1,39 @@
const { sys_role, op } = require("../../middleware/baseModel");
const { getMd5 } = require("../../tool/md5");
module.exports = {
"GET /sys_role/index": async (ctx, next) => {
const resList = await sys_role.findAll({ where: {} });
return ctx.success(resList);
},
"POST /sys_role/add": async (ctx, next) => {
let row = ctx.getBody();
let { name, menus } = row;
const res = await sys_role.create({ name, menus });
return ctx.success(res);
},
"POST /sys_role/edit": async (ctx, next) => {
let row = ctx.getBody();
let { id, name, menus } = row;
const res = await sys_role.update(
{ name, menus },
{
where: {
id: id,
},
}
);
return ctx.success(res);
},
"POST /sys_role/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_role.destroy({
where: {
id: id,
},
});
return ctx.success(res);
},
};

View File

@@ -0,0 +1,132 @@
const { sys_user, sys_role, sys_log, sys_menu, op } = require("../../middleware/baseModel");
const tokenService = require("../service/token");
const { getMd5 } = require("../../tool/md5");
const dayjs = require("dayjs");
module.exports = {
"GET /sys_user/index": async (ctx, next) => {
const resList = await sys_user.findAll({
where: {
name: { [op.not]: "zc" },
},
include: [{ association: sys_user.associations.role, attributes: ["id", "name"] }],
});
return ctx.success(resList);
},
"POST /sys_user/login": async (ctx, next) => {
let name = ctx.get("name");
let password = ctx.get("password");
const userRow = await sys_user.findOne({
where: { name: name, password: getMd5(password) },
});
if (userRow) {
let { id, name, password, roleId } = userRow;
let role = await sys_role.findOne({ where: { id: roleId } });
let token = await tokenService.create({ id, name, password });
let nowDateStr = dayjs().format("YYYY-MM-DD HH:mm:ss");
await sys_log.create({ table_name: "sys_user", operate: "登录", content: `用户<span class="bold"> ${name} </span> 于 ${nowDateStr} 登录系统` });
return ctx.success({ token: token, user: userRow, authorityMenus: role.menus });
}
return ctx.fail("账号或密码错误!");
},
"POST /sys_user/export": async (ctx, next) => {
let rows = [];
let cols = [];
let title = "sys_user";
let tableAttributes = sys_user.tableAttributes;
let colKeys = Object.keys(tableAttributes);
colKeys.forEach((key) => {
let row = tableAttributes[key];
let caption = row.comment ? row.comment : row.fieldName;
cols.push({ caption, type: row.type.__proto__.key === "INTEGER" ? "number" : "string", key: row.field });
});
const userRows = await sys_user.findAll({});
rows = userRows.map((p) => {
let tempRow = p.toJSON();
let row = [];
cols.forEach((col) => {
row.push(tempRow[col.key]);
});
return row;
});
return ctx.downFile({ title, rows, cols });
},
"POST /sys_user/authorityMenus": async (ctx, next) => {
let user_id = ctx.getAdminUserId();
if (user_id) {
const userRow = await sys_user.findOne({ where: { id: user_id } });
if (userRow) {
let { id, roleId } = userRow;
let where = {};
let role = await sys_role.findOne({ where: { id: roleId } });
// 系统角色 不用授权
if (role.type === 0) {
let menuIds = JSON.parse(role.menus);
where = {
id: {
[op.in]: menuIds,
},
};
}
let munuRows = await sys_menu.findAll({
where,
order: [["sort", "ASC"]],
});
return ctx.success(munuRows);
}
}
return ctx.success();
},
"POST /sys_user/add": async (ctx, next) => {
let row = ctx.getBody();
// md5 加密
if (row && row.password) {
row.password = getMd5(row.password);
}
const res = await sys_user.create(row);
return ctx.success(res);
},
"POST /sys_user/edit": async (ctx, next) => {
let row = ctx.getBody();
let id = ctx.get("id");
let { name, roleId, password } = row;
if (password.length !== 32) {
password = getMd5(password);
}
const resList = await sys_user.update(
{ name, roleId, password },
{
where: {
id: id,
},
}
);
return ctx.success(resList);
},
"POST /sys_user/del": async (ctx, next) => {
let id = ctx.get("id");
const res = await sys_user.destroy({
where: { id: id },
});
return ctx.success(res);
},
};