safe_controller.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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 inspectionTender(ctx) {
  70. try {
  71. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  72. const renderData = {
  73. is_inspection: ctx.url.includes('inspection') ? 1 : 0,
  74. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.tender),
  75. };
  76. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  77. renderData.accountList = accountList;
  78. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  79. const accountGroupList = unitList.map(item => {
  80. const groupList = accountList.filter(item1 => item1.company === item.name);
  81. return { groupName: item.name, groupList };
  82. }).filter(x => { return x.groupList.length > 0; });
  83. // const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  84. // renderData.accountGroup = unitList.map(item => {
  85. // const groupList = accountList.filter(item1 => item1.company === item.name);
  86. // return { groupName: item.name, groupList };
  87. // });
  88. renderData.accountGroup = accountGroupList;
  89. renderData.accountInfo = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  90. renderData.tenderList = await ctx.service.tender.getSpecList(ctx.service.tenderPermission, 'safe_inspection', ctx.session.sessionUser.is_admin ? 'all' : '');
  91. renderData.categoryData = await this.ctx.service.category.getAllCategory(this.ctx.subProject);
  92. renderData.selfCategoryLevel = this.ctx.subProject.permission.self_category_level;
  93. renderData.permissionConst = ctx.service.tenderPermission.partPermissionConst('safe_inspection');
  94. renderData.permissionBlock = ctx.service.tenderPermission.partPermissionBlock('safe_inspection');
  95. await this.layout('safe/tender.ejs', renderData, 'safe/tender_modal.ejs');
  96. } catch (err) {
  97. ctx.log(err);
  98. ctx.postError(err, '无法查看安全巡检数据');
  99. ctx.redirect(`/sp/${ctx.subProject.id}/dashboard`);
  100. }
  101. }
  102. /**
  103. * 变更管理 页面 (Get)
  104. *
  105. * @param {Object} ctx - egg全局变量
  106. * @return {void}
  107. */
  108. async inspection(ctx) {
  109. try {
  110. if (!ctx.subProject.page_show.safeInspection) throw '该功能已关闭';
  111. const status = parseInt(ctx.query.status) || 0;
  112. await this._filterInspection(ctx, status);
  113. } catch (err) {
  114. ctx.log(err);
  115. ctx.postError(err, '无法查看质量管理数据');
  116. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  117. }
  118. }
  119. // 质量巡检单功能
  120. async _filterInspection(ctx, status = 0) {
  121. try {
  122. ctx.session.sessionUser.tenderId = ctx.tender.id;
  123. const sorts = ctx.query.sort ? ctx.query.sort : 0;
  124. const orders = ctx.query.order ? ctx.query.order : 0;
  125. const filter = JSON.parse(JSON.stringify(auditConst.inspection.filter));
  126. filter.count = [];
  127. filter.count[filter.status.pending] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.pending);
  128. filter.count[filter.status.uncheck] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.uncheck);
  129. filter.count[filter.status.checking] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checking);
  130. filter.count[filter.status.rectification] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.rectification);
  131. filter.count[filter.status.checked] = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, filter.status.checked);
  132. 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);
  133. const inspectionList = await ctx.service.safeInspection.getListByStatus(ctx.tender.id, status, 1, sorts, orders);
  134. const total = await ctx.service.safeInspection.getCountByStatus(ctx.tender.id, status);
  135. const allAttList = inspectionList.length > 0 ? await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.helper._.map(inspectionList, 'id')) : [];
  136. for (const item of inspectionList) {
  137. item.attList = ctx.helper._.filter(allAttList, { qiid: item.id });
  138. }
  139. // 分页相关
  140. const page = ctx.page;
  141. const pageSize = ctx.pageSize;
  142. const pageInfo = {
  143. page,
  144. pageSizeSelect: 1,
  145. pageSize,
  146. total_num: total,
  147. total: Math.ceil(total / pageSize),
  148. queryData: JSON.stringify(ctx.urlInfo.query),
  149. };
  150. let codeRule = [];
  151. let c_connector = '1';
  152. let c_rule_first = 1;
  153. const rule_type = 'safe_inspection';
  154. const tender = await this.service.tender.getDataById(ctx.tender.id);
  155. if (tender.c_code_rules) {
  156. const c_code_rules = JSON.parse(tender.c_code_rules);
  157. codeRule = c_code_rules[rule_type + '_rule'] !== undefined ? c_code_rules[rule_type + '_rule'] : [];
  158. c_connector = c_code_rules[rule_type + '_connector'] !== undefined ? c_code_rules[rule_type + '_connector'] : '1';
  159. c_rule_first = c_code_rules[rule_type + '_rule_first'] !== undefined ? c_code_rules[rule_type + '_rule_first'] : 1;
  160. }
  161. for (const rule of codeRule) {
  162. switch (rule.rule_type) {
  163. case codeRuleConst.measure.ruleType.dealCode:
  164. rule.preview = ctx.tender.info.deal_info.dealCode;
  165. break;
  166. case codeRuleConst.measure.ruleType.tenderName:
  167. rule.preview = tender.name;
  168. break;
  169. case codeRuleConst.measure.ruleType.inDate:
  170. rule.preview = moment().format('YYYY');
  171. break;
  172. case codeRuleConst.measure.ruleType.text:
  173. rule.preview = rule.text;
  174. break;
  175. case codeRuleConst.measure.ruleType.addNo:
  176. const s = '0000000000';
  177. rule.preview = s.substr(s.length - rule.format);
  178. break;
  179. default: break;
  180. }
  181. }
  182. const renderData = {
  183. moment,
  184. tender,
  185. permission: ctx.permission.safe_inspection,
  186. rule_type,
  187. codeRule,
  188. dealCode: ctx.tender.info.deal_info.dealCode,
  189. c_connector,
  190. c_rule_first,
  191. ruleType: codeRuleConst.ruleType[rule_type],
  192. ruleConst: codeRuleConst.measure,
  193. filter,
  194. inspectionList,
  195. auditType,
  196. auditConst: auditConst.inspection,
  197. status,
  198. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection),
  199. pageInfo,
  200. };
  201. await this.layout('safe/inspection.ejs', renderData, 'safe/inspection_modal.ejs');
  202. } catch (err) {
  203. ctx.log(err);
  204. ctx.postError(err, '无法查看安全巡检数据');
  205. ctx.redirect(`/sp/${ctx.subProject.id}/safe/inspection`);
  206. }
  207. }
  208. /**
  209. * 新增变更申请 (Post)
  210. *
  211. * @param {Object} ctx - egg全局变量
  212. * @return {void}
  213. */
  214. async inspectionSave(ctx) {
  215. try {
  216. const data = JSON.parse(ctx.request.body.data);
  217. const reponseData = {
  218. err: 0, msg: '', data: {},
  219. };
  220. switch (data.type) {
  221. case 'add':
  222. if (!data.code || data.code === '') {
  223. throw '编号不能为空';
  224. }
  225. if (!data.check_item || !data.check_date) {
  226. throw '请填写检查项和日期';
  227. }
  228. reponseData.data = await ctx.service.safeInspection.add(ctx.tender.id, ctx.session.sessionUser.accountId, data.code, data.check_item, data.check_date);
  229. break;
  230. default:throw '参数有误';
  231. }
  232. ctx.body = reponseData;
  233. } catch (err) {
  234. this.log(err);
  235. ctx.body = { err: 1, msg: err.toString() };
  236. }
  237. }
  238. /**
  239. * 获取审批界面所需的 原报、审批人数据等
  240. * @param ctx
  241. * @return {Promise<void>}
  242. * @private
  243. */
  244. async _getInspectionAuditViewData(ctx) {
  245. await ctx.service.safeInspection.loadAuditViewData(ctx.inspection);
  246. }
  247. async inspectionInformation(ctx) {
  248. try {
  249. const whiteList = this.ctx.app.config.multipart.whitelist;
  250. const tender = await ctx.service.tender.getDataById(ctx.tender.id);
  251. await this._getInspectionAuditViewData(ctx);
  252. // 获取附件列表
  253. const fileList = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  254. // 获取用户人验证手机号
  255. const renderData = {
  256. moment,
  257. tender,
  258. inspection: ctx.inspection,
  259. auditConst: auditConst.inspection,
  260. fileList,
  261. whiteList,
  262. auditType,
  263. shenpiConst,
  264. deleteFilePermission: PermissionCheck.delFile(this.ctx.session.sessionUser.permission),
  265. jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.safe.inspection_information),
  266. preUrl: `/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection/${ctx.inspection.id}/information`,
  267. };
  268. // data.accountGroup = accountGroup;
  269. // 获取所有项目参与者
  270. const accountList = await ctx.service.projectAccount.getAllSubProjectAccount(ctx.subProject);
  271. renderData.accountList = accountList;
  272. const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: ctx.session.sessionProject.id } });
  273. renderData.accountGroup = unitList.map(item => {
  274. const groupList = accountList.filter(item1 => item1.company === item.name);
  275. return { groupName: item.name, groupList };
  276. }).filter(x => { return x.groupList.length > 0; });
  277. await this.layout('safe/inspection_information.ejs', renderData, 'safe/inspection_information_modal.ejs');
  278. } catch (err) {
  279. this.log(err);
  280. ctx.redirect(`/sp/${ctx.subProject.id}/safe/tender/${ctx.tender.id}/inspection`);
  281. }
  282. }
  283. async inspectionInformationSave(ctx) {
  284. try {
  285. const data = JSON.parse(ctx.request.body.data);
  286. const reponseData = {
  287. err: 0, msg: '', data: {},
  288. };
  289. switch (data.type) {
  290. case 'update-field':
  291. if (!(!ctx.inspection.readOnly || ctx.inspection.rectificationPower)) {
  292. throw '当前状态不可修改';
  293. }
  294. if (data.update.check_item !== undefined && data.update.check_item === '') {
  295. throw '检查项不能为空';
  296. }
  297. if (data.update.check_date !== undefined && data.update.check_date === '') {
  298. throw '请填写检查日期';
  299. }
  300. if (data.update.rectification_item !== undefined && data.update.rectification_item === '') {
  301. throw '整改情况不能为空';
  302. }
  303. if (data.update.rectification_date !== undefined && data.update.rectification_date === '') {
  304. throw '请填写整改日期';
  305. }
  306. const fields = ['id', 'check_item', 'check_situation', 'action', 'check_date', 'inspector', 'rectification_item', 'rectification_date'];
  307. if (!this.checkFieldExists(data.update, fields)) {
  308. throw '参数有误';
  309. }
  310. reponseData.data = await ctx.service.safeInspection.defaultUpdate(data.update);
  311. break;
  312. case 'add-audit':
  313. const id = this.app._.toInteger(data.auditorId);
  314. if (isNaN(id) || id <= 0) {
  315. throw '参数错误';
  316. }
  317. // 检查权限等
  318. if (ctx.inspection.uid !== ctx.session.sessionUser.accountId) {
  319. throw '您无权添加审核人';
  320. }
  321. if (ctx.inspection.status !== auditConst.inspection.status.uncheck && ctx.inspection.status !== auditConst.inspection.status.checkNo) {
  322. throw '当前不允许添加审核人';
  323. }
  324. ctx.inspection.auditorList = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  325. // 检查审核人是否已存在
  326. const exist = this.app._.find(ctx.inspection.auditorList, { aid: id });
  327. if (exist) {
  328. throw '该审核人已存在,请勿重复添加';
  329. }
  330. const result = await ctx.service.safeInspectionAudit.addAuditor(ctx.inspection.id, id, ctx.inspection.times);
  331. if (!result) {
  332. throw '添加审核人失败';
  333. }
  334. reponseData.data = await ctx.service.safeInspectionAudit.getUserGroup(ctx.inspection.id, ctx.inspection.times);
  335. break;
  336. case 'del-audit':
  337. const id2 = data.auditorId instanceof Number ? data.auditorId : this.app._.toNumber(data.auditorId);
  338. if (isNaN(id2) || id2 <= 0) {
  339. throw '参数错误';
  340. }
  341. const result2 = await ctx.service.safeInspectionAudit.deleteAuditor(ctx.inspection.id, id2, ctx.inspection.times);
  342. if (!result2) {
  343. throw '移除审核人失败';
  344. }
  345. reponseData.data = await ctx.service.safeInspectionAudit.getAuditors(ctx.inspection.id, ctx.inspection.times);
  346. break;
  347. case 'start-inspection':
  348. if (ctx.inspection.readOnly) {
  349. throw '当前状态不可提交';
  350. }
  351. await ctx.service.safeInspectionAudit.start(ctx.inspection.id, ctx.inspection.times);
  352. break;
  353. case 'del-inspection':
  354. if (ctx.inspection.readOnly) {
  355. throw '当前状态不可删除';
  356. }
  357. await ctx.service.safeInspection.delInspection(ctx.inspection.id);
  358. break;
  359. case 'check':
  360. if (!ctx.inspection || !(ctx.inspection.status === auditConst.inspection.status.checking || ctx.inspection.status === auditConst.inspection.status.checkNoPre)) {
  361. throw '当前质量巡检数据有误';
  362. }
  363. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  364. throw '您无权进行该操作';
  365. }
  366. await ctx.service.safeInspectionAudit.check(ctx.inspection, data);
  367. break;
  368. case 'rectification':
  369. if (!ctx.inspection || ctx.inspection.status !== auditConst.inspection.status.rectification) {
  370. throw '当前质量巡检数据有误';
  371. }
  372. if (ctx.inspection.curAuditorIds.length === 0 || ctx.inspection.curAuditorIds.indexOf(ctx.session.sessionUser.accountId) === -1) {
  373. throw '您无权进行该操作';
  374. }
  375. await ctx.service.safeInspectionAudit.rectification(ctx.inspection, data);
  376. break;
  377. default:throw '参数有误';
  378. }
  379. ctx.body = reponseData;
  380. } catch (err) {
  381. this.log(err);
  382. ctx.body = { err: 1, msg: err.toString() };
  383. }
  384. }
  385. checkFieldExists(update, fields) {
  386. for (const field of Object.keys(update)) {
  387. if (!fields.includes(field)) {
  388. return false;
  389. }
  390. }
  391. return true;
  392. }
  393. /**
  394. * 上传附件
  395. * @param {*} ctx 上下文
  396. */
  397. async uploadInspectionFile(ctx) {
  398. let stream;
  399. try {
  400. // this._checkAdvanceFileCanModify(ctx);
  401. const parts = this.ctx.multipart({
  402. autoFields: true,
  403. });
  404. const files = [];
  405. const create_time = Date.parse(new Date()) / 1000;
  406. let idx = 0;
  407. const extra_upload = ctx.inspection.status === auditConst.inspection.status.checked;
  408. while ((stream = await parts()) !== undefined) {
  409. if (!stream.filename) {
  410. // 如果没有传入直接返回
  411. return;
  412. }
  413. const fileInfo = path.parse(stream.filename);
  414. const filepath = `app/public/upload/${this.ctx.tender.id.toString()}/safe_inspection/fujian_${create_time + idx.toString() + fileInfo.ext}`;
  415. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, 'app', filepath));
  416. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  417. files.push({ filepath, name: stream.filename, ext: fileInfo.ext });
  418. ++idx;
  419. stream && (await sendToWormhole(stream));
  420. }
  421. const in_time = new Date();
  422. const payload = files.map(file => {
  423. let idx;
  424. if (Array.isArray(parts.field.name)) {
  425. idx = parts.field.name.findIndex(name => name === file.name);
  426. } else {
  427. idx = 'isString';
  428. }
  429. const newFile = {
  430. tid: ctx.tender.id,
  431. qiid: ctx.inspection.id,
  432. uid: ctx.session.sessionUser.accountId,
  433. filename: file.name,
  434. fileext: file.ext,
  435. filesize: ctx.helper.bytesToSize(idx === 'isString' ? parts.field.size : parts.field.size[idx]),
  436. filepath: file.filepath,
  437. upload_time: in_time,
  438. extra_upload,
  439. };
  440. return newFile;
  441. });
  442. // 执行文件信息写入数据库
  443. await ctx.service.safeInspectionAtt.saveFileMsgToDb(payload);
  444. // 将最新的当前标段的所有文件信息返回
  445. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  446. ctx.body = { err: 0, msg: '', data };
  447. } catch (err) {
  448. stream && (await sendToWormhole(stream));
  449. this.log(err);
  450. ctx.body = { err: 1, msg: err.toString(), data: null };
  451. }
  452. }
  453. /**
  454. * 删除附件
  455. * @param {Ojbect} ctx 上下文
  456. */
  457. async deleteInspectionFile(ctx) {
  458. try {
  459. const { id } = JSON.parse(ctx.request.body.data);
  460. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  461. if (fileInfo || Object.keys(fileInfo).length) {
  462. // 先删除文件
  463. // await fs.unlinkSync(path.resolve(this.app.baseDir, './app', fileInfo.filepath));
  464. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  465. // 再删除数据库
  466. await ctx.service.safeInspectionAtt.delete(id);
  467. } else {
  468. throw '不存在该文件';
  469. }
  470. const data = await ctx.service.safeInspectionAtt.getAllAtt(ctx.tender.id, ctx.inspection.id);
  471. ctx.body = { err: 0, msg: '请求成功', data };
  472. } catch (err) {
  473. this.log(err);
  474. ctx.body = { err: 1, msg: err.toString(), data: null };
  475. }
  476. }
  477. /**
  478. * 下载附件
  479. * @param {Object} ctx - egg全局变量
  480. * @return {void}
  481. */
  482. async downloadInspectionFile(ctx) {
  483. const id = ctx.params.fid;
  484. if (id) {
  485. try {
  486. const fileInfo = await ctx.service.safeInspectionAtt.getDataById(id);
  487. if (fileInfo !== undefined && fileInfo !== '') {
  488. // const fileName = path.join(__dirname, '../', fileInfo.filepath);
  489. // 解决中文无法下载问题
  490. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  491. let disposition = '';
  492. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  493. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename);
  494. } else if (userAgent.indexOf('firefox') >= 0) {
  495. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename) + '"';
  496. } else {
  497. /* safari等其他非主流浏览器只能自求多福了 */
  498. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename).toString('binary');
  499. }
  500. ctx.response.set({
  501. 'Content-Type': 'application/octet-stream',
  502. 'Content-Disposition': disposition,
  503. 'Content-Length': fileInfo.filesize,
  504. });
  505. // ctx.body = await fs.createReadStream(fileName);
  506. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  507. } else {
  508. throw '不存在该文件';
  509. }
  510. } catch (err) {
  511. this.log(err);
  512. this.setMessage(err.toString(), this.messageType.ERROR);
  513. }
  514. }
  515. }
  516. }
  517. return SafeController;
  518. };