import { Controller, Context } from "egg";
|
import { EntityManager } from "typeorm";
|
import { ResultInfo } from "../public/commonInterface";
|
import { LoginInfo } from "../entity/sys/loginInfo";
|
|
export default class BaseController extends Controller {
|
private _info: ResultInfo;
|
/**
|
* 返回数据结构
|
*/
|
public get info() {
|
return this._info;
|
}
|
public set info(val: ResultInfo) {
|
this._info = val;
|
}
|
|
public getInfo(): ResultInfo {
|
return {
|
result: false,
|
msg: undefined,
|
statusCode: undefined,
|
data: undefined,
|
countPrint: undefined
|
};
|
}
|
|
constructor(ctx: Context) {
|
super(ctx);
|
this._info = {
|
result: false,
|
msg: undefined,
|
statusCode: undefined,
|
data: undefined,
|
countPrint: undefined
|
};
|
}
|
|
public async getToken() {
|
const { ctx } = this;
|
try {
|
const guid = this.ctx.query.guid;
|
ctx.body = {
|
guid
|
};
|
} catch (error) {
|
console.log("------------------" + error);
|
}
|
}
|
|
/**
|
* 主数据,读写
|
*/
|
public get dbWrite(): EntityManager {
|
return this.ctx.service.common.dbWrite;
|
}
|
|
/**
|
* 主数据,只读
|
*/
|
public get dbRead(): EntityManager {
|
return this.ctx.service.common.dbRead;
|
}
|
|
/**
|
* 用户信息
|
*/
|
public get userInfo(): Promise<LoginInfo> {
|
return this.ctx.helper.userInfo();
|
}
|
|
/**
|
* 请求参数
|
*/
|
public get body() {
|
return this.ctx.request.body;
|
}
|
|
/**
|
* 设置账套信息
|
* @param dataInfo 数据对象
|
* @param idField 主键
|
*/
|
public async setAccountInfo(dataInfo, idField?: any) {
|
if (!idField) idField = this.body.idField;
|
|
let userInfo = await this.userInfo;
|
dataInfo.platCorpName = userInfo.platCorpName;
|
dataInfo.platUserCode = userInfo.platUserCode;
|
dataInfo.platUserName = userInfo.platUserName;
|
dataInfo.platUser_Id = userInfo.platUser_Id;
|
dataInfo.userProductAlias = userInfo.userProductAlias;
|
dataInfo.userProductCode = userInfo.userProductCode;
|
dataInfo.userProduct_Id = userInfo.userProduct_Id;
|
|
Object.keys(dataInfo).forEach(key => {
|
let v = dataInfo[key];
|
if (v === "") {
|
dataInfo[key] = null;
|
}
|
});
|
|
// 设置新建信息
|
let keyValue = dataInfo[idField];
|
if (!keyValue) {
|
dataInfo = Object.assign(dataInfo, {
|
createID: userInfo.user_Id,
|
creator: userInfo.userTrueName,
|
createDate: new Date()
|
});
|
} else {
|
// 设置修改信息
|
dataInfo = Object.assign(dataInfo, {
|
modifyID: userInfo.user_Id,
|
modifier: userInfo.userTrueName,
|
modifyDate: new Date()
|
});
|
}
|
}
|
|
/**
|
* 将消息内容写入redis缓存
|
* @param key 导入标示key
|
* @param msg 消息内容
|
* @param color 消息颜色
|
*/
|
public async setMsg(msg: string, color?: string) {
|
let redis = this.ctx.app.redis.clients.get("common"); // 将消息放入redis缓存
|
let key = this.body.key;
|
if (!key) {
|
return;
|
}
|
|
if (color && msg !== "-1") {
|
msg = `<span style='color:${color};font-weight:bold;'>${msg}</span>`;
|
}
|
if (color === "red") {
|
this._isMsgError = true;
|
}
|
|
await redis.rpush(key, msg);
|
await redis.expire(key, 60 * 5);
|
}
|
|
private _isMsgError: boolean = false;
|
/**
|
* 消息是否有错误
|
*/
|
public get isMsgError() {
|
return this._isMsgError;
|
}
|
public set isMsgError(val: boolean) {
|
this._isMsgError = val;
|
}
|
}
|