['string', 'desc' => '会话id'], 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'], ]; Param::checkParam2($rules, $args); $load_type = 0; return $this->_msg($args['session_id'], $load_type, $args['client_hash']); } /** * 私聊 获取历史消息 * @author benzhan */ public function actionHistoryMsg($args) { $rules = [ 'session_id' => ['string', 'desc' => '会话id'], 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'], ]; Param::checkParam2($rules, $args); $load_type = -1; return $this->_msg($args['session_id'], $load_type, $args['client_hash']); } /** * 获取消息列表 * @param $session_id string 会话id * @param $load_type int 加载方式:-1:历史消息, 0:最新消息 * @param $client_hash string 客户端的hash * * @return array */ private function _msg($session_id, $load_type, $client_hash) { $where = compact('session_id'); $userId = User::getUserId(); if ($userId) { $where['user_id'] = $userId; } $keyWord = [ '_field' => 'is_group, read_hash, read_num', ]; $objSession = new Session(); $row = $objSession->objTable->getRow($where, $keyWord); if (!$row) { // 私聊的情况,没有会话,也要返回userMap $parts = explode('-', $session_id); $userMap = null; if (count($parts) > 1) { $userMap = $objSession->getUserMap($parts); } return [ 'userMap' => $userMap, 'list' => [], 'need_clear' => false, ]; } else if ($row['is_group']) { Response::error(CODE_NO_PERMITION, 'session is not personal'); } $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 0); if ($userId) { $last = end($data['list']); $lastNum = $last['msg_num']; if ($lastNum > $row['read_num']) { $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]); } } $data['list'] = array_filter($data['list'], function ($v) { return $v['state'] == 1; }); $data['list'] = array_values($data['list']); return $data; } /** * 发送私聊消息 * @author solu * @param $args * @return array */ public function actionSendMsg($args) { $rules = [ 'session_id' => ['string', 'desc' => '会话id'], 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'], 'msg' => ['string', 'desc' => '内容'], ]; Param::checkParam2($rules, $args); $userId = User::getUserId(); $objSession = new Session(); $data = []; try { $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']); } catch (Exception $e) { Response::error($e->getCode(), $e->getMessage()); } return $data; } /** * 撤销消息 * @author solu * @param $args */ public function actionRepealMsg($args) { $rules = [ 'session_id' => ['string', 'desc' => '会话id'], 'hash' => ['string', 'desc' => '消息hash'], ]; Param::checkParam2($rules, $args); $userId = User::getUserId(); $objMsg = new PersonMsg(); try { $objMsg->repeal($userId, $args['session_id'], $args['hash']); } catch (Exception $e) { Response::error($e->getCode(), $e->getMessage()); } } /** * 私聊 发送多媒体消息 * @author solu * @param $args * @return array */ public function actionSendFile($args) { $args = array_merge($args, $_FILES); $rules = [ 'to_user' => ['string', 'desc' => '群id'], 'res' => ['array', 'desc' => '资源文件'], ]; Param::checkParam2($rules, $args); $data = null; try { $msgType = FileUrl::getType($args['res']['type']); $maxSize = 1000 * 1000; // 默认最大1MB switch ($msgType) { case FileUrl::TYPE_IMAGE: $maxSize = 1000 * 1000; // 图片最大1MB break; case FileUrl::TYPE_VIDEO: $maxSize = 3000 * 1000; // 视频最大3MB break; case FileUrl::TYPE_AUDIO: $maxSize = 2000 * 1000; // 音频最大2MB break; default: Response::error(CODE_PARAM_ERROR, 'unknown type:' . $args['res']['type']); } $size = filesize($args['res']['tmp_name']); if ($size > $maxSize) { Response::error(CODE_PARAM_ERROR, "file is too big. size:{$size}"); } $objFile = new FileUrl(); $url = $objFile->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']); $url = awsReplaceImg($url); $msg = Utils::encodeRC4($url); $userId = User::getUserId(); $extInfo = []; // 处理封面图 if ($msgType == FileUrl::TYPE_VIDEO) { $cover_url = $objFile->getVideoCover($args['res']['tmp_name'], $args['res']['name']); $cover_url && $extInfo['cover_url'] = awsReplaceImg($cover_url); } $objSession = new Session(); $sessionId = Session::getPersonSessionId($userId, $args['to_user']); $data = $objSession->sendPersonMsg($userId, $sessionId, $msgType, $msg, false, $extInfo); } catch (Exception $e) { Response::error($e->getCode(), $e->getMessage()); } return $data; } }