report_archive_controller.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. 'use strict';
  2. /**
  3. * Created by Tony on 2021/3/31.
  4. */
  5. const path = require('path');
  6. const uuidV1 = require('uuid').v1;
  7. const fs = require('fs');
  8. const MAX_ARCHIVE = 3;
  9. const tenderMenu = require('../../config/menu').tenderMenu;
  10. const measureType = require('../const/tender').measureType;
  11. const fsUtil = require('../public/js/fsUtil');
  12. const auditConst = require('../const/audit');
  13. const signConst = require('../const/sign');
  14. module.exports = app => {
  15. class ReportArchiveController extends app.BaseController {
  16. async index(ctx) {
  17. const tender = ctx.tender;
  18. const stage = ctx.stage;
  19. let stage_id = -1;
  20. let stage_order = -1;
  21. let stage_times = -1;
  22. let stage_status = -1;
  23. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  24. const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  25. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  26. //
  27. // 。。。
  28. let archiveList = [];
  29. let archiveEncryptionList = [];
  30. // console.log('tender.data.project_id: ' + tender.data.project_id);
  31. if (stage) {
  32. // console.log('ctx.stage.id: ' + ctx.stage.id);
  33. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  34. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, ctx.stage.id);
  35. stage_id = stage.id;
  36. stage_order = stage.order;
  37. stage_times = stage.times;
  38. stage_status = stage.status;
  39. if (archives.length > 0) {
  40. archiveList = JSON.parse(archives[0].content);
  41. }
  42. if (archiveEncryptions.length > 0) {
  43. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  44. }
  45. } else {
  46. // console.log('stageList[0].id: ' + stageList[0].id);
  47. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id);
  48. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, stageList[stageList.length - 1].id);
  49. // stage_id = stageList[0].id;
  50. // stage_order = stageList[0].order;
  51. // stage_times = stageList[0].times;
  52. // stage_status = stageList[0].status;
  53. if (archives && archives.length > 0) {
  54. archiveList = JSON.parse(archives[0].content);
  55. }
  56. if (archiveEncryptions && archiveEncryptions.length > 0) {
  57. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  58. }
  59. }
  60. let rpt_tpl_items = '{ customize: [], common: [] }';
  61. if (custTreeNodes.length > 0) {
  62. rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  63. }
  64. //
  65. const renderData = {
  66. tender: tender.data,
  67. rpt_tpl_data: JSON.stringify(treeNodes),
  68. cust_tpl_data: rpt_tpl_items,
  69. project_id: tender.data.project_id,
  70. tender_id: tender.id,
  71. stg_id: stage_id,
  72. stg_order: stage_order,
  73. stg_times: stage_times,
  74. stg_status: stage_status,
  75. stage_list: JSON.stringify(stageList),
  76. tenderMenu,
  77. measureType,
  78. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  79. stages: stageList,
  80. auditConst: auditConst.stage,
  81. archiveList,
  82. archiveEncryptionList,
  83. can_netcasign: false,
  84. ossPath: signConst.path.oss,
  85. };
  86. await this.layout('report/index_archive.ejs', renderData);
  87. }
  88. async getReportArchive(ctx) {
  89. const params = JSON.parse(ctx.request.body.params);
  90. // ctx.body = await this._getReport(ctx, params);
  91. const archives = await ctx.service.rptArchive.getPrjStgArchive(params.prjId, params.stgId);
  92. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(params.prjId, params.stgId);
  93. let archiveList = [];
  94. let archiveEncryptionList = [];
  95. if (archives.length > 0) {
  96. archiveList = JSON.parse(archives[0].content);
  97. }
  98. if (archiveEncryptions.length > 0) {
  99. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  100. }
  101. const lastAuditor = await ctx.service.stageAudit.getLastestAuditor(params.stgId, params.stgTimes, auditConst.stage.status.checked);
  102. ctx.body = {
  103. data: archiveList,
  104. encryptionData: archiveEncryptionList,
  105. lastAuditor,
  106. };
  107. }
  108. async addReportArchiveEncryption(ctx) {
  109. const params = JSON.parse(ctx.request.body.params);
  110. const prjId = params.prjId;
  111. const stgId = params.stgId;
  112. const rptId = params.rptId;
  113. const ttlPgs = params.ttlPgs;
  114. const content = params.content;
  115. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  116. if (orgArchiveList.length > 0) {
  117. const contentArr = JSON.parse(orgArchiveList[0].content);
  118. let hasArchive = false;
  119. for (const item of contentArr) {
  120. if (item.rpt_id === rptId) {
  121. // 考虑到报表模板的稳定性,只保留一项来记录位置就足够了,都不考虑用uuid了
  122. item.encryption = content;
  123. hasArchive = true;
  124. break;
  125. }
  126. }
  127. if (!hasArchive) {
  128. // 表示有新的要加
  129. contentArr.push({ rpt_id: rptId, total_page: ttlPgs, encryption: content });
  130. } else {
  131. //
  132. }
  133. const updatedRst = await ctx.service.rptArchiveEncryption.updateArchiveEncryption(orgArchiveList[0].id, prjId, stgId, contentArr);
  134. // console.log(updatedRst);
  135. ctx.body = { err: 0, msg: '', data: { addedRst: contentArr } };
  136. } else {
  137. // 需要增加
  138. const archiveArr = [];
  139. archiveArr.push({ rpt_id: rptId, encryption: content });
  140. const addedRst = await ctx.service.rptArchiveEncryption.createArchiveEncryption(prjId, stgId, archiveArr);
  141. // console.log(addedRst);
  142. ctx.body = { err: 0, msg: '', data: { addedRst: archiveArr } };
  143. }
  144. }
  145. async addReportArchive(ctx) {
  146. try {
  147. const stream = await ctx.getFileStream();
  148. const prjId = ctx.params.prjId;
  149. const stgId = ctx.params.stgId;
  150. const rptId = ctx.params.rptId;
  151. const newName = uuidV1();
  152. const fileName = newName + '.PDF';
  153. // console.log('adding fileName: ' + fileName);
  154. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  155. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  156. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  157. throw '上传文件失败';
  158. }
  159. // const url = await ctx.oss.delete('archive/52d3e7f0-c7fb-11eb-b8c2-51b890b95d23.PDF');
  160. // console.log(url);
  161. // const flag = true;
  162. // if (flag) {
  163. // throw 'ok';
  164. // }
  165. const updateDate = new Date();
  166. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  167. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  168. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  169. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  170. if (orgArchiveList.length > 0) {
  171. const contentArr = JSON.parse(orgArchiveList[0].content);
  172. let hasArchive = false;
  173. for (const item of contentArr) {
  174. if (item.rpt_id === rptId) {
  175. hasArchive = true;
  176. if (item.items.length >= MAX_ARCHIVE) {
  177. // 超出界限,需要删除时间最旧的那个
  178. let rmIdx = 0;
  179. for (let idx = 1; idx < item.items.length; idx++) {
  180. if (item.items[rmIdx].updateDate_time < item.items[idx].updateDate_time) {
  181. rmIdx = idx;
  182. }
  183. }
  184. // 同时删除oss文件
  185. await ctx.oss.delete('archive/' + item.items[rmIdx].uuid + '.PDF');
  186. item.items.splice(rmIdx, 1);
  187. }
  188. const newItem = { uuid: newName, updateDate_time: dtStr };
  189. item.items.push(newItem);
  190. break;
  191. }
  192. }
  193. if (!hasArchive) {
  194. // 表示有新的模板需要添加
  195. contentArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  196. }
  197. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  198. // console.log(updatedRst);
  199. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: contentArr } };
  200. } else {
  201. // 需要增加
  202. const archiveArr = [];
  203. archiveArr.push({ rpt_id: rptId, items: [{ uuid: newName, updateDate_time: dtStr }] });
  204. const addedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  205. // console.log(addedRst);
  206. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, addedRst: archiveArr } };
  207. }
  208. } catch (err) {
  209. this.log(err);
  210. ctx.body = { err: 1, msg: err.toString(), data: null };
  211. }
  212. }
  213. async updateReportArchiveEncryption(ctx) {
  214. // 在add方法中已经处理
  215. await this.addReportArchiveEncryption(ctx);
  216. }
  217. async updateReportArchive(ctx) {
  218. try {
  219. const stream = await ctx.getFileStream();
  220. const prjId = ctx.params.prjId;
  221. const stgId = ctx.params.stgId;
  222. const rptId = ctx.params.rptId;
  223. const orgName = ctx.params.orgName;
  224. const fileName = orgName + '.PDF';
  225. console.log('updating fileName: ' + fileName);
  226. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  227. const oss_result = await ctx.oss.put('archive/' + fileName, stream);
  228. if (!(oss_result && oss_result.url && oss_result.res.status === 200)) {
  229. throw '上传文件失败';
  230. }
  231. const updateDate = new Date();
  232. const montStr = (updateDate.getMonth() + 1) < 10 ? ('0' + (updateDate.getMonth() + 1)) : ((updateDate.getMonth() + 1));
  233. const dateStr = (updateDate.getDate()) < 10 ? ('0' + (updateDate.getDate())) : ((updateDate.getDate()));
  234. const dtStr = updateDate.getFullYear() + '-' + montStr + '-' + dateStr;
  235. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  236. if (orgArchiveList.length > 0) {
  237. const contentArr = JSON.parse(orgArchiveList[0].content);
  238. for (const item of contentArr) {
  239. if (item.rpt_id === rptId) {
  240. if (item.items && item.items.length > 0) {
  241. for (const subItem of item.items) {
  242. if (subItem.uuid === orgName) {
  243. subItem.updateDate_time = dtStr;
  244. break;
  245. }
  246. }
  247. } else {
  248. item.items = [{ uuid: orgName, updateDate_time: dtStr }];
  249. }
  250. break;
  251. }
  252. }
  253. const updatedRst = await ctx.service.rptArchive.updateArchive(orgArchiveList[0].id, prjId, stgId, contentArr);
  254. // console.log(updatedRst);
  255. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: contentArr } };
  256. } else {
  257. // 需要增加
  258. const archiveArr = [];
  259. archiveArr.push({ rpt_id: rptId, items: [{ uuid: orgName, updateDate_time: dtStr }] });
  260. const updatedRst = await ctx.service.rptArchive.createArchive(prjId, stgId, archiveArr);
  261. // console.log(updatedRst);
  262. ctx.body = { err: 0, msg: '', data: { fileName, updateDate, updatedRst: archiveArr } };
  263. }
  264. } catch (err) {
  265. this.log(err);
  266. ctx.body = { err: 1, msg: err.toString(), data: null };
  267. }
  268. }
  269. async removeReportArchiveEncryption(ctx) {
  270. try {
  271. const prjId = ctx.params.prjId;
  272. const stgId = ctx.params.stgId;
  273. const rptId = ctx.params.rptId;
  274. const orgArchiveList = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(prjId, stgId);
  275. if (orgArchiveList.length > 0) {
  276. const contentArr = JSON.parse(orgArchiveList[0].content);
  277. for (let idx = 0; idx < contentArr.length; idx++) {
  278. if (contentArr[idx].rpt_id === rptId) {
  279. contentArr.splice(idx, 1);
  280. break;
  281. }
  282. }
  283. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  284. ctx.body = { err: 0, msg: '', data: { updatedRst } };
  285. } else {
  286. ctx.body = { err: 0, msg: '', data: { updatedRst: null } };
  287. }
  288. } catch (err) {
  289. this.log(err);
  290. ctx.body = { err: 1, msg: err.toString(), data: null };
  291. }
  292. }
  293. async removeReportArchive(ctx) {
  294. try {
  295. const prjId = ctx.params.prjId;
  296. const stgId = ctx.params.stgId;
  297. const rptId = ctx.params.rptId;
  298. const orgName = ctx.params.orgName;
  299. const fileName = orgName + '.PDF';
  300. console.log('removing fileName: ' + fileName);
  301. const fullName = path.join(this.app.baseDir, 'app', 'public/archive', fileName);
  302. // await ctx.helper.saveStreamFile(stream, path.join(this.app.baseDir, 'app', 'public/archive', fileName));
  303. // fs.stat(fullName, function(err, data) {
  304. // if (err) {
  305. // console.log(err);
  306. // } else {
  307. // fs.unlink(fullName, function(err) {
  308. // if (err) {
  309. // console.log(err);
  310. // }
  311. // });
  312. // }
  313. // });
  314. const oss_result = await ctx.oss.delete('archive/' + fileName);
  315. if (!(oss_result && oss_result.res.status === 204)) {
  316. throw '删除归档文件失败';
  317. }
  318. const orgArchiveList = await ctx.service.rptArchive.getPrjStgArchive(prjId, stgId);
  319. if (orgArchiveList.length > 0) {
  320. const contentArr = JSON.parse(orgArchiveList[0].content);
  321. for (const item of contentArr) {
  322. if (item.rpt_id === rptId) {
  323. if (item.items && item.items.length > 0) {
  324. for (const subIdx in item.items) {
  325. if (item.items[subIdx].uuid === orgName) {
  326. item.items.splice(subIdx, 1);
  327. break;
  328. }
  329. }
  330. }
  331. break;
  332. }
  333. }
  334. const updatedRst = await ctx.service.rptArchive.updateArchive(prjId, stgId, contentArr);
  335. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst } };
  336. } else {
  337. ctx.body = { err: 0, msg: '', data: { fileName, updatedRst: null } };
  338. }
  339. } catch (err) {
  340. this.log(err);
  341. ctx.body = { err: 1, msg: err.toString(), data: null };
  342. }
  343. }
  344. async addMultiReportArchive(ctx, params) {
  345. // 暂时不支持
  346. }
  347. async getArchivedFileByUUID(ctx) {
  348. // console.log('downloading : ' + ctx.params.uuid);
  349. const uuid = ctx.params.uuid;
  350. const rptName = ctx.params.rptName;
  351. const suffix = '.PDF';
  352. try {
  353. const rptNameURI = encodeURI(rptName);
  354. const filePath = this.app.baseDir + '/app/public/archive/';
  355. // console.log('filePath: ' + filePath);
  356. // await this.ctx.helper.recursiveMkdirSync(this.app.baseDir + '/app/public/download');
  357. ctx.set({
  358. 'Content-Type': 'application/vnd.openxmlformats',
  359. 'Content-Disposition': 'attachment; filename="' + rptNameURI + suffix + "\"; filename*=utf-8''" + rptNameURI + suffix,
  360. });
  361. ctx.body = await fs.readFileSync(filePath + uuid + suffix);
  362. } catch (e) {
  363. console.log(e);
  364. }
  365. }
  366. async pdfShow(ctx) {
  367. // const renderData = {
  368. // can_netcasign: ctx.session.sessionProject.page_show.openNetCaSign === 1,
  369. // };
  370. await ctx.render('report/archive_pdf.ejs');
  371. }
  372. /*
  373. * 网证通电子签名页,(和归档报表页高度相似)
  374. */
  375. async signReport(ctx) {
  376. const tender = ctx.tender;
  377. const stage = ctx.stage;
  378. let stage_id = -1;
  379. let stage_order = -1;
  380. let stage_times = -1;
  381. let stage_status = -1;
  382. const treeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1, tender.data.project_id]);
  383. const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
  384. const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
  385. //
  386. // 。。。
  387. let archiveList = [];
  388. let archiveEncryptionList = [];
  389. // console.log('tender.data.project_id: ' + tender.data.project_id);
  390. if (stage) {
  391. // console.log('ctx.stage.id: ' + ctx.stage.id);
  392. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, ctx.stage.id);
  393. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, ctx.stage.id);
  394. stage_id = stage.id;
  395. stage_order = stage.order;
  396. stage_times = stage.times;
  397. stage_status = stage.status;
  398. if (archives.length > 0) {
  399. archiveList = JSON.parse(archives[0].content);
  400. }
  401. if (archiveEncryptions.length > 0) {
  402. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  403. }
  404. } else {
  405. // console.log('stageList[0].id: ' + stageList[0].id);
  406. const archives = await ctx.service.rptArchive.getPrjStgArchive(tender.data.project_id, stageList[stageList.length - 1].id);
  407. const archiveEncryptions = await ctx.service.rptArchiveEncryption.getPrjStgArchiveEncryption(tender.data.project_id, stageList[stageList.length - 1].id);
  408. // stage_id = stageList[0].id;
  409. // stage_order = stageList[0].order;
  410. // stage_times = stageList[0].times;
  411. // stage_status = stageList[0].status;
  412. if (archives && archives.length > 0) {
  413. archiveList = JSON.parse(archives[0].content);
  414. }
  415. if (archiveEncryptions && archiveEncryptions.length > 0) {
  416. archiveEncryptionList = JSON.parse(archiveEncryptions[0].content);
  417. }
  418. }
  419. let rpt_tpl_items = '{ customize: [], common: [] }';
  420. if (custTreeNodes.length > 0) {
  421. rpt_tpl_items = custTreeNodes[0].rpt_tpl_items;
  422. }
  423. // 获取ukey绑定数据
  424. const netcaSignData = await ctx.service.netcasign.getDataByCondition({ uid: ctx.session.sessionUser.accountId });
  425. // 获取已签名数据
  426. const signLogList = await ctx.service.netcasignLog.getLogList(ctx.tender.id);
  427. const renderData = {
  428. tender: tender.data,
  429. rpt_tpl_data: JSON.stringify(treeNodes),
  430. cust_tpl_data: rpt_tpl_items,
  431. project_id: tender.data.project_id,
  432. tender_id: tender.id,
  433. stg_id: stage_id,
  434. stg_order: stage_order,
  435. stg_times: stage_times,
  436. stg_status: stage_status,
  437. stage_list: JSON.stringify(stageList),
  438. tenderMenu,
  439. measureType,
  440. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.report.main),
  441. stages: stageList,
  442. auditConst: auditConst.stage,
  443. archiveList,
  444. archiveEncryptionList,
  445. netcaSignData,
  446. can_netcasign: ctx.session.sessionProject.page_show.openNetCaSign === 1,
  447. ossPath: signConst.path.oss,
  448. signLogList,
  449. };
  450. await this.layout('report/index_sign.ejs', renderData, 'report/index_sign_modal.ejs');
  451. }
  452. /**
  453. * 网证通电子签名接口
  454. *
  455. * @param {object} ctx - egg全局变量
  456. * @return {void}
  457. */
  458. async signPost(ctx) {
  459. const response = {
  460. err: 0,
  461. msg: '',
  462. };
  463. try {
  464. const data = JSON.parse(ctx.request.body.data);
  465. let signData;
  466. const netcaSignApi = signConst.path.api;
  467. switch (data.type) {
  468. case 'pdfIsExist':
  469. const pdfMsg = await ctx.oss.head('archive/sign/' + data.uuid + '.PDF');
  470. response.data = pdfMsg.res.status === 200;
  471. break;
  472. // 获取摘要值
  473. case 'assemblyDigest':
  474. const postData = {
  475. requestJson: JSON.stringify(data.requestJson),
  476. };
  477. const result = await ctx.helper.sendMoreRequest(netcaSignApi + '/assemblyDigest', postData, 'POST');
  478. response.data = result;
  479. break;
  480. // 生成签名pdf
  481. case 'assemblyPdf':
  482. const postData2 = {
  483. requestJson: JSON.stringify(data.requestJson),
  484. };
  485. const result3 = await this.roundNetcaSign(ctx, postData2);
  486. // const result2 = await ctx.helper.sendMoreRequest(netcaSignApi + '/assemblyPdf', postData2, 'POST');
  487. // 上传到oss
  488. // console.log(result3);
  489. if (result3.code === 0) {
  490. // const result3 = await ctx.helper.sendMoreRequest(netcaSignApi + result2.data);
  491. const oss_reuslt = await ctx.oss.put('archive/sign/' + data.requestJson.fileName + '.PDF', result3.data);
  492. if (oss_reuslt && oss_reuslt.res && oss_reuslt.res.status === 200) {
  493. if (data.end) {
  494. const versionId = oss_reuslt.res.headers['x-oss-version-id'];
  495. // 记录签名和保存
  496. await ctx.service.netcasignLog.add(data.requestJson.fileName, data.role, ctx.session.sessionUser.accountId, versionId);
  497. const signLogList = await ctx.service.netcasignLog.getLogList(ctx.tender.id);
  498. response.data = signLogList;
  499. } else {
  500. // next page
  501. }
  502. } else {
  503. throw '上传文件失败';
  504. }
  505. } else {
  506. throw result3.msg;
  507. }
  508. break;
  509. // 移除签名和已签移除pdf
  510. case 'removeSign':
  511. const oss_reuslt = await ctx.oss.delete('archive/sign/' + data.uuid + '.PDF');
  512. if (oss_reuslt && oss_reuslt.res && oss_reuslt.res.status === 204) {
  513. const delSign_result = await ctx.service.netcasignLog.removeSign(ctx.session.sessionUser.accountId, data.uuid);
  514. const signLogList = await ctx.service.netcasignLog.getLogList(ctx.tender.id);
  515. response.data = signLogList;
  516. } else {
  517. throw '删除文件失败';
  518. }
  519. break;
  520. default:throw '参数有误';
  521. }
  522. } catch (error) {
  523. response.err = 1;
  524. response.msg = error.toString();
  525. const data = JSON.parse(ctx.request.body.data);
  526. if (data && data.type === 'pdfIsExist') {
  527. response.err = 0;
  528. response.data = false;
  529. }
  530. }
  531. ctx.body = response;
  532. }
  533. async roundNetcaSign(ctx, postData2, round = 3) {
  534. let response = {
  535. code: 0,
  536. data: '',
  537. };
  538. // 无法获取到result3,因为result2生成的pdf已损坏,请重复获取result2,直到获取成功或尝试3次失败后报错为止
  539. try {
  540. const netcaSignApi = signConst.path.api;
  541. const result2 = await ctx.helper.sendMoreRequest(netcaSignApi + '/assemblyPdf', postData2, 'POST');
  542. if (result2.code === 0) {
  543. const result3 = await ctx.curl(netcaSignApi + result2.data, {
  544. timeout: 300000, // 超时 5分钟中
  545. });
  546. if (result3) {
  547. response.data = result3.data;
  548. } else {
  549. if (round > 0) {
  550. round = round - 1;
  551. response = await this.roundNetcaSign(ctx, postData2, round);
  552. } else {
  553. throw 'pdf获取失败,网证通接口无法生成pdf';
  554. }
  555. }
  556. }
  557. } catch (error) {
  558. response.code = 1;
  559. response.msg = error;
  560. }
  561. return response;
  562. }
  563. }
  564. return ReportArchiveController;
  565. };