sub_proj_permission.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class subProjPermission 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 = 'sub_project_permission';
  21. this._definePermission();
  22. }
  23. _definePermission() {
  24. this.PermissionConst = {
  25. budget: {
  26. view: { title: '查看', value: 1 },
  27. edit: { title: '编辑', value: 2 },
  28. },
  29. file: {
  30. view: { title: '查看', value: 1 },
  31. upload: { title: '上传/引用', value: 2 },
  32. delete: { title: '删除文件', value: 4 },
  33. filing: { title: '文件类别编辑', value: 3 },
  34. },
  35. manage: {
  36. rela: { title: '关联标段', value: 1 },
  37. },
  38. info: {
  39. view: { title: '查看', value: 1},
  40. edit: { title: '编辑', value: 2 },
  41. },
  42. datacollect: {
  43. view: { title: '查看', value: 1},
  44. },
  45. contract: {
  46. edit: { title: '编辑节点', value: 1 },
  47. add: { title: '添加合同', value: 2 },
  48. node: { title: '授权查看本节点合同', value: 3 },
  49. unit: { title: '授权查看本单位合同', value: 4 },
  50. },
  51. fund_trans: {
  52. view: { title: '查看', value: 1 },
  53. add: { title: '新建划拨', value: 2 },
  54. att: { title: '上传附件', value: 3 },
  55. },
  56. fund_pay: {
  57. view: { title: '查看', value: 1 },
  58. att: { title: '上传附件', value: 3 },
  59. },
  60. };
  61. this.PermissionBlock = [
  62. { key: 'datacollect', name: '决策大屏', field: 'datacollect_permission' },
  63. { key: 'info', name: '项目概况', field: 'info_permission' },
  64. { key: 'contract', name: '合同管理', field: 'contract_permission', hint: ['1、编辑节点:编辑合同管理内页树结构', '2、添加合同:允许添加合同', '3、授权查看本节点合同:授权节点下查看所有人上传的合同', '4、授权查看本单位合同:授权节点下查看本单位人员添加的所有合同'] },
  65. { key: 'file', name: '资料归集', field: 'file_permission' },
  66. { key: 'budget', name: '动态投资', field: 'budget_permission' },
  67. {
  68. key: 'financial', name: '资金监管', children: [
  69. { key: 'fund_trans', name: '资金划拨', field: 'fund_trans_permission' },
  70. { key: 'fund_pay', name: '资金支付', field: 'fund_pay_permission' },
  71. ]
  72. },
  73. ];
  74. for (const p of this.PermissionBlock) {
  75. if (p.children) {
  76. for (const c of p.children) {
  77. c.permission = [];
  78. const pConst = this.PermissionConst[c.key];
  79. if (!pConst) continue;
  80. for (const prop in pConst) {
  81. c.permission.push({ key: prop, ...pConst[prop]});
  82. }
  83. }
  84. } else {
  85. p.permission = [];
  86. const pConst = this.PermissionConst[p.key];
  87. if (!pConst) continue;
  88. for (const prop in pConst) {
  89. p.permission.push({ key: prop, ...pConst[prop]});
  90. }
  91. }
  92. }
  93. }
  94. get adminPermission () {
  95. return {
  96. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  97. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  98. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  99. filing_type: 'all',
  100. info_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.info, 'value'),
  101. datacollect_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.datacollect, 'value'),
  102. contract_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.contract, 'value'),
  103. fund_pay_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_pay, 'value'),
  104. fund_trans_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_trans, 'value'),
  105. }
  106. }
  107. async showSubTab(uid, type) {
  108. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  109. const result = await this.db.queryOne(sql, [uid]);
  110. return result.count;
  111. }
  112. async showBudget(uid) {
  113. return await this.showSubTab(uid, 'budget');
  114. }
  115. async showFile(uid) {
  116. return await this.showSubTab(uid, 'file');
  117. }
  118. parsePermission(data) {
  119. const _ = this.ctx.helper._;
  120. const datas = data instanceof Array ? data : [data];
  121. datas.forEach(x => {
  122. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  123. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  124. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  125. x.info_permission = x.info_permission ? _.map(x.info_permission.split(','), _.toInteger) : [];
  126. x.datacollect_permission = x.datacollect_permission ? _.map(x.datacollect_permission.split(','), _.toInteger) : [];
  127. x.contract_permission = x.contract_permission ? _.map(x.contract_permission.split(','), _.toInteger) : [];
  128. x.fund_pay_permission = x.fund_pay_permission ? _.map(x.fund_pay_permission.split(','), _.toInteger) : [];
  129. x.fund_trans_permission = x.fund_trans_permission ? _.map(x.fund_trans_permission.split(','), _.toInteger) : [];
  130. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  131. });
  132. }
  133. async getPermission(subProjectId) {
  134. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  135. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  136. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  137. this.parsePermission(result);
  138. return result;
  139. }
  140. async getBudgetPermission(subProjectId) {
  141. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  142. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  143. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  144. this.parsePermission(result);
  145. return result;
  146. }
  147. async getUserPermission(pid, uid) {
  148. const result = await this.getAllDataByCondition({
  149. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  150. });
  151. this.parsePermission(result);
  152. return result;
  153. }
  154. async getBudgetUserPermission(bid) {
  155. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  156. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  157. if (result) this.parsePermission(result);
  158. return result;
  159. }
  160. async getSubProjectUserPermission(spid, uid) {
  161. const result = await this.getDataByCondition({ spid, uid });
  162. if (result) this.parsePermission(result);
  163. return result;
  164. };
  165. async savePermission(subProjectId, member) {
  166. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  167. const dm = [], um = [], im = [];
  168. for (const om of orgMember) {
  169. const nm = member.find(x => { return om.uid === x.uid; });
  170. if (!nm) {
  171. dm.push(om.id);
  172. } else {
  173. um.push({
  174. id: om.id, budget_permission: nm.budget_permission.join(','),
  175. file_permission: nm.file_permission.join(','),
  176. manage_permission: nm.manage_permission.join(',')
  177. });
  178. member.splice(member.indexOf(nm), 1);
  179. }
  180. }
  181. for (const m of member) {
  182. im.push({
  183. id: this.uuid.v4(),
  184. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  185. budget_permission: m.budget_permission.join(','),
  186. file_permission: m.file_permission.join(','),
  187. manage_permission: m.manage_permission.join(',')
  188. });
  189. }
  190. const conn = await this.db.beginTransaction();
  191. try {
  192. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  193. if (um.length > 0) await conn.updateRows(this.tableName, um);
  194. if (im.length > 0) await conn.insert(this.tableName, im);
  195. await conn.commit();
  196. } catch (err) {
  197. await conn.rollback();
  198. throw err;
  199. }
  200. }
  201. async _addUser(subProject, data) {
  202. const ids = data instanceof Array ? data : [data];
  203. const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  204. if (exists.length > 0) throw '请勿重复选择账号';
  205. const insertData = ids.map(x => {
  206. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  207. });
  208. await this.db.insert(this.tableName, insertData);
  209. return insertData;
  210. }
  211. async _delUser(subProject, data) {
  212. const ids = data instanceof Array ? data : [data];
  213. const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  214. await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) });
  215. return data;
  216. }
  217. async _updateUserPermission(data) {
  218. const datas = data instanceof Array ? data : [data];
  219. const updateData = [];
  220. datas.forEach(x => {
  221. const ud = { id: x.id };
  222. for (const p of this.PermissionBlock) {
  223. if (p.children) {
  224. for (const c of p.children) {
  225. if (x[c.field] !== undefined) ud[c.field] = x[c.field] || '';
  226. }
  227. } else {
  228. if (x[p.field] !== undefined) ud[p.field] = x[p.field] || '';
  229. }
  230. }
  231. updateData.push(ud);
  232. });
  233. await this.db.updateRows(this.tableName, updateData);
  234. return updateData;
  235. }
  236. async updatePermission(subProject, data) {
  237. const result = {};
  238. if (data.add) result.add = await this._addUser(subProject, data.add);
  239. if (data.del) result.del = await this._delUser(subProject, data.del);
  240. if (data.update) result.update = await this._updateUserPermission(data.update);
  241. return result;
  242. }
  243. async getFilingType(subProjectId) {
  244. const permissionConst = {}, prefix = 'f';
  245. for (const p in this.PermissionConst.file) {
  246. const fp = this.PermissionConst.file[p];
  247. permissionConst[prefix + fp.value] = fp.title;
  248. }
  249. const result = await this.db.query(`SELECT spp.id, p.name, p.role, p.company, p.mobile, spp.file_permission, spp.filing_type, spp.create_time
  250. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  251. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  252. result.forEach(x => {
  253. const filePermission = x.file_permission.split(',');
  254. x.file_permission = filePermission.map(x => {
  255. return permissionConst[prefix + x] || '';
  256. }).join(',');
  257. });
  258. return result;
  259. }
  260. // 资料归集,授权固定分类
  261. async saveFilingType(data) {
  262. const updateData = [];
  263. data.forEach(x => {
  264. updateData.push({ id: x.id, filing_type: x.filing_type });
  265. });
  266. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  267. }
  268. checkViewPermission(ctx) {
  269. const permissionBlock = ctx.service.subProjPermission.PermissionBlock.find(x => { return x.key === ctx.controllerName; });
  270. if (!permissionBlock) return true;
  271. if (permissionBlock.children) {
  272. let canView = false;
  273. for (const c of permissionBlock.children) {
  274. const viewPermission = c.permission.find(x => { return x.key === 'view'; });
  275. if (!viewPermission || ctx.subProject.permission[c.field].indexOf(viewPermission.value) >= 0) canView = true;
  276. }
  277. return canView;
  278. } else {
  279. const viewPermission = permissionBlock.permission.find(x => { return x.key === 'view'; });
  280. if (!viewPermission) return true;
  281. return ctx.subProject.permission[permissionBlock.field].indexOf(viewPermission.value) >= 0;
  282. }
  283. };
  284. }
  285. return subProjPermission;
  286. };