signature_controller.js 6.2 KB

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