sub_proj_permission.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. view: { title: '查看所有合同', value: 5 },
  51. },
  52. fund_trans: {
  53. view: { title: '查看', value: 1 },
  54. add: { title: '新建划拨', value: 2 },
  55. att: { title: '上传附件', value: 3 },
  56. },
  57. fund_pay: {
  58. view: { title: '查看', value: 1 },
  59. att: { title: '上传附件', value: 3 },
  60. },
  61. };
  62. this.PermissionBlock = [
  63. { key: 'datacollect', name: '决策大屏', field: 'datacollect_permission' },
  64. { key: 'info', name: '项目概况', field: 'info_permission' },
  65. { key: 'contract', name: '合同管理', field: 'contract_permission', hint: ['1、编辑节点:编辑合同管理内页树结构',
  66. '2、添加合同:允许添加合同',
  67. '3、授权查看本节点合同:授权节点下查看所有人上传的合同',
  68. '4、授权查看本单位合同:授权节点下查看本单位人员添加的所有合同',
  69. '5、查看所有合同:查看所有合同,包括其他单位人员添加的合同',
  70. '注:查看合同第3、4、5必须选择其一,否则无法查看本项目合同管理',
  71. ] },
  72. { key: 'file', name: '资料归集', field: 'file_permission' },
  73. { key: 'budget', name: '动态投资', field: 'budget_permission' },
  74. {
  75. key: 'financial', name: '资金监管', children: [
  76. { key: 'fund_trans', name: '资金划拨', field: 'fund_trans_permission' },
  77. { key: 'fund_pay', name: '资金支付', field: 'fund_pay_permission' },
  78. ]
  79. },
  80. ];
  81. for (const p of this.PermissionBlock) {
  82. if (p.children) {
  83. for (const c of p.children) {
  84. c.permission = [];
  85. const pConst = this.PermissionConst[c.key];
  86. if (!pConst) continue;
  87. for (const prop in pConst) {
  88. c.permission.push({ key: prop, ...pConst[prop]});
  89. }
  90. }
  91. } else {
  92. p.permission = [];
  93. const pConst = this.PermissionConst[p.key];
  94. if (!pConst) continue;
  95. for (const prop in pConst) {
  96. p.permission.push({ key: prop, ...pConst[prop]});
  97. }
  98. }
  99. }
  100. }
  101. getRelaPermissionBlock(key) {
  102. for (const p of this.PermissionBlock) {
  103. if (p.key === key) return p;
  104. if (p.children) {
  105. for (const c of p.children) {
  106. if (c.key === key) return c;
  107. }
  108. }
  109. }
  110. }
  111. get adminPermission () {
  112. return {
  113. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  114. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  115. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  116. filing_type: 'all',
  117. info_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.info, 'value'),
  118. datacollect_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.datacollect, 'value'),
  119. contract_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.contract, 'value'),
  120. fund_pay_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_pay, 'value'),
  121. fund_trans_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_trans, 'value'),
  122. }
  123. }
  124. async showSubTab(uid, type) {
  125. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  126. const result = await this.db.queryOne(sql, [uid]);
  127. return result.count;
  128. }
  129. async showBudget(uid) {
  130. return await this.showSubTab(uid, 'budget');
  131. }
  132. async showFile(uid) {
  133. return await this.showSubTab(uid, 'file');
  134. }
  135. parsePermission(data) {
  136. const _ = this.ctx.helper._;
  137. const datas = data instanceof Array ? data : [data];
  138. datas.forEach(x => {
  139. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  140. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  141. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  142. x.info_permission = x.info_permission ? _.map(x.info_permission.split(','), _.toInteger) : [];
  143. x.datacollect_permission = x.datacollect_permission ? _.map(x.datacollect_permission.split(','), _.toInteger) : [];
  144. x.contract_permission = x.contract_permission ? _.map(x.contract_permission.split(','), _.toInteger) : [];
  145. x.fund_pay_permission = x.fund_pay_permission ? _.map(x.fund_pay_permission.split(','), _.toInteger) : [];
  146. x.fund_trans_permission = x.fund_trans_permission ? _.map(x.fund_trans_permission.split(','), _.toInteger) : [];
  147. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  148. });
  149. }
  150. async getPermission(subProjectId) {
  151. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  152. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  153. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  154. this.parsePermission(result);
  155. return result;
  156. }
  157. async getBudgetPermission(subProjectId) {
  158. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  159. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  160. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  161. this.parsePermission(result);
  162. return result;
  163. }
  164. async getUserPermission(pid, uid) {
  165. const result = await this.getAllDataByCondition({
  166. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  167. });
  168. this.parsePermission(result);
  169. return result;
  170. }
  171. async getBudgetUserPermission(bid) {
  172. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  173. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  174. if (result) this.parsePermission(result);
  175. return result;
  176. }
  177. async getSubProjectUserPermission(spid, uid) {
  178. const result = await this.getDataByCondition({ spid, uid });
  179. if (result) this.parsePermission(result);
  180. return result;
  181. };
  182. async savePermission(subProjectId, member) {
  183. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  184. const dm = [], um = [], im = [];
  185. for (const om of orgMember) {
  186. const nm = member.find(x => { return om.uid === x.uid; });
  187. if (!nm) {
  188. dm.push(om.id);
  189. } else {
  190. um.push({
  191. id: om.id, budget_permission: nm.budget_permission.join(','),
  192. file_permission: nm.file_permission.join(','),
  193. manage_permission: nm.manage_permission.join(',')
  194. });
  195. member.splice(member.indexOf(nm), 1);
  196. }
  197. }
  198. for (const m of member) {
  199. im.push({
  200. id: this.uuid.v4(),
  201. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  202. budget_permission: m.budget_permission.join(','),
  203. file_permission: m.file_permission.join(','),
  204. manage_permission: m.manage_permission.join(',')
  205. });
  206. }
  207. const conn = await this.db.beginTransaction();
  208. try {
  209. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  210. if (um.length > 0) await conn.updateRows(this.tableName, um);
  211. if (im.length > 0) await conn.insert(this.tableName, im);
  212. await conn.commit();
  213. } catch (err) {
  214. await conn.rollback();
  215. throw err;
  216. }
  217. }
  218. async _addUser(subProject, data) {
  219. const ids = data instanceof Array ? data : [data];
  220. const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  221. if (exists.length > 0) throw '请勿重复选择账号';
  222. const insertData = ids.map(x => {
  223. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  224. });
  225. await this.db.insert(this.tableName, insertData);
  226. return insertData;
  227. }
  228. async _delUser(subProject, data) {
  229. const ids = data instanceof Array ? data : [data];
  230. const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  231. await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) });
  232. return data;
  233. }
  234. async _updateUserPermission(data) {
  235. const datas = data instanceof Array ? data : [data];
  236. const updateData = [];
  237. // const contractData = [];
  238. for (const x of datas) {
  239. const ud = { id: x.id };
  240. for (const p of this.PermissionBlock) {
  241. if (p.children) {
  242. for (const c of p.children) {
  243. if (x[c.field] !== undefined) ud[c.field] = x[c.field] || '';
  244. }
  245. } else {
  246. if (x[p.field] !== undefined) ud[p.field] = x[p.field] || '';
  247. // if (p.field === 'contract_permission') {
  248. // const spAudit = await this.getDataById(x.id);
  249. // if (spAudit) {
  250. // const contractAudit = await this.ctx.service.contractAudit.getDataByCondition({ spid: spAudit.spid, uid: spAudit.uid });
  251. // const contractPermission = x.contract_permission ? this._.map(x.contract_permission.split(','), this._.toInteger) : [];
  252. // const newContractPermission = contractPermission.length > 0 ? await this.getContractPermission(contractPermission) : [];
  253. // console.log(newContractPermission, contractAudit);
  254. // if (!contractAudit && contractPermission.length === 0) continue;
  255. // if (contractAudit) {
  256. // if (contractPermission.length === 0 || this._.intersection([3,4,5], contractPermission).length === 0) {
  257. // contractData.push({ delete: { id: contractAudit.id }});
  258. // } else if (contractAudit.permission_add !== newContractPermission.permission_add ||
  259. // contractAudit.permission_edit !== newContractPermission.permission_edit ||
  260. // contractAudit.permission_show_unit !== newContractPermission.permission_show_unit ||
  261. // contractAudit.permission_show_node !== newContractPermission.permission_show_node) {
  262. // contractData.push({ update: { id: contractAudit.id,
  263. // permission_add: newContractPermission.permission_add,
  264. // permission_edit: newContractPermission.permission_edit,
  265. // permission_show_unit: newContractPermission.permission_show_unit,
  266. // permission_show_node: newContractPermission.permission_show_node,
  267. // }});
  268. // }
  269. // } else if (!contractAudit && contractPermission.length > 0) {
  270. // contractData.push({ add: {
  271. // spid: spAudit.spid,
  272. // tid: null,
  273. // uid: spAudit.uid,
  274. // permission_add: newContractPermission.permission_add,
  275. // permission_edit: newContractPermission.permission_edit,
  276. // permission_show_unit: newContractPermission.permission_show_unit,
  277. // permission_show_node: newContractPermission.permission_show_node,
  278. // create_time: new Date(),
  279. // }});
  280. // }
  281. // }
  282. // }
  283. }
  284. }
  285. updateData.push(ud);
  286. }
  287. const conn = await this.db.beginTransaction();
  288. try {
  289. // if (contractData.length > 0) {
  290. // for (const d of contractData) {
  291. // if (d.add) {
  292. // await conn.insert(this.ctx.service.contractAudit.tableName, d.add);
  293. // } else if (d.update) {
  294. // await conn.update(this.ctx.service.contractAudit.tableName, d.update);
  295. // } else if (d.delete) {
  296. // await conn.delete(this.ctx.service.contractAudit.tableName, d.delete);
  297. // }
  298. // }
  299. // }
  300. await conn.updateRows(this.tableName, updateData);
  301. await conn.commit();
  302. } catch(err) {
  303. await conn.rollback();
  304. throw err;
  305. }
  306. return updateData;
  307. }
  308. async updatePermission(subProject, data) {
  309. const result = {};
  310. if (data.add) result.add = await this._addUser(subProject, data.add);
  311. if (data.del) result.del = await this._delUser(subProject, data.del);
  312. if (data.update) result.update = await this._updateUserPermission(data.update);
  313. return result;
  314. }
  315. async getFilingType(subProjectId) {
  316. const permissionConst = {}, prefix = 'f';
  317. for (const p in this.PermissionConst.file) {
  318. const fp = this.PermissionConst.file[p];
  319. permissionConst[prefix + fp.value] = fp.title;
  320. }
  321. 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
  322. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  323. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  324. result.forEach(x => {
  325. const filePermission = x.file_permission.split(',');
  326. x.file_permission = filePermission.map(x => {
  327. return permissionConst[prefix + x] || '';
  328. }).join(',');
  329. });
  330. return result;
  331. }
  332. // 资料归集,授权固定分类
  333. async saveFilingType(data) {
  334. const updateData = [];
  335. data.forEach(x => {
  336. updateData.push({ id: x.id, filing_type: x.filing_type });
  337. });
  338. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  339. }
  340. checkViewPermission(ctx) {
  341. const permissionBlock = ctx.service.subProjPermission.PermissionBlock.find(x => { return x.key === ctx.controllerName; });
  342. if (!permissionBlock) return true;
  343. if (permissionBlock.children) {
  344. let canView = false;
  345. for (const c of permissionBlock.children) {
  346. const viewPermission = c.permission.find(x => { return x.key === 'view'; });
  347. if (!viewPermission || ctx.subProject.permission[c.field].indexOf(viewPermission.value) >= 0) canView = true;
  348. }
  349. return canView;
  350. } else if (ctx.controllerName === 'contract') {
  351. if (ctx.url.indexOf('contract/tender') >= 0) return true;
  352. const contractViewPermission = this._.map(permissionBlock.permission.filter(x => { return ['view', 'unit', 'node'].indexOf(x.key) >= 0; }), 'value');
  353. if (!contractViewPermission) return true;
  354. return this._.intersection(ctx.subProject.permission[permissionBlock.field], contractViewPermission).length > 0;
  355. } else {
  356. const viewPermission = permissionBlock.permission.find(x => { return x.key === 'view'; });
  357. if (!viewPermission) return true;
  358. return ctx.subProject.permission[permissionBlock.field].indexOf(viewPermission.value) >= 0;
  359. }
  360. };
  361. async getContractAuditList(spid, uid = null) {
  362. const uidSql = uid ? 'AND uid in (' + uid.join(',') + ')' : '';
  363. const sql = `SELECT * FROM ?? WHERE spid = ? AND contract_permission <> ''` + uidSql;
  364. const sqlParams = [this.tableName, spid];
  365. const result = await this.db.query(sql, sqlParams);
  366. const list = [];
  367. for (const r of result) {
  368. const permission = await this.getContractPermission(this._.map(r.contract_permission.split(','), this._.toInteger));
  369. list.push({
  370. id: r.id,
  371. spid: r.spid,
  372. tid: null,
  373. uid: r.uid,
  374. permission_add: permission.permission_add,
  375. permission_edit: permission.permission_edit,
  376. permission_show_node: permission.permission_show_node,
  377. permission_show_unit: permission.permission_show_unit,
  378. });
  379. }
  380. return list;
  381. }
  382. async getNewContractPermission(permission, newContractPermission) {
  383. const oldPermission = await this.getContractPermission(this._.map(permission.split(','), this._.toInteger));
  384. if (newContractPermission.permission_add !== undefined) {
  385. oldPermission.permission_add = newContractPermission.permission_add;
  386. }
  387. if (newContractPermission.permission_edit !== undefined) {
  388. oldPermission.permission_edit = newContractPermission.permission_edit;
  389. }
  390. if (newContractPermission.permission_show_node !== undefined) {
  391. oldPermission.permission_show_node = newContractPermission.permission_show_node;
  392. }
  393. if (newContractPermission.permission_show_unit !== undefined) {
  394. oldPermission.permission_show_unit = newContractPermission.permission_show_unit;
  395. }
  396. const permissionArr = [];
  397. if (oldPermission.permission_edit) permissionArr.push(1);
  398. if (oldPermission.permission_add) permissionArr.push(2);
  399. if (oldPermission.permission_show_node) permissionArr.push(3);
  400. if (oldPermission.permission_show_unit) permissionArr.push(4);
  401. if (!oldPermission.permission_show_unit && !oldPermission.permission_show_node) permissionArr.push(5);
  402. return permissionArr.join(',');
  403. }
  404. async getContractPermission(cp) {
  405. const permission = {
  406. permission_edit: cp.indexOf(1) !== -1 ? 1: 0,
  407. permission_add: cp.indexOf(2) !== -1 ? 1: 0,
  408. permission_show_node: cp.indexOf(3) !== -1 ? 1: 0,
  409. permission_show_unit: cp.indexOf(4) !== -1 ? 1: 0,
  410. };
  411. return permission;
  412. }
  413. async getFinancailPermission(trans_permission, pay_permission) {
  414. const permission = {
  415. transfer_show: trans_permission.indexOf(1) !== -1,
  416. transfer_add: trans_permission.indexOf(2) !== -1,
  417. transfer_file: trans_permission.indexOf(3) !== -1,
  418. pay_show: pay_permission.indexOf(1) !== -1,
  419. pay_file: pay_permission.indexOf(3) !== -1,
  420. };
  421. return permission;
  422. }
  423. }
  424. return subProjPermission;
  425. };