import DbClient from '../client/db' import { ErrMsg, App } from '../../types' import { Context } from 'koa' import i18n from '../../service/i18n' /** * @author jiangwei * @desc 检查器基类,封装通用方法 * @export * @class Base */ export default class Base { protected app: App protected ctx: Context protected dbClient: DbClient protected errorMsg: ErrMsg[] = [] protected __ = i18n.__ private membersMap = new Map() constructor (app: App, ctx: Context) { this.app = app this.ctx = ctx this.dbClient = new DbClient(app, ctx) if (this.ctx.state.lang) { i18n.setLocale(this.ctx.state.lang) } } get ErrorMsgs() { return this.errorMsg } /** * @author jiangwei * @desc 格式化错误信息 * @param {string} reason * @param {number} row * @param {string} celName * @param {string} content * @memberof Base */ public pushError(reason: string, row: number, celName: string, content: string) { this.errorMsg.push({ reason: i18n.__(reason), row: row + 1, celName: i18n.__(celName), content: content || i18n.__('err.excel.empty') }) } public pushErrorWithParams (reason: {name: string, params: {[propName: string]: any}}, row: number, celName: string, content: string) { this.errorMsg.push({ reason: i18n.__(reason.name, reason.params), row: row + 1, celName: i18n.__(celName), content: i18n.__(content) }) } /** * @author jiangwei * @desc 获取执行者 ID 通用方法 * @param {string} name * @param {string} _projectId * @param {number} row * @param {boolean} throwErr 是否抛错 * @returns {Promise} * @memberof Base */ async executor(name: string, _projectId: string, throwErr: boolean = false, row: number): Promise { if (!name) return null name = String(name) let _executorId = null if (this.membersMap.size && this.membersMap.has(name)) { _executorId = this.membersMap.get(name) } else { const members = await this.dbClient.getMembersByProjectId(_projectId) members.forEach((member) => { if (member._userId.name === name) { _executorId = member._userId._id } this.membersMap.set(member._userId.name, member._userId._id) }) } if (!_executorId && throwErr) { this.pushError('err.excel.executor.invalid', row, 'executor', name) } return _executorId } }