PersonController.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * 私聊相关Api
  4. * @author benzhan
  5. */
  6. class PersonController extends BaseController {
  7. protected $ajaxLoginActions = [
  8. 'msg',
  9. 'sendMsg',
  10. 'repealMsg',
  11. 'sendFile',
  12. ];
  13. public function __construct() {
  14. parent::__construct(true);
  15. }
  16. /**
  17. * 私聊 获取最新消息
  18. * @author benzhan
  19. */
  20. public function actionNewMsg($args) {
  21. $rules = [
  22. 'session_id' => ['string', 'desc' => '会话id'],
  23. 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'],
  24. ];
  25. Param::checkParam2($rules, $args);
  26. $load_type = 0;
  27. return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
  28. }
  29. /**
  30. * 私聊 获取历史消息
  31. * @author benzhan
  32. */
  33. public function actionHistoryMsg($args) {
  34. $rules = [
  35. 'session_id' => ['string', 'desc' => '会话id'],
  36. 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'],
  37. ];
  38. Param::checkParam2($rules, $args);
  39. $load_type = -1;
  40. return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
  41. }
  42. /**
  43. * 获取消息列表
  44. * @param $session_id string 会话id
  45. * @param $load_type int 加载方式:-1:历史消息, 0:最新消息
  46. * @param $client_hash string 客户端的hash
  47. *
  48. * @return array
  49. */
  50. private function _msg($session_id, $load_type, $client_hash) {
  51. $where = compact('session_id');
  52. $userId = User::getUserId();
  53. if ($userId) {
  54. $where['user_id'] = $userId;
  55. }
  56. $keyWord = [
  57. '_field' => 'is_group, read_hash, read_num',
  58. ];
  59. $objSession = new Session();
  60. $row = $objSession->objTable->getRow($where, $keyWord);
  61. if (!$row) {
  62. // 私聊的情况,没有会话,也要返回userMap
  63. $parts = explode('-', $session_id);
  64. $userMap = null;
  65. if (count($parts) > 1) {
  66. $userMap = $objSession->getUserMap($parts);
  67. }
  68. return [
  69. 'userMap' => $userMap,
  70. 'list' => [],
  71. 'need_clear' => false,
  72. ];
  73. } else if ($row['is_group']) {
  74. Response::error(CODE_NO_PERMITION, 'session is not personal');
  75. }
  76. $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 0);
  77. if ($userId) {
  78. $last = end($data['list']);
  79. $lastNum = $last['msg_num'];
  80. if ($lastNum > $row['read_num']) {
  81. $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]);
  82. }
  83. }
  84. $data['list'] = array_filter($data['list'], function ($v) {
  85. return $v['state'] == 1;
  86. });
  87. $data['list'] = array_values($data['list']);
  88. return $data;
  89. }
  90. /**
  91. * 发送私聊消息
  92. * @author solu
  93. * @param $args
  94. * @return array
  95. */
  96. public function actionSendMsg($args) {
  97. $rules = [
  98. 'session_id' => ['string', 'desc' => '会话id'],
  99. 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
  100. 'msg' => ['string', 'desc' => '内容'],
  101. ];
  102. Param::checkParam2($rules, $args);
  103. $userId = User::getUserId();
  104. $objSession = new Session();
  105. $data = [];
  106. try {
  107. $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']);
  108. } catch (Exception $e) {
  109. Response::error($e->getCode(), $e->getMessage());
  110. }
  111. return $data;
  112. }
  113. /**
  114. * 撤销消息
  115. * @author solu
  116. * @param $args
  117. */
  118. public function actionRepealMsg($args) {
  119. $rules = [
  120. 'session_id' => ['string', 'desc' => '会话id'],
  121. 'hash' => ['string', 'desc' => '消息hash'],
  122. ];
  123. Param::checkParam2($rules, $args);
  124. $userId = User::getUserId();
  125. $objMsg = new PersonMsg();
  126. try {
  127. $objMsg->repeal($userId, $args['session_id'], $args['hash']);
  128. } catch (Exception $e) {
  129. Response::error($e->getCode(), $e->getMessage());
  130. }
  131. }
  132. /**
  133. * 私聊 发送多媒体消息
  134. * @author solu
  135. * @param $args
  136. * @return array
  137. */
  138. public function actionSendFile($args) {
  139. $args = array_merge($args, $_FILES);
  140. $rules = [
  141. 'to_user' => ['string', 'desc' => '群id'],
  142. 'res' => ['array', 'desc' => '资源文件'],
  143. ];
  144. Param::checkParam2($rules, $args);
  145. $data = null;
  146. try {
  147. $msgType = FileUrl::getType($args['res']['type']);
  148. $maxSize = 1000 * 1000; // 默认最大1MB
  149. switch ($msgType) {
  150. case FileUrl::TYPE_IMAGE:
  151. $maxSize = 1000 * 1000; // 图片最大1MB
  152. break;
  153. case FileUrl::TYPE_VIDEO:
  154. $maxSize = 3000 * 1000; // 视频最大3MB
  155. break;
  156. case FileUrl::TYPE_AUDIO:
  157. $maxSize = 2000 * 1000; // 音频最大2MB
  158. break;
  159. default:
  160. Response::error(CODE_PARAM_ERROR, 'unknown type:' . $args['res']['type']);
  161. }
  162. $size = filesize($args['res']['tmp_name']);
  163. if ($size > $maxSize) {
  164. Response::error(CODE_PARAM_ERROR, "file is too big. size:{$size}");
  165. }
  166. $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  167. $url = awsReplaceImg($url);
  168. $msg = Utils::encodeRC4($url);
  169. $userId = User::getUserId();
  170. $objSession = new Session();
  171. $sessionId = Session::getPersonSessionId($userId, $args['to_user']);
  172. $data = $objSession->sendPersonMsg($userId, $sessionId, $msgType, $msg);
  173. } catch (Exception $e) {
  174. Response::error($e->getCode(), $e->getMessage());
  175. }
  176. return $data;
  177. }
  178. }