// +---------------------------------------------------------------------- namespace App\Services\Api; use App\Models\AccountLogModel; use App\Models\ImChatModel; use App\Models\ImChatParamsModel; use App\Models\MemberModel; use App\Services\BaseService; use App\Services\ConfigService; use App\Services\RedisService; use Illuminate\Support\Facades\DB; /** * 聊天服务管理-服务类 * @author laravel开发员 * @since 2020/11/11 * Class ImChatService * @package App\Services\Api */ class ImChatService extends BaseService { // 静态对象 protected static $instance = null; /** * 构造函数 * @author laravel开发员 * @since 2020/11/11 * ImChatService constructor. */ public function __construct() { $this->model = new ImChatModel(); } /** * 静态入口 * @return static|null */ public static function make() { if (!self::$instance) { self::$instance = (new static()); } return self::$instance; } /** * 消息列表 * @param $userId * @param $params * @param int $pageSize * @return array */ public function getDataList($userId, $params, $pageSize=0) { $page = request()->post('page', 1); $cacheKey = "caches:chats:history_{$page}_".md5($userId.json_encode($params). $pageSize); $datas = RedisService::get($cacheKey); $data = isset($datas['data'])? $datas['data'] : []; if($datas && $data) { return [ 'total'=> isset($datas['total'])? $datas['total'] : 0, 'list'=> $data, 'pageSize'=> $pageSize, 'cache'=> true, ]; } $where = ['a.status'=> 1,'a.mark'=>1]; $expire = ConfigService::make()->getConfigByCode('chat_log_expire'); $expire = $expire? $expire*86400 : 60*86400; $field = ['a.id','a.chat_key','a.message','a.from_user_id','a.to_user_id','a.is_connect','a.video_time','a.msg_type','a.create_time','a.is_read','a.status','b.avatar as from_avatar','b.nickname as from_nickname','c.avatar as to_avatar','c.nickname as to_nickname']; $datas = $this->model->from('imchat as a') ->leftJoin('member as b','b.id','=','a.from_user_id') ->leftJoin('member as c','c.id','=','a.to_user_id') ->where(function($query) use($userId){ if($userId){ $query->where('a.from_user_id', $userId)->orWhere('a.to_user_id', $userId); } }) ->where($where) ->where('a.create_time','>=', time() - $expire) ->where(function($query) use($params){ $chatKey = isset($params['chat_key'])? trim($params['chat_key']) : ''; if($chatKey){ $query->where('a.chat_key', $chatKey); } $isRead = isset($params['is_read'])? intval($params['is_read']) : 0; if($isRead){ $query->where('a.is_read', $isRead); } $isRead = isset($params['is_read'])? intval($params['is_read']) : 0; if($isRead){ $query->where('a.is_read', $isRead); } $isShow = isset($params['is_show'])? intval($params['is_show']) : 0; if($isShow){ $query->where('a.is_show', $isRead); } }) ->select($field) ->orderBy('a.create_time','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $datas = $datas ? $datas->toArray() : []; if ($datas) { $chatKey = ''; foreach ($datas['data'] as $k=> &$item) { $item['show_time'] = $k == 0? 1 : 0; $chatKey = isset($item['chat_key'])? $item['chat_key'] : ''; $item['time_text'] = isset($item['create_time']) && $item['create_time']? dateFormat($item['create_time']) : ''; $item['from_avatar'] = isset($item['from_avatar']) && $item['from_avatar'] ? get_image_url($item['from_avatar']) : get_image_url('/images/member/logo.png'); $item['to_avatar'] = isset($item['to_avatar']) && $item['to_avatar'] ? get_image_url($item['to_avatar']) : get_image_url('/images/member/logo.png'); $info = MerchantService::make()->getCacheInfoByUser($item['from_user_id'], 'id,name,logo,status'); $status = isset($info['status'])? $info['status'] : 0; if($info && $status==2){ $item['from_avatar'] = $info['logo']; $item['from_nickname'] = $info['name']; } // 商家 $info = MerchantService::make()->getCacheInfoByUser($item['to_user_id'], 'id,name,logo,status'); $status = isset($info['status'])? $info['status'] : 0; if($info && $status==2){ $item['to_avatar'] = $info['logo']?$info['logo']:get_image_url('/images/member/logo.png'); $item['to_nickname'] = $info['name']; } if(empty($item['from_avatar'])){ $item['from_suffix_name'] = mb_substr($item['from_nickname'],0,1); } if(empty($item['to_avatar'])){ $item['to_suffix_name'] = mb_substr($item['to_nickname'],0,1); } $item['video_time_text'] = ''; if(isset($item['video_time']) && $item['video_time']){ $data = []; $time = intval($item['video_time']); if($time>3600){ $hour = intval($time/3600); $data[] = $hour<10?'0'.$hour:$hour; } if($time%3600 > 0){ $minute = intval($time%3600/60); $data[] = $minute<10?'0'.$minute:$minute; } if($time%60 > 0){ $second = intval($time%60); $data[] = $second<10?'0'.$second:$second; } $item['video_time_text'] = $data? implode(':', $data) : ''; } } unset($item); // 已读处理 if($chatKey && $userId){ $this->model->where(['chat_key'=> $chatKey,'to_user_id'=> $userId,'mark'=>1,'status'=>1])->update(['update_time'=>time(),'is_read'=>1]); // 清除缓存 RedisService::keyDel("caches:chats:history*"); RedisService::keyDel("caches:chats:group*"); RedisService::keyDel("caches:messages:bar*"); } arsort($datas['data'], true); $datas['data'] = array_reverse($datas['data'], false); RedisService::set($cacheKey, $datas, rand(3, 5)); } return [ 'total'=> isset($datas['total'])? $datas['total'] : 0, 'list'=> isset($datas['data'])? $datas['data'] : [], 'pageSize'=> $pageSize, 'cache'=> false, ]; } /** * 消息列表 * @param $userId * @param $params * @param int $pageSize * @return array */ public function getDataListFromatKey($userId, $params, $pageSize=0) { $page = request()->post('page', 1); $cacheKey = "caches:chats:group_{$page}_".md5($userId.json_encode($params).$pageSize); $datas = RedisService::get($cacheKey); $data = isset($datas['data'])? $datas['data'] : []; if($datas && $data) { return [ 'unread'=> isset($datas['unReadCount'])? $datas['unReadCount'] : 0, 'total'=> isset($datas['total'])? $datas['total'] : 0, 'list'=> $data, 'pageSize'=> $pageSize, 'cache'=> true, ]; } $where = ['a.status'=> 1,'a.mark'=>1]; $expire = ConfigService::make()->getConfigByCode('chat_log_expire'); $expire = $expire? $expire*86400 : 60*86400; $field = ['a.id','a.chat_key','a.from_user_id','a.to_user_id','a.msg_type','a.create_time','a.video_time','a.is_connect','a.is_read','a.from_is_show','a.to_is_show','a.status','b.avatar as from_avatar','b.nickname as from_nickname','c.avatar as to_avatar','c.nickname as to_nickname']; $datas = $this->model->from('imchat as a') ->leftJoin('member as b','b.id','=','a.from_user_id') ->leftJoin('member as c','c.id','=','a.to_user_id') ->where(function($query) use($userId){ if($userId){ $query->where(['a.from_user_id'=>$userId,'a.from_is_show'=>1]) ->orWhere(function($query) use($userId){ $query->where(['a.to_user_id'=>$userId,'a.to_is_show'=>1]); }); } }) ->where($where) ->where('a.chat_key','>', 0) ->where('a.create_time','>=', time() - $expire) ->where(function($query) use($params){ $chatKey = isset($params['chat_key'])? trim($params['chat_key']) : ''; if($chatKey){ $query->where('a.chat_key', $chatKey); } $isRead = isset($params['is_read'])? intval($params['is_read']) : 0; if($isRead){ $query->where('a.is_read', $isRead); } $isRead = isset($params['is_read'])? intval($params['is_read']) : 0; if($isRead){ $query->where('a.is_read', $isRead); } }) ->select($field) ->groupBy('chat_key') // ->orderBy('a.is_read','desc') ->orderBy('a.create_time','desc') ->orderBy('a.id','desc') ->paginate($pageSize > 0 ? $pageSize : 9999999); $datas = $datas ? $datas->toArray() : []; $unReadCount = 0; if ($datas) { foreach ($datas['data'] as &$item) { $item['from_avatar'] = isset($item['from_avatar']) && $item['from_avatar'] ? get_image_url($item['from_avatar']) : get_image_url('/images/member/logo.png'); $item['to_avatar'] = isset($item['to_avatar']) && $item['to_avatar'] ? get_image_url($item['to_avatar']) : get_image_url('/images/member/logo.png'); $data = $this->getNewChat($item['chat_key']); $item['description'] = isset($data['description']) && $data['description']? $data['description'] : (isset($data['message'])? mb_substr($data['message'],0,20,'utf-8'):'有新消息'); $item['create_time'] = isset($data['create_time']) && $data['create_time']? $data['create_time'] : ''; $item['time_text'] = isset($item['create_time']) && $item['create_time'] ? dateFormat($item['create_time']) : ''; $item['create_time'] = isset($item['create_time']) ? datetime($item['create_time'], 'Y-m-d H.i.s') : ''; if($item['from_user_id'] == $userId){ $item['avatar'] = $item['to_avatar']; $item['nickname'] = $item['to_nickname']; $info = MerchantService::make()->getCacheInfoByUser($item['to_user_id'], 'id,name,logo,status'); $status = isset($info['status'])? $info['status'] : 0; if($info && $status==2){ $item['avatar'] = $info['logo']?$info['logo']:get_image_url('/images/member/logo.png'); $item['nickname'] = $info['name']; } $item['unread'] = $this->getUnreadCount($userId, $item['chat_key']); $item['touid'] = $item['to_user_id']; }else{ $item['avatar'] = $item['from_avatar']; $item['nickname'] = $item['from_nickname']; $info = MerchantService::make()->getCacheInfoByUser($item['from_user_id'], 'id,name,logo,status'); $status = isset($info['status'])? $info['status'] : 0; if($info && $status==2){ $item['avatar'] = $info['logo']?$info['logo'] : get_image_url('/images/member/logo.png'); $item['nickname'] = $info['name']; } $item['unread'] = $this->getUnreadCount($userId,$item['chat_key']); $item['touid'] = $item['from_user_id']; } $item['video_time_text'] = ''; if(isset($item['video_time']) && $item['video_time']){ $data = []; $time = intval($item['video_time']); if($time>3600){ $hour = intval($time/3600); $data[] = $hour<10?'0'.$hour:$hour; } if($time%3600 > 0){ $minute = intval($time%3600/60); $data[] = $minute<10?'0'.$minute:$minute; } if($time%60 > 0){ $second = intval($time%60); $data[] = $second<10?'0'.$second:$second; } $item['video_time_text'] = $data? implode(':', $data) : ''; } $unReadCount += intval($item['unread']); } unset($item); $datas['unReadCount'] = $unReadCount; RedisService::set($cacheKey, $datas, rand(3, 5)); } return [ 'unread'=> $unReadCount, 'total'=> isset($datas['total'])? $datas['total'] : 0, 'list'=> isset($datas['data'])? $datas['data'] : [], 'pageSize'=> $pageSize, 'cache'=> false, ]; } /** * 获取最新消息 * @param $chatKey * @return mixed */ public function getNewChat($chatKey) { $cacheKey = "caches:chats:new_{$chatKey}"; $data = RedisService::get($cacheKey); if($data){ return $data; } $where = ['chat_key'=>$chatKey,'status'=>1,'mark'=>1]; $data = $this->model->where($where)->select('id','description','message','create_time') //->orderBy('is_read','desc') ->orderBy('create_time','desc') ->orderBy('id','desc') ->first(); $data = $data? $data->toArray() : []; if($data){ RedisService::set($cacheKey, $data, rand(3,5)); } return $data; } /** * 未读聊天消息数量 * @param $userId * @return array|mixed */ public function getUnreadCount($userId, $chatKey=0) { $cacheKey = "caches:chats:unReadCount:{$userId}_{$chatKey}"; $data = RedisService::get($cacheKey); if($data){ return intval($data); } $where = ['to_user_id'=> $userId,'status'=>1,'to_is_show'=>1,'is_read'=>2,'mark'=>1]; if($chatKey){ $where['chat_key'] = $chatKey; } $data = $this->model->where($where)->count('id'); $data = $data? intval($data) : 0; RedisService::set($cacheKey, $data, rand(3, 5)); return $data; } /** * 今日消息数量 * @param $userId * @return array|mixed */ public function getCount($userId, $chatKey=0) { $where = ['to_user_id'=> $userId,'status'=>1,'to_is_show'=>1,'mark'=>1]; if($chatKey){ $where['chat_key'] = $chatKey; } $data = $this->model->where($where)->where('create_time','>=', time() - 2 * 86400)->count('id'); $data = $data? intval($data) : 0; return $data; } /** * 验证是否还有聊天次数或者时长 * @param $fromUserId 发送或发起用户ID * @param $toUserId 聊天对方用户ID * @param int $chatType 聊天类型:1-文字类型,2-视频/语音 * @return mixed */ public function checkChat($fromUserId, $toUserId, $chatType=1) { // 如果是客服,发送不限制 $checkIsCustom = MemberModel::where(['id'=> $fromUserId,'mark'=>1])->value('is_custom'); if($checkIsCustom == 1){ return true; } $params = ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1]) ->select(['id','chat_num','chat_time']) ->first(); $params = $params? $params->toArray() : []; $chatNum = isset($params['chat_num'])? intval($params['chat_num']) : 0; $chatTime = isset($params['chat_time'])? intval($params['chat_time']) : 0; // 未购买过次数的获取系统默认免费参数 if(empty($params)) { $chatNum = ConfigService::make()->getConfigByCode('chat_free_num'); $chatTime = ConfigService::make()->getConfigByCode('chat_free_time'); $chatTime = $chatTime*60; if($chatNum>0) { $data = [ 'from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId, 'chat_num'=> $chatNum>0? intval($chatNum) : 0, 'chat_time'=> $chatTime>0? intval($chatTime) : 0, 'update_time'=> time(), 'remark'=> '更新系统免费参数', 'mark'=> 1 ]; ImChatParamsModel::insert($data); } } return $chatType==1? $chatNum : $chatTime; } /** * 更新聊天参数 * @param $fromUserId * @param $toUserId * @param $num * @param int $chatType */ public function updateChatParams($fromUserId, $toUserId, $num=1, $chatType=1) { if(empty($fromUserId) || empty($toUserId)){ return false; } if(!$id = ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1])->value('id')) { $chatNum = ConfigService::make()->getConfigByCode('chat_free_num'); $chatTime = ConfigService::make()->getConfigByCode('chat_free_time'); $chatTime = $chatTime*60; $data = [ 'from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId, 'chat_num'=> $chatNum>0? intval($chatNum) : 0, 'chat_time'=> $chatTime>0? intval($chatTime) : 0, 'update_time'=> time(), 'remark'=> '更新系统免费参数', 'mark'=> 1 ]; return ImChatParamsModel::insertGetId($data); }else{ if($chatType == 1){ return ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1]) ->decrement('chat_num', $num); }else{ return ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1]) ->decrement('chat_time', $num); } } } /** * 已读 * @param $userId 用户ID * @param $chatKey 聊天窗口ID * @return bool */ public function setRead($userId, $chatKey) { $this->model->where(['chat_key'=> $chatKey]) ->where(['to_user_id'=>$userId]) ->update(['is_read'=>1,'update_time'=>time()]); // 清除缓存 RedisService::keyDel("caches:chats:history*"); RedisService::keyDel("caches:chats:group*"); RedisService::keyDel("caches:messages:bar*"); return true; } /** * 隐藏 * @param $userId 用户ID * @param $chatKey 聊天窗口ID * @return bool */ public function setHide($userId, $chatKey) { $this->model->where(['chat_key'=> $chatKey]) ->where(['from_user_id'=>$userId]) ->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]); $this->model->where(['chat_key'=> $chatKey]) ->where(['to_user_id'=>$userId]) ->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]); // 清除缓存 RedisService::keyDel("caches:chats:history*"); RedisService::keyDel("caches:chats:group*"); RedisService::keyDel("caches:messages:bar*"); return true; } /** * 按窗口清除 * @param $userId 用户ID * @param $chatKey 聊天窗口KEY * @return bool */ public function clear($userId, $chatKey) { $this->model->where(['from_user_id'=>$userId,'chat_key'=>$chatKey])->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]); $this->model->where(['to_user_id'=>$userId,'chat_key'=>$chatKey])->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]); return true; } /** * 清除历史 * @param $userId * @param $type * @return mixed */ public function clearAll($userId, $type=1) { if($type == 1){ // 列表窗 $this->model->where(['from_user_id'=>$userId])->update(['from_is_show'=>2,'is_read'=>1,'update_time'=>time()]); $this->model->where(['to_user_id'=>$userId])->update(['to_is_show'=>2,'is_read'=>1,'update_time'=>time()]); }else{ // 删除 $this->model->where('mark', 1)->where(function($query) use($userId){ $query->where(['from_user_id'=>$userId])->whereOr(['to_user_id'=>$userId]); })->update(['mark'=>0,'update_time'=>time()]); } // 删除历史 $this->model->where('mark', 0)->where('update_time','<', time() - 7*86400)->delete(); // 站内消息 MessageService::make()->clearAll($userId); return true; } /** * 聊天付费 * @param $userId * @param $params * @return bool */ public function buyChat($userId, $params) { $userInfo = MemberService::make()->getCacheInfo(['id'=>$userId,'status'=>1]); $balance = isset($userInfo['balance'])? floatval($userInfo['balance']) : 0; if(empty($userInfo)){ $this->error = 2401; return false; } $chatType = isset($params['type'])? intval($params['type']) : 0; $configKey = $chatType==1? 'chat_buy_num_money':'chat_buy_time_money'; $config = ConfigService::make()->getConfigByCode($configKey); $config = $config? explode('/', $config) : []; $money = isset($config[0])?floatval($config[0]) : 0; $num = isset($config[1])?intval($config[1]) : 0; $fromUserId = isset($params['from_user_id'])? intval($params['from_user_id']) : 0; $toUserId = isset($params['to_user_id'])? intval($params['to_user_id']) : 0; if($money<=0 || $num<=0 || $toUserId<=0 || $fromUserId<=0){ $this->error = 2402; return false; } // 验证余额是否足够 if($balance < $money){ $this->error = 2403; return false; } // 购买数量 DB::beginTransaction(); if(!MemberModel::where(['id'=> $userId])->decrement('balance', $money)){ DB::rollBack(); $this->error = 2404; return false; } // 账户明细 $orderNo = get_order_num('CH'); $data = [ 'user_id'=> $userId, 'source_order_no'=> $orderNo, 'source_uid'=> $toUserId, 'type'=> 8, 'coin_type'=> 4, 'user_type'=> 1, 'money'=> $money, 'balance'=> $balance, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> $chatType==1? "聊天付费购买{$num}次":"视频聊天付费购买{$num}分钟", 'status'=> 1, ]; if(!AccountLogModel::insertGetId($data)){ DB::rollBack(); $this->error = 2405; return false; } // 聊天参数处理 if($chatType == 1){ $data = ['chat_num'=> DB::raw("chat_num + {$num}"),'update_time'=> time()]; if(!ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1]) ->update($data)){ DB::rollBack(); $this->error = 2405; return false; } }else{ $time = $num*60; $data = ['chat_time'=> DB::raw("chat_time + {$time}"),'update_time'=> time()]; if(!ImChatParamsModel::where(['from_user_id'=> $fromUserId, 'to_user_id'=> $toUserId,'mark'=>1]) ->update($data)){ DB::rollBack(); $this->error = 2406; return false; } } $this->error = 2407; DB::commit(); $log = [ 'user_id'=> 0, 'merch_id'=> 0, 'source_uid'=> $userId, 'source_order_no'=> $orderNo, 'type'=> 13, 'coin_type'=> 2, 'user_type'=> 5, 'money'=> $money, 'balance'=> 0, 'create_time'=> time(), 'update_time'=> time(), 'remark'=> '平台聊天收入', 'status'=>1, 'mark'=>1 ]; if(!AccountLogModel::insertGetId($log)){ $this->error = 2641; return false; } // 平台流水 FinanceService::make()->saveLog(0, $money, 1, 1); return true; } /** * 待通知消息 * @param $noticeUserId * @return array|mixed */ public function countByWait($noticeUserId) { $cacheKey = "caches:chat:counts:user_{$noticeUserId}"; $count = RedisService::get($cacheKey); if($count){ return $count; } $where = ['to_user_id'=> $noticeUserId,'status'=>1,'msg_type'=> 1,'mark'=>1]; $count = $this->model->where($where)->where('create_time','>=',time() - 86400)->count('id'); RedisService::set($cacheKey, $count, rand(3,5)); return $count; } }