wap_controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. module.exports = app => {
  13. class WapController extends app.BaseController {
  14. /**
  15. * 登录页面
  16. *
  17. * @param {Object} ctx - egg全局页面
  18. * @return {void}
  19. */
  20. async index(ctx) {
  21. const errorMessage = ctx.session.loginError;
  22. // 显示完删除
  23. ctx.session.loginError = null;
  24. // 获取系统维护信息
  25. const maintainData = await ctx.service.maintain.getDataById(1);
  26. if (!ctx.app.config.is_debug) {
  27. await ctx.service.maintain.syncMaintainData();
  28. }
  29. let projectData = '';
  30. if (ctx.session.wapTenderID) {
  31. const tenderData = await ctx.service.tender.getDataById(ctx.session.wapTenderID);
  32. if (tenderData) projectData = await ctx.service.project.getDataById(tenderData.project_id);
  33. }
  34. const renderData = {
  35. maintainData,
  36. maintainConst,
  37. errorMessage,
  38. projectData,
  39. };
  40. await ctx.render('wap/login.ejs', renderData);
  41. }
  42. /**
  43. * 登录操作
  44. *
  45. * @param {Object} ctx - egg全局变量
  46. * @return {void}
  47. */
  48. async login(ctx) {
  49. let loginType = ctx.request.body.type;
  50. try {
  51. loginType = parseInt(loginType);
  52. const result = await ctx.service.projectAccount.accountLogin(ctx.request.body, loginType);
  53. if (!result) {
  54. throw '用户名或密码错误';
  55. }
  56. if (result === 2) {
  57. throw '该账号已被停用,请联系销售人员';
  58. }
  59. // 调用 rotateCsrfSecret 刷新用户的 CSRF token
  60. ctx.rotateCsrfSecret();
  61. // 判断是否已经有对应用户信息,没有则跳转初始化页面
  62. const needBoot = await ctx.service.customer.isNeedBoot(ctx.request.body);
  63. const url = needBoot ? '/boot' : '/wap/dashboard';
  64. const query = URL.parse(ctx.request.header.referer, true).query;
  65. let referer = '';
  66. if (ctx.session.wapTenderID) {
  67. referer = query.referer ? query.referer + '#shenpi' : url;
  68. ctx.session.wapTenderID = null;
  69. }
  70. ctx.redirect(referer ? referer : url);
  71. } catch (error) {
  72. this.log(error);
  73. ctx.session.loginError = error;
  74. ctx.redirect('/wap');
  75. }
  76. }
  77. /**
  78. * 退出登录
  79. *
  80. * @param {Object} ctx - egg全局变量
  81. * @return {void}
  82. */
  83. async logout(ctx) {
  84. // 删除session并跳转
  85. ctx.session = null;
  86. ctx.redirect('/wap');
  87. }
  88. /**
  89. * 待办页
  90. *
  91. * @param {Object} ctx - egg全局变量
  92. * @return {void}
  93. */
  94. async dashboard(ctx) {
  95. // 获取待审批的期
  96. const auditStages = await ctx.service.stageAudit.getAuditStageByWap(ctx.session.sessionUser.accountId);
  97. for (const audit of auditStages) {
  98. await this.ctx.service.stage.checkStageGatherData(audit);
  99. audit.gather_tp = ctx.helper.add(audit.contract_tp, audit.qc_tp);
  100. audit.end_contract_tp = ctx.helper.add(audit.contract_tp, audit.pre_contract_tp);
  101. audit.end_qc_tp = ctx.helper.add(audit.qc_tp, audit.pre_qc_tp);
  102. audit.end_gather_tp = ctx.helper.add(audit.end_contract_tp, audit.end_qc_tp);
  103. audit.pre_gather_tp = ctx.helper.add(audit.pre_contract_tp, audit.pre_qc_tp);
  104. }
  105. const renderData = {
  106. auditStages,
  107. };
  108. await ctx.render('wap/dashboard.ejs', renderData);
  109. }
  110. /**
  111. * 标段列表页
  112. *
  113. * @param {Object} ctx - egg全局变量
  114. * @return {void}
  115. */
  116. async list(ctx) {
  117. try {
  118. // 获取用户新建标段权利
  119. const accountInfo = await this.ctx.service.projectAccount.getDataById(this.ctx.session.sessionUser.accountId);
  120. const userPermission = accountInfo !== undefined && accountInfo.permission !== '' ? JSON.parse(accountInfo.permission) : null;
  121. const tenderList = await this.ctx.service.tender.getList('', userPermission);
  122. for (const t of tenderList) {
  123. if (t.user_id === this.ctx.session.sessionUser.accountId && (
  124. t.ledger_status === auditConst.ledger.status.checkNo || t.ledger_status === auditConst.ledger.status.uncheck)) {
  125. const sum = await this.ctx.service.ledger.addUp({tender_id: t.id/*, is_leaf: true*/});
  126. t.total_price = sum.total_price;
  127. t.deal_tp = sum.deal_tp;
  128. }
  129. if (t.ledger_status === auditConst.ledger.status.checked) {
  130. t.lastStage = await this.ctx.service.stage.getLastestStage(t.id, true);
  131. if (t.lastStage) {
  132. await this.ctx.service.stage.checkStageGatherData(t.lastStage);
  133. }
  134. }
  135. }
  136. const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
  137. const valuations = await this.ctx.service.valuation.getProjectValidValuation(this.ctx.session.sessionProject.id);
  138. const renderData = {
  139. tenderList,
  140. categoryData,
  141. auditConst,
  142. userPermission,
  143. valuations,
  144. uid: this.ctx.session.sessionUser.accountId,
  145. pid: this.ctx.session.sessionProject.id,
  146. };
  147. await ctx.render('wap/list.ejs', renderData);
  148. } catch (err) {
  149. this.log(err);
  150. this.ctx.redirect('/wap/dashboard');
  151. }
  152. }
  153. /**
  154. * 标段详细页
  155. *
  156. * @param {Object} ctx - egg全局变量
  157. * @return {void}
  158. */
  159. async tender(ctx) {
  160. try {
  161. const tender = ctx.tender.data;
  162. if (tender.user_id === this.ctx.session.sessionUser.accountId && (
  163. tender.ledger_status === auditConst.ledger.status.checkNo || tender.ledger_status === auditConst.ledger.status.uncheck)) {
  164. const sum = await this.ctx.service.ledger.addUp({tender_id: tender.id/*, is_leaf: true*/});
  165. tender.total_price = sum.total_price;
  166. tender.deal_tp = sum.deal_tp;
  167. }
  168. const stages = await ctx.service.stage.getValidStages(ctx.tender.id);
  169. const lastStage = stages.length > 0 ? stages[0] : null; //await ctx.service.stage.getLastestStage(ctx.tender.id);
  170. if (lastStage) {
  171. await this.ctx.service.stage.checkStageGatherData(lastStage);
  172. tender.gather_tp = ctx.helper.add(lastStage.contract_tp, lastStage.qc_tp);
  173. tender.end_contract_tp = ctx.helper.add(lastStage.contract_tp, lastStage.pre_contract_tp);
  174. tender.end_qc_tp = ctx.helper.add(lastStage.qc_tp, lastStage.pre_qc_tp);
  175. tender.end_gather_tp = ctx.helper.add(tender.end_contract_tp, tender.end_qc_tp);
  176. tender.pre_gather_tp = ctx.helper.add(lastStage.pre_contract_tp, lastStage.pre_qc_tp);
  177. tender.yf_tp = lastStage.yf_tp;
  178. tender.qc_ratio = ctx.helper.mul(ctx.helper.div(tender.end_qc_tp, ctx.tender.info.deal_param.contractPrice, 2), 100);
  179. tender.sum = ctx.helper.add(tender.total_price, tender.end_qc_tp);
  180. tender.pre_ratio = ctx.helper.mul(ctx.helper.div(tender.pre_gather_tp, tender.sum, 2), 100);
  181. tender.cur_ratio = ctx.helper.mul(ctx.helper.div(tender.gather_tp, tender.sum, 2), 100);
  182. tender.other_tp = ctx.helper.sub(ctx.helper.sub(tender.sum, tender.pre_gather_tp), tender.gather_tp);
  183. tender.other_ratio = Math.max(0, 100 - tender.pre_ratio - tender.cur_ratio);
  184. }
  185. const monthProgress = [];
  186. for (const s of stages) {
  187. if (s.s_time) {
  188. let progress = monthProgress.find(function(x) {
  189. return x.month === s.s_time;
  190. });
  191. if (!progress) {
  192. progress = { month: s.s_time };
  193. monthProgress.push(progress);
  194. }
  195. progress.tp = ctx.helper.add(ctx.helper.add(progress.tp, s.contract_tp), s.qc_tp);
  196. }
  197. }
  198. monthProgress.sort(function(x, y) {
  199. return Date.parse(x.month) - Date.parse(y.month);
  200. });
  201. let sum = 0;
  202. for (const p of monthProgress) {
  203. p.ratio = ctx.helper.mul(ctx.helper.div(p.tp, tender.sum, 4), 100);
  204. sum = ctx.helper.add(sum, p.tp);
  205. p.end_tp = sum;
  206. p.end_ratio = ctx.helper.mul(ctx.helper.div(p.end_tp, tender.sum, 4), 100);
  207. }
  208. const renderData = {
  209. tender,
  210. stages,
  211. auditConst: auditConst.stage,
  212. monthProgress,
  213. stagesEcharts: JSON.parse(JSON.stringify(stages)).reverse(),
  214. };
  215. if (stages.length > 0) {
  216. for (const s of stages) {
  217. // s.curAuditor = null;
  218. // 根据期状态返回展示用户
  219. s.curAuditor = await ctx.service.stageAudit.getAuditorByStatus(s.id, s.status, s.times);
  220. if (s.status === auditConst.stage.status.checkNoPre) {
  221. s.curAuditor2 = await ctx.service.stageAudit.getAuditorByStatus(s.id, auditConst.stage.status.checking);
  222. }
  223. }
  224. renderData.stage = stages[0];
  225. const times = renderData.stage.status === auditConst.stage.status.checkNo ? renderData.stage.times - 1 : renderData.stage.times;
  226. renderData.stage.user = await ctx.service.projectAccount.getAccountInfoById(renderData.stage.user_id);
  227. renderData.stage.auditors = await ctx.service.stageAudit.getAuditors(renderData.stage.id, times);
  228. // 获取审批流程中左边列表
  229. renderData.stage.auditors2 = await ctx.service.stageAudit.getAuditGroupByList(renderData.stage.id, times);
  230. }
  231. await ctx.render('wap/tender.ejs', renderData);
  232. } catch (err) {
  233. this.log(err);
  234. ctx.redirect('/wap/list');
  235. }
  236. }
  237. }
  238. return WapController;
  239. };