wap_controller.js 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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.uid === 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.uid === 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.uid === ctx.session.sessionUser.accountId;
  488. });
  489. if (curAudit && curAudit.status === auditConst.changePlan.status.checking) {
  490. c.showApprovalBtn = true;
  491. }
  492. }
  493. }
  494. const renderData = {
  495. tender: ctx.tender,
  496. changes,
  497. changeProjects,
  498. changeApplys,
  499. changePlans,
  500. auditChangeConst: auditConst.change,
  501. auditChangeProjectConst: auditConst.changeProject,
  502. auditChangeApplyConst: auditConst.changeApply,
  503. auditChangePlanConst: auditConst.changePlan,
  504. changeConst,
  505. tpUnit: ctx.tender.info.decimal.tp,
  506. auditType: auditConst.auditType,
  507. };
  508. await ctx.render('wap/shenpi_change_index.ejs', renderData);
  509. } catch (err) {
  510. this.log(err);
  511. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  512. }
  513. }
  514. /**
  515. * 变更审批详细页
  516. *
  517. * @param {Object} ctx - egg全局变量
  518. * @return {void}
  519. */
  520. async change(ctx) {
  521. try {
  522. const tender = ctx.tender.data;
  523. const change = ctx.change;
  524. const times = change.status !== auditConst.change.status.checkNo && change.status !== auditConst.change.status.revise ? change.times : change.times - 1;
  525. const auditList = await ctx.service.changeAudit.getListOrderByTimes(change.cid, times);
  526. const auditGroupList = await ctx.service.changeAudit.getListGroupByTimes(change.cid, times);
  527. await ctx.service.change.loadChangeAuditViewData(ctx.change);
  528. const renderData = {
  529. moment,
  530. tender,
  531. change,
  532. auditList,
  533. auditGroupList,
  534. auditConst: auditConst.change,
  535. changeConst,
  536. tpUnit: ctx.tender.info.decimal.tp,
  537. auditType,
  538. };
  539. await ctx.render('wap/shenpi_change.ejs', renderData);
  540. } catch (err) {
  541. this.log(err);
  542. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  543. }
  544. }
  545. /**
  546. * 变更立项审批详细页
  547. *
  548. * @param {Object} ctx - egg全局变量
  549. * @return {void}
  550. */
  551. async changeProject(ctx) {
  552. try {
  553. const tender = ctx.tender.data;
  554. const change = ctx.change;
  555. await ctx.service.changeProject.loadChangeAuditViewData(ctx.change);
  556. const renderData = {
  557. moment,
  558. tender,
  559. change,
  560. auditConst: auditConst.changeProject,
  561. changeConst,
  562. tpUnit: ctx.tender.info.decimal.tp,
  563. auditType,
  564. };
  565. await ctx.render('wap/shenpi_change_project.ejs', renderData);
  566. } catch (err) {
  567. this.log(err);
  568. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  569. }
  570. }
  571. /**
  572. * 变更申请审批详细页
  573. *
  574. * @param {Object} ctx - egg全局变量
  575. * @return {void}
  576. */
  577. async changeApply(ctx) {
  578. try {
  579. const tender = ctx.tender.data;
  580. const change = ctx.change;
  581. await ctx.service.changeApply.loadChangeAuditViewData(ctx.change);
  582. const renderData = {
  583. moment,
  584. tender,
  585. change,
  586. auditConst: auditConst.changeApply,
  587. changeConst,
  588. tpUnit: ctx.tender.info.decimal.tp,
  589. auditType,
  590. };
  591. await ctx.render('wap/shenpi_change_apply.ejs', renderData);
  592. } catch (err) {
  593. this.log(err);
  594. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  595. }
  596. }
  597. /**
  598. * 变更方案审批详细页
  599. *
  600. * @param {Object} ctx - egg全局变量
  601. * @return {void}
  602. */
  603. async changePlan(ctx) {
  604. try {
  605. const tender = ctx.tender.data;
  606. const change = ctx.change;
  607. await ctx.service.changePlan.loadChangeAuditViewData(ctx.change);
  608. const renderData = {
  609. moment,
  610. tender,
  611. change,
  612. auditConst: auditConst.changeApply,
  613. changeConst,
  614. tpUnit: ctx.tender.info.decimal.tp,
  615. auditType,
  616. };
  617. await ctx.render('wap/shenpi_change_plan.ejs', renderData);
  618. } catch (err) {
  619. this.log(err);
  620. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  621. }
  622. }
  623. /**
  624. * 修订审批详细页
  625. *
  626. * @param {Object} ctx - egg全局变量
  627. * @return {void}
  628. */
  629. async revise(ctx) {
  630. try {
  631. const tender = ctx.tender.data;
  632. const revise = await ctx.service.ledgerRevise.getDataByCondition({ id: ctx.params.rid });
  633. revise.user = await ctx.service.projectAccount.getAccountInfoById(revise.uid);
  634. const times = revise.status === auditConst.revise.status.checkNo ? revise.times - 1 : revise.times;
  635. revise.curAuditor = await ctx.service.reviseAudit.getAuditorByStatus(revise.id, revise.status, times);
  636. revise.auditors = await ctx.service.reviseAudit.getAuditors(revise.id, times);
  637. console.log(times, revise.auditors);
  638. const renderData = {
  639. tender,
  640. revise,
  641. auditConst: auditConst.revise,
  642. };
  643. await ctx.render('wap/shenpi_revise.ejs', renderData);
  644. } catch (err) {
  645. this.log(err);
  646. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  647. }
  648. }
  649. /**
  650. * 预付款列表页
  651. *
  652. * @param {Object} ctx - egg全局变量
  653. * @return {void}
  654. */
  655. async advance(ctx) {
  656. try {
  657. const tender = ctx.tender.data;
  658. const { decimal } = ctx.tender.info;
  659. this.decimal = decimal.pay ? decimal.payTp : decimal.tp;
  660. const advancePayTotalList = [];
  661. const advanceList = [];
  662. for (const t of advanceConst.typeCol) {
  663. const advancePayTotal = ctx.tender.info.deal_param[t.key + 'Advance'];
  664. advancePayTotalList.push(advancePayTotal);
  665. const advances = await ctx.service.advance.getAdvanceList(ctx.tender.id, t.type, this.decimal, advancePayTotal);
  666. advanceList.push(advances);
  667. }
  668. const renderData = {
  669. tender,
  670. advancePayTotalList,
  671. advanceList,
  672. auditConst: auditConst.advance,
  673. advanceConst,
  674. };
  675. await ctx.render('wap/shenpi_advance.ejs', renderData);
  676. } catch (err) {
  677. this.log(err);
  678. ctx.redirect('/wap/sp/' + ctx.subProject.id + '/list');
  679. }
  680. }
  681. /**
  682. * 预付款详情页 GET
  683. * @param {Object} ctx 全局上下文
  684. */
  685. async advanceDetail(ctx) {
  686. try {
  687. const tender = ctx.tender.data;
  688. ctx.advance.advancePayTotal = ctx.tender.info.deal_param[ advanceConst.typeCol[ctx.advance.type].key + 'Advance'];
  689. const times = ctx.advance.status === auditConst.advance.status.checkNo ? ctx.advance.times - 1 : ctx.advance.times;
  690. if (ctx.advance.status === auditConst.advance.status.checkNo) {
  691. ctx.advance.curAuditor = await ctx.service.advanceAudit.getAuditorByStatus(ctx.advance.id, ctx.advance.status, times);
  692. ctx.advance.auditors = await ctx.service.advanceAudit.getAuditors(ctx.advance.id, times);
  693. }
  694. // 获取审批流程中左边列表
  695. ctx.advance.auditors2 = await ctx.service.advanceAudit.getAuditGroupByList(ctx.advance.id, times);
  696. const renderData = {
  697. tender,
  698. advance: ctx.advance,
  699. auditConst: auditConst.advance,
  700. advanceConst,
  701. };
  702. await ctx.render('wap/shenpi_advance_detail.ejs', renderData);
  703. } catch (error) {
  704. this.log(error);
  705. ctx.redirect('/wap/tender/' + ctx.tender.id + '/advance');
  706. }
  707. }
  708. /**
  709. * 变更令审批
  710. * @param {Object} ctx - egg全局变量
  711. * @return {void}
  712. */
  713. async changeApproval(ctx) {
  714. try {
  715. const cid = ctx.request.body.change_id;
  716. const changeData = await ctx.service.change.getDataByCondition({ cid });
  717. if (!changeData) {
  718. throw '变更令数据错误';
  719. }
  720. // 判断是否到你审批,如果不是则无法审批
  721. const curAuditor = await ctx.service.changeAudit.getCurAuditors(changeData.cid, changeData.times);
  722. if (!curAuditor || (curAuditor && ctx.helper._.findIndex(curAuditor, { uid: ctx.session.sessionUser.accountId }) === -1)) {
  723. throw '该变更令当前您无权操作';
  724. }
  725. const readySettle = await ctx.service.settle.getReadySettle(changeData.tid);
  726. if (readySettle && readySettle.settle_order !== ctx.tender.data.settle_order) {
  727. throw '结算数据发生变化,请刷新页面再提交';
  728. }
  729. const status = parseInt(ctx.request.body.status);
  730. const pid = this.ctx.session.sessionProject.id;
  731. let result = false;
  732. switch (status) {
  733. case 3:// 审批通过
  734. result = await ctx.service.change.approvalSuccess(pid, ctx.request.body, changeData);
  735. break;
  736. // case 4:// 审批终止
  737. // result = await ctx.service.change.approvalStop(ctx.request.body);
  738. // break;
  739. case 5:// 审批退回到原报人
  740. result = await ctx.service.change.approvalCheckNo(pid, ctx.request.body, changeData);
  741. break;
  742. case 6:// 审批退回到上一个审批人
  743. result = await ctx.service.change.approvalCheckNoPre(pid, ctx.request.body, changeData);
  744. break;
  745. default:break;
  746. }
  747. if (!result) {
  748. throw '审批失败';
  749. }
  750. ctx.redirect(ctx.request.header.referer);
  751. } catch (err) {
  752. console.log(err);
  753. ctx.redirect(ctx.request.header.referer);
  754. }
  755. }
  756. /**
  757. * 收方单附件上传页
  758. * @param {Object} ctx - egg全局变量
  759. * @return {void}
  760. */
  761. async shoufangUpload(ctx) {
  762. try {
  763. const tid = parseInt(ctx.query.tid) || 0;
  764. const order = parseInt(ctx.query.order) || 0;
  765. const sfid = parseInt(ctx.query.sfid) || 0;
  766. if (!tid || !order || !sfid) {
  767. throw '参数有误';
  768. }
  769. const tender = await ctx.service.tender.getDataById(tid);
  770. if (!tender) {
  771. throw '该标段不存在';
  772. }
  773. const sfInfo = await ctx.service.stageShoufang.getDataByCondition({ id: sfid, tid, order });
  774. if (!sfInfo) {
  775. throw '该收方单不存在';
  776. }
  777. // 查找对应的台账或计量单元名称
  778. const ledger = await ctx.service.ledger.getData(tid);
  779. const ledgerInfo = ctx.helper._.find(ledger, { id: sfInfo.lid });
  780. let name = ledgerInfo.b_code;
  781. if (sfInfo.pid) {
  782. const pos = await ctx.service.pos.getPosData({ tid });
  783. const posInfo = ctx.helper._.find(pos, { lid: sfInfo.lid, id: sfInfo.pid });
  784. name += ' / ' + posInfo.name;
  785. }
  786. const renderData = {
  787. tender,
  788. order,
  789. name,
  790. sfid,
  791. whiteList: this.ctx.app.config.multipart.whitelist,
  792. };
  793. await ctx.render('wap/shoufangupload.ejs', renderData);
  794. } catch (error) {
  795. this.log(error);
  796. ctx.redirect('/wap/login');
  797. }
  798. }
  799. /**
  800. * 上传附件
  801. * @param {Object} ctx - egg全局变量
  802. * @return {void}
  803. */
  804. async shoufangUpFile(ctx) {
  805. const responseData = {
  806. err: 0,
  807. msg: '',
  808. data: [],
  809. };
  810. let stream;
  811. try {
  812. const parts = ctx.multipart({ autoFields: true });
  813. const files = [];
  814. let index = 0;
  815. while ((stream = await parts()) !== undefined) {
  816. // 判断是否存在
  817. const sfInfo = await ctx.service.stageShoufang.getDataById(parts.field.sfid);
  818. if (!sfInfo) {
  819. throw '该清单 / 计量单元下不存在收方单';
  820. }
  821. // 判断用户是否选择上传文件
  822. if (!stream.filename) {
  823. throw '请选择上传的文件!';
  824. }
  825. // const dirName = 'app/public/upload/stage/' + moment().format('YYYYMMDD');
  826. // 判断文件夹是否存在,不存在则直接创建文件夹
  827. // if (!fs.existsSync(path.join(this.app.baseDir, dirName))) {
  828. // await fs.mkdirSync(path.join(this.app.baseDir, dirName));
  829. // }
  830. const fileInfo = path.parse(stream.filename);
  831. const now_time = new Date();
  832. const create_time = Date.parse(now_time) / 1000;
  833. const filepath = `app/public/upload/${sfInfo.tid}/stage/shoufang/fujian_${create_time + index.toString() + fileInfo.ext}`;
  834. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, filepath));
  835. await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
  836. // console.log(await fs.existsSync(path.resolve(this.app.baseDir, 'app', filepath)));
  837. // const fileInfo = path.parse(stream.filename);
  838. // const fileName = 'stage' + create_time + '_' + index + fileInfo.ext;
  839. // 保存文件
  840. // await ctx.helper.saveStreamFile(stream, path.resolve(this.app.baseDir, dirName, fileName));
  841. if (stream) {
  842. await sendToWormhole(stream);
  843. }
  844. // 保存数据到att表
  845. const fileData = {
  846. tid: sfInfo.tid,
  847. sid: sfInfo.sid,
  848. sfid: sfInfo.id,
  849. in_time: now_time,
  850. filename: fileInfo.name,
  851. fileext: fileInfo.ext,
  852. filesize: Array.isArray(parts.field.size) ? parts.field.size[index] : parts.field.size,
  853. filepath,
  854. };
  855. // if (ctx.reUploadPermission) {
  856. // fileData.re_upload = 1;
  857. // }
  858. const result = await ctx.service.stageShoufangAtt.save(fileData);
  859. if (!result) {
  860. throw '导入数据库保存失败';
  861. }
  862. const attData = await ctx.service.stageShoufangAtt.getDataByFid(result.insertId);
  863. attData.in_time = moment(create_time * 1000).format('YYYY-MM-DD');
  864. files.length !== 0 ? files.unshift(attData) : files.push(attData);
  865. ++index;
  866. }
  867. responseData.data = files;
  868. } catch (err) {
  869. this.log(err);
  870. // 失败需要消耗掉stream 以防卡死
  871. if (stream) {
  872. await sendToWormhole(stream);
  873. }
  874. this.setMessage(err.toString(), this.messageType.ERROR);
  875. responseData.err = 1;
  876. responseData.msg = err.toString();
  877. }
  878. ctx.body = responseData;
  879. }
  880. /**
  881. * 删除附件
  882. * @param {Object} ctx - egg全局变量
  883. * @return {void}
  884. */
  885. async shoufangDeleteFile(ctx) {
  886. const responseData = {
  887. err: 0,
  888. msg: '',
  889. data: '',
  890. };
  891. try {
  892. const data = JSON.parse(ctx.request.body.data);
  893. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(data.id);
  894. if (!fileInfo || !Object.keys(fileInfo).length) {
  895. throw '该文件不存在';
  896. }
  897. if (fileInfo !== undefined && fileInfo !== '') {
  898. // 先删除文件
  899. // await fs.unlinkSync(path.join(this.app.baseDir, fileInfo.filepath));
  900. await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + fileInfo.filepath);
  901. // 再删除数据库
  902. await ctx.service.stageShoufangAtt.deleteById(data.id);
  903. responseData.data = '';
  904. } else {
  905. throw '不存在该文件';
  906. }
  907. } catch (err) {
  908. responseData.err = 1;
  909. responseData.msg = err;
  910. }
  911. ctx.body = responseData;
  912. }
  913. /**
  914. * 编辑附件
  915. * @param {Object} ctx - egg全局变量
  916. * @return {void}
  917. */
  918. async shoufangEditFile(ctx) {
  919. const responseData = {
  920. err: 0,
  921. msg: '',
  922. data: '',
  923. };
  924. try {
  925. const data = JSON.parse(ctx.request.body.data);
  926. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(data.id);
  927. if (!fileInfo || !Object.keys(fileInfo).length) {
  928. throw '该文件不存在';
  929. }
  930. await ctx.service.stageShoufangAtt.edit(data);
  931. } catch (err) {
  932. responseData.err = 1;
  933. responseData.msg = err;
  934. }
  935. ctx.body = responseData;
  936. }
  937. /**
  938. * 下载附件
  939. * @param {Object} ctx - egg全局变量
  940. * @return {void}
  941. */
  942. async messageDownloadFile(ctx) {
  943. const id = ctx.params.fid;
  944. if (id) {
  945. try {
  946. const fileInfo = await ctx.service.messageAtt.getDataById(id);
  947. if (fileInfo !== undefined && fileInfo !== '') {
  948. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  949. // 解决中文无法下载问题
  950. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  951. let disposition = '';
  952. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  953. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  954. } else if (userAgent.indexOf('firefox') >= 0) {
  955. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  956. } else {
  957. /* safari等其他非主流浏览器只能自求多福了 */
  958. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  959. }
  960. ctx.response.set({
  961. 'Content-Type': 'application/octet-stream',
  962. 'Content-Disposition': disposition,
  963. 'Content-Length': fileInfo.filesize,
  964. });
  965. // ctx.body = await fs.createReadStream(fileName);
  966. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  967. } else {
  968. throw '不存在该文件';
  969. }
  970. } catch (err) {
  971. this.log(err);
  972. this.setMessage(err.toString(), this.messageType.ERROR);
  973. }
  974. }
  975. }
  976. /**
  977. * 下载附件
  978. * @param {Object} ctx - egg全局变量
  979. * @return {void}
  980. */
  981. async shoufangDownloadFile(ctx) {
  982. const id = ctx.params.fid;
  983. if (id) {
  984. try {
  985. const fileInfo = await ctx.service.stageShoufangAtt.getDataById(id);
  986. if (fileInfo !== undefined && fileInfo !== '') {
  987. // const fileName = path.join(this.app.baseDir, fileInfo.filepath);
  988. // 解决中文无法下载问题
  989. const userAgent = (ctx.request.header['user-agent'] || '').toLowerCase();
  990. let disposition = '';
  991. if (userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
  992. disposition = 'attachment; filename=' + encodeURIComponent(fileInfo.filename + fileInfo.fileext);
  993. } else if (userAgent.indexOf('firefox') >= 0) {
  994. disposition = 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileInfo.filename + fileInfo.fileext) + '"';
  995. } else {
  996. /* safari等其他非主流浏览器只能自求多福了 */
  997. disposition = 'attachment; filename=' + new Buffer(fileInfo.filename + fileInfo.fileext).toString('binary');
  998. }
  999. ctx.response.set({
  1000. 'Content-Type': 'application/octet-stream',
  1001. 'Content-Disposition': disposition,
  1002. 'Content-Length': fileInfo.filesize,
  1003. });
  1004. // ctx.body = await fs.createReadStream(fileName);
  1005. ctx.body = await ctx.helper.ossFileGet(fileInfo.filepath);
  1006. } else {
  1007. throw '不存在该文件';
  1008. }
  1009. } catch (err) {
  1010. this.log(err);
  1011. this.setMessage(err.toString(), this.messageType.ERROR);
  1012. }
  1013. }
  1014. }
  1015. async msg(ctx) {
  1016. try {
  1017. const msgId = parseInt(ctx.params.id) || 0;
  1018. if (!msgId) {
  1019. throw '参数有误';
  1020. }
  1021. const msgInfo = await ctx.service.message.getDataById(msgId);
  1022. const files = await ctx.service.messageAtt.getAtt(msgId);
  1023. if (!msgInfo) {
  1024. throw '项目通知不存在';
  1025. }
  1026. if (msgInfo && msgInfo.type === 1 && msgInfo.project_id !== ctx.session.sessionProject.id) {
  1027. throw '非该项目通知无权查看';
  1028. }
  1029. if (msgInfo) {
  1030. msgInfo.content = JSON.parse(JSON.stringify(msgInfo.content).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;'));
  1031. }
  1032. const renderData = {
  1033. msgInfo,
  1034. moment,
  1035. files,
  1036. };
  1037. await ctx.render('wap/msg.ejs', renderData);
  1038. } catch (error) {
  1039. console.log(error);
  1040. this.log(error);
  1041. ctx.redirect('/wap/dashboard');
  1042. }
  1043. }
  1044. }
  1045. return WapController;
  1046. };