SystemMessageServices.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\message;
  13. use app\dao\message\SystemMessageDao;
  14. use app\services\BaseServices;
  15. use think\exception\ValidateException;
  16. /**
  17. * 站内信
  18. * Class SystemMessageServices
  19. * @package app\services\message
  20. * @mixin SystemMessageDao
  21. */
  22. class SystemMessageServices extends BaseServices
  23. {
  24. /**
  25. * SystemMessageServices constructor.
  26. * @param SystemMessageDao $dao
  27. */
  28. public function __construct(SystemMessageDao $dao)
  29. {
  30. $this->dao = $dao;
  31. }
  32. /**
  33. * 移动端消息列表
  34. * @param $uid
  35. * @return array
  36. * @throws \think\db\exception\DataNotFoundException
  37. * @throws \think\db\exception\DbException
  38. * @throws \think\db\exception\ModelNotFoundException
  39. */
  40. public function getMessageSystemList($uid)
  41. {
  42. [$page, $limit] = $this->getPageValue();
  43. $where['is_del'] = 0;
  44. $where['uid'] = $uid;
  45. $list = $this->dao->getMessageList($where, '*', $page, $limit);
  46. $count = $this->dao->getCount($where);
  47. if (!$list) return ['list' => [], 'count' => 0];
  48. foreach ($list as &$item) {
  49. $item['add_time'] = time_tran($item['add_time']);
  50. }
  51. $message = $this->dao->count(['uid' => $uid, 'look' => 0, 'is_del' => 0]);
  52. return ['list' => $list, 'count' => $count, 'service_num' => $message];
  53. }
  54. /**
  55. * 消息详情
  56. * @param int $id
  57. * @param int $uid
  58. * @return array
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function getInfo(int $id, int $uid = 0)
  64. {
  65. $info = $this->dao->getOne(['id' => $id]);
  66. if (!$info || $info['is_del'] == 1) {
  67. throw new ValidateException('数据不存在');
  68. }
  69. if ($uid && $info['uid'] != $uid) {
  70. throw new ValidateException('数据不存在');
  71. }
  72. $info = $info->toArray();
  73. if ($info['look'] == 0) {
  74. $this->update($info['id'], ['look' => 1]);
  75. }
  76. $info['add_time'] = time_tran($info['add_time']);
  77. return $info;
  78. }
  79. /**
  80. * 站内信发放
  81. * @param int $uid
  82. * @param array $noticeInfo
  83. * @return \crmeb\basic\BaseModel|\think\Model
  84. */
  85. public function systemSend(int $uid, array $noticeInfo)
  86. {
  87. $data = [];
  88. $data['mark'] = $noticeInfo['mark'];
  89. $data['uid'] = $uid;
  90. $data['title'] = $noticeInfo['title'];
  91. $data['content'] = $noticeInfo['content'];
  92. $data['type'] = 1;
  93. $data['add_time'] = time();
  94. return $this->dao->save($data);
  95. }
  96. /**
  97. * 一键已读
  98. * @param int $uid
  99. * @return bool
  100. */
  101. public function setAllRead(int $uid)
  102. {
  103. if (!$this->dao->count(['uid' => $uid, 'is_del' => 0, 'look' => 0])) {
  104. throw new ValidateException('暂无未读消息');
  105. }
  106. $this->dao->update(['uid' => $uid], ['look' => 1]);
  107. return true;
  108. }
  109. /**
  110. * 删除消息
  111. * @param int $uid
  112. * @param array $ids
  113. * @return bool
  114. */
  115. public function setBatchDel(int $uid, array $ids = [])
  116. {
  117. if (!$this->dao->count(['uid' => $uid, 'is_del' => 0])) {
  118. throw new ValidateException('暂无消息');
  119. }
  120. $where = ['uid' => $uid];
  121. if ($ids) {//单个删除验证数据
  122. if (count($ids) == 1) $this->getInfo((int)$ids[0], $uid);
  123. $where['id'] = $ids;
  124. }
  125. $this->dao->update($where, ['is_del' => 1]);
  126. return true;
  127. }
  128. }