MessageService.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\MessageModel;
  13. use App\Services\BaseService;
  14. use App\Services\RedisService;
  15. /**
  16. * 消息服务-服务类
  17. * @author laravel开发员
  18. * @since 2020/11/11
  19. * @package App\Services\Api
  20. */
  21. class MessageService extends BaseService
  22. {
  23. // 静态对象
  24. protected static $instance = null;
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * NoticeService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new MessageModel();
  34. }
  35. /**
  36. * 静态入口
  37. */
  38. public static function make()
  39. {
  40. if (!self::$instance) {
  41. self::$instance = new static();
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * @param $params
  47. * @param int $pageSize
  48. * @return array
  49. */
  50. public function getDataList($params, $pageSize = 20)
  51. {
  52. $query = $this->getQuery($params);
  53. $list = $query->select(['a.*'])
  54. ->orderBy('a.create_time','desc')
  55. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  56. $list = $list? $list->toArray() :[];
  57. if($list){
  58. foreach($list['data'] as &$item){
  59. $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
  60. $item['time_text'] = $item['create_time']? dateFormat($item['create_time']) : '';
  61. $msgType = $item['msg_type']? $item['msg_type'] : 1;
  62. if($msgType == 1 || $msgType == 3){
  63. $item['content'] = format_message($item['content']);
  64. }else if($msgType==2){
  65. $item['content'] = $item['content']? get_image_url($item['content']) : '';
  66. }else if($msgType == 4){
  67. $item['content'] = $item['content']? json_decode($item['content'], true) : [];
  68. }
  69. if(isset($item['from_user']) && $item['from_user']){
  70. $item['from_user_name'] = $item['from_user']['nickname']? $item['from_user']['nickname'] : '用户'.$item['from_uid'];
  71. $item['from_user_avatar'] = $item['from_user']['avatar']? get_image_url($item['from_user']['avatar']) : get_image_url('/images/member/logo.png');
  72. }else if($item['from_uid'] <=1){
  73. $item['from_user_name'] = '客服';
  74. $item['from_user_avatar'] = get_image_url('/images/member/custom.png');
  75. }
  76. if(isset($item['to_user']) && $item['to_user']){
  77. $item['to_user_name'] = $item['to_user']['nickname']? $item['to_user']['nickname'] : '用户'.$item['to_uid'];
  78. $item['to_user_avatar'] = $item['to_user']['avatar']? get_image_url($item['to_user']['avatar']) : get_image_url('/images/member/logo.png');
  79. }else if($item['to_uid'] <=1){
  80. $item['to_user_name'] = '客服';
  81. $item['to_user_avatar'] = get_image_url('/images/member/custom.png');
  82. }
  83. }
  84. if(count($list['data'])) {
  85. $this->model->where(['chat_key' => $list['data'][0]['chat_key'], 'is_read' => 2, 'mark' => 1])->where('id', '<=', $list['data'][0]['id'])->update(['is_read' => 1, 'update_time' => time()]);
  86. RedisService::keyDel("caches:messages:unread_count*");
  87. }
  88. }
  89. return [
  90. 'pageSize'=> $pageSize,
  91. 'total'=>isset($list['total'])? $list['total'] : 0,
  92. 'list'=> isset($list['data'])? array_reverse($list['data']) : []
  93. ];
  94. }
  95. /**
  96. * 查询
  97. * @param $params
  98. * @return mixed
  99. */
  100. public function getQuery($params)
  101. {
  102. $where = ['a.mark' => 1];
  103. $status = isset($params['status'])? $params['status'] : 0;
  104. $type = isset($params['type'])? $params['type'] : 0;
  105. if($status>0){
  106. $where['a.status'] = $status;
  107. }
  108. if($type>0){
  109. $where['a.type'] = $type;
  110. }
  111. return $this->model->with(['fromUser','toUser'])->from('message as a')
  112. ->where($where)
  113. ->where(function ($query) use($params){
  114. $keyword = isset($params['keyword'])? $params['keyword'] : '';
  115. if($keyword){
  116. $query->where('a.title','like',"%{$keyword}%");
  117. }
  118. $fromUserId= isset($params['from_uid'])? intval($params['from_uid']) : 0;
  119. if($fromUserId>0){
  120. $query->where('a.from_uid', $fromUserId);
  121. }
  122. $toUserId= isset($params['to_uid'])? intval($params['to_uid']) : 0;
  123. if($fromUserId>0){
  124. $query->where('a.to_uid', $toUserId);
  125. }
  126. $isRead= isset($params['is_read'])? intval($params['is_read']) : 0;
  127. if($isRead>0){
  128. $query->where('a.is_read', $isRead);
  129. }
  130. }) ->where(function ($query) use($params){
  131. $userId = isset($params['user_id'])? intval($params['user_id']) : 0;
  132. if($userId>0){
  133. $query->where('a.from_uid',$userId)->orWhere('a.to_uid', $userId);
  134. }
  135. });
  136. }
  137. /**
  138. * 消息推送处理
  139. * @param $userId 用户
  140. * @param $msgData 消息数据
  141. * @return bool
  142. */
  143. public function pushMessage($userId, $msgData)
  144. {
  145. return true;
  146. }
  147. /**
  148. * 未读通知消息
  149. * @param $type
  150. * @return array|mixed
  151. */
  152. public function getUnreadCount($userId)
  153. {
  154. $cacheKey = "caches:messages:unread_count_{$userId}";
  155. $count = RedisService::get($cacheKey);
  156. if($count){
  157. return $count;
  158. }
  159. $count = $this->model->where(function($query) use($userId){
  160. if($userId){
  161. $query->where('to_uid',$userId);
  162. }
  163. })->where(['is_read'=>2,'type'=>1,'status'=>1,'mark'=>1])
  164. ->count('id');
  165. if($count){
  166. RedisService::set($cacheKey, $count, rand(300,600));
  167. }
  168. return $count;
  169. }
  170. }