payment_controller.js 57 KB

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