import * as path from "path";
|
import * as fs from "fs";
|
import BaseController from "../baseController";
|
import { Post } from "egg-shell-decorators";
|
import { SysMvcTableInfo } from "../../entity/sys/core/sysMvcTableInfo";
|
import { SysMvcVueData } from "../../entity/sys/core/sysMvcVueData";
|
import { SysMenuVue } from "../../entity/sys/core/sysMenuVue";
|
|
export default class MvcVueDataController extends BaseController {
|
//#region 保存创建vue文件
|
/**
|
* 保存创建vue文件
|
*/
|
@Post()
|
public async saveBaseVue() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let vueData_Id = body.vueData_Id;
|
let designMode = body.designMode; // 设计模式:用户自定义、系统
|
|
try {
|
if (designMode == "system") {
|
//#region 系统设计
|
if (vueData_Id === 0) {
|
//主数据
|
let mvcInfo = await this.dbRead.getRepository(SysMvcTableInfo).findOne(body.table_Id);
|
if (mvcInfo) {
|
mvcInfo.vueData = body.vueData;
|
await this.dbWrite.save(mvcInfo);
|
}
|
} else {
|
if (vueData_Id > 0) {
|
let tempInfo = await this.dbRead.getRepository(SysMvcVueData).findOne(vueData_Id);
|
if (tempInfo != null) {
|
tempInfo.vueDataName = body.vueDataName;
|
tempInfo.vueData = body.vueData;
|
tempInfo.table_Id = body.Table_Id;
|
await this.dbWrite.save(tempInfo);
|
}
|
} else {
|
let vueInfo = new SysMvcVueData();
|
vueInfo.vueDataName = body.vueDataName;
|
vueInfo.vueData = body.vueData;
|
vueInfo.table_Id = body.table_Id;
|
vueInfo.userProduct_Id = 0;
|
await this.dbWrite.save(vueInfo);
|
vueData_Id = vueInfo.vueData_Id;
|
}
|
}
|
this.info.result = true;
|
this.info.msg = "保存成功";
|
this.info.data = {
|
vueData_Id: vueData_Id
|
};
|
|
if (body.router && body.type == "json") {
|
let rootPath = path.resolve();
|
let appPath = rootPath + `\\..\\siemens2-site\\`;
|
var baseVuePath = appPath + `static` + body.router.replace(/\//gi, "\\") + ".json";
|
let pathToCreate = baseVuePath.substring(0, baseVuePath.lastIndexOf("\\"));
|
ctx.helper.mkdir(pathToCreate);
|
|
fs.writeFileSync(baseVuePath, body.vueData);
|
this.info.msg = "数据和JSON文件保存成功";
|
|
var mainPath = appPath + "src\\views" + body.router.replace(/\//gi, "\\") + ".vue";
|
pathToCreate = mainPath.substring(0, mainPath.lastIndexOf("\\"));
|
ctx.helper.mkdir(pathToCreate);
|
|
// 生成biz文件,选择器不生成文件
|
if (!fs.existsSync(mainPath) && body.router.indexOf("/selector") != 0) {
|
let vueData = body.vueData;
|
let jsonVueData = JSON.parse(vueData);
|
let menu_Id = jsonVueData.dataOptions.menu_Id;
|
var menuInfo = await this.dbWrite.getRepository(SysMenuVue).findOne(menu_Id);
|
if (menuInfo != null) {
|
var mainCode = body.mainCode.replace("{name}", menuInfo.vueName);
|
|
fs.writeFileSync(mainPath, mainCode);
|
this.info.msg = "文件创建成功";
|
}
|
}
|
}
|
//#endregion
|
} else {
|
//#region 用户设计
|
let vueInfo = await this.dbRead.getRepository(SysMvcVueData).findOne(vueData_Id);
|
if (!vueInfo || (vueInfo && vueInfo.userProduct_Id === 0)) {
|
vueInfo = new SysMvcVueData();
|
vueInfo.fromVueData_Id = body.vueData_Id; // 新建时确认来源ID
|
}
|
vueInfo.vueDataName = body.vueDataName;
|
vueInfo.vueData = body.vueData;
|
vueInfo.table_Id = body.table_Id;
|
|
await this.setAccountInfo(vueInfo);
|
await this.dbWrite.save(vueInfo);
|
vueData_Id = vueInfo.vueData_Id;
|
|
this.info.result = true;
|
this.info.msg = "保存成功";
|
this.info.data = {
|
vueData_Id: vueData_Id
|
};
|
|
//#endregion
|
}
|
} catch (error) {
|
console.log("保存Vue错误:" + error.message);
|
this.info.result = false;
|
this.info.result = error.message;
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 更新vue标题
|
/**
|
* 更新vue标题
|
*/
|
@Post()
|
public async updateTitle() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
//主数据
|
if (body.vueData_Id === 0) {
|
let mvcInfo = await this.dbRead.getRepository(SysMvcTableInfo).findOne(body.table_Id);
|
if (mvcInfo) {
|
mvcInfo.cnName = body.title;
|
await this.dbWrite.save(mvcInfo);
|
}
|
} else {
|
if (body.vueData_Id > 0) {
|
await this.dbRead.update(SysMvcVueData, body.vueData_Id, {
|
vueDataName: body.title
|
});
|
|
this.info.result = true;
|
this.info.msg = "更新成功";
|
} else {
|
this.info.result = true;
|
this.info.msg = "数据不存在";
|
}
|
}
|
ctx.body = this.info;
|
} catch (error) {
|
console.log("更新Vue标题错误:" + error.message);
|
ctx.body = {
|
result: false,
|
msg: error.toString()
|
};
|
}
|
}
|
//#endregion
|
|
//#region 删除vue文件。
|
/**
|
* 删除vue文件
|
*/
|
@Post()
|
public async deleteItem() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
if (body.vueData_Id > 0) {
|
await this.dbRead.delete(SysMvcVueData, body.vueData_Id);
|
|
this.info.result = true;
|
this.info.msg = "删除成功";
|
} else {
|
this.info.result = true;
|
this.info.msg = "数据不存在";
|
}
|
ctx.body = this.info;
|
} catch (error) {
|
console.log("删除Vue错误:" + error.message);
|
ctx.body = {
|
result: false,
|
msg: error.toString()
|
};
|
}
|
}
|
//#endregion
|
}
|