dashboard_controller.js 9.8 KB

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