tender_permission.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2025/7/17
  7. * @version
  8. */
  9. module.exports = app => {
  10. class tenderPermission extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @param {String} tableName - 表名
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'tender_permission';
  21. this.PermissionConst = {
  22. quality: {
  23. view: { title: '查看', value: 1, isDefault: 1 },
  24. upload: { title: '上传文件', value: 2 },
  25. add: { title: '新增功能', value: 3 },
  26. },
  27. };
  28. this.PermissionBlock = [
  29. { key: 'quality', name: '质量管理', field: 'quality' },
  30. ];
  31. for (const p of this.PermissionBlock) {
  32. if (p.children) {
  33. for (const c of p.children) {
  34. c.permission = [];
  35. const pConst = this.PermissionConst[c.key];
  36. if (!pConst) continue;
  37. for (const prop in pConst) {
  38. c.permission.push({ key: prop, ...pConst[prop]});
  39. }
  40. }
  41. } else {
  42. p.permission = [];
  43. const pConst = this.PermissionConst[p.key];
  44. if (!pConst) continue;
  45. for (const prop in pConst) {
  46. p.permission.push({ key: prop, ...pConst[prop]});
  47. }
  48. }
  49. }
  50. this.AdminPermission = {};
  51. for (const p in this.PermissionConst) {
  52. this.AdminPermission[p] = this.ctx.helper.mapAllSubField(this.PermissionConst[p], 'value');
  53. }
  54. }
  55. partPermissionConst(part) {
  56. if (!part) return this.PermissionConst;
  57. const parts = part instanceof Array ? part : [part];
  58. const result = {};
  59. for (const p of parts) {
  60. result[p] = this.PermissionConst[p];
  61. }
  62. return result;
  63. }
  64. partPermissionBlock(part) {
  65. if (!part) return this.PermissionBlock;
  66. const parts = part instanceof Array ? part : [part];
  67. const result = this.PermissionBlock.filter(x => { return parts.indexOf(x.key) >= 0; });
  68. return result;
  69. }
  70. parsePermission(data) {
  71. const _ = this.ctx.helper._;
  72. const datas = data instanceof Array ? data : [data];
  73. datas.forEach(x => {
  74. for (const p in this.PermissionConst) {
  75. x[p] = x[p] ? _.map(x[p].split(','), _.toInteger) : [];
  76. }
  77. });
  78. }
  79. // 权限检查
  80. conversePermission(permission) {
  81. const result = {};
  82. for (const block of this.PermissionBlock) {
  83. const per = {};
  84. for(const p of block.permission) {
  85. per[p.key] = permission[block.key].indexOf(p.value) >= 0;
  86. }
  87. result[block.key] = per;
  88. }
  89. return result;
  90. }
  91. getAdminPermission() {
  92. return this.conversePermission(this.AdminPermission);
  93. }
  94. async getUserPermission(tid, uid) {
  95. const result = await this.getDataByCondition({ tid, uid });
  96. this.parsePermission(result);
  97. return this.conversePermission(result);
  98. }
  99. // 用户权限编辑
  100. async getPartsPermission(tid, parts) {
  101. if (!parts || parts.length === 0) return [];
  102. const partSql = parts.map(x => { return `${x} <> ''`}).join(' OR ');
  103. 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})`;
  104. const result = await this.db.query(sql, [tid]);
  105. this.parsePermission(result);
  106. return result;
  107. }
  108. async savePermission(tid, member, permissionBlock) {
  109. const orgMember = await this.getAllDataByCondition({ where: { tid } });
  110. const updateMember = [], insertMember = [];
  111. for (const om of orgMember) {
  112. const nmi = member.findIndex(x => { return om.uid == x.uid; });
  113. if (nmi < 0) {
  114. const um = { id: om.id };
  115. for (const p of permissionBlock) {
  116. um[p] = '';
  117. }
  118. updateMember.push(um);
  119. } else {
  120. const nm = member[nmi];
  121. const um = { id: om.id };
  122. for (const p in this.PermissionConst) {
  123. if (nm[p]) um[p] = nm[p].join(',');
  124. }
  125. updateMember.push(um);
  126. member.splice(nmi, 1);
  127. }
  128. }
  129. for (const m of member) {
  130. const im = { id: this.uuid.v4(), pid: this.ctx.session.sessionProject.id, tid, uid: m.uid };
  131. for (const p in this.PermissionConst) {
  132. if (m[p]) im[p] = m[p].join(',');
  133. }
  134. insertMember.push(im);
  135. }
  136. const conn = await this.db.beginTransaction();
  137. try {
  138. if (updateMember.length > 0) await conn.updateRows(this.tableName, updateMember);
  139. if (insertMember.length > 0) await conn.insert(this.tableName, insertMember);
  140. await conn.commit();
  141. } catch (err) {
  142. await conn.rollback();
  143. throw err;
  144. }
  145. }
  146. }
  147. return tenderPermission;
  148. };