dashboard_controller.js 10 KB

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