report_archive_controller.js 28 KB

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