schangxiang@126.com
2025-09-09 3d8966ba2c81e7e0365c8b123e861d18ee4f94f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
}