| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\MessageModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- /**
- * 消息服务-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class MessageService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * NoticeService constructor.
- */
- public function __construct()
- {
- $this->model = new MessageModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 20)
- {
- $query = $this->getQuery($params);
- $list = $query->select(['a.*'])
- ->orderBy('a.create_time','desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach($list['data'] as &$item){
- $item['create_time'] = $item['create_time']? datetime($item['create_time'],'Y-m-d H.i.s') : '';
- $item['time_text'] = $item['create_time']? dateFormat($item['create_time']) : '';
- $msgType = $item['msg_type']? $item['msg_type'] : 1;
- if($msgType == 1 || $msgType == 3){
- $item['content'] = format_message($item['content']);
- }else if($msgType==2){
- $item['content'] = $item['content']? get_image_url($item['content']) : '';
- }else if($msgType == 4){
- $item['content'] = $item['content']? json_decode($item['content'], true) : [];
- }
- if(isset($item['from_user']) && $item['from_user']){
- $item['from_user_name'] = $item['from_user']['realname']? $item['from_user']['realname'] : '用户'.$item['from_uid'];
- $item['from_user_avatar'] = $item['from_user']['avatar']? get_image_url($item['from_user']['avatar']) : get_image_url('/images/member/logo.png');
- }else if($item['from_uid'] <=1){
- $item['from_user_name'] = '客服';
- $item['from_user_avatar'] = get_image_url('/images/member/custom.png');
- }
- if(isset($item['to_user']) && $item['to_user']){
- $item['to_user_name'] = $item['to_user']['realname']? $item['to_user']['realname'] : '用户'.$item['to_uid'];
- $item['to_user_avatar'] = $item['to_user']['avatar']? get_image_url($item['to_user']['avatar']) : get_image_url('/images/member/logo.png');
- }else if($item['to_uid'] <=1){
- $item['to_user_name'] = '客服';
- $item['to_user_avatar'] = get_image_url('/images/member/custom.png');
- }
- }
- if(count($list['data'])) {
- $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()]);
- RedisService::keyDel("caches:messages:unread_count*");
- }
- }
- return [
- 'pageSize'=> $pageSize,
- 'total'=>isset($list['total'])? $list['total'] : 0,
- 'list'=> isset($list['data'])? array_reverse($list['data']) : []
- ];
- }
- /**
- * 查询
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['a.mark' => 1];
- $status = isset($params['status'])? $params['status'] : 0;
- $type = isset($params['type'])? $params['type'] : 0;
- if($status>0){
- $where['a.status'] = $status;
- }
- if($type>0){
- $where['a.type'] = $type;
- }
- return $this->model->with(['fromUser','toUser'])->from('message as a')
- ->where($where)
- ->where(function ($query) use($params){
- $keyword = isset($params['keyword'])? $params['keyword'] : '';
- if($keyword){
- $query->where('a.title','like',"%{$keyword}%");
- }
- $fromUserId= isset($params['from_uid'])? intval($params['from_uid']) : 0;
- if($fromUserId>0){
- $query->where('a.from_uid', $fromUserId);
- }
- $toUserId= isset($params['to_uid'])? intval($params['to_uid']) : 0;
- if($fromUserId>0){
- $query->where('a.to_uid', $toUserId);
- }
- $isRead= isset($params['is_read'])? intval($params['is_read']) : 0;
- if($isRead>0){
- $query->where('a.is_read', $isRead);
- }
- }) ->where(function ($query) use($params){
- $userId = isset($params['user_id'])? intval($params['user_id']) : 0;
- if($userId>0){
- $query->where('a.from_uid',$userId)->orWhere('a.to_uid', $userId);
- }
- });
- }
- /**
- * 消息推送处理
- * @param $userId 用户
- * @param $msgData 消息数据
- * @return bool
- */
- public function pushMessage($userId, $msgData)
- {
- return true;
- }
- /**
- * 未读通知消息
- * @param $type
- * @return array|mixed
- */
- public function getUnreadCount($userId)
- {
- $cacheKey = "caches:messages:unread_count_{$userId}";
- $count = RedisService::get($cacheKey);
- if($count){
- return $count;
- }
- $count = $this->model->where(function($query) use($userId){
- if($userId){
- $query->where('to_uid',$userId);
- }
- })->where(['is_read'=>2,'type'=>1,'status'=>1,'mark'=>1])
- ->count('id');
- if($count){
- RedisService::set($cacheKey, $count, rand(300,600));
- }
- return $count;
- }
- /**
- * 验证是否有敏感词
- * @param $message
- * @return bool
- */
- public function checkMessage($message)
- {
- $chatSensitive = ConfigService::make()->getConfigByCode('chat_sensitive','');
- $chatSensitiveList = $chatSensitive? explode('、', $chatSensitive) : [];
- if(empty($chatSensitiveList)){
- return $message;
- }
- $pattern = '/\b(' . implode('|', array_map('preg_quote', $chatSensitiveList)) . ')\b/iu';
- $result = preg_replace($pattern, '***', $message);
- return !preg_match("/^\*+$/",$result)? $result : '';
- }
- }
|