dashboard_controller.js 9.8 KB

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