notice_push.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. 'use strict';
  2. /**
  3. * 推送表模型
  4. *
  5. * @author LanJianRong
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. module.exports = app => {
  10. class NoticePush extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'notice';
  20. }
  21. /**
  22. * 将未读记录设置成已读
  23. * @param {Number} id 推送记录id
  24. */
  25. async set(id) {
  26. await this.update({ is_read: 1 }, { id });
  27. }
  28. /**
  29. * 查询所有推送记录
  30. * @param {Integer} pid - 项目id
  31. * @param {Integer} uid - 查询人id
  32. */
  33. async getNotice(pid, uid) {
  34. let notice = await this.db.select(this.tableName, {
  35. where: { pid, uid },
  36. orders: [['create_time', 'desc']],
  37. limit: 30, offset: 0
  38. });
  39. notice = notice.map(v => {
  40. const extra = JSON.parse(v.content)
  41. delete v.content
  42. return { ...v, ...extra }
  43. }).sort((a, b) => a.name === b.name).reduce((prev, curr, idx) => {
  44. if (!prev.length) {
  45. prev.push(curr)
  46. }
  47. if (prev[idx-1] && curr.tid !== prev[idx-1].tid) {
  48. prev.push(curr)
  49. }
  50. return prev
  51. }, [])
  52. return notice;
  53. }
  54. }
  55. return NoticePush;
  56. };