payment_controller.js 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. 'use strict';
  2. const accountGroup = require('../const/account_group').group;
  3. const JV = require('../reports/rpt_component/jpc_value_define');
  4. const shenpiConst = require('../const/shenpi');
  5. const auditConst = require('../const/audit').stage;
  6. const paymentConst = require('../const/payment');
  7. const moment = require('moment');
  8. const path = require('path');
  9. const sendToWormhole = require('stream-wormhole');
  10. const fs = require('fs');
  11. module.exports = app => {
  12. class PaymentController extends app.BaseController {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. ctx.showProject = true;
  22. // ctx.showTitle = true;
  23. }
  24. /**
  25. * 支付审批列表页
  26. *
  27. * @param {Object} ctx - egg全局页面
  28. * @return {void}
  29. */
  30. async index(ctx) {
  31. try {
  32. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  33. if (!auditPermission) {
  34. throw '权限不足';
  35. }
  36. // 列表读取及目录读取
  37. const renderData = {
  38. auditPermission,
  39. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.index),
  40. };
  41. if (ctx.session.sessionUser.is_admin) {
  42. const projectId = ctx.session.sessionProject.id;
  43. const permissionAudits = await ctx.service.paymentPermissionAudit.getList(projectId);
  44. // 获取所有项目参与者
  45. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  46. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  47. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  48. });
  49. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: projectId } });
  50. const accountGroupList = unitList.map(item => {
  51. const groupList = accountList.filter(item1 => item1.company === item.name);
  52. return { groupName: item.name, groupList };
  53. });
  54. // const accountGroupList = accountGroup.map((item, idx) => {
  55. // const groupList = accountList.filter(item => item.account_group === idx);
  56. // return { groupName: item, groupList };
  57. // });
  58. renderData.permissionAudits = permissionAudits;
  59. renderData.accountList = accountList;
  60. renderData.accountGroup = accountGroupList;
  61. }
  62. await this.layout('payment/index.ejs', renderData, 'payment/modal.ejs');
  63. } catch (err) {
  64. console.log(err);
  65. this.log(err);
  66. ctx.session.postError = err.toString();
  67. ctx.redirect(this.menu.menu.dashboard.url);
  68. }
  69. }
  70. async listLoad(ctx) {
  71. const responseData = {
  72. err: 0, msg: '', data: {},
  73. };
  74. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  75. if (!auditPermission) {
  76. throw '权限不足';
  77. }
  78. // 先获取你创建的标段及参与的标段
  79. const tenderList = await ctx.service.paymentTender.getList(ctx.session.sessionUser.accountId, auditPermission);
  80. // 获取你创建的目录及对应目录下的所有目录
  81. const folderList = await ctx.service.paymentFolder.getList(ctx.session.sessionUser.accountId, tenderList, auditPermission);
  82. if (tenderList.length > 0) {
  83. for (const t of tenderList) {
  84. t.have_notice = await ctx.service.paymentDetail.haveNotice2Tender(t.id, ctx.session.sessionUser.accountId);
  85. t.have_detail = await ctx.service.paymentDetail.haveDetail2Tender(t.id);
  86. }
  87. }
  88. responseData.data.folderList = ctx.helper._.orderBy(folderList, ['in_time'], ['asc']);
  89. responseData.data.tenderList = ctx.helper._.orderBy(tenderList, ['in_time'], ['asc']);
  90. ctx.body = responseData;
  91. }
  92. async permissionSave(ctx) {
  93. try {
  94. if (ctx.session.sessionUser.is_admin === 0) throw '没有设置权限';
  95. const projectId = ctx.session.sessionProject.id;
  96. const responseData = {
  97. err: 0, msg: '', data: null,
  98. };
  99. const data = JSON.parse(ctx.request.body.data);
  100. if (!data.type) {
  101. throw '提交数据错误';
  102. }
  103. let uids;
  104. let result = false;
  105. let auditList = [];
  106. switch (data.type) {
  107. case 'add-audit':
  108. // 判断用户是单个还是数组
  109. uids = data.id instanceof Array ? data.id : [data.id];
  110. // 判断该用户的组是否已加入到表中,已加入则提示无需添加
  111. auditList = await ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { pid: projectId, uid: uids } });
  112. const addAidList = ctx.helper._.difference(uids, ctx.helper._.map(auditList, 'uid'));
  113. if (addAidList.length === 0) {
  114. throw '用户已存在权限中,无需重复添加';
  115. }
  116. const accountList = await ctx.service.projectAccount.getAllDataByCondition({ where: { id: addAidList } });
  117. await ctx.service.paymentPermissionAudit.saveAudits(projectId, accountList);
  118. responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  119. break;
  120. case 'del-audit':
  121. uids = data.id instanceof Array ? data.id : [data.id];
  122. auditList = await ctx.service.paymentPermissionAudit.getAllDataByCondition({ where: { id: uids } });
  123. if (auditList.length !== uids.length) {
  124. throw '该用户已不存在权限中,移除失败';
  125. }
  126. await ctx.service.paymentPermissionAudit.delAudit(uids);
  127. responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  128. break;
  129. case 'save-permission-one':
  130. result = await ctx.service.paymentPermissionAudit.updateOnePermission(data.updateData);
  131. if (!result) {
  132. throw '修改权限失败';
  133. }
  134. break;
  135. // case 'save-permission-all':
  136. // result = await ctx.service.paymentPermissionAudit.updateAllPermission(projectId, data.permission_type, data.value);
  137. // if (!result) {
  138. // throw '修改权限失败';
  139. // }
  140. // responseData.data = await ctx.service.paymentPermissionAudit.getList(projectId);
  141. // break;
  142. default: throw '参数有误';
  143. }
  144. ctx.body = responseData;
  145. } catch (err) {
  146. this.log(err);
  147. ctx.body = { err: 1, msg: err.toString(), data: null };
  148. }
  149. }
  150. async save(ctx) {
  151. try {
  152. const projectId = ctx.session.sessionProject.id;
  153. const auditPermission = await this.ctx.service.paymentPermissionAudit.getOnePermission(ctx.session.sessionUser.is_admin, ctx.session.sessionUser.accountId);
  154. if (!auditPermission) {
  155. throw '权限不足';
  156. }
  157. const responseData = {
  158. err: 0, msg: '', data: {},
  159. };
  160. const data = JSON.parse(ctx.request.body.data);
  161. if (!data.type) {
  162. throw '提交数据错误';
  163. }
  164. if (!auditPermission.admin) {
  165. throw '您没有权限操作此功能';
  166. }
  167. let type = '';
  168. switch (data.type) {
  169. case 'add-folder':
  170. await ctx.service.paymentFolder.addFolder(projectId, ctx.session.sessionUser.accountId, data.parentId, data.name);
  171. break;
  172. case 'add-tender':
  173. await ctx.service.paymentTender.addTender(projectId, ctx.session.sessionUser.accountId, data.folderId, data.name);
  174. break;
  175. case 'edit-name':
  176. type = data.postData.type;
  177. const updateData = {
  178. name: data.postData.name,
  179. };
  180. const conditionData = {
  181. id: data.postData.id,
  182. };
  183. if (type === 'tender') {
  184. const tenderInfo = await ctx.service.paymentTender.getDataById(conditionData.id);
  185. if (!tenderInfo) throw '标段不存在';
  186. if (tenderInfo.uid !== ctx.session.sessionUser.accountId && !ctx.session.sessionUser.is_admin) {
  187. throw '您没有权限重命名此标段';
  188. }
  189. await ctx.service.paymentTender.update(updateData, conditionData);
  190. } else {
  191. const folderInfo = await ctx.service.paymentFolder.getDataById(conditionData.id);
  192. if (!folderInfo) throw '文件夹不存在';
  193. if (folderInfo.uid !== ctx.session.sessionUser.accountId && !ctx.session.sessionUser.is_admin) {
  194. throw '您没有权限重命名此文件夹';
  195. }
  196. await ctx.service.paymentFolder.update(updateData, conditionData);
  197. }
  198. break;
  199. case 'del':
  200. type = data.postData.type;
  201. if (type === 'tender') {
  202. await ctx.service.paymentTender.deleteTender(data.postData.id);
  203. } else {
  204. await ctx.service.paymentFolder.deleteFolder(data.postData.id);
  205. }
  206. break;
  207. case 'mode-setting':
  208. if (!ctx.session.sessionUser.is_admin) {
  209. throw '您无权操作';
  210. }
  211. const projectInfo = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  212. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : ctx.helper._.cloneDeep(paymentConst.setting_modes);
  213. const checked = data.checked;
  214. if (modes[data.mode_type]) {
  215. const detailCount = await ctx.service.paymentDetail.getCountByPidType(ctx.session.sessionProject.id, modes[data.mode_type].value);
  216. if (detailCount > 0 && !checked) {
  217. throw '已存在对应模块的详情,无法关闭该模块';
  218. }
  219. const mode = modes[data.mode_type];
  220. if (mode.checked === checked) {
  221. throw '已选中该模块,无须重复选中';
  222. }
  223. mode.checked = checked;
  224. } else if (paymentConst.setting_modes[data.mode_type]) {
  225. modes[data.mode_type] = ctx.helper._.cloneDeep(paymentConst.setting_modes[data.mode_type]);
  226. modes[data.mode_type].checked = checked;
  227. } else {
  228. throw '该模块不存在';
  229. }
  230. await ctx.service.project.defaultUpdate({ id: ctx.session.sessionProject.id, payment_setting: JSON.stringify(modes) });
  231. break;
  232. default: throw '参数有误';
  233. }
  234. // 先获取你创建的标段及参与的标段
  235. const tenderList = await ctx.service.paymentTender.getList(ctx.session.sessionUser.accountId, auditPermission);
  236. // 获取你创建的目录及对应目录下的所有目录
  237. const folderList = await ctx.service.paymentFolder.getList(ctx.session.sessionUser.accountId, tenderList, auditPermission);
  238. responseData.data.folderList = ctx.helper._.orderBy(folderList, ['in_time'], ['asc']);
  239. responseData.data.tenderList = ctx.helper._.orderBy(tenderList, ['in_time'], ['asc']);
  240. ctx.body = responseData;
  241. } catch (err) {
  242. this.log(err);
  243. ctx.body = { err: 1, msg: err.toString(), data: null };
  244. }
  245. }
  246. async setting(ctx) {
  247. try {
  248. if (!ctx.session.sessionUser.is_admin) {
  249. throw '您无权打开此页';
  250. }
  251. const projectInfo = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  252. const modes = projectInfo.payment_setting ? JSON.parse(projectInfo.payment_setting) : ctx.helper._.cloneDeep(paymentConst.setting_modes);
  253. for (const m in modes) {
  254. const detailCount = await ctx.service.paymentDetail.getCountByPidType(ctx.session.sessionProject.id, modes[m].value);
  255. modes[m].can_check = !detailCount && modes[m].value !== 1;
  256. }
  257. const renderData = {
  258. setting_modes: paymentConst.setting_modes,
  259. modes,
  260. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.setting),
  261. };
  262. await this.layout('payment/setting.ejs', renderData);
  263. } catch (err) {
  264. console.log(err);
  265. this.log(err);
  266. ctx.session.postError = err.toString();
  267. ctx.redirect('/payment');
  268. }
  269. }
  270. /**
  271. * 获取审批界面所需的 原报、审批人数据等
  272. * @param ctx
  273. * @return {Promise<void>}
  274. * @private
  275. */
  276. async _getDetailAuditViewData(ctx) {
  277. const times = ctx.detail.status === auditConst.status.checkNo ? ctx.detail.times - 1 : ctx.detail.times;
  278. ctx.detail.user = await ctx.service.projectAccount.getAccountInfoById(ctx.detail.uid);
  279. ctx.detail.auditHistory = [];
  280. if (times >= 1) {
  281. for (let i = 1; i <= times; i++) {
  282. ctx.detail.auditHistory.push(await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, i));
  283. }
  284. }
  285. // 获取审批流程中左边列表
  286. ctx.detail.auditors2 = ctx.detail.status === auditConst.status.checkNo && ctx.detail.uid !== ctx.session.sessionUser.accountId ?
  287. await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, times) :
  288. await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  289. if (ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) {
  290. ctx.detail.auditorList = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  291. }
  292. // 是否已验证手机短信
  293. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  294. ctx.detail.authMobile = pa.auth_mobile;
  295. }
  296. /**
  297. * 支付表单页面
  298. *
  299. * @param {Object} ctx - egg全局页面
  300. * @return {void}
  301. */
  302. async detail(ctx) {
  303. try {
  304. await this._getDetailAuditViewData(ctx);
  305. // 报表信息实时更新
  306. if (ctx.trInfo.uid === ctx.session.sessionUser.accountId && ctx.trInfo.is_del === 0 && ctx.trInfo.rpt_id &&
  307. (ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo)) {
  308. const rptTpl = await ctx.service.rptTpl.getDataById(ctx.trInfo.rpt_id);
  309. if (rptTpl) {
  310. const pageRst = await ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  311. const rptMsg = pageRst.items[0];
  312. // 判断与原有的报表审批人列表是否有区别
  313. let difference = false;
  314. let auditDifference = false;
  315. let needChange = false;
  316. if (ctx.trInfo.report_items_json) {
  317. const report_items_json = JSON.parse(ctx.trInfo.report_items_json);
  318. const items = ['cells', 'signature_audit_cells', 'signature_cells', 'signature_date_cells', 'interact_cells'];
  319. for (const item of items) {
  320. if (report_items_json[item] &&
  321. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(rptMsg[item])), report_items_json[item], ctx.helper._.isEqual))) {
  322. // 因为interact_cells里存在undefind值,必须先用JSON.parse和JSON.stringify转义才能对比
  323. difference = true;
  324. needChange = true;
  325. if (item === 'signature_cells') {
  326. auditDifference = true;
  327. }
  328. // break;
  329. }
  330. }
  331. } else {
  332. difference = true;
  333. }
  334. if (difference) {
  335. // 删除rpt_audit重新配置
  336. const updateData = {
  337. id: ctx.trInfo.id,
  338. report_items_json: JSON.stringify(ctx.helper._.cloneDeep(rptMsg)),
  339. };
  340. // 删除rpt_audit重新配置
  341. if (auditDifference) {
  342. updateData.rpt_audit = null;
  343. ctx.trInfo.rpt_audit = null;
  344. }
  345. if (needChange) {
  346. updateData.is_change = 1;
  347. ctx.trInfo.is_change = 1;
  348. }
  349. await ctx.service.paymentTenderRpt.defaultUpdate(updateData);
  350. ctx.trInfo.report_items_json = rptMsg;
  351. }
  352. }
  353. }
  354. const auditIdList = ctx.helper._.map(ctx.detail.auditors, 'aid');
  355. const rptAuditIdList = ctx.helper._.map(ctx.detail.rptAudits, 'uid');
  356. const uidList = ctx.helper._.uniq([...auditIdList, ...rptAuditIdList]);
  357. // 获取附件列表
  358. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.detail.id, 'desc');
  359. const renderData = {
  360. trInfo: ctx.trInfo,
  361. paymentConst,
  362. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.detail),
  363. auditConst,
  364. shenpiConst,
  365. attList,
  366. moment,
  367. whiteList: ctx.app.config.multipart.whitelist,
  368. uidList,
  369. preUrl: '/payment/' + ctx.tender.id + '/detail/' + ctx.detail.id,
  370. OSS_PATH: ctx.app.config.fujianOssPath,
  371. };
  372. renderData.nextDetail = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.trInfo.id, order: ctx.detail.order + 1 });
  373. let report_json = JSON.parse(ctx.detail.report_json);
  374. const content = [];
  375. // 获取当前报表人
  376. const rptAudit = await ctx.service.paymentRptAudit.getDataByCondition({ td_id: ctx.detail.id, uid: ctx.session.sessionUser.accountId });
  377. if (report_json.items[0].interact_cells.length > 0) {
  378. for (const [i, cell] of report_json.items[0].interact_cells.entries()) {
  379. cell.index = i;
  380. }
  381. const numberList = ctx.helper._.filter(report_json.items[0].interact_cells, { DataType: 'intact_type_number' });
  382. for (const [i, cell] of report_json.items[0].interact_cells.entries()) {
  383. if (cell.Label.indexOf('大写') !== -1 && cell.link === undefined) {
  384. if (numberList.length > 0) {
  385. const numberInfo = ctx.helper._.find(ctx.helper._.orderBy(numberList, ['index'], ['desc']), function(item) {
  386. return item.index < i;
  387. });
  388. cell.link = numberInfo ? numberInfo.index : '';
  389. } else {
  390. cell.link = '';
  391. }
  392. }
  393. const push_item = {
  394. type: paymentConst.rpt_dataType[cell.DataType],
  395. value: cell.Prefix ? ctx.helper._.replace(cell.Value, cell.Prefix, '') : cell.Value,
  396. label: cell.Label,
  397. index: i,
  398. };
  399. if (cell.link !== undefined) push_item.link = cell.link;
  400. const thisBandName = paymentConst.rpt_band_name[cell.BandName] ? cell.BandName : 'content';
  401. const oneShowContent = ctx.helper._.find(content, { BandName: thisBandName });
  402. if (oneShowContent) {
  403. oneShowContent.items.push(push_item);
  404. } else {
  405. content.push({
  406. BandName: thisBandName,
  407. title: paymentConst.rpt_band_name[thisBandName],
  408. items: [push_item],
  409. });
  410. }
  411. }
  412. renderData.numberList = numberList;
  413. if (rptAudit && ((ctx.detail.status !== auditConst.status.checkNo && ctx.detail.status !== auditConst.status.checked) ||
  414. (ctx.detail.status === auditConst.status.checked && !renderData.nextDetail) ||
  415. (ctx.detail.status === auditConst.status.checkNo && ctx.detail.uid === ctx.session.sessionUser.accountId))) {
  416. // 获取个人签字,单位章,个人章地址
  417. const userInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  418. const stampPathList = userInfo.stamp_path ? userInfo.stamp_path.split('!;!') : [];
  419. const companyInfo = userInfo.company ? await ctx.service.constructionUnit.getDataByCondition({ pid: ctx.session.sessionProject.id, name: userInfo.company }) : {};
  420. if (rptAudit.signature_msg) {
  421. const sign_msg = JSON.parse(rptAudit.signature_msg);
  422. report_json = await ctx.service.paymentDetail.signOneSignatureData(report_json, rptAudit.signature_name, sign_msg);
  423. rptAudit.signature_msg = sign_msg;
  424. } else {
  425. rptAudit.signature_msg = paymentConst.signature_msg;
  426. rptAudit.signature_msg.sign_path = userInfo.sign_path ? userInfo.sign_path : '';
  427. }
  428. renderData.signPath = userInfo.sign_path ? userInfo.sign_path : '';
  429. renderData.stampPathList = stampPathList;
  430. renderData.currentStamp = rptAudit && rptAudit.signature_msg.stamp_path ? rptAudit.signature_msg.stamp_path : (stampPathList.length > 0 ? stampPathList[0] : '');
  431. renderData.companyStamp = companyInfo && companyInfo.sign_path ? companyInfo.sign_path : '';
  432. }
  433. }
  434. renderData.rptAudit = rptAudit;
  435. renderData.content = content;
  436. renderData.report_json = report_json;
  437. if ((ctx.detail.status === auditConst.status.uncheck || ctx.detail.status === auditConst.status.checkNo) && ctx.session.sessionUser.accountId === ctx.detail.uid) {
  438. // data.accountGroup = accountGroup;
  439. // 获取所有项目参与者
  440. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  441. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  442. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  443. });
  444. renderData.accountList = accountList;
  445. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  446. const accountGroupList = unitList.map(item => {
  447. const groupList = accountList.filter(item1 => item1.company === item.name);
  448. return { groupName: item.name, groupList };
  449. });
  450. renderData.accountGroup = accountGroupList;
  451. }
  452. await this.layout('payment/detail.ejs', renderData, 'payment/detail_modal.ejs');
  453. } catch (err) {
  454. console.log(err);
  455. this.log(err);
  456. ctx.session.postError = err.toString();
  457. if (ctx.detail.tender_id && ctx.detail.tr_id) {
  458. ctx.redirect('/payment' + ctx.detail.tender_id + '/list/' + ctx.detail.tr_id);
  459. }
  460. ctx.redirect(this.menu.menu.dashboard.url);
  461. }
  462. }
  463. async detailSave(ctx) {
  464. try {
  465. const data = JSON.parse(ctx.request.body.data);
  466. if (!data.type) {
  467. throw '提交数据错误';
  468. }
  469. // 检查权限等
  470. if (ctx.detail.uid !== ctx.session.sessionUser.accountId && data.type !== 'update_sign') {
  471. throw '您无权操作';
  472. }
  473. if (data.type !== 'update_sign' && ctx.detail.status !== auditConst.status.uncheck && ctx.detail.status !== auditConst.status.checkNo) {
  474. throw '您无权操作';
  475. }
  476. const responseData = {
  477. err: 0, msg: '', data: {},
  478. };
  479. switch (data.type) {
  480. case 'update_rpt':
  481. responseData.data = await ctx.service.paymentDetail.updateReportJson(ctx.detail.id, data.report_json);
  482. break;
  483. case 'update_sign':
  484. if (ctx.detail.status === auditConst.status.checked) {
  485. // 判断是否存在下一期,是则无法签章
  486. const detailMsg = await ctx.service.paymentDetail.getDataByCondition({ tr_id: ctx.detail.tr_id, order: ctx.detail.order + 1 });
  487. if (detailMsg) {
  488. throw '您无法操作签字/签章';
  489. }
  490. }
  491. responseData.data = await ctx.service.paymentRptAudit.updateSignatureMsg(ctx.detail.id, ctx.session.sessionUser.accountId, data.signature_msg);
  492. break;
  493. case 'add_audit':
  494. const auditorId = this.app._.toInteger(data.auditorId);
  495. if (isNaN(auditorId) || auditorId <= 0) {
  496. throw '参数错误';
  497. }
  498. ctx.detail.auditorList = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  499. // 检查审核人是否已存在
  500. const exist = this.app._.find(ctx.detail.auditorList, { aid: auditorId });
  501. if (exist) {
  502. throw '该审核人已存在,请勿重复添加';
  503. }
  504. const shenpiInfo = await ctx.service.paymentShenpiAudit.getDataByCondition({ tr_id: ctx.trInfo.id, sp_status: shenpiConst.sp_status.gdzs });
  505. const is_gdzs = shenpiInfo && ctx.trInfo.sp_status === shenpiConst.sp_status.gdzs ? 1 : 0;
  506. const result = await ctx.service.paymentDetailAudit.addAuditor(ctx.detail.id, auditorId, ctx.detail.times, is_gdzs);
  507. if (!result) {
  508. throw '添加审核人失败';
  509. }
  510. responseData.data = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  511. break;
  512. case 'del_audit':
  513. const auditorId2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  514. if (isNaN(auditorId2) || auditorId2 <= 0) {
  515. throw '参数错误';
  516. }
  517. const result2 = await ctx.service.paymentDetailAudit.deleteAuditor(ctx.detail.id, auditorId2, ctx.detail.times);
  518. if (!result2) {
  519. throw '移除审核人失败';
  520. }
  521. responseData.data = await ctx.service.paymentDetailAudit.getAuditors(ctx.detail.id, ctx.detail.times);
  522. break;
  523. case 'follow_rpt_audit':
  524. if (ctx.trInfo.shenpi_status === shenpiConst.sp_status.gdspl) {
  525. throw '当前表单已设置为固定审批流,无法同步';
  526. }
  527. const result3 = await ctx.service.paymentDetailAudit.followAuditByRptAudit(ctx.detail);
  528. if (!result3) {
  529. throw '同步表单角色失败';
  530. }
  531. responseData.data = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(ctx.detail.id, ctx.detail.times);
  532. break;
  533. default: throw '参数有误';
  534. }
  535. ctx.body = responseData;
  536. } catch (err) {
  537. this.log(err);
  538. ctx.body = { err: 1, msg: err.toString(), data: null };
  539. }
  540. }
  541. async deleteDetail(ctx) {
  542. try {
  543. const detail_id = ctx.request.body.detail_id;
  544. const detailInfo = await ctx.service.paymentDetail.getDataById(detail_id);
  545. if (!detailInfo) {
  546. throw '所选报表表单详情已删除';
  547. }
  548. // 获取最新的期数
  549. const detail_highOrder = await ctx.service.paymentDetail.count({
  550. tr_id: detailInfo.tr_id,
  551. });
  552. if (!(ctx.session.sessionUser.is_admin || ((detailInfo.status === auditConst.status.uncheck || detailInfo.status === auditConst.status.checkNo) && detailInfo.uid === ctx.session.sessionUser.accountId)) || detail_highOrder !== detailInfo.order) {
  553. throw '您无权删除所选报表表单详情';
  554. }
  555. const result = await ctx.service.paymentDetail.deleteDetail(detail_id);
  556. if (!result) {
  557. throw '删除报表表单详情失败,请重试';
  558. }
  559. ctx.redirect('/payment/' + ctx.tender.id + '/list/' + detailInfo.tr_id);
  560. } catch (err) {
  561. this.log(err);
  562. ctx.session.postError = err.toString();
  563. ctx.redirect(ctx.request.header.referer);
  564. }
  565. }
  566. /**
  567. * 期审批流程(Get)
  568. * @param ctx
  569. * @return {Promise<void>}
  570. */
  571. async detailAuditors(ctx) {
  572. try {
  573. const responseData = {
  574. err: 0, msg: '', data: {},
  575. };
  576. const order = JSON.parse(ctx.request.body.data).order;
  577. const tr_id = ctx.params.trid;
  578. const detailInfo = await ctx.service.paymentDetail.getDataByCondition({ tr_id, order });
  579. // 获取审批流程中右边列表
  580. const auditHistory = [];
  581. const times = detailInfo.status === auditConst.status.checkNo ? detailInfo.times - 1 : detailInfo.times;
  582. if (times >= 1) {
  583. for (let i = 1; i <= times; i++) {
  584. auditHistory.push(await ctx.service.paymentDetailAudit.getAuditors(detailInfo.id, i));
  585. }
  586. }
  587. responseData.data.auditHistory = auditHistory;
  588. // 获取审批流程中左边列表
  589. responseData.data.auditors = await ctx.service.paymentDetailAudit.getAuditorsWithOwner(detailInfo.id, times);
  590. responseData.data.user = await ctx.service.projectAccount.getAccountInfoById(detailInfo.uid);
  591. ctx.body = responseData;
  592. } catch (error) {
  593. this.log(error);
  594. ctx.body = { err: 1, msg: error.toString(), data: null };
  595. }
  596. }
  597. async _returnRptProjectList(ctx, formProcess = false) {
  598. // 获取报表表单列表
  599. if (ctx.payment.auditPermission.view_all || ctx.tender.uid === ctx.session.sessionUser.accountId || formProcess) {
  600. const rptProject = await ctx.service.rptTreeNode.getDataByCondition({ pid: ctx.session.sessionProject.id, name: '01.支付审批报表' });
  601. const rptProjectList = [];
  602. const newRptList = rptProject && rptProject.items ? JSON.parse(rptProject.items) : [];
  603. ctx.rptListNum = 0;
  604. await this.getNewRptProjectList(ctx, newRptList, rptProjectList, 1);
  605. const tenderRptList = await ctx.service.paymentTenderRpt.getProcessList(ctx.tender.id);
  606. if (tenderRptList === -1) {
  607. throw '未配置表单设置,请联系管理员处理';
  608. }
  609. return await ctx.service.paymentTenderRpt.checkAndUpdateList(tenderRptList, rptProjectList, formProcess);
  610. }
  611. return await ctx.service.paymentTenderRpt.getList(ctx.tender.id, ctx.session.sessionUser.accountId);
  612. }
  613. // 循环获取到到rptProject
  614. async getNewRptProjectList(ctx, newRptList, rptProjectList, level = 1) {
  615. if (newRptList.length > 0) {
  616. for (const r of newRptList) {
  617. r.level = level;
  618. r.index = ctx.rptListNum;
  619. ctx.rptListNum = ctx.rptListNum + 1;
  620. rptProjectList.push(r);
  621. if (r.items && r.items.length > 0) {
  622. await this.getNewRptProjectList(ctx, r.items, rptProjectList, level + 1);
  623. }
  624. }
  625. }
  626. }
  627. async process(ctx) {
  628. try {
  629. if (!ctx.payment.auditPermission || !ctx.payment.auditPermission.admin || !(ctx.session.sessionUser.is_admin || ctx.tender.uid === ctx.session.sessionUser.accountId)) {
  630. throw '权限不足';
  631. }
  632. let [tenderRptList, rptProjectList] = await this._returnRptProjectList(ctx, true);
  633. tenderRptList = ctx.helper._.filter(tenderRptList, { type: 0, is_del: 0 });
  634. // for (const tr of tenderRptList) {
  635. // if (tr.status === shenpiConst.sp_status.gdspl) {
  636. // tr.auditList = await ctx.service.paymentShenpiAudit.getAuditList(ctx.tender.id, tr.id, tr.sp_status);
  637. // } else if (tr.status === shenpiConst.sp_status.gdzs) {
  638. // tr.audit = await ctx.service.paymentShenpiAudit.getAudit(ctx.tender.id, tr.id, tr.sp_status);
  639. // }
  640. // }
  641. // 获取所有项目参与者
  642. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  643. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  644. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  645. });
  646. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  647. const accountGroupList = unitList.map(item => {
  648. const groupList = accountList.filter(item1 => item1.company === item.name);
  649. return { groupName: item.name, groupList };
  650. });
  651. const renderData = {
  652. tender: ctx.tender,
  653. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.process),
  654. rptProjectList,
  655. tenderRptList,
  656. shenpi: shenpiConst,
  657. accountList,
  658. accountGroup: accountGroupList,
  659. };
  660. await this.layout('payment/process.ejs', renderData, 'payment/process_modal.ejs');
  661. } catch (err) {
  662. console.log(err);
  663. this.log(err);
  664. ctx.session.postError = err.toString();
  665. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/payment');
  666. }
  667. }
  668. async processSave(ctx) {
  669. try {
  670. if (!ctx.payment.auditPermission || !ctx.payment.auditPermission.admin) {
  671. throw '权限不足';
  672. }
  673. const responseData = {
  674. err: 0, msg: '', data: {},
  675. };
  676. const data = JSON.parse(ctx.request.body.data);
  677. if (!data.type) {
  678. throw '提交数据错误';
  679. }
  680. switch (data.type) {
  681. case 'add-rpt':
  682. responseData.data = await ctx.service.paymentTenderRpt.setRpt(ctx.tender.id, data.rpt_list);
  683. break;
  684. case 'change-status':
  685. responseData.data = await ctx.service.paymentTenderRpt.setStatus(data.tr_id, data.status);
  686. break;
  687. case 'get-audits':
  688. responseData.data = await ctx.service.paymentShenpiAudit.getShenpiAudit(data.tr_id, data.status);
  689. break;
  690. case 'add-audit':
  691. responseData.data = await ctx.service.paymentShenpiAudit.addAudit(data);
  692. break;
  693. case 'del-audit':
  694. responseData.data = await ctx.service.paymentShenpiAudit.removeAudit(data);
  695. break;
  696. case 'update-report':
  697. responseData.data = await ctx.service.paymentTenderRpt.updateReport(data);
  698. break;
  699. default: throw '参数有误';
  700. }
  701. ctx.body = responseData;
  702. } catch (err) {
  703. this.log(err);
  704. ctx.body = { err: 1, msg: err.toString(), data: null };
  705. }
  706. }
  707. async rptList(ctx) {
  708. try {
  709. const tenderRptList = await this._returnRptProjectList(ctx);
  710. if (tenderRptList === -1) {
  711. throw '未配置表单设置,请联系管理员处理';
  712. }
  713. if (tenderRptList.length === 0) {
  714. throw '未配置表单设置,请联系管理员处理';
  715. }
  716. for (const tr of tenderRptList) {
  717. tr.have_notice = await ctx.service.paymentDetail.haveNotice2TenderRpt(tr.id, ctx.session.sessionUser.accountId);
  718. }
  719. const trid = ctx.params.trid ? parseInt(ctx.params.trid) : 0;
  720. const trInfo = trid ? ctx.helper._.find(tenderRptList, { id: trid }) : tenderRptList[0];
  721. if (!trInfo) {
  722. throw '该模块已关闭或未进行表单审批设置';
  723. }
  724. // 获取列表
  725. const trDetailList = await ctx.service.paymentDetail.getValidDetails(trInfo.id);
  726. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  727. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  728. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  729. });
  730. if (trDetailList.length > 0) {
  731. for (const s of trDetailList) {
  732. // s.curAuditor = null;
  733. // 根据期状态返回展示用户
  734. s.curAuditor = await ctx.service.paymentDetailAudit.getAuditorByStatus(s.id, s.status, s.times);
  735. const userInfo = ctx.helper._.find(accountList, { id: s.uid });
  736. s.user_name = userInfo ? userInfo.name : '';
  737. }
  738. trDetailList[0].emptySign = await ctx.service.paymentRptAudit.haveEmptySign(trDetailList[0].id);
  739. }
  740. const renderData = {
  741. tender: ctx.tender,
  742. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.payment.list),
  743. tenderRptList,
  744. trInfo,
  745. trDetailList,
  746. rptMsg: null,
  747. auditConst,
  748. accountGroup: [],
  749. accountList: [],
  750. paymentConst,
  751. preUrl: '/payment/' + ctx.tender.id + '/list/' + trInfo.id,
  752. };
  753. // 获取报表信息,新增时及设置报表角色时使用
  754. if (trInfo.uid === ctx.session.sessionUser.accountId && trInfo.is_del === 0 && trInfo.rpt_id) {
  755. const rptTpl = await ctx.service.rptTpl.getDataById(trInfo.rpt_id);
  756. if (rptTpl) {
  757. const pageRst = await ctx.service.jpcReport.getAllPreviewPagesCommon(rptTpl, 'A4');
  758. renderData.rptMsg = pageRst.items[0];
  759. // 判断与原有的报表审批人列表是否有区别
  760. let difference = false;
  761. let auditDifference = false;
  762. let needChange = false;
  763. if (trInfo.report_items_json) {
  764. const report_items_json = JSON.parse(trInfo.report_items_json);
  765. const items = ['cells', 'signature_audit_cells', 'signature_cells', 'signature_date_cells', 'interact_cells'];
  766. for (const item of items) {
  767. if (report_items_json[item] &&
  768. !ctx.helper._.isEmpty(ctx.helper._.differenceWith(JSON.parse(JSON.stringify(renderData.rptMsg[item])), report_items_json[item], ctx.helper._.isEqual))) {
  769. // 因为interact_cells里存在undefind值,必须先用JSON.parse和JSON.stringify转义才能对比
  770. difference = true;
  771. needChange = true;
  772. if (item === 'signature_cells') {
  773. auditDifference = true;
  774. }
  775. // break;
  776. }
  777. }
  778. } else {
  779. difference = true;
  780. }
  781. if (difference) {
  782. // 删除rpt_audit重新配置
  783. const updateData = {
  784. id: trInfo.id,
  785. report_items_json: JSON.stringify(ctx.helper._.cloneDeep(renderData.rptMsg)),
  786. };
  787. // 删除rpt_audit重新配置
  788. if (auditDifference) {
  789. updateData.rpt_audit = null;
  790. trInfo.rpt_audit = null;
  791. }
  792. if (needChange) {
  793. updateData.is_change = 1;
  794. trInfo.is_change = 1;
  795. }
  796. await ctx.service.paymentTenderRpt.defaultUpdate(updateData);
  797. trInfo.report_items_json = renderData.rptMsg;
  798. }
  799. if (trInfo.rpt_audit) {
  800. trInfo.rpt_audit = JSON.parse(trInfo.rpt_audit);
  801. const prjAccList = await ctx.service.projectAccount.getAllAccountByProjectId(ctx.session.sessionProject.id);
  802. for (const audit of trInfo.rpt_audit) {
  803. if (audit.uid) {
  804. const info = ctx.helper._.find(prjAccList, { id: audit.uid });
  805. audit.name = info.name;
  806. }
  807. }
  808. } else {
  809. trInfo.rpt_audit = [];
  810. for (const sc of renderData.rptMsg.signature_cells) {
  811. trInfo.rpt_audit.push({
  812. uid: null,
  813. rpt_name: sc.signature_name,
  814. });
  815. }
  816. }
  817. renderData.trInfo = trInfo;
  818. }
  819. if (renderData.rptMsg) {
  820. // 获取所有项目参与者
  821. const accountList = await ctx.service.projectAccount.getAllDataByCondition({
  822. where: { project_id: ctx.session.sessionProject.id, enable: 1 },
  823. columns: ['id', 'name', 'company', 'role', 'enable', 'is_admin', 'account_group', 'mobile'],
  824. });
  825. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  826. const accountGroupList = unitList.map(item => {
  827. const groupList = accountList.filter(item1 => item1.company === item.name);
  828. return { groupName: item.name, groupList };
  829. });
  830. renderData.accountList = accountList;
  831. renderData.accountGroup = accountGroupList;
  832. }
  833. }
  834. await this.layout('payment/list.ejs', renderData, 'payment/list_modal.ejs');
  835. } catch (err) {
  836. console.log(err);
  837. this.log(err);
  838. ctx.session.postError = err.toString();
  839. ctx.redirect(this.request && this.request.headers && this.request.headers.referer ? this.request.headers.referer : '/payment');
  840. }
  841. }
  842. async rptSave(ctx) {
  843. try {
  844. const tr_id = ctx.params.trid;
  845. if (!tr_id) {
  846. throw '参数有误';
  847. }
  848. const trInfo = await ctx.service.paymentTenderRpt.getDataById(tr_id);
  849. if (!trInfo) {
  850. throw '标段报表不存在';
  851. }
  852. if (ctx.session.sessionUser.accountId !== trInfo.uid) {
  853. throw '权限不足';
  854. }
  855. const responseData = {
  856. err: 0, msg: '', data: {},
  857. };
  858. const data = JSON.parse(ctx.request.body.data);
  859. if (!data.type) {
  860. throw '提交数据错误';
  861. }
  862. switch (data.type) {
  863. case 'rpt_audit':
  864. responseData.data = await ctx.service.paymentTenderRpt.updateRptAudit(trInfo, data.rpt_audit);
  865. break;
  866. case 'add-detail':
  867. responseData.data = await ctx.service.paymentDetail.addDetail(trInfo, data.code, data.s_time);
  868. break;
  869. default: throw '参数有误';
  870. }
  871. ctx.body = responseData;
  872. } catch (err) {
  873. this.log(err);
  874. ctx.body = { err: 1, msg: err.toString(), data: null };
  875. }
  876. }
  877. /**
  878. * 上报和重新上报
  879. * @param ctx
  880. * @return {Promise<void>}
  881. */
  882. async startAudit(ctx) {
  883. try {
  884. if (ctx.detail.uid !== ctx.session.sessionUser.accountId) {
  885. throw '您无权上报报表表单详情数据';
  886. }
  887. if (ctx.detail.status === auditConst.status.checking || ctx.detail.status === auditConst.status.checked) {
  888. throw '该报表表单详情数据当前无法上报';
  889. }
  890. await ctx.service.paymentDetailAudit.start(ctx.detail.id, ctx.detail.times);
  891. ctx.redirect(ctx.request.header.referer);
  892. } catch (err) {
  893. this.log(err);
  894. ctx.session.postError = err.toString();
  895. ctx.redirect(ctx.request.header.referer);
  896. }
  897. }
  898. /**
  899. * 审批
  900. * @param ctx
  901. * @return {Promise<void>}
  902. */
  903. async checkAudit(ctx) {
  904. try {
  905. if (!ctx.detail || ctx.detail.status !== auditConst.status.checking) {
  906. throw '当前报表表单详情数据有误';
  907. }
  908. if (!ctx.detail.curAuditor || ctx.detail.curAuditor.aid !== ctx.session.sessionUser.accountId) {
  909. throw '您无权进行该操作';
  910. }
  911. const data = {
  912. checkType: parseInt(ctx.request.body.checkType),
  913. opinion: ctx.request.body.opinion,
  914. };
  915. if (!data.checkType || isNaN(data.checkType)) {
  916. throw '提交数据错误';
  917. }
  918. if (data.checkType === auditConst.status.checkNo) {
  919. if (!data.checkType || isNaN(data.checkType)) {
  920. throw '提交数据错误';
  921. }
  922. }
  923. await ctx.service.paymentDetailAudit.check(ctx.detail.id, data, ctx.detail.times);
  924. ctx.redirect(ctx.request.header.referer);
  925. } catch (err) {
  926. this.log(err);
  927. ctx.session.postError = err.toString();
  928. ctx.redirect(ctx.request.header.referer);
  929. }
  930. }
  931. /**
  932. * 上传附件
  933. * @param {Object} ctx - egg全局变量
  934. * @return {void}
  935. */
  936. async uploadDetailFile(ctx) {
  937. const responseData = {
  938. err: 0,
  939. msg: '',
  940. data: [],
  941. };
  942. let stream;
  943. try {
  944. const parts = ctx.multipart({ autoFields: true });
  945. const files = [];
  946. let index = 0;
  947. const extra_upload = ctx.detail.status === auditConst.status.checked;
  948. const original_data = {
  949. tender_id: ctx.tender.id,
  950. tr_id: ctx.trInfo.id,
  951. td_id: ctx.detail.id,
  952. }
  953. while ((stream = await parts()) !== undefined) {
  954. // 判断用户是否选择上传文件
  955. if (!stream.filename) {
  956. throw '请选择上传的文件!';
  957. }
  958. const fileInfo = path.parse(stream.filename);
  959. const create_time = Date.parse(new Date()) / 1000;
  960. const filepath = `app/public/upload/payment/${original_data.tender_id}/detail/fujian_${create_time + index.toString() + fileInfo.ext}`;
  961. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  962. await sendToWormhole(stream);
  963. // 保存数据到att表
  964. const fileData = {
  965. upload_time: new Date(),
  966. filename: fileInfo.name,
  967. fileext: fileInfo.ext,
  968. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  969. filepath,
  970. extra_upload,
  971. };
  972. const result = await ctx.service.paymentDetailAtt.save(original_data, fileData, ctx.session.sessionUser.accountId);
  973. if (!result) {
  974. throw '导入数据库保存失败';
  975. }
  976. fileData.uid = ctx.session.sessionUser.accountId;
  977. fileData.id = result.insertId;
  978. fileData.orginpath = ctx.app.config.fujianOssPath + filepath;
  979. delete fileData.filepath;
  980. if (!ctx.helper.canPreview(fileData.fileext)) {
  981. fileData.filepath = `/payment/${original_data.tender_id}/detail/${original_data.td_id}/file/${fileData.id}/download`;
  982. } else {
  983. fileData.filepath = ctx.app.config.fujianOssPath + filepath;
  984. }
  985. files.push(fileData);
  986. ++index;
  987. }
  988. responseData.data = files;
  989. } catch (err) {
  990. this.log(err);
  991. // 失败需要消耗掉stream 以防卡死
  992. if (stream) {
  993. await sendToWormhole(stream);
  994. }
  995. this.setMessage(err.toString(), this.messageType.ERROR);
  996. }
  997. ctx.body = responseData;
  998. }
  999. /**
  1000. * 下载附件
  1001. * @param {Object} ctx - egg全局变量
  1002. * @return {void}
  1003. */
  1004. async downloadDetailFile(ctx) {
  1005. const id = ctx.params.fid;
  1006. if (id) {
  1007. try {
  1008. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(id);
  1009. if (fileInfo !== undefined && fileInfo !== '') {
  1010. // 解决中文无法下载问题
  1011. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  1012. let disposition = '';
  1013. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  1014. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  1015. } else if (userAgent.indexOf('firefox') >= 0) {
  1016. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  1017. } else {
  1018. /* safari等其他非主流浏览器只能自求多福了 */
  1019. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  1020. }
  1021. ctx.response.set({
  1022. 'Content-Type': 'application/octet-stream',
  1023. 'Content-Disposition': disposition,
  1024. 'Content-Length': fileInfo.filesize,
  1025. });
  1026. // ctx.body = await fs.createReadStream(fileName);
  1027. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1028. } else {
  1029. throw '不存在该文件';
  1030. }
  1031. } catch (err) {
  1032. this.log(err);
  1033. this.setMessage(err.toString(), this.messageType.ERROR);
  1034. }
  1035. }
  1036. }
  1037. /**
  1038. * 删除附件
  1039. * @param {Object} ctx - egg全局变量
  1040. * @return {void}
  1041. */
  1042. async deleteDetailFile(ctx) {
  1043. const responseData = {
  1044. err: 0,
  1045. msg: '',
  1046. data: '',
  1047. };
  1048. try {
  1049. const data = JSON.parse(ctx.request.body.data);
  1050. const fileInfo = await ctx.service.paymentDetailAtt.getDataById(data.id);
  1051. if (!fileInfo || !Object.keys(fileInfo).length) {
  1052. throw '该文件不存在';
  1053. }
  1054. if (!fileInfo.extra_upload && ctx.detail.status === auditConst.status.checked) {
  1055. throw '无权限删除';
  1056. }
  1057. if (fileInfo !== undefined && fileInfo !== '') {
  1058. // 先删除文件
  1059. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  1060. // 再删除数据库
  1061. await ctx.service.paymentDetailAtt.deleteById(data.id);
  1062. responseData.data = '';
  1063. } else {
  1064. throw '不存在该文件';
  1065. }
  1066. } catch (err) {
  1067. responseData.err = 1;
  1068. responseData.msg = err;
  1069. }
  1070. ctx.body = responseData;
  1071. }
  1072. }
  1073. return PaymentController;
  1074. };