dashboard_controller.js 8.9 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;').replace(/\\t/g, '&#9;')),
  64. msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  65. sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '&#34;').replace(/'/g, '&#39;').replace(/\\t/g, '&#9;')),
  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;').replace(/\\t/g, '&#9;')),
  107. };
  108. console.log(renderData.msgList);
  109. await this.layout('dashboard/msg.ejs', renderData);
  110. }
  111. /**
  112. * 控制面板-通知添加和编辑页面
  113. *
  114. * @param {Object} ctx - egg全局变量
  115. * @return {void}
  116. */
  117. async msgAdd(ctx) {
  118. let id = ctx.params.id;
  119. id = parseInt(id);
  120. try {
  121. if (isNaN(id) || id < 0) {
  122. throw '参数错误';
  123. }
  124. const rule = ctx.service.message.rule();
  125. const jsValidator = await this.jsValidator.convert(rule).build();
  126. const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
  127. const renderData = {
  128. jsValidator,
  129. msgInfo,
  130. };
  131. await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
  132. } catch (error) {
  133. console.log(error);
  134. // this.setMessage(error.toString(), this.messageType.ERROR);
  135. ctx.redirect(ctx.request.header.referer);
  136. }
  137. }
  138. /**
  139. * 控制面板-通知保存
  140. *
  141. * @param {Object} ctx - egg全局变量
  142. * @return {void}
  143. */
  144. async msgSet(ctx) {
  145. try {
  146. let id = ctx.params.id;
  147. id = parseInt(id);
  148. if (isNaN(id) || id < 0) {
  149. throw '参数错误';
  150. }
  151. const rule = ctx.service.message.rule();
  152. ctx.helper.validate(rule);
  153. const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id);
  154. if (result) {
  155. ctx.redirect('/dashboard/msg/list');
  156. }
  157. } catch (error) {
  158. console.log(error);
  159. ctx.redirect(ctx.request.header.referer);
  160. }
  161. }
  162. /**
  163. * 控制面板-通知删除
  164. *
  165. * @param {Object} ctx - egg全局变量
  166. * @return {void}
  167. */
  168. async msgDelete(ctx) {
  169. try {
  170. let id = ctx.params.id;
  171. id = parseInt(id);
  172. if (isNaN(id) || id <= 0) {
  173. throw '参数错误';
  174. }
  175. const msgInfo = await ctx.service.message.getDataById(id);
  176. if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
  177. throw '通知不存在或无权限操作';
  178. }
  179. const result = await ctx.service.message.deleteById(msgInfo.id);
  180. if (result) {
  181. ctx.redirect('/dashboard/msg/list');
  182. }
  183. } catch (error) {
  184. console.log(error);
  185. ctx.redirect(ctx.request.header.referer);
  186. }
  187. }
  188. }
  189. return DashboardController;
  190. };