wap_controller.js 49 KB

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