'use strict'; /** * * * @author Mai * @date 2025/7/17 * @version */ module.exports = app => { class tenderPermission extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'tender_permission'; this.PermissionConst = { quality: { view: { title: '查看', value: 1, isDefault: 1 }, upload: { title: '上传文件', value: 2 }, add: { title: '新增功能', value: 3 }, }, }; this.PermissionBlock = [ { key: 'quality', name: '质量管理', field: 'quality' }, ]; for (const p of this.PermissionBlock) { if (p.children) { for (const c of p.children) { c.permission = []; const pConst = this.PermissionConst[c.key]; if (!pConst) continue; for (const prop in pConst) { c.permission.push({ key: prop, ...pConst[prop]}); } } } else { p.permission = []; const pConst = this.PermissionConst[p.key]; if (!pConst) continue; for (const prop in pConst) { p.permission.push({ key: prop, ...pConst[prop]}); } } } this.AdminPermission = {}; for (const p in this.PermissionConst) { this.AdminPermission[p] = this.ctx.helper.mapAllSubField(this.PermissionConst[p], 'value'); } } partPermissionConst(part) { if (!part) return this.PermissionConst; const parts = part instanceof Array ? part : [part]; const result = {}; for (const p of parts) { result[p] = this.PermissionConst[p]; } return result; } partPermissionBlock(part) { if (!part) return this.PermissionBlock; const parts = part instanceof Array ? part : [part]; const result = this.PermissionBlock.filter(x => { return parts.indexOf(x.key) >= 0; }); return result; } parsePermission(data) { const _ = this.ctx.helper._; const datas = data instanceof Array ? data : [data]; datas.forEach(x => { for (const p in this.PermissionConst) { x[p] = x[p] ? _.map(x[p].split(','), _.toInteger) : []; } }); } // 权限检查 conversePermission(permission) { const result = {}; for (const block of this.PermissionBlock) { const per = {}; for(const p of block.permission) { per[p.key] = permission[block.key].indexOf(p.value) >= 0; } result[block.key] = per; } return result; } getAdminPermission() { return this.conversePermission(this.AdminPermission); } async getUserPermission(tid, uid) { const result = await this.getDataByCondition({ tid, uid }); this.parsePermission(result); return this.conversePermission(result); } // 用户权限编辑 async getPartsPermission(tid, parts) { if (!parts || parts.length === 0) return []; const partSql = parts.map(x => { return `${x} <> ''`}).join(' OR '); const sql = `SELECT qp.*, pa.name, pa.role FROM ${this.tableName} qp LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON qp.uid = pa.id WHERE qp.tid = ? AND (${partSql})`; const result = await this.db.query(sql, [tid]); this.parsePermission(result); return result; } async savePermission(tid, member, permissionBlock) { const orgMember = await this.getAllDataByCondition({ where: { tid } }); const updateMember = [], insertMember = []; for (const om of orgMember) { const nmi = member.findIndex(x => { return om.uid == x.uid; }); if (nmi < 0) { const um = { id: om.id }; for (const p of permissionBlock) { um[p] = ''; } updateMember.push(um); } else { const nm = member[nmi]; const um = { id: om.id }; for (const p in this.PermissionConst) { if (nm[p]) um[p] = nm[p].join(','); } updateMember.push(um); member.splice(nmi, 1); } } for (const m of member) { const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, tid, uid: m.uid }; for (const p in this.PermissionConst) { if (m[p]) im[p] = m[p].join(','); } insertMember.push(im); } const conn = await this.db.beginTransaction(); try { if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember); if (insertMember.length > 0) await conn.insert(this.tableName, insertMember); await conn.commit(); } catch (err) { await conn.rollback(); throw err; } } } return tenderPermission; };