wap_controller.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  1. 'use strict';
  2. /**
  3. * 登录页面控制器
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/15
  7. * @version
  8. */
  9. const URL = require('url');
  10. const maintainConst = require('../const/maintain');
  11. const auditConst = require('../const/audit');
  12. const changeConst = require('../const/change');
  13. const advanceConst = require('../const/advance');
  14. const fs = require('fs');
  15. const path = require('path');
  16. const sendToWormhole = require('stream-wormhole');
  17. const moment = require('moment');
  18. const auditType = require('../const/audit').auditType;
  19. module.exports = app => {
  20. class WapController extends app.BaseController {
  21. /**
  22. * 登录页面
  23. *
  24. * @param {Object} ctx - egg全局页面
  25. * @return {void}
  26. */
  27. async index(ctx) {
  28. if (!ctx.helper.isMobile(ctx.request.header['user-agent'])) {
  29. ctx.redirect('/');
  30. return;
  31. }
  32. const errorMessage = ctx.session.loginError;
  33. // 显示完删除
  34. ctx.session.loginError = null;
  35. // 获取系统维护信息
  36. const maintainData = await ctx.service.maintain.getDataById(1);
  37. if (!ctx.app.config.is_debug) {
  38. await ctx.service.maintain.syncMaintainData();
  39. }
  40. let projectData = '';
  41. if (ctx.session.wapTenderID) {
  42. const tenderData = await ctx.service.tender.getDataById(ctx.session.wapTenderID);
  43. if (tenderData) projectData = await ctx.service.project.getDataById(tenderData.project_id);
  44. }
  45. const renderData = {
  46. maintainData,
  47. maintainConst,
  48. errorMessage,
  49. projectData,
  50. };
  51. await ctx.render('wap/login.ejs', renderData);
  52. }
  53. /**
  54. * 登录操作
  55. *
  56. * @param {Object} ctx - egg全局变量
  57. * @return {void}
  58. */
  59. async login(ctx) {
  60. let loginType = ctx.request.body.type;
  61. try {
  62. loginType = parseInt(loginType);
  63. const result = await ctx.service.projectAccount.accountLogin(ctx.request.body, loginType);
  64. if (!result) {
  65. throw '用户名或密码错误';
  66. }
  67. if (result === 2) {
  68. // 判断是否有设置停用提示,有则展示
  69. const msg = await ctx.service.projectStopmsg.getMsg(ctx.session.sessionProject.id);
  70. throw msg;
  71. }
  72. // 调用 rotateCsrfSecret 刷新用户的 CSRF token
  73. ctx.rotateCsrfSecret();
  74. // 判断是否已经有对应用户信息,没有则跳转初始化页面
  75. const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
  76. const url = needBoot ? '/boot' : '/wap/dashboard';
  77. const query = URL.parse(ctx.request.header.referer, true).query;
  78. let referer = '';
  79. if (ctx.session.wapTenderID) {
  80. referer = query.referer ? query.referer + '#shenpi' : url;
  81. ctx.session.wapTenderID = null;
  82. }
  83. ctx.redirect(referer ? referer : url);
  84. } catch (error) {
  85. this.log(error);
  86. ctx.session.loginError = error;
  87. ctx.redirect('/wap');
  88. }
  89. }
  90. /**
  91. * 退出登录
  92. *
  93. * @param {Object} ctx - egg全局变量
  94. * @return {void}
  95. */
  96. async logout(ctx) {
  97. // 删除session并跳转
  98. ctx.session = null;
  99. ctx.redirect('/wap');
  100. }
  101. /**
  102. * 待办页
  103. *
  104. * @param {Object} ctx - egg全局变量
  105. * @return {void}
  106. */
  107. async dashboard(ctx) {
  108. // 获取待审批的期
  109. const auditStages = await ctx.service.stageAudit.getAuditStageByWap(ctx.session.sessionUser.accountId);
  110. const subProjects = [];
  111. for (const audit of auditStages) {
  112. if (audit.status !== auditConst.stage.status.uncheck) {
  113. const isLastage = await ctx.service.stage.isLastStage(audit.tid, audit.id);
  114. if (isLastage) await this.ctx.service.stage.checkStageGatherData(audit);
  115. }
  116. audit.gather_tp = ctx.helper.sum([audit.contract_tp, audit.qc_tp, audit.pc_tp]);
  117. audit.end_contract_tp = ctx.helper.sum([audit.contract_tp, audit.pre_contract_tp, audit.contract_pc_tp]);
  118. audit.end_qc_tp = ctx.helper.sum([audit.qc_tp, audit.pre_qc_tp, audit.qc_pc_tp]);
  119. audit.pre_gather_tp = ctx.helper.add(audit.pre_contract_tp, audit.pre_qc_tp);
  120. audit.end_gather_tp = ctx.helper.add(audit.gather_tp, audit.pre_gather_tp);
  121. let sp = null;
  122. if (audit.spid) {
  123. if (ctx.helper._.findIndex(subProjects, { id: audit.spid }) !== -1) {
  124. sp = ctx.helper._.find(subProjects, { id: audit.spid });
  125. } else {
  126. sp = await ctx.service.subProject.getDataById(audit.spid);
  127. subProjects.push(sp);
  128. }
  129. }
  130. if (sp) {
  131. const pageShow = JSON.parse(sp.page_show);
  132. audit.closeWapYfSf = pageShow.closeWapYfSf || 0;
  133. } else {
  134. audit.closeWapYfSf = 0;
  135. }
  136. }
  137. // 获取待审批的变更期
  138. const auditChanges = await ctx.service.changeAudit.getAuditChangeByWap(ctx.session.sessionUser.accountId);
  139. // 获取待审批的变更立项
  140. const auditChangeProjects = [];
  141. const changeProjects = await ctx.service.changeProjectAudit.getAuditChangeProjectByWap(ctx.session.sessionUser.accountId);
  142. for (const cp of changeProjects) {
  143. let sp = null;
  144. if (cp.spid) {
  145. if (ctx.helper._.findIndex(subProjects, { id: cp.spid }) !== -1) {
  146. sp = ctx.helper._.find(subProjects, { id: cp.spid });
  147. } else {
  148. sp = await ctx.service.subProject.getDataById(cp.spid);
  149. subProjects.push(sp);
  150. }
  151. }
  152. if (sp) {
  153. const pageShow = JSON.parse(sp.page_show);
  154. if (pageShow.openChangeProject) {
  155. auditChangeProjects.push(cp);
  156. }
  157. } else {
  158. auditChangeProjects.push(cp);
  159. }
  160. }
  161. // 获取待审批的变更申请
  162. const auditChangeApplys = [];
  163. const changeApplys = await ctx.service.changeApplyAudit.getAuditChangeApplyByWap(ctx.session.sessionUser.accountId);
  164. for (const ca of changeApplys) {
  165. let sp = null;
  166. if (ca.spid) {
  167. if (ctx.helper._.findIndex(subProjects, { id: ca.spid }) !== -1) {
  168. sp = ctx.helper._.find(subProjects, { id: ca.spid });
  169. } else {
  170. sp = await ctx.service.subProject.getDataById(ca.spid);
  171. subProjects.push(sp);
  172. }
  173. }
  174. if (sp) {
  175. const pageShow = JSON.parse(sp.page_show);
  176. if (pageShow.openChangeApply) {
  177. auditChangeApplys.push(ca);
  178. }
  179. } else {
  180. auditChangeApplys.push(ca);
  181. }
  182. }
  183. // 获取待审批的变更方案
  184. const auditChangePlans = [];
  185. const changePlans = await ctx.service.changePlanAudit.getAuditChangePlanByWap(ctx.session.sessionUser.accountId);
  186. for (const cp of changePlans) {
  187. let sp = null;
  188. if (cp.spid) {
  189. if (ctx.helper._.findIndex(subProjects, { id: cp.spid }) !== -1) {
  190. sp = ctx.helper._.find(subProjects, { id: cp.spid });
  191. } else {
  192. sp = await ctx.service.subProject.getDataById(cp.spid);
  193. subProjects.push(sp);
  194. }
  195. }
  196. if (sp) {
  197. const pageShow = JSON.parse(sp.page_show);
  198. if (pageShow.openChangePlan) {
  199. auditChangePlans.push(cp);
  200. }
  201. } else {
  202. auditChangePlans.push(cp);
  203. }
  204. }
  205. // 获取待审批的台账修订
  206. const auditRevise = await ctx.service.reviseAudit.getAuditReviseByWap(ctx.session.sessionUser.accountId);
  207. for (const revise of auditRevise) {
  208. const yb_audit = await ctx.service.projectAccount.getAccountInfoById(revise.audit_id);
  209. revise.yb_name = yb_audit.name;
  210. }
  211. // 获取待审批的预付款
  212. const auditAdvance = await ctx.service.advanceAudit.getAuditAdvanceByWap(ctx.session.sessionUser.accountId);
  213. const renderData = {
  214. auditStages,
  215. auditChanges,
  216. auditChangeProjects,
  217. auditChangeApplys,
  218. auditChangePlans,
  219. auditRevise,
  220. auditAdvance,
  221. changeConst,
  222. advanceConst,
  223. tpUnit: 2,
  224. };
  225. await ctx.render('wap/dashboard.ejs', renderData);
  226. }
  227. /**
  228. * 标段列表页
  229. *
  230. * @param {Object} ctx - egg全局变量
  231. * @return {void}
  232. */
  233. async list(ctx) {
  234. try {
  235. // 获取用户新建标段权利
  236. const accountInfo = await this.ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  237. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  238. const tenderList = await this.ctx.service.tender.getBuildList('', userPermission);
  239. for (const t of tenderList) {
  240. await this.ctx.service.tenderCache.loadTenderCache(t, this.ctx.session.sessionUser.accountId);
  241. }
  242. const categoryData = await this.ctx.service.category.getAllCategory(ctx.subProject);
  243. const valuations = await this.ctx.service.valuation.getProjectValidValuation(this.ctx.session.sessionProject.id);
  244. const renderData = {
  245. tenderList,
  246. categoryData,
  247. auditConst,
  248. userPermission,
  249. valuations,
  250. uid: this.ctx.session.sessionUser.accountId,
  251. pid: this.ctx.session.sessionProject.id,
  252. };
  253. await ctx.render('wap/list.ejs', renderData);
  254. } catch (err) {
  255. this.log(err);
  256. this.ctx.redirect('/wap/dashboard');
  257. }
  258. }
  259. /**
  260. * 标段详细页
  261. *
  262. * @param {Object} ctx - egg全局变量
  263. * @return {void}
  264. */
  265. async tender(ctx) {
  266. try {
  267. const tender = ctx.tender.data;
  268. if (tender.user_id === this.ctx.session.sessionUser.accountId && (
  269. tender.ledger_status === auditConst.ledger.status.checkNo || tender.ledger_status === auditConst.ledger.status.uncheck)) {
  270. const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});
  271. tender.total_price = sum.total_price;
  272. tender.deal_tp = sum.deal_tp;
  273. }
  274. const [change_tp, change_p_tp, change_n_tp, change_valuation_tp, change_unvaluation_tp] = await ctx.service.change.getChangeTp(ctx.tender.id);
  275. tender.change_tp = change_tp;
  276. tender.change_p_tp = change_p_tp;
  277. tender.change_n_tp = change_n_tp;
  278. tender.change_valuation_tp = change_valuation_tp;
  279. tender.change_unvaluation_tp = change_unvaluation_tp;
  280. const stages = await ctx.service.stage.getValidStages(ctx.tender.id);
  281. const lastStage = stages.length > 0 ? stages[0] : null; //await ctx.service.stage.getLastestStage(ctx.tender.id);
  282. if (lastStage) {
  283. await this.ctx.service.stage.checkStageGatherData(lastStage);
  284. tender.gather_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.qc_tp, lastStage.pc_tp]);
  285. tender.end_contract_tp = ctx.helper.sum([lastStage.contract_tp, lastStage.pre_contract_tp, lastStage.contract_pc_tp]);
  286. tender.end_qc_tp = ctx.helper.sum([lastStage.qc_tp, lastStage.pre_qc_tp, lastStage.qc_pc_tp]);
  287. tender.pre_gather_tp = ctx.helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  288. tender.end_gather_tp = ctx.helper.add(tender.gather_tp, tender.pre_gather_tp);
  289. tender.yf_tp = lastStage.yf_tp;
  290. tender.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, ctx.tender.info.deal_param.contractPrice, 2), 100);
  291. tender.sum = ctx.helper.add(tender.total_price, tender.change_tp);
  292. tender.pre_ratio = ctx.helper.mul(ctx.helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  293. tender.cur_ratio = ctx.helper.mul(ctx.helper.div(tender.gather_tp, tender.sum, 2), 100);
  294. tender.other_tp = ctx.helper.sub(ctx.helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  295. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  296. }
  297. const monthProgress = [];
  298. for (const s of stages) {
  299. if (s.s_time) {
  300. let progress = monthProgress.find(function(x) {
  301. return x.month === s.s_time;
  302. });
  303. if (!progress) {
  304. progress = { month: s.s_time };
  305. monthProgress.push(progress);
  306. }
  307. progress.tp = ctx.helper.add(ctx.helper.add(progress.tp, s.contract_tp), s.qc_tp);
  308. }
  309. }
  310. monthProgress.sort(function(x, y) {
  311. return Date.parse(x.month) - Date.parse(y.month);
  312. });
  313. let sum = 0;
  314. for (const p of monthProgress) {
  315. p.ratio = ctx.helper.mul(ctx.helper.div(p.tp, tender.sum, 4), 100);
  316. sum = ctx.helper.add(sum, p.tp);
  317. p.end_tp = sum;
  318. p.end_ratio = ctx.helper.mul(ctx.helper.div(p.end_tp, tender.sum, 4), 100);
  319. }
  320. // 台账修订列表
  321. const revises = await ctx.service.ledgerRevise.getReviseList(ctx.tender.id);
  322. for (const lr of revises) {
  323. if (lr.valid) {
  324. lr.curAuditor = await ctx.service.reviseAudit.getAuditorByStatus(lr.id, lr.status, lr.times);
  325. }
  326. }
  327. // 预付款期数获取
  328. const advanceList = [];
  329. for (const t of advanceConst.typeCol) {
  330. const advance = await ctx.service.advance.getLastestAdvance(ctx.tender.id, t.type, true);
  331. advanceList.push(advance);
  332. }
  333. const renderData = {
  334. tender,
  335. stages,
  336. revises,
  337. advanceList,
  338. auditConst: auditConst.stage,
  339. auditReviseConst: auditConst.revise,
  340. advanceConst,
  341. tpUnit: ctx.tender.info.decimal.tp,
  342. monthProgress,
  343. stagesEcharts: JSON.parse(JSON.stringify(stages)).reverse(),
  344. auditType: auditConst.auditType,
  345. };
  346. if (stages.length > 0) {
  347. for (const s of stages) {
  348. // s.curAuditor = null;
  349. // 根据期状态返回展示用户
  350. s.curAuditors = await ctx.service.stageAudit.getAuditorsByStatus(s.id, s.status, s.times);
  351. if (s.status === auditConst.stage.status.checkNoPre) {
  352. s.curAuditors2 = await ctx.service.stageAudit.getAuditorsByStatus(s.id, auditConst.stage.status.checking);
  353. }
  354. }
  355. renderData.stage = stages[0];
  356. const times = renderData.stage.status === auditConst.stage.status.checkNo ? renderData.stage.times - 1 : renderData.stage.times;
  357. renderData.stage.user = await ctx.service.projectAccount.getAccountInfoById(renderData.stage.user_id);
  358. renderData.stage.auditors = await ctx.service.stageAudit.getAuditors(renderData.stage.id, times);
  359. // 获取审批流程中左边列表
  360. renderData.stage.auditors2 = await ctx.service.stageAudit.getAuditGroupByList(renderData.stage.id, times);
  361. }
  362. await ctx.render('wap/tender.ejs', renderData);
  363. } catch (err) {
  364. this.log(err);
  365. ctx.redirect('/wap/list');
  366. }
  367. }
  368. /**
  369. * 期审批详细页
  370. *
  371. * @param {Object} ctx - egg全局变量
  372. * @return {void}
  373. */
  374. async stage(ctx) {
  375. try {
  376. const tender = ctx.tender.data;
  377. // const stages = await ctx.service.stage.getValidStages(ctx.tender.id);
  378. // const lastStage = stages.length > 0 ? stages[0] : null;
  379. // if (lastStage) {
  380. // await this.ctx.service.stage.checkStageGatherData(lastStage);
  381. // }
  382. const stage = ctx.stage;
  383. if (stage) {
  384. if (stage.status !== auditConst.stage.status.checked) await this.ctx.service.stage.checkStageGatherData(stage);
  385. stage.tp = this.ctx.helper.sum([stage.contract_tp, stage.qc_tp, stage.pc_tp]);
  386. stage.pre_tp = this.ctx.helper.add(stage.pre_contract_tp, stage.pre_qc_tp);
  387. stage.end_tp = this.ctx.helper.add(stage.pre_tp, stage.tp);
  388. }
  389. await ctx.service.stage.loadStageAuditViewData(stage);
  390. const renderData = {
  391. moment,
  392. tender,
  393. stage,
  394. auditConst: auditConst.stage,
  395. auditType,
  396. };
  397. // const times = renderData.stage.status === auditConst.stage.status.checkNo ? renderData.stage.times - 1 : renderData.stage.times;
  398. // renderData.stage.user = await ctx.service.projectAccount.getAccountInfoById(renderData.stage.user_id);
  399. // renderData.stage.auditors = await ctx.service.stageAudit.getAuditors(renderData.stage.id, times);
  400. // // 获取审批流程中左边列表
  401. // renderData.stage.auditors2 = await ctx.service.stageAudit.getAuditGroupByList(renderData.stage.id, times);
  402. renderData.stage.lastAuditors = await ctx.service.stageAudit.getAuditorsByStatus(stage.id, stage.status, stage.times);
  403. await ctx.render('wap/shenpi_stage.ejs', renderData);
  404. } catch (err) {
  405. this.log(err);
  406. ctx.redirect('/wap/list');
  407. }
  408. }
  409. /**
  410. * 工程变更列表页
  411. *
  412. * @param {Object} ctx - egg全局变量
  413. * @return {void}
  414. */
  415. async changeIndex(ctx) {
  416. try {
  417. // 变更令列表
  418. const changes = await ctx.service.change.getListByStatus(ctx.tender.id, 0, 0);
  419. for (const c of changes) {
  420. c.showApprovalBtn = false;
  421. c.curAuditors = await ctx.service.changeAudit.getAuditorsByStatus(c.cid, c.status, c.times);
  422. if (c.status === auditConst.change.status.checkNoPre) {
  423. c.curAuditors2 = await ctx.service.changeAudit.getAuditorsByStatus(c.cid, auditConst.change.status.checking, c.times);
  424. const curAudit = c.curAuditors2.find(function(x) {
  425. return x.uid === ctx.session.sessionUser.accountId;
  426. });
  427. if (curAudit && curAudit.status === auditConst.change.status.checking) {
  428. c.showApprovalBtn = true;
  429. }
  430. } else {
  431. const curAudit = c.curAuditors.find(function(x) {
  432. return x.uid === ctx.session.sessionUser.accountId;
  433. });
  434. if (curAudit && curAudit.status === auditConst.change.status.checking) {
  435. c.showApprovalBtn = true;
  436. }
  437. }
  438. }
  439. // 变更立项列表
  440. let changeProjects = [];
  441. if (ctx.subProject.page_show.openChangeProject) {
  442. changeProjects = await ctx.service.changeProject.getListByStatus(ctx.tender.id, 0, 0);
  443. for (const c of changeProjects) {
  444. c.showApprovalBtn = false;
  445. c.curAuditors = await ctx.service.changeProjectAudit.getAuditorsByStatus(c.id, c.status, c.times);
  446. const curAudit = c.curAuditors.find(function(x) {
  447. return x.uid === ctx.session.sessionUser.accountId;
  448. });
  449. if (curAudit && curAudit.status === auditConst.changeProject.status.checking) {
  450. c.showApprovalBtn = true;
  451. }
  452. }
  453. }
  454. // 变更申请列表
  455. let changeApplys = [];
  456. if (ctx.subProject.page_show.openChangeApply) {
  457. changeApplys = await ctx.service.changeApply.getListByStatus(ctx.tender.id, 0, 0);
  458. for (const c of changeApplys) {
  459. c.showApprovalBtn = false;
  460. c.curAuditors = await ctx.service.changeApplyAudit.getAuditorsByStatus(c.id, c.status, c.times);
  461. const curAudit = c.curAuditors.find(function(x) {
  462. return x.uid === ctx.session.sessionUser.accountId;
  463. });
  464. if (curAudit && curAudit.status === auditConst.changeApply.status.checking) {
  465. c.showApprovalBtn = true;
  466. }
  467. }
  468. }
  469. // 变更方案列表
  470. let changePlans = [];
  471. if (ctx.subProject.page_show.openChangePlan) {
  472. changePlans = await ctx.service.changePlan.getListByStatus(ctx.tender.id, 0, 0);
  473. for (const c of changePlans) {
  474. c.showApprovalBtn = false;
  475. c.curAuditors = await ctx.service.changePlanAudit.getAuditorsByStatus(c.id, c.status, c.times);
  476. const curAudit = c.curAuditors.find(function(x) {
  477. return x.uid === ctx.session.sessionUser.accountId;
  478. });
  479. if (curAudit && curAudit.status === auditConst.changePlan.status.checking) {
  480. c.showApprovalBtn = true;
  481. }
  482. }
  483. }
  484. const renderData = {
  485. tender: ctx.tender,
  486. changes,
  487. changeProjects,
  488. changeApplys,
  489. changePlans,
  490. auditChangeConst: auditConst.change,
  491. auditChangeProjectConst: auditConst.changeProject,
  492. auditChangeApplyConst: auditConst.changeApply,
  493. auditChangePlanConst: auditConst.changePlan,
  494. changeConst,
  495. tpUnit: ctx.tender.info.decimal.tp,
  496. auditType: auditConst.auditType,
  497. };
  498. await ctx.render('wap/shenpi_change_index.ejs', renderData);
  499. } catch (err) {
  500. this.log(err);
  501. ctx.redirect('/wap/list');
  502. }
  503. }
  504. /**
  505. * 变更审批详细页
  506. *
  507. * @param {Object} ctx - egg全局变量
  508. * @return {void}
  509. */
  510. async change(ctx) {
  511. try {
  512. const tender = ctx.tender.data;
  513. const change = ctx.change;
  514. const times = change.status !== auditConst.change.status.checkNo && change.status !== auditConst.change.status.revise ? change.times : change.times - 1;
  515. const auditList = await ctx.service.changeAudit.getListOrderByTimes(change.cid, times);
  516. const auditGroupList = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  517. await ctx.service.change.loadChangeAuditViewData(ctx.change);
  518. const renderData = {
  519. moment,
  520. tender,
  521. change,
  522. auditList,
  523. auditGroupList,
  524. auditConst: auditConst.change,
  525. changeConst,
  526. tpUnit: ctx.tender.info.decimal.tp,
  527. auditType,
  528. };
  529. await ctx.render('wap/shenpi_change.ejs', renderData);
  530. } catch (err) {
  531. this.log(err);
  532. ctx.redirect('/wap/list');
  533. }
  534. }
  535. /**
  536. * 变更立项审批详细页
  537. *
  538. * @param {Object} ctx - egg全局变量
  539. * @return {void}
  540. */
  541. async changeProject(ctx) {
  542. try {
  543. const tender = ctx.tender.data;
  544. const change = ctx.change;
  545. await ctx.service.changeProject.loadChangeAuditViewData(ctx.change);
  546. const renderData = {
  547. moment,
  548. tender,
  549. change,
  550. auditConst: auditConst.changeProject,
  551. changeConst,
  552. tpUnit: ctx.tender.info.decimal.tp,
  553. auditType,
  554. };
  555. await ctx.render('wap/shenpi_change_project.ejs', renderData);
  556. } catch (err) {
  557. this.log(err);
  558. ctx.redirect('/wap/list');
  559. }
  560. }
  561. /**
  562. * 变更申请审批详细页
  563. *
  564. * @param {Object} ctx - egg全局变量
  565. * @return {void}
  566. */
  567. async changeApply(ctx) {
  568. try {
  569. const tender = ctx.tender.data;
  570. const change = ctx.change;
  571. await ctx.service.changeApply.loadChangeAuditViewData(ctx.change);
  572. const renderData = {
  573. moment,
  574. tender,
  575. change,
  576. auditConst: auditConst.changeApply,
  577. changeConst,
  578. tpUnit: ctx.tender.info.decimal.tp,
  579. auditType,
  580. };
  581. await ctx.render('wap/shenpi_change_apply.ejs', renderData);
  582. } catch (err) {
  583. this.log(err);
  584. ctx.redirect('/wap/list');
  585. }
  586. }
  587. /**
  588. * 变更方案审批详细页
  589. *
  590. * @param {Object} ctx - egg全局变量
  591. * @return {void}
  592. */
  593. async changePlan(ctx) {
  594. try {
  595. const tender = ctx.tender.data;
  596. const change = ctx.change;
  597. await ctx.service.changePlan.loadChangeAuditViewData(ctx.change);
  598. const renderData = {
  599. moment,
  600. tender,
  601. change,
  602. auditConst: auditConst.changeApply,
  603. changeConst,
  604. tpUnit: ctx.tender.info.decimal.tp,
  605. auditType,
  606. };
  607. await ctx.render('wap/shenpi_change_plan.ejs', renderData);
  608. } catch (err) {
  609. this.log(err);
  610. ctx.redirect('/wap/list');
  611. }
  612. }
  613. /**
  614. * 修订审批详细页
  615. *
  616. * @param {Object} ctx - egg全局变量
  617. * @return {void}
  618. */
  619. async revise(ctx) {
  620. try {
  621. const tender = ctx.tender.data;
  622. const revise = await ctx.service.ledgerRevise.getDataByCondition({ id: ctx.params.rid });
  623. revise.user = await ctx.service.projectAccount.getAccountInfoById(revise.uid);
  624. const times = revise.status === auditConst.revise.status.checkNo ? revise.times - 1 : revise.times;
  625. revise.curAuditor = await ctx.service.reviseAudit.getAuditorByStatus(revise.id, revise.status, times);
  626. revise.auditors = await ctx.service.reviseAudit.getAuditors(revise.id, times);
  627. console.log(times, revise.auditors);
  628. const renderData = {
  629. tender,
  630. revise,
  631. auditConst: auditConst.revise,
  632. };
  633. await ctx.render('wap/shenpi_revise.ejs', renderData);
  634. } catch (err) {
  635. this.log(err);
  636. ctx.redirect('/wap/list');
  637. }
  638. }
  639. /**
  640. * 预付款列表页
  641. *
  642. * @param {Object} ctx - egg全局变量
  643. * @return {void}
  644. */
  645. async advance(ctx) {
  646. try {
  647. const tender = ctx.tender.data;
  648. const { decimal } = ctx.tender.info;
  649. this.decimal = decimal.pay ? decimal.payTp : decimal.tp;
  650. const advancePayTotalList = [];
  651. const advanceList = [];
  652. for (const t of advanceConst.typeCol) {
  653. const advancePayTotal = ctx.tender.info.deal_param[t.key + 'Advance'];
  654. advancePayTotalList.push(advancePayTotal);
  655. const advances = await ctx.service.advance.getAdvanceList(ctx.tender.id, t.type, this.decimal, advancePayTotal);
  656. advanceList.push(advances);
  657. }
  658. const renderData = {
  659. tender,
  660. advancePayTotalList,
  661. advanceList,
  662. auditConst: auditConst.advance,
  663. advanceConst,
  664. };
  665. await ctx.render('wap/shenpi_advance.ejs', renderData);
  666. } catch (err) {
  667. this.log(err);
  668. ctx.redirect('/wap/list');
  669. }
  670. }
  671. /**
  672. * 预付款详情页 GET
  673. * @param {Object} ctx 全局上下文
  674. */
  675. async advanceDetail(ctx) {
  676. try {
  677. const tender = ctx.tender.data;
  678. ctx.advance.advancePayTotal = ctx.tender.info.deal_param[ advanceConst.typeCol[ctx.advance.type].key + 'Advance'];
  679. const times = ctx.advance.status === auditConst.advance.status.checkNo ? ctx.advance.times - 1 : ctx.advance.times;
  680. if (ctx.advance.status === auditConst.advance.status.checkNo) {
  681. ctx.advance.curAuditor = await ctx.service.advanceAudit.getAuditorByStatus(ctx.advance.id, ctx.advance.status, times);
  682. ctx.advance.auditors = await ctx.service.advanceAudit.getAuditors(ctx.advance.id, times);
  683. }
  684. // 获取审批流程中左边列表
  685. ctx.advance.auditors2 = await ctx.service.advanceAudit.getAuditGroupByList(ctx.advance.id, times);
  686. const renderData = {
  687. tender,
  688. advance: ctx.advance,
  689. auditConst: auditConst.advance,
  690. advanceConst,
  691. };
  692. await ctx.render('wap/shenpi_advance_detail.ejs', renderData);
  693. } catch (error) {
  694. this.log(error);
  695. ctx.redirect('/wap/tender/' + ctx.tender.id + '/advance');
  696. }
  697. }
  698. /**
  699. * 变更令审批
  700. * @param {Object} ctx - egg全局变量
  701. * @return {void}
  702. */
  703. async changeApproval(ctx) {
  704. try {
  705. const cid = ctx.request.body.change_id;
  706. const changeData = await ctx.service.change.getDataByCondition({ cid });
  707. if (!changeData) {
  708. throw '变更令数据错误';
  709. }
  710. // 判断是否到你审批,如果不是则无法审批
  711. const curAuditor = await ctx.service.changeAudit.getCurAuditors(changeData.cid, changeData.times);
  712. if (!curAuditor || (curAuditor && ctx.helper._.findIndex(curAuditor, { uid: ctx.session.sessionUser.accountId }) === -1)) {
  713. throw '该变更令当前您无权操作';
  714. }
  715. const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
  716. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  717. throw '结算数据发生变化,请刷新页面再提交';
  718. }
  719. const status = parseInt(ctx.request.body.status);
  720. const pid = this.ctx.session.sessionProject.id;
  721. let result = false;
  722. switch (status) {
  723. case 3:// 审批通过
  724. result = await ctx.service.change.approvalSuccess(pid, ctx.request.body, changeData);
  725. break;
  726. // case 4:// 审批终止
  727. // result = await ctx.service.change.approvalStop(ctx.request.body);
  728. // break;
  729. case 5:// 审批退回到原报人
  730. result = await ctx.service.change.approvalCheckNo(pid, ctx.request.body, changeData);
  731. break;
  732. case 6:// 审批退回到上一个审批人
  733. result = await ctx.service.change.approvalCheckNoPre(pid, ctx.request.body, changeData);
  734. break;
  735. default:break;
  736. }
  737. if (!result) {
  738. throw '审批失败';
  739. }
  740. ctx.redirect(ctx.request.header.referer);
  741. } catch (err) {
  742. console.log(err);
  743. ctx.redirect(ctx.request.header.referer);
  744. }
  745. }
  746. /**
  747. * 收方单附件上传页
  748. * @param {Object} ctx - egg全局变量
  749. * @return {void}
  750. */
  751. async shoufangUpload(ctx) {
  752. try {
  753. const tid = parseInt(ctx.query.tid) || 0;
  754. const order = parseInt(ctx.query.order) || 0;
  755. const sfid = parseInt(ctx.query.sfid) || 0;
  756. if (!tid || !order || !sfid) {
  757. throw '参数有误';
  758. }
  759. const tender = await ctx.service.tender.getDataById(tid);
  760. if (!tender) {
  761. throw '该标段不存在';
  762. }
  763. const sfInfo = await ctx.service.stageShoufang.getDataByCondition({ id: sfid, tid, order });
  764. if (!sfInfo) {
  765. throw '该收方单不存在';
  766. }
  767. // 查找对应的台账或计量单元名称
  768. const ledger = await ctx.service.ledger.getData(tid);
  769. const ledgerInfo = ctx.helper._.find(ledger, { id: sfInfo.lid });
  770. let name = ledgerInfo.b_code;
  771. if (sfInfo.pid) {
  772. const pos = await ctx.service.pos.getPosData({ tid });
  773. const posInfo = ctx.helper._.find(pos, { lid: sfInfo.lid, id: sfInfo.pid });
  774. name += ' / ' + posInfo.name;
  775. }
  776. const renderData = {
  777. tender,
  778. order,
  779. name,
  780. sfid,
  781. whiteList: this.ctx.app.config.multipart.whitelist,
  782. };
  783. await ctx.render('wap/shoufangupload.ejs', renderData);
  784. } catch (error) {
  785. this.log(error);
  786. ctx.redirect('/wap/login');
  787. }
  788. }
  789. /**
  790. * 上传附件
  791. * @param {Object} ctx - egg全局变量
  792. * @return {void}
  793. */
  794. async shoufangUpFile(ctx) {
  795. const responseData = {
  796. err: 0,
  797. msg: '',
  798. data: [],
  799. };
  800. let stream;
  801. try {
  802. const parts = ctx.multipart({ autoFields: true });
  803. const files = [];
  804. let index = 0;
  805. while ((stream = await parts()) !== undefined) {
  806. // 判断是否存在
  807. const sfInfo = await ctx.service.stageShoufang.getDataById(parts.field.sfid);
  808. if (!sfInfo) {
  809. throw '该清单 / 计量单元下不存在收方单';
  810. }
  811. // 判断用户是否选择上传文件
  812. if (!stream.filename) {
  813. throw '请选择上传的文件!';
  814. }
  815. // const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
  816. // 判断文件夹是否存在,不存在则直接创建文件夹
  817. // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
  818. // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
  819. // }
  820. const fileInfo = path.parse(stream.filename);
  821. const now_time = new Date();
  822. const create_time = Date.parse(now_time) / 1000;
  823. const filepath = `app/public/upload/${sfInfo.tid}/stage/shoufang/fujian_${create_time + index.toString() + fileInfo.ext}`;
  824. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  825. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  826. // console.log(await fs.existsSync(path.resolve(this.app.baseDir, 'app', filepath)));
  827. // const fileInfo = path.parse(stream.filename);
  828. // const fileName = 'stage' + create_time + '_' + index + fileInfo.ext;
  829. // 保存文件
  830. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, dirName, fileName));
  831. if (stream) {
  832. await sendToWormhole(stream);
  833. }
  834. // 保存数据到att表
  835. const fileData = {
  836. tid: sfInfo.tid,
  837. sid: sfInfo.sid,
  838. sfid: sfInfo.id,
  839. in_time: now_time,
  840. filename: fileInfo.name,
  841. fileext: fileInfo.ext,
  842. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  843. filepath,
  844. };
  845. // if (ctx.reUploadPermission) {
  846. // fileData.re_upload = 1;
  847. // }
  848. const result = await ctx.service.stageShoufangAtt.save(fileData);
  849. if (!result) {
  850. throw '导入数据库保存失败';
  851. }
  852. const attData = await ctx.service.stageShoufangAtt.getDataByFid(result.insertId);
  853. attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
  854. files.length !== 0 ? files.unshift(attData) : files.push(attData);
  855. ++index;
  856. }
  857. responseData.data = files;
  858. } catch (err) {
  859. this.log(err);
  860. // 失败需要消耗掉stream 以防卡死
  861. if (stream) {
  862. await sendToWormhole(stream);
  863. }
  864. this.setMessage(err.toString(), this.messageType.ERROR);
  865. responseData.err = 1;
  866. responseData.msg = err.toString();
  867. }
  868. ctx.body = responseData;
  869. }
  870. /**
  871. * 删除附件
  872. * @param {Object} ctx - egg全局变量
  873. * @return {void}
  874. */
  875. async shoufangDeleteFile(ctx) {
  876. const responseData = {
  877. err: 0,
  878. msg: '',
  879. data: '',
  880. };
  881. try {
  882. const data = JSON.parse(ctx.request.body.data);
  883. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(data.id);
  884. if (!fileInfo || !Object.keys(fileInfo).length) {
  885. throw '该文件不存在';
  886. }
  887. if (fileInfo !== undefined && fileInfo !== '') {
  888. // 先删除文件
  889. // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  890. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  891. // 再删除数据库
  892. await ctx.service.stageShoufangAtt.deleteById(data.id);
  893. responseData.data = '';
  894. } else {
  895. throw '不存在该文件';
  896. }
  897. } catch (err) {
  898. responseData.err = 1;
  899. responseData.msg = err;
  900. }
  901. ctx.body = responseData;
  902. }
  903. /**
  904. * 编辑附件
  905. * @param {Object} ctx - egg全局变量
  906. * @return {void}
  907. */
  908. async shoufangEditFile(ctx) {
  909. const responseData = {
  910. err: 0,
  911. msg: '',
  912. data: '',
  913. };
  914. try {
  915. const data = JSON.parse(ctx.request.body.data);
  916. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(data.id);
  917. if (!fileInfo || !Object.keys(fileInfo).length) {
  918. throw '该文件不存在';
  919. }
  920. await ctx.service.stageShoufangAtt.edit(data);
  921. } catch (err) {
  922. responseData.err = 1;
  923. responseData.msg = err;
  924. }
  925. ctx.body = responseData;
  926. }
  927. /**
  928. * 下载附件
  929. * @param {Object} ctx - egg全局变量
  930. * @return {void}
  931. */
  932. async messageDownloadFile(ctx) {
  933. const id = ctx.params.fid;
  934. if (id) {
  935. try {
  936. const fileInfo = await ctx.service.messageAtt.getDataById(id);
  937. if (fileInfo !== undefined && fileInfo !== '') {
  938. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  939. // 解决中文无法下载问题
  940. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  941. let disposition = '';
  942. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  943. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  944. } else if (userAgent.indexOf('firefox') >= 0) {
  945. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  946. } else {
  947. /* safari等其他非主流浏览器只能自求多福了 */
  948. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  949. }
  950. ctx.response.set({
  951. 'Content-Type': 'application/octet-stream',
  952. 'Content-Disposition': disposition,
  953. 'Content-Length': fileInfo.filesize,
  954. });
  955. // ctx.body = await fs.createReadStream(fileName);
  956. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  957. } else {
  958. throw '不存在该文件';
  959. }
  960. } catch (err) {
  961. this.log(err);
  962. this.setMessage(err.toString(), this.messageType.ERROR);
  963. }
  964. }
  965. }
  966. /**
  967. * 下载附件
  968. * @param {Object} ctx - egg全局变量
  969. * @return {void}
  970. */
  971. async shoufangDownloadFile(ctx) {
  972. const id = ctx.params.fid;
  973. if (id) {
  974. try {
  975. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(id);
  976. if (fileInfo !== undefined && fileInfo !== '') {
  977. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  978. // 解决中文无法下载问题
  979. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  980. let disposition = '';
  981. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  982. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  983. } else if (userAgent.indexOf('firefox') >= 0) {
  984. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  985. } else {
  986. /* safari等其他非主流浏览器只能自求多福了 */
  987. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  988. }
  989. ctx.response.set({
  990. 'Content-Type': 'application/octet-stream',
  991. 'Content-Disposition': disposition,
  992. 'Content-Length': fileInfo.filesize,
  993. });
  994. // ctx.body = await fs.createReadStream(fileName);
  995. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  996. } else {
  997. throw '不存在该文件';
  998. }
  999. } catch (err) {
  1000. this.log(err);
  1001. this.setMessage(err.toString(), this.messageType.ERROR);
  1002. }
  1003. }
  1004. }
  1005. async msg(ctx) {
  1006. try {
  1007. const msgId = parseInt(ctx.params.id) || 0;
  1008. if (!msgId) {
  1009. throw '参数有误';
  1010. }
  1011. const msgInfo = await ctx.service.message.getDataById(msgId);
  1012. const files = await ctx.service.messageAtt.getAtt(msgId);
  1013. if (!msgInfo) {
  1014. throw '项目通知不存在';
  1015. }
  1016. if (msgInfo && msgInfo.type === 1 && msgInfo.project_id !== ctx.session.sessionProject.id) {
  1017. throw '非该项目通知无权查看';
  1018. }
  1019. if (msgInfo) {
  1020. msgInfo.content = JSON.parse(JSON.stringify(msgInfo.content).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;'));
  1021. }
  1022. const renderData = {
  1023. msgInfo,
  1024. moment,
  1025. files,
  1026. };
  1027. await ctx.render('wap/msg.ejs', renderData);
  1028. } catch (error) {
  1029. console.log(error);
  1030. this.log(error);
  1031. ctx.redirect('/wap/dashboard');
  1032. }
  1033. }
  1034. }
  1035. return WapController;
  1036. };