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