123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- /**
- * 私聊相关Api
- * @author benzhan
- */
- class PersonController extends BaseController {
- protected $ajaxLoginActions = [
- 'msg',
- 'sendMsg',
- 'repealMsg',
- 'sendFile',
- ];
- public function __construct() {
- parent::__construct(true);
- }
- /**
- * 私聊 获取最新消息
- * @author benzhan
- */
- public function actionNewMsg($args) {
- $rules = [
- 'session_id' => ['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}");
- }
- $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
- $url = awsReplaceImg($url);
- $msg = Utils::encodeRC4($url);
- $userId = User::getUserId();
- $objSession = new Session();
- $sessionId = Session::getPersonSessionId($userId, $args['to_user']);
- $data = $objSession->sendPersonMsg($userId, $sessionId, $msgType, $msg);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- return $data;
- }
- }
|