wap_controller.js 11 KB

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