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); }, };