signature_controller.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2019/9/26.
  4. */
  5. const fs = require('fs');
  6. const sourceTypeConst = require('../const/source_type');
  7. module.exports = app => {
  8. class ReportController extends app.BaseController {
  9. /**
  10. * 创建电子签名角色
  11. *
  12. * @param {Object} ctx - egg全局context
  13. * @return {void}
  14. */
  15. async createSignatureRole(ctx) {
  16. const params = JSON.parse(ctx.request.body.params);
  17. // console.log(params);
  18. const rst = await ctx.service.signatureRole.createRole(params.name, params.bind_acc_id, params.prj_id, params.tender_id);
  19. // console.log(rst);
  20. ctx.body = { data: rst };
  21. // ctx.body = { data: { msg: 'test the network' } };
  22. ctx.status = 201;
  23. }
  24. /**
  25. * 创建签名角色关联
  26. *
  27. * @param {Object} ctx - egg全局context
  28. * @return {void}
  29. */
  30. async createRoleRel(ctx) {
  31. const params = JSON.parse(ctx.request.body.params);
  32. // console.log(params);
  33. const rst = await ctx.service.roleRptRel.createRoleRelationship(params.tender_id, params.rpt_id, params.rel_content);
  34. // console.log(rst);
  35. ctx.body = { data: rst };
  36. // ctx.body = { data: { msg: 'test the network' } };
  37. ctx.status = 201;
  38. }
  39. /**
  40. * 更新签名角色关联
  41. *
  42. * @param {Object} ctx - egg全局context
  43. * @return {void}
  44. */
  45. async updateRoleRel(ctx) {
  46. const params = JSON.parse(ctx.request.body.params);
  47. // console.log(params);
  48. const bzId = params.business_id;
  49. const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];
  50. const rst = await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content);
  51. const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_id, params.stage_id);
  52. // const roleRel = params.rel_content;
  53. await encodeSignatureDataUri(roleRel, this.app.baseDir);
  54. // console.log(rst);
  55. ctx.body = { data: rst, signatureRelInfo: roleRel };
  56. // ctx.body = { data: { msg: 'test the network' } };
  57. ctx.status = 201;
  58. }
  59. /**
  60. * 获取多个标段签名角色关联
  61. *
  62. * @param {Object} ctx - egg全局context
  63. * @return {void}
  64. */
  65. async getMultiRoleRptRels(ctx) {
  66. const params = JSON.parse(ctx.request.body.params);
  67. const conParams = params.selectedTenders;
  68. const rst = await ctx.service.roleRptRel.getCrossTenderRoleRptRels(conParams);
  69. ctx.body = { data: rst };
  70. ctx.status = 201;
  71. }
  72. /**
  73. * 跨标段更新签名角色关联
  74. *
  75. * @param {Object} ctx - egg全局context
  76. * @return {void}
  77. */
  78. async updateCrossTendersRoleRelationship(ctx) {
  79. const params = JSON.parse(ctx.request.body.params);
  80. const conParams = params.selectedTenders;
  81. const roleRel = params.rel_content;
  82. const bzId = params.business_id;
  83. const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];
  84. // console.log(params.rel_content);
  85. await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content); // 传过来的数据是额外项目的,还需要单独处理当前的
  86. const rst = await ctx.service.roleRptRel.updateMultiRoleRelationship(conParams, bzId, srcKey, roleRel);
  87. ctx.body = { data: rst };
  88. ctx.status = 201;
  89. }
  90. /**
  91. * 更新最近使用签名
  92. *
  93. * @param {Object} ctx - egg全局context
  94. * @return {void}
  95. */
  96. async updateSignatureUsed(ctx) {
  97. const params = JSON.parse(ctx.request.body.params);
  98. const used_time = ctx.request.body.create_time;
  99. const signused = await ctx.service.signatureUsed.updateUsed(params, used_time);
  100. const signUsedList = await ctx.service.signatureUsed.getSignatureUsedByTenderId(params.tender_id);
  101. // const rst = await ctx.service.signatureRole.createRole(params.name, params.bind_acc_id, params.prj_id, params.tender_id);
  102. // console.log(rst);
  103. ctx.body = { data: signUsedList };
  104. // ctx.body = { data: { msg: 'test the network' } };
  105. ctx.status = 201;
  106. }
  107. }
  108. return ReportController;
  109. };
  110. function isFileExisted(file) {
  111. return new Promise(function(resolve, reject) {
  112. fs.access(file, err => {
  113. if (err) {
  114. reject(false);
  115. } else {
  116. resolve(true);
  117. }
  118. });
  119. });
  120. }
  121. async function encodeSignatureDataUri(roleRel, baseDir) {
  122. if (roleRel) {
  123. for (const singleRoleRel of roleRel) {
  124. if (singleRoleRel.rel_content !== null && singleRoleRel.rel_content !== undefined && singleRoleRel.rel_content !== '') {
  125. const roleRelContent = JSON.parse(singleRoleRel.rel_content);
  126. for (const role of roleRelContent) {
  127. // console.log(role);
  128. if (role.sign_path !== '') {
  129. const filePath = baseDir + '/app' + role.sign_path;
  130. try {
  131. const res = await isFileExisted(filePath);
  132. if (res) {
  133. const bData = fs.readFileSync(filePath);
  134. const base64Str = bData.toString('base64');
  135. const datauri = 'data:image/png;base64,' + base64Str;
  136. role.sign_pic = datauri;
  137. } else {
  138. console.log('文件不存在:' + filePath);
  139. }
  140. } catch (err) {
  141. console.error(err);
  142. }
  143. }
  144. }
  145. singleRoleRel.rel_content = JSON.stringify(roleRelContent);
  146. } else {
  147. singleRoleRel.rel_content = [];
  148. }
  149. }
  150. }
  151. }