333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import BaseService from "../baseService";
import { SysRoleAuth } from "../../entity/sys/core/sysRoleAuth";
import { SysRoleAuthData } from "../../entity/sys/core/sysRoleAuthData";
import { ResultInfo } from "../../public/commonInterface";
import { SysRole } from "../../entity/sys/core/sysRole";
 
const AUTH_TYPE = {
  MENU: 0,
  ROLE: 1, // 角色
  STORAGE: 2, // 仓库
  DEPT: 3, // 部门
  CONTROLCENTER: 4, // 系统中心
  CHANNEL: 5, // 渠道,
  CONSIGNOR: 6, // 货主
  PDAMENU: 7, // PDA模块菜单
  APPMENU: 8 // APP
};
 
export default class RoleAuthService extends BaseService {
  private sysMenus = [];
  private sysMenuAuths = [];
  private sysAppMenus = [];
  private sysAppMenuAuths = [];
  /* ***************************************************
    初始化权限数据
  * ****************************************************/
  //#region GetAuthMenu 初始化权限
  public async GetAuthMenu(role_Id: number, authType: number) {
    let html = "";
    if (authType == AUTH_TYPE.MENU) {
      let sql = `select menu_Id, menuName, parentId, VueAuth AS auth,
        case when exists(select menu_Id from Sys_Menu C Where C.parentId=M.menu_Id And C.VueEnable=1) then 'true' else 'false' end as hasChild 
        from Sys_Menu M
        where VueEnable=1 And VueUrl IS NOT NULL And VueFilePath IS NOT NULL
        order by M.VueOrderNo desc, M.menu_Id;`;
      this.sysMenus = await this.dbRead.query(sql);
      this.sysMenuAuths = await this.dbRead.find(SysRoleAuth, {
        role_Id: role_Id
      });
      //功能权限
      html = await this.getMenuAuth(0, 0, role_Id);
    } else if (authType == AUTH_TYPE.APPMENU) {
      let sql = `select menu_Id, menuName, parentId, M.VueAuth AS auth, 
        case when exists(select menu_Id from Sys_MenuApp C Where C.parentId=M.menu_Id And C.Enable=1) then 'true' else 'false' end as hasChild 
        from Sys_MenuApp M
        where Enable=1
        order by M.OrderNo desc, M.menu_Id;`;
      this.sysAppMenus = await this.dbRead.query(sql);
      this.sysAppMenuAuths = await this.dbRead.find(SysRoleAuthData, {
        where: {
          role_Id: role_Id,
          dataType_Id: authType
        }
      });
      //APP权限
      html = await this.getAppMenuAuth(0, 0);
    }
 
    return html;
  }
  //#endregion
 
  //#region getMenuAuth 功能权限
  public async getMenuAuth(parentId: number, levelId: number, role_Id: number) {
    levelId++;
    let html = "";
    if (levelId > 50) return ""; //防止死循环
 
    let dataList = this.sysMenus.filter(item => item.parentId === parentId);
    for (let item of dataList) {
      let menu_Id = item.menu_Id;
      let menuName = item.menuName;
      let auth = item.auth;
      let hasChild = item.hasChild;
 
      let children = "";
      if (hasChild === "true") {
        children += `, "children":`;
        children += await this.getMenuAuth(menu_Id, levelId, role_Id);
        children += "\r\n";
      }
      if (html) html += ",";
      html +=
        '{"menu_Id":' +
        menu_Id +
        ', "menuName":"' +
        menuName +
        '", "parentId":' +
        parentId +
        ', "auth":' +
        (await this.getMenuNodeAuth(menu_Id, auth)) +
        ', "hasChild":"' +
        hasChild +
        '", "state":"open"' +
        children +
        "}\r\n";
    }
 
    html = "[" + html + "]";
    return html;
  }
 
  private async getMenuNodeAuth(menu_Id: number, auth: String) {
    if (!auth) {
      auth = "Browse=显示";
    }
    let html = "";
    let authNodes = auth.split(",");
    let roleAuth = this.sysMenuAuths.find(item => item.menu_Id === menu_Id);
    let authValues: Array<any> = [];
    if (roleAuth != null && roleAuth.authValue != null) {
      authValues = roleAuth.authValue.split(",");
    }
 
    for (let a of authNodes) {
      let authNode = a.split("=");
      if (authNode.length < 2) {
        return true;
      }
      let nodeName = authNode[0];
      let nodeLabel = authNode[1];
      let authValue = authValues.find(s => {
        return s.indexOf(nodeName) >= 0;
      });
      let currentValue = "0";
      if (authValue != null && authValue.indexOf("1") >= 0) currentValue = "1";
      if (html) html += ",";
      html += '{"label":"' + nodeLabel + '", "nodeName":"' + nodeName + '", "value":' + currentValue + "}";
    }
    html = "[" + html + "]";
 
    return html;
  }
  //#endregion
 
  //#region getAppMenuAuth APP功能权限
  public async getAppMenuAuth(parentId: number, levelId: number) {
    levelId++;
    let html = "";
    if (levelId > 50) return ""; //防止死循环
 
    let dataList = this.sysAppMenus.filter(item => item.parentId === parentId);
    for (let item of dataList) {
      let menu_Id = item.menu_Id;
      let menuName = item.menuName;
      let auth = item.auth;
      let hasChild = item.hasChild;
 
      let children = "";
      if (hasChild === "true") {
        children += `, "children":`;
        children += await this.getAppMenuAuth(menu_Id, levelId);
        children += "\r\n";
      }
 
      if (html) html += ",";
      html +=
        '{"menu_Id":' +
        menu_Id +
        ', "menuName":"' +
        menuName +
        '", "parentId":' +
        parentId +
        ', "auth":' +
        (await this.getAppMenuNodeAuth(menu_Id, auth)) +
        ', "hasChild":"' +
        hasChild +
        '", "state":"open"' +
        children +
        "}\r\n";
    }
 
    html = "[" + html + "]";
    return html;
  }
 
  //#region getAppMenuNodeAuth
  private async getAppMenuNodeAuth(menu_Id: number, auth: string) {
    let authNodes = auth.split(",");
    let html = "";
    let existAuth = this.sysAppMenuAuths.find(item => Number(item.node_Id) === menu_Id);
    let authValues: Array<any> = [];
    if (existAuth && existAuth.authValue) {
      authValues = existAuth.authValue.split(",");
    }
 
    for (let a of authNodes) {
      let authNode = a.split("=");
      let nodeName = authNode[0];
      let nodeLabel = authNode[1];
      let authValue = authValues.find(s => {
        return s.indexOf(nodeName) >= 0;
      });
      let currentValue = "0";
 
      if (authValue != null && authValue.indexOf("1") >= 0) currentValue = "1";
      if (html) html += ",";
      html += '{"label":"' + nodeLabel + '", "nodeName":"' + nodeName + '", "value":' + currentValue + "}";
    }
    html = "[" + html + "]";
 
    return html;
  }
  //#endregion
 
  //#endregion
 
  /* ***************************************************
    保存权限数据
  * ****************************************************/
  //#region saveAuthMenu
  public async saveAuthMenu(menuList: Array<any>, authType): Promise<ResultInfo> {
    let info: ResultInfo = {
      result: false
    };
    switch (authType) {
      case AUTH_TYPE.MENU: //功能权限
        info = await this.saveMenuAuth(menuList);
        break;
      case AUTH_TYPE.APPMENU:
        info = await this.saveRoleAuth(menuList, authType);
        break;
    }
 
    return info;
  }
  //#endregion
 
  //#region saveMenuAuth 保存功能权限
  public async saveMenuAuth(menuList: Array<any>): Promise<ResultInfo> {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let info: ResultInfo = {
      result: false
    };
    if (menuList.length == 0) {
      info.result = true;
      info.msg = "保存成功";
      return info;
    }
 
    var roleInfo = await this.dbRead.findOne(SysRole, {
      where: {
        role_Id: menuList[0].role_Id,
        userProduct_Id: userInfo.userProduct_Id
      }
    });
    if (!roleInfo) {
      info.result = false;
      info.msg = "没有权限操作次角色";
      return info;
    }
 
    try {
      for (var menuInfo of menuList) {
        var roleAuthInfo = await this.dbRead.findOne(SysRoleAuth, {
          where: {
            role_Id: menuInfo.role_Id,
            menu_Id: menuInfo.menu_Id
          }
        });
 
        if (roleAuthInfo) {
          roleAuthInfo.authValue = menuInfo.authValue;
          await this.dbWrite.update(SysRoleAuth, roleAuthInfo.auth_Id, {
            authValue: menuInfo.authValue
          });
        } else {
          await this.dbWrite.insert(SysRoleAuth, menuInfo);
        }
      }
 
      info.result = true;
      info.msg = "保存成功";
    } catch (error) {
      info.result = false;
      info.msg = "保存失败," + error.message;
    }
 
    return info;
  }
 
  //#endregion
 
  //#region saveRoleAuth 保存角色权限
  public async saveRoleAuth(menuList: Array<any>, dataType_Id: number): Promise<ResultInfo> {
    let { ctx } = this;
    let userInfo = await ctx.helper.userInfo();
    let info: ResultInfo = {
      result: false
    };
    if (menuList.length === 0) {
      info.result = true;
      info.msg = "保存成功";
      return info;
    }
 
    var roleInfo = await this.dbRead.findOne(SysRole, {
      where: {
        role_Id: menuList[0].role_Id,
        userProduct_Id: userInfo.userProduct_Id
      }
    });
    if (!roleInfo) {
      info.result = false;
      info.msg = "没有权限操作此角色";
      return info;
    }
 
    for (let a of menuList) {
      let roleAuthData = await this.dbRead.findOne(SysRoleAuthData, {
        where: {
          role_Id: a.role_Id,
          dataType_Id: dataType_Id,
          node_Id: a.menu_Id
        }
      });
 
      if (!roleAuthData) {
        roleAuthData = new SysRoleAuthData();
        roleAuthData.dataType_Id = dataType_Id;
        roleAuthData.role_Id = a.role_Id;
        roleAuthData.node_Id = a.menu_Id;
        roleAuthData.authValue = a.authValue;
        await this.dbWrite.insert(SysRoleAuthData, roleAuthData);
      } else {
        await this.dbWrite.update(SysRoleAuthData, roleAuthData.auth_Id, {
          dataType_Id: dataType_Id,
          role_Id: a.role_Id,
          node_Id: a.menu_Id,
          authValue: a.authValue
        });
      }
    }
 
    info.result = true;
    info.msg = "保存成功";
 
    return info;
  }
 
  //#endregion
}