wap_controller.js 11 KB

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