dashboard_controller.js 9.7 KB

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