safe_controller.js 52 KB

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