dashboard_controller.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. /**
  3. * 控制面板
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/23
  7. * @version
  8. */
  9. const auditConst = require('../const/audit');
  10. const officeList = require('../const/cld_office').list;
  11. module.exports = app => {
  12. class DashboardController extends app.BaseController {
  13. /**
  14. * 控制面板页面
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. async index(ctx) {
  20. const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);
  21. const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);
  22. const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);
  23. const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId);
  24. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  25. const lastNotice = pa.last_notice ? pa.last_notice : (pa.last_notice === 0 ? new Date() : new Date(pa.last_login * 1000));
  26. const noticeLedger = await ctx.service.ledgerAudit.getNoticeTender(ctx.session.sessionProject.id, pa.id, lastNotice);
  27. const noticeStage = await ctx.service.stageAudit.getNoticeStage(ctx.session.sessionProject.id, pa.id, lastNotice);
  28. const noticeChange = await ctx.service.changeAudit.getNoticeChange(ctx.session.sessionProject.id, pa.id, lastNotice);
  29. const noticeRevise = await ctx.service.reviseAudit.getNoticeRevise(ctx.session.sessionProject.id, pa.id, lastNotice);
  30. const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
  31. // 获取销售人员数据
  32. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  33. const officeName = officeList[salesmanData.office];
  34. // 获取版本信息
  35. const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
  36. // 获取项目通知
  37. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id);
  38. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  39. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  40. // 获取系统通知
  41. const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, 2, 0, 2);
  42. const renderData = {
  43. auditTenders,
  44. auditStages,
  45. auditChanges,
  46. auditRevise,
  47. role: pa.role,
  48. authMobile: pa.auth_mobile,
  49. acLedger: auditConst.ledger,
  50. acStage: auditConst.stage,
  51. acChange: auditConst.flow,
  52. acRevise: auditConst.revise,
  53. noticeLedger,
  54. noticeStage,
  55. noticeChange,
  56. noticeRevise,
  57. projectData,
  58. salesmanData,
  59. officeName,
  60. versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34').replace(/'/g, '&#39')),
  61. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34').replace(/'/g, '&#39')),
  62. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34').replace(/'/g, '&#39')),
  63. userMsgPermission,
  64. uid: ctx.session.sessionUser.accountId,
  65. };
  66. await this.layout('dashboard/index.ejs', renderData, 'dashboard/modal.ejs');
  67. await ctx.service.projectAccount.defaultUpdate({
  68. id: this.ctx.session.sessionUser.accountId,
  69. last_notice: new Date(),
  70. });
  71. }
  72. /**
  73. * 控制面板-通知页面
  74. *
  75. * @param {Object} ctx - egg全局变量
  76. * @return {void}
  77. */
  78. async msgList(ctx) {
  79. const page = ctx.page;
  80. const type = ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
  81. const total = type === 1 ?
  82. await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, type }) :
  83. await ctx.service.message.count({ status: 1, type });
  84. const limit = 5;
  85. const offset = limit * (this.ctx.page - 1);
  86. const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, limit, offset, type);
  87. const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
  88. const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
  89. const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
  90. // 分页相关
  91. const pageInfo = {
  92. page,
  93. total: Math.ceil(total / limit),
  94. queryData: JSON.stringify(ctx.urlInfo.query),
  95. };
  96. const renderData = {
  97. uid: ctx.session.sessionUser.accountId,
  98. type,
  99. pageInfo,
  100. userMsgPermission,
  101. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34').replace(/'/g, '&#39')),
  102. };
  103. await this.layout('dashboard/msg.ejs', renderData);
  104. }
  105. /**
  106. * 控制面板-通知添加和编辑页面
  107. *
  108. * @param {Object} ctx - egg全局变量
  109. * @return {void}
  110. */
  111. async msgAdd(ctx) {
  112. let id = ctx.params.id;
  113. id = parseInt(id);
  114. try {
  115. if (isNaN(id) || id < 0) {
  116. throw '参数错误';
  117. }
  118. const rule = ctx.service.message.rule();
  119. const jsValidator = await this.jsValidator.convert(rule).build();
  120. const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
  121. const renderData = {
  122. jsValidator,
  123. msgInfo,
  124. };
  125. await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
  126. } catch (error) {
  127. console.log(error);
  128. // this.setMessage(error.toString(), this.messageType.ERROR);
  129. ctx.redirect(ctx.request.header.referer);
  130. }
  131. }
  132. /**
  133. * 控制面板-通知保存
  134. *
  135. * @param {Object} ctx - egg全局变量
  136. * @return {void}
  137. */
  138. async msgSet(ctx) {
  139. try {
  140. let id = ctx.params.id;
  141. id = parseInt(id);
  142. if (isNaN(id) || id < 0) {
  143. throw '参数错误';
  144. }
  145. const rule = ctx.service.message.rule();
  146. ctx.helper.validate(rule);
  147. const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id);
  148. if (result) {
  149. ctx.redirect('/dashboard/msg/list');
  150. }
  151. } catch (error) {
  152. console.log(error);
  153. ctx.redirect(ctx.request.header.referer);
  154. }
  155. }
  156. /**
  157. * 控制面板-通知删除
  158. *
  159. * @param {Object} ctx - egg全局变量
  160. * @return {void}
  161. */
  162. async msgDelete(ctx) {
  163. try {
  164. let id = ctx.params.id;
  165. id = parseInt(id);
  166. if (isNaN(id) || id <= 0) {
  167. throw '参数错误';
  168. }
  169. const msgInfo = await ctx.service.message.getDataById(id);
  170. if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
  171. throw '通知不存在或无权限操作';
  172. }
  173. const result = await ctx.service.message.deleteById(msgInfo.id);
  174. if (result) {
  175. ctx.redirect('/dashboard/msg/list');
  176. }
  177. } catch (error) {
  178. console.log(error);
  179. ctx.redirect(ctx.request.header.referer);
  180. }
  181. }
  182. }
  183. return DashboardController;
  184. };