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
|
}
|