noticeController.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. const Controller = require('../extend/baseController');
  3. class NoticeController extends Controller {
  4. tag() {
  5. return this.ctx.service.noticeService;
  6. }
  7. // 我发布的通知
  8. async listForPage() {
  9. const { ctx, service } = this;
  10. const user = ctx.user;
  11. const pop = [
  12. {
  13. path: 'userid',
  14. select: 'loginName userName',
  15. // select: 'loginName',
  16. },
  17. ];
  18. if (user.role._id != this.app.config.defaultAdminRoleId) {
  19. ctx.query.userid = ctx.user._id; // 不是admin的情况
  20. if (ctx.query.status == 1) { // 不是admin情况选择 我发布的通知
  21. delete ctx.query.status;
  22. ctx.query.sort = { createTime: -1 };
  23. if (!ctx.query.searchtitle) {
  24. delete ctx.query.searchtitle;
  25. } else {
  26. ctx.query.title = ctx.query.searchtitle;
  27. ctx.query.title = { $regex: ctx.query.searchtitle };
  28. delete ctx.query.searchtitle;
  29. }
  30. this.ctx.logger.info(ctx.query);
  31. const result0 = await this.tag().listForPage(ctx.query, pop);
  32. ctx.success(result0);
  33. } else { // 不是admin情况下选择 发布给我的通知
  34. const result1 = await service.noticeService.pcListForPage(ctx.query, user);
  35. ctx.success(result1);
  36. }
  37. } else { // 是admin情况下只能看 所有通知
  38. delete ctx.query.userid;
  39. delete ctx.query.status;
  40. ctx.query.sort = { createTime: -1 };
  41. if (!ctx.query.searchtitle) {
  42. delete ctx.query.searchtitle;
  43. } else {
  44. ctx.query.title = ctx.query.searchtitle;
  45. ctx.query.title = { $regex: ctx.query.searchtitle };
  46. delete ctx.query.searchtitle;
  47. }
  48. const result2 = await this.tag().listForPage(ctx.query, pop);
  49. ctx.success(result2);
  50. }
  51. }
  52. // PC端发给我的通知
  53. async pcListForPage() {
  54. const { ctx, service } = this;
  55. const user = ctx.user;
  56. const result = await service.noticeService.pcListForPage(ctx.query, user);
  57. ctx.success(result);
  58. }
  59. // 已读接口
  60. async listForApplets() {
  61. const { ctx, service } = this;
  62. const user = ctx.user;
  63. const query = {};
  64. query.userid = user._id;
  65. const applist = await service.readNoticeService.appList(query);
  66. let finallist;
  67. if (user) {
  68. const list = await service.noticeService.listForApplets({ $or: [{ deptId: user.dept1._id }, { deptId: user.dept2._id }, { deptId: user.dept3._id }], _id: { $in: applist } }, { title: 1, content: 1, createTime: 1 });
  69. // const list2 = await service.noticeService.listForApplets({ deptId:user.dept2._id,_id:{$in:applist}},{title:1,createTime:1});
  70. // const list3 = await service.noticeService.listForApplets({ deptId:user.dept3._id,_id:{$in:applist}},{title:1,createTime:1});
  71. // finallist = list1.concat(list2).concat(list3);
  72. finallist = list;
  73. }
  74. ctx.success(finallist);
  75. }
  76. // 未读
  77. async listForAppletsNoread() {
  78. const { ctx, service } = this;
  79. const user = ctx.user;
  80. const query = {};
  81. query.userid = user._id;
  82. const applist = await service.readNoticeService.appList(query);
  83. let finallist;
  84. if (user) {
  85. const list = await service.noticeService.listForApplets({ $or: [{ deptId: user.dept1._id }, { deptId: user.dept2._id }, { deptId: user.dept3._id }], _id: { $nin: applist } }, { title: 1, content: 1, createTime: 1 });
  86. // const list2 = await service.noticeService.listForApplets({ deptId:user.dept2._id,_id:{$nin:applist}},{title:1,createTime:1});
  87. // const list3 = await service.noticeService.listForApplets({ deptId:user.dept3._id,_id:{$nin:applist}},{title:1,createTime:1});
  88. // finallist = list1.concat(list2).concat(list3);
  89. finallist = list;
  90. }
  91. ctx.success(finallist);
  92. }
  93. async listForAppletsFirst() {
  94. const { ctx, service } = this;
  95. const user = ctx.user;
  96. let finallist;
  97. if (user) {
  98. const list = await service.noticeService.listForAppletsFirst({ $or: [{ deptId: user.dept1._id }, { deptId: user.dept2._id }, { deptId: user.dept3._id }] }, { title: 1, createTime: 1 });
  99. finallist = list;
  100. }
  101. if (finallist.length > 0) {
  102. ctx.success(finallist[0]);
  103. } else {
  104. ctx.success([]);
  105. }
  106. }
  107. }
  108. module.exports = NoticeController;