signature_controller.js 6.3 KB

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