sub_proj_permission.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. pConst.title = c.name;
  84. }
  85. } else {
  86. p.permission = [];
  87. const pConst = this.PermissionConst[p.key];
  88. if (!pConst) continue;
  89. for (const prop in pConst) {
  90. p.permission.push({ key: prop, ...pConst[prop]});
  91. }
  92. pConst.title = p.name;
  93. }
  94. }
  95. }
  96. get adminPermission () {
  97. return {
  98. budget_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.budget, 'value'),
  99. file_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.file, 'value'),
  100. manage_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.manage, 'value'),
  101. filing_type: 'all',
  102. info_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.info, 'value'),
  103. datacollect_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.datacollect, 'value'),
  104. contract_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.contract, 'value'),
  105. fund_pay_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_pay, 'value'),
  106. fund_trans_permission: this.ctx.helper.mapAllSubField(this.PermissionConst.fund_trans, 'value'),
  107. }
  108. }
  109. async showSubTab(uid, type) {
  110. const sql = `SELECT count(*) as count FROM ${this.tableName} WHERE ${type}_permission <> '' AND uid = ?`;
  111. const result = await this.db.queryOne(sql, [uid]);
  112. return result.count;
  113. }
  114. async showBudget(uid) {
  115. return await this.showSubTab(uid, 'budget');
  116. }
  117. async showFile(uid) {
  118. return await this.showSubTab(uid, 'file');
  119. }
  120. parsePermission(data) {
  121. const _ = this.ctx.helper._;
  122. const datas = data instanceof Array ? data : [data];
  123. datas.forEach(x => {
  124. x.budget_permission = x.budget_permission ? _.map(x.budget_permission.split(','), _.toInteger) : [];
  125. x.file_permission = x.file_permission ? _.map(x.file_permission.split(','), _.toInteger) : [];
  126. x.manage_permission = x.manage_permission ? _.map(x.manage_permission.split(','), _.toInteger) : [];
  127. x.info_permission = x.info_permission ? _.map(x.info_permission.split(','), _.toInteger) : [];
  128. x.datacollect_permission = x.datacollect_permission ? _.map(x.datacollect_permission.split(','), _.toInteger) : [];
  129. x.contract_permission = x.contract_permission ? _.map(x.contract_permission.split(','), _.toInteger) : [];
  130. x.fund_pay_permission = x.fund_pay_permission ? _.map(x.fund_pay_permission.split(','), _.toInteger) : [];
  131. x.fund_trans_permission = x.fund_trans_permission ? _.map(x.fund_trans_permission.split(','), _.toInteger) : [];
  132. x.filing_type = x.filing_type ? _.map(x.filing_type.split(','), _.toInteger): [];
  133. });
  134. }
  135. async getPermission(subProjectId) {
  136. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  137. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  138. On spp.uid = p.id WHERE spp.spid = ?`, [subProjectId]);
  139. this.parsePermission(result);
  140. return result;
  141. }
  142. async getBudgetPermission(subProjectId) {
  143. const result = await this.db.query(`SELECT spp.*, p.name, p.role
  144. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  145. On spp.uid = p.id WHERE spp.spid = ? and budget_permission <> ''`, [subProjectId]);
  146. this.parsePermission(result);
  147. return result;
  148. }
  149. async getUserPermission(pid, uid) {
  150. const result = await this.getAllDataByCondition({
  151. where: { uid: this.ctx.session.sessionUser.accountId, pid: this.ctx.session.sessionProject.id }
  152. });
  153. this.parsePermission(result);
  154. return result;
  155. }
  156. async getBudgetUserPermission(bid) {
  157. const subProj = await this.service.subProject.getDataByCondition({ budget_id: bid });
  158. const result = await this.getDataByCondition({ spid: subProj.id, uid: this.ctx.session.sessionUser.accountId });
  159. if (result) this.parsePermission(result);
  160. return result;
  161. }
  162. async getSubProjectUserPermission(spid, uid) {
  163. const result = await this.getDataByCondition({ spid, uid });
  164. if (result) this.parsePermission(result);
  165. return result;
  166. };
  167. async savePermission(subProjectId, member) {
  168. const orgMember = await this.getAllDataByCondition({ where: { spid: subProjectId } });
  169. const dm = [], um = [], im = [];
  170. for (const om of orgMember) {
  171. const nm = member.find(x => { return om.uid === x.uid; });
  172. if (!nm) {
  173. dm.push(om.id);
  174. } else {
  175. um.push({
  176. id: om.id, budget_permission: nm.budget_permission.join(','),
  177. file_permission: nm.file_permission.join(','),
  178. manage_permission: nm.manage_permission.join(',')
  179. });
  180. member.splice(member.indexOf(nm), 1);
  181. }
  182. }
  183. for (const m of member) {
  184. im.push({
  185. id: this.uuid.v4(),
  186. spid: subProjectId, pid: this.ctx.session.sessionProject.id, uid: m.uid,
  187. budget_permission: m.budget_permission.join(','),
  188. file_permission: m.file_permission.join(','),
  189. manage_permission: m.manage_permission.join(',')
  190. });
  191. }
  192. const conn = await this.db.beginTransaction();
  193. try {
  194. if (dm.length > 0) await conn.delete(this.tableName, { id: dm });
  195. if (um.length > 0) await conn.updateRows(this.tableName, um);
  196. if (im.length > 0) await conn.insert(this.tableName, im);
  197. await conn.commit();
  198. } catch (err) {
  199. await conn.rollback();
  200. throw err;
  201. }
  202. }
  203. async _addUser(subProject, data) {
  204. const ids = data instanceof Array ? data : [data];
  205. const exists = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  206. if (exists.length > 0) throw '请勿重复选择账号';
  207. const insertData = ids.map(x => {
  208. return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
  209. });
  210. await this.db.insert(this.tableName, insertData);
  211. return insertData;
  212. }
  213. async _delUser(subProject, data) {
  214. const ids = data instanceof Array ? data : [data];
  215. const permissions = await this.getAllDataByCondition({ where: { spid: subProject.id, uid: ids } });
  216. await this.db.delete(this.tableName, { id: permissions.map(x => { return x.id; }) });
  217. return data;
  218. }
  219. async _updateUserPermission(data) {
  220. const datas = data instanceof Array ? data : [data];
  221. const updateData = [];
  222. datas.forEach(x => {
  223. const ud = { id: x.id };
  224. for (const p of this.PermissionBlock) {
  225. if (p.children) {
  226. for (const c of p.children) {
  227. if (x[c.field] !== undefined) ud[c.field] = x[c.field] || '';
  228. }
  229. } else {
  230. if (x[p.field] !== undefined) ud[p.field] = x[p.field] || '';
  231. }
  232. }
  233. updateData.push(ud);
  234. });
  235. await this.db.updateRows(this.tableName, updateData);
  236. return updateData;
  237. }
  238. async updatePermission(subProject, data) {
  239. const result = {};
  240. if (data.add) result.add = await this._addUser(subProject, data.add);
  241. if (data.del) result.del = await this._delUser(subProject, data.del);
  242. if (data.update) result.update = await this._updateUserPermission(data.update);
  243. return result;
  244. }
  245. async getFilingType(subProjectId) {
  246. const permissionConst = {}, prefix = 'f';
  247. for (const p in this.PermissionConst.file) {
  248. const fp = this.PermissionConst.file[p];
  249. permissionConst[prefix + fp.value] = fp.title;
  250. }
  251. 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
  252. FROM ${this.tableName} spp LEFT JOIN ${this.ctx.service.projectAccount.tableName} p
  253. On spp.uid = p.id WHERE spp.spid = ? and file_permission <> ''`, [subProjectId]);
  254. result.forEach(x => {
  255. const filePermission = x.file_permission.split(',');
  256. x.file_permission = filePermission.map(x => {
  257. return permissionConst[prefix + x] || '';
  258. }).join(',');
  259. });
  260. return result;
  261. }
  262. // 资料归集,授权固定分类
  263. async saveFilingType(data) {
  264. const updateData = [];
  265. data.forEach(x => {
  266. updateData.push({ id: x.id, filing_type: x.filing_type });
  267. });
  268. if (updateData.length > 0) await this.db.updateRows(this.tableName, updateData);
  269. }
  270. checkViewPermission(ctx) {
  271. const permissionBlock = ctx.service.subProjPermission.PermissionBlock.find(x => { return x.key === ctx.controllerName; });
  272. if (!permissionBlock) return true;
  273. if (permissionBlock.children) {
  274. let canView = false;
  275. for (const c of permissionBlock.children) {
  276. const viewPermission = c.permission.find(x => { return x.key === 'view'; });
  277. if (!viewPermission || ctx.subProject.permission[c.field].indexOf(viewPermission.value) >= 0) canView = true;
  278. }
  279. return canView;
  280. } else {
  281. const viewPermission = permissionBlock.permission.find(x => { return x.key === 'view'; });
  282. if (!viewPermission) return true;
  283. return ctx.subProject.permission[permissionBlock.field].indexOf(viewPermission.value) >= 0;
  284. }
  285. };
  286. async getFinancailPermission(trans_permission, pay_permission) {
  287. const permission = {
  288. transfer_show: trans_permission.indexOf(1) !== -1,
  289. transfer_add: trans_permission.indexOf(2) !== -1,
  290. transfer_file: trans_permission.indexOf(3) !== -1,
  291. pay_show: pay_permission.indexOf(1) !== -1,
  292. pay_file: pay_permission.indexOf(3) !== -1,
  293. };
  294. return permission;
  295. }
  296. }
  297. return subProjPermission;
  298. };