dashboard_controller.js 9.2 KB

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