105 lines
3.3 KiB
JavaScript
105 lines
3.3 KiB
JavaScript
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 或者项目 配置不正确');
|
|
}
|
|
}
|
|
|
|
|
|
};
|