dashboard_controller.js 8.7 KB

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