safe_controller.js 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. 'use strict';
  2. /**
  3. * 标段管理控制器
  4. *
  5. * @author Mai
  6. * @date 2025/7/17
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const auditType = require('../const/audit').auditType;
  11. const shenpiConst = require('../const/shenpi');
  12. const codeRuleConst = require('../const/code_rule');
  13. const contractConst = require('../const/contract');
  14. const moment = require('moment');
  15. const sendToWormhole = require('stream-wormhole');
  16. const fs = require('fs');
  17. const path = require('path');
  18. const PermissionCheck = require('../const/account_permission').PermissionCheck;
  19. module.exports = app => {
  20. class SafeController extends app.BaseController {
  21. constructor(ctx) {
  22. super(ctx);
  23. ctx.showProject = true;
  24. // ctx.showTitle = true;
  25. }
  26. loadMenu(ctx) {
  27. super.loadMenu(ctx);
  28. // 虚拟menu,以保证标题显示正确
  29. ctx.menu = {
  30. name: '安全管理',
  31. display: false,
  32. caption: '安全管理',
  33. controller: 'safe',
  34. };
  35. }
  36. async tender(ctx) {
  37. try {
  38. if (!ctx.subProject.page_show.safePayment) throw '该功能已关闭';
  39. const renderData = {
  40. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  41. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.tender),
  42. };
  43. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  44. renderData.accountList = accountList;
  45. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  46. const accountGroupList = unitList.map(item => {
  47. const groupList = accountList.filter(item1 => item1.company === item.name);
  48. return { groupName: item.name, groupList };
  49. }).filter(x => { return x.groupList.length > 0; });
  50. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  51. // renderData.accountGroup = unitList.map(item => {
  52. // const groupList = accountList.filter(item1 => item1.company === item.name);
  53. // return { groupName: item.name, groupList };
  54. // });
  55. renderData.accountGroup = accountGroupList;
  56. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  57. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'safe_payment', ctx.session.sessionUser.is_admin ? 'all' : '');
  58. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  59. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  60. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('safe_payment');
  61. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('safe_payment');
  62. await this.layout('safe/tender.ejs', renderData, 'safe/tender_modal.ejs');
  63. } catch (err) {
  64. ctx.log(err);
  65. ctx.postError(err, '无法查看安全计量数据');
  66. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  67. }
  68. }
  69. async stage(ctx) {
  70. try {
  71. if (!ctx.subProject.page_show.safePayment) throw '该功能已关闭';
  72. const renderData = {
  73. auditType: auditConst.auditType,
  74. auditConst: auditConst.common,
  75. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.stage),
  76. };
  77. renderData.stages = await this.ctx.service.safeStage.getAllStages(ctx.tender.id, 'DESC');
  78. for (const s of renderData.stages) {
  79. if (s.audit_status !== auditConst.common.status.checked) await this.ctx.service.safeStage.loadUser(s);
  80. s.can_del = (s.create_user_id === ctx.session.sessionUser.accountId || ctx.session.sessionUser.is_admin)
  81. && (s.audit_status === auditConst.common.status.uncheck || s.audit_status === auditConst.common.status.checkNo);
  82. }
  83. await this.layout('safe_calc/stage.ejs', renderData, 'safe_calc/stage_modal.ejs');
  84. } catch (err) {
  85. ctx.log(err);
  86. ctx.postError(err, '查看安全计量数据错误');
  87. ctx.redirect(`/sp/${ctx.subProject.id}/safe`);
  88. }
  89. }
  90. async addStage(ctx) {
  91. try {
  92. if (!ctx.permission.safe_payment.add) throw '您无权创建计量期';
  93. const stage_date = ctx.request.body.stage_date;
  94. if (!stage_date) throw '请选择日期';
  95. const stage_code = ctx.request.body.stage_code;
  96. const stages = await ctx.service.safeStage.getAllStages(ctx.tender.id, 'DESC');
  97. const unCompleteCount = stages.filter(s => { return s.status !== auditConst.common.status.checked; }).length;
  98. if (unCompleteCount.length > 0) throw `最新一起未审批通过,请审批通过后再新增`;
  99. const newStage = await ctx.service.safeStage.add(ctx.tender.id, stage_code, stage_date);
  100. if (!newStage) throw '新增期失败';
  101. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage/${newStage.stage_order}/bills`);
  102. } catch (err) {
  103. ctx.log(err);
  104. ctx.postError(err, '新增期失败');
  105. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  106. }
  107. }
  108. async delStage(ctx) {
  109. try {
  110. // if (!ctx.session.sessionUser.is_admin && ctx.request.body.confirm !== '确认删除本期') throw '请输入文本确认删除本期';
  111. const stage_id = ctx.request.body.stage_id;
  112. const stage = await ctx.service.safeStage.getDataById(stage_id);
  113. if (!stage) throw '删除的期不存在,请刷新页面';
  114. if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权删除本期';
  115. // 获取最新的期数
  116. const stageCount = await ctx.service.safeStage.count({ tid: ctx.tender.id });
  117. if (stage.stage_order !== stageCount) throw '非最新一期,不可删除';
  118. await ctx.service.safeStage.delete(stage_id);
  119. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  120. } catch (err) {
  121. ctx.log(err);
  122. ctx.postError(err, '删除期失败');
  123. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  124. }
  125. }
  126. async saveStage(ctx) {
  127. try {
  128. const stage_id = ctx.request.body.stage_id;
  129. const data = {
  130. stage_date: ctx.request.body.stage_date,
  131. stage_code: ctx.request.body.stage_code,
  132. };
  133. const stage = await ctx.service.safeStage.getStage(stage_id);
  134. if (!stage) throw '删除的期不存在,请刷新页面';
  135. if (!ctx.session.sessionUser.is_admin && stage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权修改该数据';
  136. await this.ctx.service.safeStage.save(stage, data);
  137. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  138. } catch (err) {
  139. ctx.log(err);
  140. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  141. }
  142. }
  143. async _getStageAuditViewData(ctx) {
  144. await this.ctx.service.safeStage.loadAuditViewData(ctx.safeStage);
  145. }
  146. async safeBills(ctx) {
  147. try {
  148. await this._getStageAuditViewData(ctx);
  149. // 获取附件列表
  150. const attList = await ctx.service.paymentDetailAtt.getPaymentDetailAttachment(ctx.safeStage.id, 'desc');
  151. const stdBills = await ctx.service.stdGclList.getSafeGcl();
  152. // 流程审批人相关数据
  153. // const accountList = await ctx.service.projectAccount.getAllSubProjectAccountByPermission(ctx.subProject, 'payment_permission');
  154. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  155. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  156. const accountGroup = unitList.map(item => {
  157. const groupList = accountList.filter(item1 => item1.company === item.name);
  158. return { groupName: item.name, groupList };
  159. }).filter(x => { return x.groupList.length > 0; });
  160. // 是否已验证手机短信
  161. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  162. const renderData = {
  163. auditConst: auditConst.common,
  164. accountList,
  165. accountGroup,
  166. shenpiConst,
  167. auditType: auditConst.auditType,
  168. authMobile: pa.auth_mobile,
  169. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.bills),
  170. stdBills,
  171. attList,
  172. };
  173. await this.layout('safe_calc/index.ejs', renderData, 'safe_calc/modal.ejs');
  174. } catch (err) {
  175. ctx.log(err);
  176. ctx.postError(err, '读取安全生产费错误');
  177. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  178. }
  179. }
  180. async safeCompare(ctx) {
  181. try {
  182. await this._getStageAuditViewData(ctx);
  183. // 流程审批人相关数据
  184. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  185. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  186. const accountGroup = unitList.map(item => {
  187. const groupList = accountList.filter(item1 => item1.company === item.name);
  188. return { groupName: item.name, groupList };
  189. }).filter(x => { return x.groupList.length > 0; });
  190. // 是否已验证手机短信
  191. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  192. const renderData = {
  193. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.compare),
  194. auditConst: auditConst.common,
  195. accountList,
  196. accountGroup,
  197. shenpiConst,
  198. auditType: auditConst.auditType,
  199. authMobile: pa.auth_mobile,
  200. };
  201. await this.layout('safe_calc/compare.ejs', renderData, 'safe_calc/audit_modal.ejs');
  202. } catch (err) {
  203. ctx.log(err);
  204. ctx.postError(err, '读取安全生产费错误');
  205. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage`);
  206. }
  207. }
  208. async safeLoad(ctx) {
  209. try {
  210. const data = JSON.parse(ctx.request.body.data);
  211. const filter = data.filter.split(';');
  212. const responseData = { err: 0, msg: '', data: {}, hpack: [] };
  213. for (const f of filter) {
  214. switch (f) {
  215. case 'bills':
  216. responseData.data.bills = ctx.safeStage.readOnly
  217. ? await ctx.service.safeStageBills.getReadData(ctx.safeStage)
  218. : await ctx.service.safeStageBills.getEditData(ctx.safeStage);
  219. break;
  220. case 'billsCompare':
  221. responseData.data[f] = await ctx.service.safeStageBills.getCompareData(ctx.safeStage);
  222. break;
  223. case 'auditFlow':
  224. responseData.data[f] = await ctx.service.safeStageAudit.getViewFlow(ctx.safeStage);
  225. break;
  226. case 'att':
  227. responseData.data[f] = await ctx.service.safeStageFile.getData(ctx.safeStage.id, 'bills', 'DESC');
  228. break;
  229. default:
  230. responseData.data[f] = [];
  231. break;
  232. }
  233. }
  234. ctx.body = responseData;
  235. } catch (err) {
  236. this.log(err);
  237. ctx.body = { err: 1, msg: err.toString(), data: null };
  238. }
  239. }
  240. async _billsBase(stage, type, data) {
  241. if (isNaN(data.id) || data.id <= 0) throw '数据错误';
  242. if (type !== 'add') {
  243. if (isNaN(data.count) || data.count <= 0) data.count = 1;
  244. }
  245. switch (type) {
  246. case 'add':
  247. return await this.ctx.service.safeStageBills.addSafeBillsNode(stage, data.id, data.count);
  248. case 'delete':
  249. return await this.ctx.service.safeStageBills.delete(stage.id, data.id, data.count);
  250. case 'up-move':
  251. return await this.ctx.service.safeStageBills.upMoveNode(stage.id, data.id, data.count);
  252. case 'down-move':
  253. return await this.ctx.service.safeStageBills.downMoveNode(stage.id, data.id, data.count);
  254. case 'up-level':
  255. return await this.ctx.service.safeStageBills.upLevelNode(stage.id, data.id, data.count);
  256. case 'down-level':
  257. return await this.ctx.service.safeStageBills.downLevelNode(stage.id, data.id, data.count);
  258. }
  259. }
  260. async safeUpdate(ctx) {
  261. try {
  262. const data = JSON.parse(ctx.request.body.data);
  263. if (!data.postType || !data.postData) throw '数据错误';
  264. const responseData = { err: 0, msg: '', data: {} };
  265. switch (data.postType) {
  266. case 'add':
  267. case 'delete':
  268. case 'up-move':
  269. case 'down-move':
  270. case 'up-level':
  271. case 'down-level':
  272. responseData.data = await this._billsBase(ctx.safeStage, data.postType, data.postData);
  273. break;
  274. case 'update':
  275. responseData.data = await this.ctx.service.safeStageBills.updateCalc(ctx.safeStage, data.postData);
  276. break;
  277. case 'add-std':
  278. responseData.data = await this.ctx.service.safeStageBills.addStdNodeWithParent(ctx.safeStage, data.postData.id, data.postData.stdData);
  279. break;
  280. default:
  281. throw '未知操作';
  282. }
  283. ctx.body = responseData;
  284. } catch (err) {
  285. this.log(err);
  286. ctx.body = this.ajaxErrorBody(err, '数据错误');
  287. }
  288. }
  289. async safeDecimal(ctx) {
  290. try {
  291. const data = JSON.parse(ctx.request.body.data);
  292. const result = await this.ctx.service.safeStageBills.setDecimal(data.decimal);
  293. ctx.body = { err: 0, msg: '', data: result };
  294. } catch (err) {
  295. this.log(err);
  296. ctx.body = this.ajaxErrorBody(err, '设置小数位数错误');
  297. }
  298. }
  299. async uploadStageFile(ctx) {
  300. let stream;
  301. try {
  302. const parts = ctx.multipart({autoFields: true});
  303. let index = 0;
  304. const create_time = Date.parse(new Date()) / 1000;
  305. let stream = await parts();
  306. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  307. const rela_type = parts.fields.type;
  308. const rela_id = parts.field.rela_id;
  309. const uploadfiles = [];
  310. while (stream !== undefined) {
  311. if (!stream.filename) throw '未发现上传文件!';
  312. const fileInfo = path.parse(stream.filename);
  313. const filepath = `app/public/upload/${ctx.safeStage.tid}/safeStage/${ctx.moment().format('YYYYMMDD')}/${create_time + '_' + index + fileInfo.ext}`;
  314. // 保存文件
  315. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  316. await sendToWormhole(stream);
  317. // 插入到stage_pay对应的附件列表中
  318. uploadfiles.push({
  319. rela_id,
  320. filename: fileInfo.name,
  321. fileext: fileInfo.ext,
  322. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  323. filepath,
  324. });
  325. ++index;
  326. if (Array.isArray(parts.field.size) && index < parts.field.size.length) {
  327. stream = await parts();
  328. } else {
  329. stream = undefined;
  330. }
  331. }
  332. const result = await ctx.service.safeStageFile.addFiles(ctx.safeStage, 'bills', uploadfiles, user);
  333. ctx.body = {err: 0, msg: '', data: result};
  334. } catch (error) {
  335. ctx.log(error);
  336. // 失败需要消耗掉stream 以防卡死
  337. if (stream) await sendToWormhole(stream);
  338. ctx.body = this.ajaxErrorBody(error, '上传附件失败,请重试');
  339. }
  340. }
  341. async deleteStageFile(ctx) {
  342. try{
  343. const data = JSON.parse(ctx.request.body.data);
  344. if (!data && !data.id) throw '缺少参数';
  345. const result = await ctx.service.safeStageFile.delFiles(data.id);
  346. ctx.body = { err: 0, msg: '', data: result };
  347. } catch(error) {
  348. ctx.log(error);
  349. ctx.ajaxErrorBody(error, '删除附件失败');
  350. }
  351. }
  352. /**
  353. * 添加审批人
  354. * @param ctx
  355. * @return {Promise<void>}
  356. */
  357. async addStageAudit(ctx) {
  358. try {
  359. const data = JSON.parse(ctx.request.body.data);
  360. const id = this.app._.toInteger(data.auditorId);
  361. if (isNaN(id) || id <= 0) throw '参数错误';
  362. // 检查权限等
  363. if (ctx.safeStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权添加审核人';
  364. if (ctx.safeStage.audit_status !== auditConst.common.status.uncheck && ctx.safeStage.audit_status !== auditConst.common.status.checkNo) {
  365. throw '当前不允许添加审核人';
  366. }
  367. // 检查审核人是否已存在
  368. const exist = await ctx.service.safeStageAudit.getDataByCondition({ stage_id: ctx.safeStage.id, audit_times: ctx.safeStage.audit_times, audit_id: id });
  369. if (exist) throw '该审核人已存在,请勿重复添加';
  370. const auditorInfo = await this.ctx.service.projectAccount.getDataById(id);
  371. if (!auditorInfo) throw '添加的审批人不存在';
  372. const shenpiInfo = await ctx.service.shenpiAudit.getDataByCondition({ tid: ctx.tender.id, sp_type: shenpiConst.sp_type.safe_payment, sp_status: shenpiConst.sp_status.gdzs });
  373. const is_gdzs = shenpiInfo && ctx.tender.info.shenpi.safe_payment === shenpiConst.sp_status.gdzs ? 1 : 0;
  374. const result = await ctx.service.safeStageAudit.addAuditor(ctx.safeStage.id, auditorInfo, ctx.safeStage.audit_times, is_gdzs);
  375. if (!result) throw '添加审核人失败';
  376. const auditors = await ctx.service.safeStageAudit.getAuditorGroup(ctx.safeStage.id, ctx.safeStage.audit_times);
  377. ctx.body = { err: 0, msg: '', data: auditors };
  378. } catch (err) {
  379. ctx.log(err);
  380. ctx.body = { err: 1, msg: err.toString(), data: null };
  381. }
  382. }
  383. /**
  384. * 移除审批人
  385. * @param ctx
  386. * @return {Promise<void>}
  387. */
  388. async deleteStageAudit(ctx) {
  389. try {
  390. const data = JSON.parse(ctx.request.body.data);
  391. const id = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  392. if (isNaN(id) || id <= 0) throw '参数错误';
  393. const result = await ctx.service.safeStageAudit.deleteAuditor(ctx.safeStage.id, id, ctx.safeStage.audit_times);
  394. if (!result) throw '移除审核人失败';
  395. const auditors = await ctx.service.safeStageAudit.getAuditors(ctx.safeStage.id, ctx.safeStage.audit_times);
  396. ctx.body = { err: 0, msg: '', data: auditors };
  397. } catch (err) {
  398. ctx.log(err);
  399. ctx.body = { err: 1, msg: err.toString(), data: null };
  400. }
  401. }
  402. async stageAuditStart(ctx) {
  403. try {
  404. if (ctx.safeStage.create_user_id !== ctx.session.sessionUser.accountId) throw '您无权上报该期数据';
  405. if (ctx.safeStage.revising) throw '台账修订中,不可上报';
  406. if (ctx.safeStage.audit_status !== auditConst.common.status.uncheck && ctx.safeStage.audit_status !== auditConst.common.status.checkNo) throw '该期数据当前无法上报';
  407. await ctx.service.safeStageAudit.start(ctx.safeStage);
  408. ctx.redirect(ctx.request.header.referer);
  409. } catch (err) {
  410. ctx.log(err);
  411. ctx.postError(err, '上报失败');
  412. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/stage/${ctx.safeStage.stage_order}/bills`);
  413. }
  414. }
  415. async stageAuditCheck(ctx) {
  416. try {
  417. if (!ctx.safeStage || (ctx.safeStage.audit_status !== auditConst.common.status.checking && ctx.safeStage.audit_status !== auditConst.common.status.checkNoPre)) {
  418. throw '当前期数据有误';
  419. }
  420. if (ctx.safeStage.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0) {
  421. throw '您无权进行该操作';
  422. }
  423. if (ctx.safeStage.revising) throw '台账修订中,不可审批';
  424. const checkType = parseInt(ctx.request.body.checkType);
  425. const opinion = ctx.request.body.opinion.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
  426. await ctx.service.safeStageAudit.check(ctx.safeStage, checkType, opinion);
  427. } catch (err) {
  428. ctx.log(err);
  429. ctx.postError(err, '审批失败');
  430. }
  431. ctx.redirect(ctx.request.header.referer);
  432. }
  433. async stageAuditCheckAgain(ctx) {
  434. try {
  435. if (!ctx.safeStage.isLatest) throw '非最新一期,不可重新审批';
  436. if (ctx.safeStage.audit_status !== auditConst.common.status.checked) throw '未审批完成,不可重新审批';
  437. if (ctx.safeStage.revising) throw '台账修订中,不可重审';
  438. if (ctx.session.sessionUser.loginStatus === 0) {
  439. const user = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  440. if (!user.auth_mobile) throw '未绑定手机号';
  441. const code = ctx.request.body.code;
  442. const cacheKey = 'smsCode:' + ctx.session.sessionUser.accountId;
  443. const cacheCode = await app.redis.get(cacheKey);
  444. if (cacheCode === null || code === undefined || cacheCode !== (code + pa.auth_mobile)) {
  445. throw '验证码不正确!';
  446. }
  447. }
  448. const adminCheckAgain = ctx.request.body.confirm === '确认设置终审审批' && ctx.session.sessionUser.is_admin;
  449. if (ctx.safeStage.finalAuditorIds.indexOf(ctx.session.sessionUser.accountId) < 0 && !adminCheckAgain) throw '您无权重新审批';
  450. await ctx.service.safeStageAudit.checkAgain(ctx.safeStage, adminCheckAgain);
  451. } catch (err) {
  452. ctx.log(err);
  453. ctx.postError(err, '重新审批失败');
  454. }
  455. ctx.redirect(ctx.request.header.referer);
  456. }
  457. async stageAuditCheckCancel(ctx) {
  458. try {
  459. if (ctx.safeStage.revising) throw '台账修订中,不可撤回';
  460. if (!ctx.safeStage.cancancel) throw '您无权进行该操作';
  461. await ctx.service.safeStageAudit.checkCancel(ctx.safeStage);
  462. } catch (err) {
  463. ctx.log(err);
  464. ctx.postError(err, '撤回失败');
  465. }
  466. ctx.redirect(ctx.request.header.referer);
  467. }
  468. /**
  469. * 期审批流程(POST)
  470. * @param ctx
  471. * @return {Promise<void>}
  472. */
  473. async loadAuditors(ctx) {
  474. try {
  475. const order = JSON.parse(ctx.request.body.data).order;
  476. const tenderId = ctx.params.tid;
  477. const stage = await ctx.service.safeStage.getStageByOrder(tenderId, order);
  478. await ctx.service.safeStage.loadUser(stage);
  479. await ctx.service.safeStage.loadAuditViewData(stage);
  480. ctx.body = { err: 0, msg: '', data: stage };
  481. } catch (error) {
  482. ctx.log(error);
  483. ctx.body = { err: 1, msg: error.toString(), data: null };
  484. }
  485. }
  486. async loadPaySafeData(ctx) {
  487. try {
  488. // 先获取你创建的标段及参与的标段
  489. const tenderList = await ctx.service.paymentTender.getAllDataByCondition({ where: { spid: ctx.subProject.id } });
  490. // 获取你创建的目录及对应目录下的所有目录
  491. const folderList = await ctx.service.paymentFolder.getAllDataByCondition({ where: { spid: ctx.subProject.id } });
  492. for (const tender of tenderList) {
  493. tender.details = await ctx.service.paymentDetail.getAllDataByCondition({ where: { tender_id: tender.id, type: 1 } });
  494. }
  495. ctx.body = { err: 0, msg: '', data: { tenderList, folderList } };
  496. } catch(err) {
  497. ctx.log(error);
  498. ctx.ajaxErrorBody(err, '获取安全生产费旧数据失败');
  499. }
  500. }
  501. async copyPaySafeData(ctx) {
  502. try {
  503. const data = JSON.parse(ctx.request.body.data);
  504. if (!data.tid) throw '参数错误';
  505. await ctx.service.safeStage.copyPaySafeData(data.tid);
  506. ctx.body = {err: 0, msg: '迁移旧数据成功', data: null };
  507. } catch(err) {
  508. ctx.log(err);
  509. ctx.ajaxErrorBody(err, '迁移旧数据失败');
  510. }
  511. }
  512. async inspectionTender(ctx) {
  513. try {
  514. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  515. const renderData = {
  516. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  517. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.tender),
  518. };
  519. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  520. renderData.accountList = accountList;
  521. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  522. const accountGroupList = unitList.map(item => {
  523. const groupList = accountList.filter(item1 => item1.company === item.name);
  524. return { groupName: item.name, groupList };
  525. }).filter(x => { return x.groupList.length > 0; });
  526. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  527. // renderData.accountGroup = unitList.map(item => {
  528. // const groupList = accountList.filter(item1 => item1.company === item.name);
  529. // return { groupName: item.name, groupList };
  530. // });
  531. renderData.accountGroup = accountGroupList;
  532. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  533. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'safe_inspection', ctx.session.sessionUser.is_admin ? 'all' : '');
  534. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  535. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  536. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('safe_inspection');
  537. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('safe_inspection');
  538. await this.layout('safe/tender.ejs', renderData, 'safe/tender_modal.ejs');
  539. } catch (err) {
  540. ctx.log(err);
  541. ctx.postError(err, '无法查看安全巡检数据');
  542. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  543. }
  544. }
  545. /**
  546. * 变更管理 页面 (Get)
  547. *
  548. * @param {Object} ctx - egg全局变量
  549. * @return {void}
  550. */
  551. async inspection(ctx) {
  552. try {
  553. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  554. const status = parseInt(ctx.query.status) || 0;
  555. await this._filterInspection(ctx, status);
  556. } catch (err) {
  557. ctx.log(err);
  558. ctx.postError(err, '无法查看质量管理数据');
  559. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  560. }
  561. }
  562. // 质量巡检单功能
  563. async _filterInspection(ctx, status = 0) {
  564. try {
  565. ctx.session.sessionUser.tenderId = ctx.tender.id;
  566. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  567. const orders = ctx.query.order ? ctx.query.order : 0;
  568. const filter = JSON.parse(JSON.stringify(auditConst.inspection.filter));
  569. filter.count = [];
  570. filter.count[filter.status.pending] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.pending);
  571. filter.count[filter.status.uncheck] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.uncheck);
  572. filter.count[filter.status.checking] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checking);
  573. filter.count[filter.status.rectification] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.rectification);
  574. filter.count[filter.status.checked] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checked);
  575. filter.count[filter.status.checkStop] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checkStop);// await ctx.service.change.pendingDatas(tender.id, ctx.session.sessionUser.accountId);
  576. const inspectionList = await ctx.service.safeInspection.getListByStatus(ctx.tender.id, status, 1, sorts, orders);
  577. const total = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, status);
  578. const allAttList = inspectionList.length > 0 ? await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.helper._.map(inspectionList, 'id')) : [];
  579. for (const c of inspectionList) {
  580. c.attList = ctx.helper._.filter(allAttList, { qiid: c.id });
  581. c.curAuditors = await ctx.service.safeInspectionAudit.getAuditorsByStatus(c.id, c.status, c.times);
  582. if (c.status === auditConst.inspection.status.checkNoPre) {
  583. c.curAuditors2 = await ctx.service.safeInspectionAudit.getAuditorsByStatus(c.id, auditConst.inspection.status.checking, c.times);
  584. }
  585. }
  586. // 分页相关
  587. const page = ctx.page;
  588. const pageSize = ctx.pageSize;
  589. const pageInfo = {
  590. page,
  591. pageSizeSelect: 1,
  592. pageSize,
  593. total_num: total,
  594. total: Math.ceil(total / pageSize),
  595. queryData: JSON.stringify(ctx.urlInfo.query),
  596. };
  597. let codeRule = [];
  598. let c_connector = '1';
  599. let c_rule_first = 1;
  600. const rule_type = 'safe_inspection';
  601. const tender = await this.service.tender.getDataById(ctx.tender.id);
  602. if (tender.c_code_rules) {
  603. const c_code_rules = JSON.parse(tender.c_code_rules);
  604. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  605. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  606. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  607. }
  608. for (const rule of codeRule) {
  609. switch (rule.rule_type) {
  610. case codeRuleConst.measure.ruleType.dealCode:
  611. rule.preview = ctx.tender.info.deal_info.dealCode;
  612. break;
  613. case codeRuleConst.measure.ruleType.tenderName:
  614. rule.preview = tender.name;
  615. break;
  616. case codeRuleConst.measure.ruleType.inDate:
  617. rule.preview = moment().format('YYYY');
  618. break;
  619. case codeRuleConst.measure.ruleType.text:
  620. rule.preview = rule.text;
  621. break;
  622. case codeRuleConst.measure.ruleType.addNo:
  623. const s = '0000000000';
  624. rule.preview = s.substr(s.length - rule.format);
  625. break;
  626. default: break;
  627. }
  628. }
  629. const renderData = {
  630. moment,
  631. tender,
  632. permission: ctx.permission.safe_inspection,
  633. rule_type,
  634. codeRule,
  635. dealCode: ctx.tender.info.deal_info.dealCode,
  636. c_connector,
  637. c_rule_first,
  638. ruleType: codeRuleConst.ruleType[rule_type],
  639. ruleConst: codeRuleConst.measure,
  640. filter,
  641. inspectionList,
  642. auditType,
  643. auditConst: auditConst.inspection,
  644. status,
  645. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection),
  646. pageInfo,
  647. };
  648. await this.layout('safe/inspection.ejs', renderData, 'safe/inspection_modal.ejs');
  649. } catch (err) {
  650. ctx.log(err);
  651. ctx.postError(err, '无法查看安全巡检数据');
  652. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  653. }
  654. }
  655. /**
  656. * 新增变更申请 (Post)
  657. *
  658. * @param {Object} ctx - egg全局变量
  659. * @return {void}
  660. */
  661. async inspectionSave(ctx) {
  662. try {
  663. const data = JSON.parse(ctx.request.body.data);
  664. const reponseData = {
  665. err: 0, msg: '', data: {},
  666. };
  667. switch (data.type) {
  668. case 'add':
  669. if (!data.code || data.code === '') {
  670. throw '编号不能为空';
  671. }
  672. if (!data.check_item || !data.check_date) {
  673. throw '请填写检查项和日期';
  674. }
  675. reponseData.data = await ctx.service.safeInspection.add(ctx.tender.id, ctx.session.sessionUser.accountId, data.code, data.check_item, data.check_date);
  676. break;
  677. default:throw '参数有误';
  678. }
  679. ctx.body = reponseData;
  680. } catch (err) {
  681. this.log(err);
  682. ctx.body = { err: 1, msg: err.toString() };
  683. }
  684. }
  685. /**
  686. * 获取审批界面所需的 原报、审批人数据等
  687. * @param ctx
  688. * @return {Promise<void>}
  689. * @private
  690. */
  691. async _getInspectionAuditViewData(ctx) {
  692. await ctx.service.safeInspection.loadAuditViewData(ctx.inspection);
  693. }
  694. async inspectionInformation(ctx) {
  695. try {
  696. const whiteList = this.ctx.app.config.multipart.whitelist;
  697. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  698. await this._getInspectionAuditViewData(ctx);
  699. // 获取附件列表
  700. const fileList = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  701. // 获取用户人验证手机号
  702. const renderData = {
  703. moment,
  704. tender,
  705. inspection: ctx.inspection,
  706. auditConst: auditConst.inspection,
  707. fileList,
  708. whiteList,
  709. auditType,
  710. shenpiConst,
  711. deleteFilePermission: PermissionCheck.delFile(this.ctx.session.sessionUser.permission),
  712. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection_information),
  713. preUrl: `/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection/${ctx.inspection.id}/information`,
  714. };
  715. // data.accountGroup = accountGroup;
  716. // 获取所有项目参与者
  717. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  718. renderData.accountList = accountList;
  719. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  720. renderData.accountGroup = unitList.map(item => {
  721. const groupList = accountList.filter(item1 => item1.company === item.name);
  722. return { groupName: item.name, groupList };
  723. }).filter(x => { return x.groupList.length > 0; });
  724. await this.layout('safe/inspection_information.ejs', renderData, 'safe/inspection_information_modal.ejs');
  725. } catch (err) {
  726. this.log(err);
  727. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection`);
  728. }
  729. }
  730. async inspectionInformationSave(ctx) {
  731. try {
  732. const data = JSON.parse(ctx.request.body.data);
  733. const reponseData = {
  734. err: 0, msg: '', data: {},
  735. };
  736. switch (data.type) {
  737. case 'update-field':
  738. if (!(!ctx.inspection.readOnly || ctx.inspection.rectificationPower)) {
  739. throw '当前状态不可修改';
  740. }
  741. if (data.update.check_item !== undefined && data.update.check_item === '') {
  742. throw '检查项不能为空';
  743. }
  744. if (data.update.check_date !== undefined && data.update.check_date === '') {
  745. throw '请填写检查日期';
  746. }
  747. if (data.update.rectification_item !== undefined && data.update.rectification_item === '') {
  748. throw '整改情况不能为空';
  749. }
  750. if (data.update.rectification_date !== undefined && data.update.rectification_date === '') {
  751. throw '请填写整改日期';
  752. }
  753. const fields = ['id', 'check_item', 'check_situation', 'action', 'check_date', 'inspector', 'rectification_item', 'rectification_date'];
  754. if (!this.checkFieldExists(data.update, fields)) {
  755. throw '参数有误';
  756. }
  757. reponseData.data = await ctx.service.safeInspection.defaultUpdate(data.update);
  758. break;
  759. case 'add-audit':
  760. const id = this.app._.toInteger(data.auditorId);
  761. if (isNaN(id) || id <= 0) {
  762. throw '参数错误';
  763. }
  764. // 检查权限等
  765. if (ctx.inspection.uid !== ctx.session.sessionUser.accountId) {
  766. throw '您无权添加审核人';
  767. }
  768. if (ctx.inspection.status !== auditConst.inspection.status.uncheck && ctx.inspection.status !== auditConst.inspection.status.checkNo) {
  769. throw '当前不允许添加审核人';
  770. }
  771. ctx.inspection.auditorList = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  772. // 检查审核人是否已存在
  773. const exist = this.app._.find(ctx.inspection.auditorList, { aid: id });
  774. if (exist) {
  775. throw '该审核人已存在,请勿重复添加';
  776. }
  777. const result = await ctx.service.safeInspectionAudit.addAuditor(ctx.inspection.id, id, ctx.inspection.times);
  778. if (!result) {
  779. throw '添加审核人失败';
  780. }
  781. reponseData.data = await ctx.service.safeInspectionAudit.getUserGroup(ctx.inspection.id, ctx.inspection.times);
  782. break;
  783. case 'del-audit':
  784. const id2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  785. if (isNaN(id2) || id2 <= 0) {
  786. throw '参数错误';
  787. }
  788. const result2 = await ctx.service.safeInspectionAudit.deleteAuditor(ctx.inspection.id, id2, ctx.inspection.times);
  789. if (!result2) {
  790. throw '移除审核人失败';
  791. }
  792. reponseData.data = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  793. break;
  794. case 'start-inspection':
  795. if (ctx.inspection.readOnly) {
  796. throw '当前状态不可提交';
  797. }
  798. await ctx.service.safeInspectionAudit.start(ctx.inspection.id, ctx.inspection.times);
  799. break;
  800. case 'del-inspection':
  801. if (ctx.inspection.readOnly) {
  802. throw '当前状态不可删除';
  803. }
  804. await ctx.service.safeInspection.delInspection(ctx.inspection.id);
  805. break;
  806. case 'check':
  807. if (!ctx.inspection || !(ctx.inspection.status === auditConst.inspection.status.checking || ctx.inspection.status === auditConst.inspection.status.checkNoPre)) {
  808. throw '当前质量巡检数据有误';
  809. }
  810. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  811. throw '您无权进行该操作';
  812. }
  813. await ctx.service.safeInspectionAudit.check(ctx.inspection, data);
  814. break;
  815. case 'rectification':
  816. if (!ctx.inspection || ctx.inspection.status !== auditConst.inspection.status.rectification) {
  817. throw '当前质量巡检数据有误';
  818. }
  819. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  820. throw '您无权进行该操作';
  821. }
  822. await ctx.service.safeInspectionAudit.rectification(ctx.inspection, data);
  823. break;
  824. default:throw '参数有误';
  825. }
  826. ctx.body = reponseData;
  827. } catch (err) {
  828. this.log(err);
  829. ctx.body = { err: 1, msg: err.toString() };
  830. }
  831. }
  832. checkFieldExists(update, fields) {
  833. for (const field of Object.keys(update)) {
  834. if (!fields.includes(field)) {
  835. return false;
  836. }
  837. }
  838. return true;
  839. }
  840. /**
  841. * 上传附件
  842. * @param {*} ctx 上下文
  843. */
  844. async uploadInspectionFile(ctx) {
  845. let stream;
  846. try {
  847. // this._checkAdvanceFileCanModify(ctx);
  848. const parts = this.ctx.multipart({
  849. autoFields: true,
  850. });
  851. const files = [];
  852. const create_time = Date.parse(new Date()) / 1000;
  853. let idx = 0;
  854. const extra_upload = ctx.inspection.status === auditConst.inspection.status.checked;
  855. while ((stream = await parts()) !== undefined) {
  856. if (!stream.filename) {
  857. // 如果没有传入直接返回
  858. return;
  859. }
  860. const fileInfo = path.parse(stream.filename);
  861. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/safe_inspection/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  862. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  863. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  864. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  865. ++idx;
  866. stream && (await sendToWormhole(stream));
  867. }
  868. const in_time = new Date();
  869. const payload = files.map(file => {
  870. let idx;
  871. if (Array.isArray(parts.field.name)) {
  872. idx = parts.field.name.findIndex(name => name === file.name);
  873. } else {
  874. idx = 'isString';
  875. }
  876. const newFile = {
  877. tid: ctx.tender.id,
  878. qiid: ctx.inspection.id,
  879. uid: ctx.session.sessionUser.accountId,
  880. filename: file.name,
  881. fileext: file.ext,
  882. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  883. filepath: file.filepath,
  884. upload_time: in_time,
  885. extra_upload,
  886. };
  887. return newFile;
  888. });
  889. // 执行文件信息写入数据库
  890. await ctx.service.safeInspectionAtt.saveFileMsgToDb(payload);
  891. // 将最新的当前标段的所有文件信息返回
  892. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  893. ctx.body = { err: 0, msg: '', data };
  894. } catch (err) {
  895. stream && (await sendToWormhole(stream));
  896. this.log(err);
  897. ctx.body = { err: 1, msg: err.toString(), data: null };
  898. }
  899. }
  900. /**
  901. * 删除附件
  902. * @param {Ojbect} ctx 上下文
  903. */
  904. async deleteInspectionFile(ctx) {
  905. try {
  906. const { id } = JSON.parse(ctx.request.body.data);
  907. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  908. if (fileInfo || Object.keys(fileInfo).length) {
  909. // 先删除文件
  910. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  911. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  912. // 再删除数据库
  913. await ctx.service.safeInspectionAtt.delete(id);
  914. } else {
  915. throw '不存在该文件';
  916. }
  917. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  918. ctx.body = { err: 0, msg: '请求成功', data };
  919. } catch (err) {
  920. this.log(err);
  921. ctx.body = { err: 1, msg: err.toString(), data: null };
  922. }
  923. }
  924. /**
  925. * 下载附件
  926. * @param {Object} ctx - egg全局变量
  927. * @return {void}
  928. */
  929. async downloadInspectionFile(ctx) {
  930. const id = ctx.params.fid;
  931. if (id) {
  932. try {
  933. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  934. if (fileInfo !== undefined && fileInfo !== '') {
  935. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  936. // 解决中文无法下载问题
  937. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  938. let disposition = '';
  939. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  940. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  941. } else if (userAgent.indexOf('firefox') >= 0) {
  942. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  943. } else {
  944. /* safari等其他非主流浏览器只能自求多福了 */
  945. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  946. }
  947. ctx.response.set({
  948. 'Content-Type': 'application/octet-stream',
  949. 'Content-Disposition': disposition,
  950. 'Content-Length': fileInfo.filesize,
  951. });
  952. // ctx.body = await fs.createReadStream(fileName);
  953. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  954. } else {
  955. throw '不存在该文件';
  956. }
  957. } catch (err) {
  958. this.log(err);
  959. this.setMessage(err.toString(), this.messageType.ERROR);
  960. }
  961. }
  962. }
  963. }
  964. return SafeController;
  965. };