safe_controller.js 54 KB

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