//#region import
|
import { getManager } from "typeorm";
|
import { SysUserLog } from "../entity/sys/core/sysUserLog";
|
import { vSysUserLogin } from "../entity/sys/core/vSysUserLogin";
|
import { PlatUserProduct } from "../entity/sys/plat/platUserProduct";
|
import { PlatUser } from "../entity/sys/plat/platUser";
|
import { PlatProductVersionModule } from "../entity/sys/plat/platProductVersionModule";
|
import { SysRoleAuthDataAll } from "../entity/sys/core/sysRoleAuthDataAll";
|
import { SysMenuVue } from "../entity/sys/core/sysMenuVue";
|
import { SysRoleAuth } from "../entity/sys/core/sysRoleAuth";
|
import { Post, Get } from "egg-shell-decorators";
|
|
const constValidateCode = "ValidateCode_";
|
let uuid = require("node-uuid");
|
import * as moment from "moment";
|
import BaseController from "./baseController";
|
import { SysDataAuthModule } from "../entity/sys/core/sysDataAuthModule";
|
import { SysRoleAuthData } from "../entity/sys/core/sysRoleAuthData";
|
import { BaseConsignor } from "../entity/basicInfo/consignor/baseConsignor";
|
let captchapng = require("captchapng");
|
import * as ip from "ip";
|
import { SysMvcUserData } from "../entity/sys/core/sysMvcUserData";
|
import { SysMvcVueData } from "../entity/sys/core/sysMvcVueData";
|
import { SysUser } from "../entity/sys/core/sysUser";
|
|
|
//#endregion
|
|
export default class AuthController extends BaseController {
|
//#region getToken
|
@Get()
|
public async getToken() {
|
const { ctx, app } = this;
|
try {
|
const guid = this.ctx.query.guid;
|
const key = "Token_" + guid;
|
let redis = app.redis.clients.get("userInfo");
|
|
//判断参数是否合法
|
if (!guid) {
|
this.info.result = false;
|
this.info.statusCode = 400;
|
this.info.msg = "请求参数不完整或不正确";
|
ctx.body = this.body;
|
return;
|
}
|
|
//插入缓存
|
let token = await redis.get(key);
|
if (!token) {
|
token = {
|
guid: guid,
|
signToken: uuid.v1(),
|
expireTime: moment().add(30, "minutes").format("YYYY-MM-DD HH:mm:ss")
|
};
|
redis && redis.set(key, JSON.stringify(token));
|
} else {
|
token = JSON.parse(token);
|
if (typeof token === "string") {
|
token = {
|
guid: guid,
|
signToken: uuid.v1(),
|
expireTime: moment().add(30, "minutes").format("YYYY-MM-DD HH:mm:ss")
|
};
|
redis && redis.set(key, JSON.stringify(token));
|
} else {
|
token.expireTime = moment().add(30, "minutes").format("YYYY-MM-DD HH:mm:ss");
|
redis && redis.set(key, JSON.stringify(token));
|
}
|
}
|
//更新token
|
let logInfo = await this.dbRead.findOne(SysUserLog, {
|
gUID: guid
|
});
|
if (logInfo) {
|
logInfo.signTokenInfo = JSON.stringify(token);
|
await this.dbWrite.save(logInfo);
|
}
|
|
token = ctx.helper.objectToCase(token);
|
this.info.result = true;
|
this.info.statusCode = 200;
|
this.info.data = token;
|
this.info.msg = ip.address();
|
} catch (error) {
|
try {
|
this.info.result = false;
|
this.info.statusCode = 500;
|
let msg = JSON.stringify(error);
|
this.info.msg = msg;
|
await this.ctx.service.common.errorLog("getToken", msg);
|
} catch (err) {
|
let msg = "嵌套错误:" + error.message + ", " + err.message;
|
this.info.result = false;
|
this.info.statusCode = 500;
|
this.info.msg = msg;
|
await this.ctx.service.common.errorLog("getToken", msg);
|
}
|
}
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region getValidateCode
|
/**
|
* 获得验证码
|
*/
|
@Post()
|
public async getValidateCode() {
|
const { ctx, app } = this;
|
let guid = ctx.get("guid");
|
let key = constValidateCode + guid;
|
let redis = await app.redis.clients.get("userInfo");
|
|
let code = Math.round(Math.random() * 90000 + 10000);
|
await redis.set(key, code);
|
let p = new captchapng(100, 30, code);
|
p.color(0, 0, 0, 0);
|
// p.color(80, 80, 80, 255);
|
p.color(220, 10, 150, 255);
|
let img = p.getBase64();
|
// let imgbase64 = new Buffer(img, "base64");
|
|
ctx.body = {
|
result: true,
|
data: img,
|
msg: null
|
};
|
}
|
|
//#endregion
|
|
//#region options
|
/**
|
* options请求
|
*/
|
@Post()
|
public options() {
|
let { ctx } = this;
|
ctx.response.set("Access-Control-Allow-Methods", "*");
|
ctx.response.set("Access-Control-Allow-Headers", "*,accesstoken,content-type,guid,nonce,signature,timestamp");
|
ctx.response.set("Access-Control-Expose-Headers", "*");
|
ctx.response.status = 200;
|
}
|
//#endregion
|
|
//#region login 后台系统登录
|
/**
|
* 后台系统登录
|
*/
|
@Post()
|
public async login() {
|
let { ctx, app } = this;
|
let guid = ctx.get("guid");
|
let body = ctx.request.body;
|
let redis = await app.redis.clients.get("userInfo");
|
const key = constValidateCode + guid;
|
|
try {
|
let validateCode = await redis.get(key);
|
await redis.del(key);
|
|
if (body.validateCode != "pass") {
|
//校验数据
|
if (body.validateCode !== validateCode) {
|
ctx.body = {
|
result: false,
|
msg: "验证码不正确"
|
};
|
return;
|
}
|
}
|
|
if (!body.userPwd) {
|
ctx.body = {
|
result: false,
|
msg: "密码不能为空"
|
};
|
return;
|
}
|
|
// 密码加密
|
let userPwd = ctx.helper.md5EncodingSalt(body.userPwd);
|
|
let prodInfo: any;
|
// 获得登录信息
|
let loginRep = this.dbRead.getRepository(vSysUserLogin);
|
let userList = await loginRep.find({
|
where: [
|
{
|
userName: body.userName,
|
userPwd: userPwd
|
},
|
{
|
mobile: body.userName,
|
userPwd: userPwd
|
}
|
]
|
});
|
|
if (userList.length == 0) {
|
ctx.body = {
|
result: false,
|
msg: "账号或密码不正确,请重新登录!"
|
};
|
return;
|
}
|
|
if (userList && userList.length > 1) {
|
if (body.userProductCode) {
|
//获得产品信息
|
prodInfo = await this.dbRead.getRepository(PlatUserProduct).findOne({
|
userProductCode: body.userProductCode
|
});
|
if (prodInfo == null) {
|
ctx.body = {
|
result: false,
|
msg: "账套编号不存在!"
|
};
|
return;
|
}
|
// 筛选出当前账套用户
|
userList = userList.filter(w => w.userProduct_Id == prodInfo.userProduct_Id);
|
} else {
|
ctx.body = {
|
result: false,
|
msg: "存在多个账套,请输入账套编号",
|
statusCode: 501
|
};
|
return;
|
}
|
}
|
|
let userInfo = userList[0];
|
if (!userInfo || userInfo.enable != 1) {
|
ctx.body = {
|
result: false,
|
msg: "用户不可用"
|
};
|
return;
|
}
|
//获取账套,单用户时
|
if (!prodInfo) {
|
prodInfo = await getManager("YrtPlat").getRepository(PlatUserProduct).findOne({
|
platUser_Id: userInfo.platUser_Id
|
});
|
if (prodInfo == null) {
|
ctx.body = {
|
result: false,
|
msg: "账套编号不存在!"
|
};
|
return;
|
}
|
}
|
|
if (body != null && userInfo.userPwd == userPwd) {
|
if(body.sourceMachine==undefined || body.sourceMachine==""){
|
ctx.body = {
|
result: false,
|
msg: "来源设备不能为空!"
|
};
|
return;
|
}
|
//更新登录状态 【Editby shaocx,2024-03-20】
|
let userRep = this.dbWrite.getRepository(SysUser);
|
//获得登陆用户
|
let loginUser = await this.dbRead.getRepository(SysUser).findOne({
|
userName: body.userName
|
});
|
if (loginUser == null) {
|
ctx.body = {
|
result: false,
|
msg: "账户'"+body.userName+"'不存在!"
|
};
|
return;
|
}
|
//如果是超级管理员角色,就不处理这个 [Editby shaocx,2024-03-23]
|
if(loginUser.role_Id!=1){
|
if(body.sourceMachine==='PDA'){
|
if(loginUser.loginStateForPda==1){
|
ctx.body = {
|
result: false,
|
msg: "账户'"+body.userName+"'已经在PDA上在线!"
|
};
|
return;
|
}
|
}else{
|
if(loginUser.loginState==1){
|
ctx.body = {
|
result: false,
|
msg: "账户'"+body.userName+"'已经在电脑上在线!"
|
};
|
return;
|
}
|
}
|
}
|
|
if(body.sourceMachine==='PDA'){
|
loginUser.loginStateForPda=1;
|
loginUser.loginIPForPda=this.ctx.request.ip;
|
loginUser.loginDateForPda=new Date();
|
}else{
|
loginUser.loginState=1;
|
loginUser.loginIP=this.ctx.request.ip;
|
loginUser.loginDate=new Date();
|
}
|
|
await userRep.save(loginUser);
|
|
//获得平台信息
|
let platInfo =
|
(await getManager("YrtPlat").getRepository(PlatUser).findOne({
|
platUser_Id: userInfo.platUser_Id
|
})) || new PlatUser();
|
|
//获得平台权限
|
let moduleAuthList = await getManager("YrtPlat").getRepository(PlatProductVersionModule).find({
|
product_Id: prodInfo.product_Id,
|
version_Id: prodInfo.version_Id
|
});
|
|
let platUserInfo = {
|
platUser_Id: platInfo.platUser_Id,
|
platUserCode: platInfo.platUserCode,
|
platUserName: platInfo.platUserName || platInfo.mobile,
|
platCorpName: platInfo.platCorpName,
|
|
userProduct_Id: prodInfo.userProduct_Id,
|
userProductCode: userInfo.userProductCode,
|
userProductAlias: userInfo.userProductAlias || prodInfo.productName,
|
|
role_Id: userInfo.role_Id,
|
roleName: userInfo.roleName,
|
user_Id: userInfo.user_Id,
|
userName: userInfo.userName,
|
userTrueName: userInfo.userTrueName,
|
dept_Id: userInfo.dept_Id || 0,
|
deptName: userInfo.deptName,
|
|
mobile: userInfo.mobile,
|
accessToken: uuid.v1(),
|
isAdministrator: userInfo.isAdministrator == 1,
|
userType: "user",
|
pic: userInfo.pic
|
};
|
|
let accessTokenKey = "AccessToken_" + guid;
|
let signTokenKey = "Token_" + guid;
|
redis && (await redis.set(accessTokenKey, JSON.stringify(platUserInfo)));
|
let token = redis && (await redis.get(signTokenKey));
|
|
//记录登录日志
|
let logRep = this.dbWrite.getRepository(SysUserLog);
|
let log = logRep.create();
|
Object.assign(log, platUserInfo);
|
log.gUID = guid;
|
log.loginInfo = JSON.stringify(platUserInfo);
|
log.signTokenInfo = token;
|
log.operateType = "系统登录";
|
log.action = "系统登录";
|
log.iP = this.ctx.request.ip;
|
|
let _moduleAuthList = moduleAuthList.map(s => {
|
return {
|
versionModule_Id: s.versionModule_Id,
|
product_Id: s.product_Id,
|
productModule_Id: s.productModule_Id,
|
moduleNumber: s.moduleNumber,
|
moduleName: s.moduleName,
|
isSupport: s.isSupport
|
};
|
});
|
log.saaSAuth = JSON.stringify(_moduleAuthList);
|
await logRep.save(log);
|
|
|
|
// 清空SaaS权限
|
let key = "SaaSAuth_" + guid;
|
redis && redis.set(key, null);
|
|
//获得菜单权限
|
let menuList = await ctx.service.sys.menuVue.getVueMenu();
|
|
ctx.body = {
|
result: true,
|
msg: "登陆成功",
|
data: platUserInfo,
|
dynamic: menuList
|
};
|
} else {
|
ctx.body = {
|
result: false,
|
msg: "用户名或密码不正确"
|
};
|
}
|
} catch (error) {
|
let msg = error.message;
|
ctx.body = {
|
result: false,
|
msg: msg
|
};
|
await this.ctx.service.common.errorLog("login", msg);
|
}
|
}
|
//#endregion
|
|
//#region login 后台系统登出
|
/**
|
* 后台系统登出
|
*/
|
@Post()
|
public async logOut() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
if(body.sourceMachine==undefined || body.sourceMachine==""){
|
ctx.body = {
|
result: false,
|
msg: "来源设备不能为空!"
|
};
|
return;
|
}
|
|
try {
|
//更新登录状态 【Editby shaocx,2024-03-20】
|
let userRep = this.dbWrite.getRepository(SysUser);
|
//获得登陆用户
|
let loginUser = await this.dbRead.getRepository(SysUser).findOne({
|
userName: body.userName
|
});
|
if (loginUser == null) {
|
ctx.body = {
|
result: false,
|
msg: "账户'"+body.userName+"'不存在!"
|
};
|
return;
|
}
|
// if(loginUser.loginState){
|
// ctx.body = {
|
// result: false,
|
// msg: "账户已经在线!"
|
// };
|
// return;
|
// }
|
if(body.sourceMachine==='PDA'){
|
loginUser.loginStateForPda=0;
|
loginUser.loginIPForPda="";
|
loginUser.loginDateForPda=null;
|
}else{
|
loginUser.loginState=0;
|
loginUser.loginIP="";
|
loginUser.loginDate=null;
|
}
|
|
await userRep.save(loginUser);
|
|
//记录登录日志
|
let logRep = this.dbWrite.getRepository(SysUserLog);
|
let log = logRep.create();
|
|
//log.gUID = guid;
|
//log.loginInfo = JSON.stringify(platUserInfo);
|
// log.signTokenInfo = token;
|
log.userProduct_Id=1007;
|
log.operateType = "系统登出";
|
log.action = "系统登出";
|
log.iP = this.ctx.request.ip;
|
log.creator=this.ctx.body.userName;
|
await logRep.save(log);
|
|
|
ctx.body = {
|
result: true,
|
msg: "登出成功",
|
};
|
} catch (error) {
|
let msg = error.message;
|
ctx.body = {
|
result: false,
|
msg: msg
|
};
|
await this.ctx.service.common.errorLog("logOut", msg);
|
}
|
}
|
//#endregion
|
|
//#region 登录验证 - 用户登录
|
@Post()
|
public async loginUser() {
|
let { ctx, app } = this;
|
let redis = await app.redis.clients.get("userInfo");
|
let guid = ctx.header.guid;
|
this.body.guid = guid;
|
const key = constValidateCode + guid;
|
|
let validateCode = await redis.get(key);
|
await redis.del(key);
|
try {
|
if (this.body.validateCode != "passd") {
|
//校验数据
|
if (this.body.validateCode != validateCode) {
|
this.info.result = false;
|
this.info.msg = "验证码不正确";
|
ctx.body = this.info;
|
return;
|
}
|
}
|
|
//获得产品信息
|
let prodInfo = await this.dbRead.findOne(PlatUserProduct, {
|
userProductCode: this.body.userProductCode
|
});
|
if (!prodInfo) {
|
this.info.result = false;
|
this.info.msg = "账套编号不存在!";
|
ctx.body = this.info;
|
return;
|
}
|
//获得平台信息
|
let platInfo = await this.dbRead.findOne(PlatUser, {
|
platUser_Id: prodInfo.platUser_Id
|
});
|
|
let where = "(mobile=:mobile OR consignorCode=:mobile) And userProductCode=:userProductCode";
|
let userInfo = await this.dbRead
|
.createQueryBuilder(BaseConsignor, "t")
|
.where(where, {
|
mobile: this.body.userName,
|
userProductCode: this.body.userProductCode
|
})
|
.getOne();
|
if (!userInfo) {
|
this.info.result = false;
|
this.info.msg = "账号或密码不正确!";
|
ctx.body = this.info;
|
return;
|
}
|
|
if (userInfo.enable != 1) {
|
this.info.result = false;
|
this.info.msg = "用户不可用";
|
ctx.body = this.info;
|
return;
|
}
|
|
let userPwd = ctx.helper.md5EncodingSalt(this.body.userPwd);
|
if (this.body != null && userInfo.password == userPwd) {
|
this.info.result = true;
|
this.info.msg = "登陆成功";
|
this.info.statusCode = 0;
|
|
let log = await this.dbRead.findOne(SysUserLog, {
|
where: {
|
guid: guid
|
},
|
order: {
|
log_Id: "DESC"
|
}
|
});
|
if (log != null && log.loginInfo) {
|
//#region 已存在更新
|
let platUserInfo = JSON.parse(log.loginInfo);
|
if (platUserInfo.userName != userInfo.consignorCode) {
|
platUserInfo = {
|
platUser_Id: platInfo.platUser_Id,
|
|
userProduct_Id: prodInfo.userProduct_Id,
|
userProductCode: userInfo.userProductCode,
|
userProductAlias: userInfo.userProductAlias ? userInfo.userProductAlias : prodInfo.productName,
|
versionName: null,
|
|
role_Id: 0,
|
user_Id: userInfo.consignor_Id,
|
userName: userInfo.consignorCode,
|
userTrueName: userInfo.consignorName,
|
|
consignor_Id: userInfo.consignor_Id,
|
consignorCode: userInfo.consignorCode,
|
consignorName: userInfo.consignorName,
|
|
mobile: userInfo.mobile,
|
accessToken: ctx.service.common.getGUID().toUpperCase(),
|
isAdministrator: false,
|
userType: "consignor"
|
};
|
let loginInfo = JSON.stringify(platUserInfo);
|
await this.dbWrite.update(SysUserLog, log.log_Id, {
|
userTrueName: platUserInfo.userTrueName,
|
loginInfo: loginInfo,
|
modifyDate: new Date(),
|
operateType: "系统登录",
|
action: "系统登录",
|
iP: this.ctx.request.ip
|
});
|
}
|
|
let accessTokenKey = "AccessToken_" + guid;
|
// redis 缓存数据
|
let loginInfo = JSON.stringify(platUserInfo);
|
await redis.set(accessTokenKey, loginInfo);
|
await redis.expire(accessTokenKey, 60 * 60 * 24 * 7);
|
this.info.data = platUserInfo;
|
//#endregion
|
} else {
|
//#region 重新登录
|
let platUserInfo = {
|
platUser_Id: platInfo.platUser_Id,
|
|
userProduct_Id: prodInfo.userProduct_Id,
|
userProductCode: userInfo.userProductCode,
|
userProductAlias: userInfo.userProductAlias ? userInfo.userProductAlias : prodInfo.productName,
|
versionName: null,
|
|
role_Id: 0,
|
user_Id: userInfo.consignor_Id,
|
userName: userInfo.consignorCode,
|
userTrueName: userInfo.consignorName,
|
|
consignor_Id: userInfo.consignor_Id,
|
consignorCode: userInfo.consignorCode,
|
consignorName: userInfo.consignorName,
|
|
mobile: userInfo.mobile,
|
accessToken: ctx.service.common.getGUID(),
|
isAdministrator: false,
|
userType: "consignor"
|
};
|
|
let accessTokenKey = "AccessToken_" + guid;
|
let signTokenKey = "Token_" + guid;
|
// redis 缓存数据
|
await redis.set(accessTokenKey, JSON.stringify(platUserInfo));
|
await redis.expire(accessTokenKey, 60 * 60 * 24 * 7);
|
this.info.data = platUserInfo;
|
|
let token = await redis.get(signTokenKey);
|
//记录登录日志
|
log = new SysUserLog();
|
log.userTrueName = platUserInfo.userTrueName;
|
log = Object.assign(log, platUserInfo);
|
log.gUID = this.body.guid;
|
log.loginInfo = JSON.stringify(platUserInfo);
|
log.signTokenInfo = token;
|
log.operateType = "系统登录";
|
log.action = "系统登录";
|
log.iP = this.ctx.request.ip;
|
|
await this.dbWrite.save(log);
|
//#endregion
|
}
|
|
//获得菜单权限
|
let menuList = await ctx.service.sys.menuVue.getVueMenu();
|
this.info.dynamic = menuList;
|
|
ctx.body = this.info;
|
return;
|
} else {
|
this.info.result = false;
|
this.info.msg = "用户名或密码不正确";
|
ctx.body = this.info;
|
return;
|
}
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误消息:" + ex.message;
|
ctx.body = this.info;
|
return;
|
}
|
}
|
//#endregion
|
|
//#region hasSaasAuth
|
/**
|
* 获得SaaS模块权限
|
*/
|
@Post()
|
public async hasSaasAuth() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let idList = body.IDs.split(",");
|
let saaSAuthList = await ctx.helper.getSaaSAuthList();
|
saaSAuthList = ctx.helper.arrayToCase(saaSAuthList);
|
let m = saaSAuthList.filter(item => {
|
return idList.some(s => {
|
return s === item.moduleName;
|
});
|
});
|
|
ctx.body = {
|
result: true,
|
data: m
|
};
|
} catch (error) {
|
ctx.body = {
|
result: false,
|
msg: error.toString()
|
};
|
}
|
}
|
|
//#endregion
|
|
//#region getAuth
|
/**
|
* 获得模块权限
|
*/
|
@Post()
|
public async getAuth() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
try {
|
let menu_Id = body.id;
|
let userInfo = await ctx.helper.userInfo();
|
let role_Id = (userInfo && userInfo.role_Id) || 0;
|
let userJson = {}; // 用户自定义字段参数设置
|
let subUserJsons = []; // 用户自定义字段参数设置 - 明细表
|
let userUIJson = {}; // 用户自定义UI参数设置
|
let global_openSortable = await this.ctx.service.common.getConfigBool("global_openSortable"); // 列表页面开始排序
|
let global_closeUserUIJson = await this.ctx.service.common.getConfigBool("global_closeUserUIJson"); // 关闭自定义UI
|
if (menu_Id) {
|
// 获得自定义设置
|
let tableInfo = await this.dbRead.findOne(SysMvcUserData, {
|
tableView: body.tableView,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
if (tableInfo && tableInfo.userJson) {
|
userJson = JSON.parse(tableInfo.userJson);
|
}
|
|
// 获得明细表自定义设置
|
let subTableViews = body.subTableViews;
|
if (Array.isArray(subTableViews)) {
|
for (let subTableView of subTableViews) {
|
tableInfo = await this.dbRead.findOne(SysMvcUserData, {
|
tableView: subTableView,
|
userProduct_Id: userInfo.userProduct_Id
|
});
|
if (tableInfo && tableInfo.userJson) {
|
subUserJsons.push({
|
subTableView: subTableView,
|
userJson: JSON.parse(tableInfo.userJson)
|
});
|
}
|
}
|
}
|
// 获取自定义UI设计
|
let vueInfo = await this.dbRead.findOne(SysMvcVueData, {
|
userProduct_Id: userInfo.userProduct_Id,
|
table_Id: body.table_Id,
|
fromVueData_Id: body.fromVueData_Id
|
});
|
if (vueInfo && vueInfo.vueData) userUIJson = JSON.parse(vueInfo.vueData);
|
}
|
|
// 权限点
|
let authValues = "";
|
// 超级管理获得所有权限
|
if (userInfo.isAdministrator) {
|
if (!authValues) {
|
let authInfo = await this.dbRead.getRepository(SysMenuVue).findOne({
|
select: ["vueAuth"],
|
where: {
|
menu_Id: menu_Id
|
}
|
});
|
if (authInfo) {
|
let vueAuthList = authInfo.vueAuth.split(",");
|
authValues = vueAuthList
|
.map(x => {
|
let nodes = x.split("=");
|
x = nodes[0] + "=1";
|
x = ctx.helper.caseStyle(x.trim());
|
return x;
|
})
|
.join(",");
|
}
|
}
|
ctx.body = {
|
result: true,
|
data: authValues,
|
userJson: userJson,
|
subUserJsons: subUserJsons,
|
userUIJson: userUIJson,
|
global_openSortable: global_openSortable,
|
global_closeUserUIJson: global_closeUserUIJson
|
};
|
return;
|
}
|
|
let roleAuthInfo = await this.dbRead.findOne(SysRoleAuthDataAll, {
|
select: ["isAllAuth"],
|
where: {
|
role_Id: role_Id
|
}
|
});
|
|
if (roleAuthInfo && roleAuthInfo.isAllAuth == 1) {
|
let menuInfo = await this.dbRead.getRepository(SysMenuVue).findOne({
|
select: ["vueAuth"],
|
where: {
|
menu_Id: menu_Id
|
}
|
});
|
if (menuInfo && menuInfo.vueAuth) {
|
let vueAuthList = menuInfo.vueAuth.split(",");
|
vueAuthList.forEach(x => {
|
let v = x.split("=");
|
if (authValues) {
|
authValues = authValues + ",";
|
}
|
authValues = authValues + ctx.helper.caseStyle(v[0]) + "=1";
|
return true;
|
});
|
}
|
}
|
|
if (!authValues) {
|
let authInfo = await this.dbRead.getRepository(SysRoleAuth).findOne({
|
select: ["authValue"],
|
where: {
|
role_Id: role_Id,
|
menu_Id: menu_Id
|
}
|
});
|
if (authInfo) {
|
let vueAuthList = authInfo.authValue.split(",");
|
authValues = vueAuthList
|
.map(x => {
|
x = ctx.helper.caseStyle(x.trim());
|
return x;
|
})
|
.join(",");
|
}
|
}
|
|
ctx.body = {
|
result: true,
|
data: authValues,
|
userJson: userJson,
|
subUserJsons: subUserJsons,
|
userUIJson: userUIJson,
|
global_openSortable: global_openSortable,
|
global_closeUserUIJson: global_closeUserUIJson
|
};
|
} catch (error) {
|
try {
|
let msg = JSON.stringify(error);
|
ctx.body = {
|
result: false,
|
msg: msg
|
};
|
await this.ctx.service.common.errorLog("getAuth", msg);
|
} catch (err) {
|
let msg = "嵌套错误:" + error.message + ", " + err.message;
|
ctx.body = {
|
result: false,
|
msg: msg
|
};
|
await this.ctx.service.common.errorLog("getAuth", msg);
|
}
|
}
|
}
|
|
//#endregion
|
|
//#region 获得数据权限模块数据结构
|
/**
|
* 获得数据权限模块数据结构
|
*/
|
@Post()
|
public async getDataAuth() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
let userInfo = await ctx.helper.userInfo();
|
try {
|
let user_Id = body.id;
|
let moduleList = await this.dbRead.find(SysDataAuthModule, {
|
where: { enable: 1 },
|
order: { orderNo: "DESC", module_Id: "ASC" }
|
});
|
|
let authDataList = await this.dbRead.find(SysRoleAuthData, {
|
user_Id: user_Id
|
});
|
|
let resultDataList: Array<any> = [];
|
for (let s of moduleList) {
|
let sql = s.sQL.replace(/{userProduct_Id}/gi, userInfo.userProduct_Id.toString());
|
let itemList = await this.dbRead.query(sql);
|
let authNodeList: Array<any> = [];
|
for (let item of itemList) {
|
let id = item["ID"];
|
let name = item["Name"];
|
let itemAuth = authDataList.find(w => {
|
return w.dataType_Id == s.module_Id && w.node_Id == id;
|
});
|
authNodeList.push({
|
node_Id: id,
|
nodeName: name,
|
isCheck: itemAuth != null && itemAuth.authValue == "1"
|
});
|
}
|
// 全部复选框
|
const allNode = authDataList.find(node => node.dataType_Id == s.module_Id && node.node_Id == 0);
|
if (allNode) {
|
authNodeList.push({
|
node_Id: 0,
|
nodeName: "全部",
|
isCheck: allNode != null && allNode.authValue == "1"
|
});
|
}
|
resultDataList.push({
|
module_Id: s.module_Id,
|
moduleName: s.moduleName,
|
authNodeList: authNodeList
|
});
|
}
|
|
this.info.data = resultDataList;
|
this.info.result = true;
|
ctx.body = this.info;
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误消息:" + ex.message;
|
ctx.body = this.info;
|
}
|
}
|
//#endregion
|
|
//#region 保存用户数据权限 saveUserDataAuth
|
/**
|
* 保存用户数据权限
|
*/
|
@Post()
|
public async saveUserDataAuth() {
|
let { ctx } = this;
|
let body = ctx.request.body;
|
|
try {
|
let oModuleList = JSON.parse(body.code);
|
for (let moduleInfo of oModuleList) {
|
let node_IdList = "-1";
|
let authDataList = await this.dbRead.find(SysRoleAuthData, {
|
where: {
|
user_Id: moduleInfo.user_Id,
|
dataType_Id: moduleInfo.module_Id
|
}
|
});
|
let dataList = [];
|
let rowIndex = 0;
|
for (let authData of moduleInfo.authNodeList) {
|
rowIndex++;
|
node_IdList += "," + authData.node_Id;
|
|
let authDataInfo = authDataList.find(item => String(item.node_Id) === String(authData.node_Id));
|
if (!authDataInfo) {
|
authDataInfo = new SysRoleAuthData();
|
}
|
authDataInfo.authValue = authData.isCheck ? "1" : "0";
|
authDataInfo.dataType_Id = moduleInfo.module_Id;
|
authDataInfo.node_Id = authData.node_Id;
|
authDataInfo.user_Id = moduleInfo.user_Id;
|
dataList.push(authDataInfo);
|
if (rowIndex % 50 == 1) {
|
await this.dbWrite.save(SysRoleAuthData, dataList);
|
dataList = [];
|
}
|
}
|
if (dataList.length) {
|
await this.dbWrite.save(SysRoleAuthData, dataList);
|
}
|
|
let sql = `Delete from Sys_RoleAuthData Where user_Id=${moduleInfo.user_Id} And DataType_Id=${moduleInfo.module_Id} And node_Id not in(${node_IdList})`;
|
await this.dbWrite.query(sql);
|
}
|
|
this.info.result = true;
|
this.info.msg = "保存成功!";
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "保存失败,错误信息:" + ex.message;
|
}
|
|
ctx.body = this.info;
|
}
|
//#endregion
|
|
//#region 更新app updateApp
|
/**
|
* 更新app
|
*/
|
@Post()
|
public async updateApp() {
|
let { ctx } = this;
|
|
try {
|
let version = (await this.ctx.service.common.getConfig("global_appVersion")) || "1.0.0.0";
|
this.info.result = true;
|
this.info.data = {
|
version: version
|
};
|
} catch (ex) {
|
this.info.result = false;
|
this.info.msg = "错误信息:" + ex.message;
|
}
|
|
ctx.body = this.info;
|
}
|
//#endregion
|
}
|