PersonController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. $keyWord = [
  53. '_field' => 'is_group, read_hash, read_num',
  54. ];
  55. $objSession = new Session();
  56. $row = $objSession->objTable->getRow($where, $keyWord);
  57. if (!$row) {
  58. return [
  59. 'userMap' => null,
  60. 'list' => [],
  61. 'need_clear' => false,
  62. ];
  63. } else if ($row['is_group']) {
  64. Response::error(CODE_NO_PERMITION, 'session is not personal');
  65. }
  66. $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 0);
  67. $userId = User::getUserId();
  68. if ($userId) {
  69. $c = count($data['list']);
  70. $lastNum = $data['list'][$c-1]['msg_num'];
  71. if ($lastNum > $row['read_num']) {
  72. $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]);
  73. }
  74. }
  75. $data['list'] = array_filter($data['list'], function ($v) {
  76. return $v['state'] == 1;
  77. });
  78. return $data;
  79. }
  80. /**
  81. * 发送私聊消息
  82. * @author solu
  83. * @param $args
  84. * @return array
  85. */
  86. public function actionSendMsg($args) {
  87. $rules = [
  88. 'session_id' => ['string', 'desc' => '会话id'],
  89. 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
  90. 'msg' => ['string', 'desc' => '内容'],
  91. ];
  92. Param::checkParam2($rules, $args);
  93. $userId = User::getUserId();
  94. $objSession = new Session();
  95. $data = [];
  96. try {
  97. $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']);
  98. } catch (Exception $e) {
  99. Response::error($e->getCode(), $e->getMessage());
  100. }
  101. return $data;
  102. }
  103. /**
  104. * 撤销消息
  105. * @author solu
  106. * @param $args
  107. */
  108. public function actionRepealMsg($args) {
  109. $rules = [
  110. 'session_id' => ['string', 'desc' => '会话id'],
  111. 'hash' => ['string', 'desc' => '消息hash'],
  112. ];
  113. Param::checkParam2($rules, $args);
  114. $userId = User::getUserId();
  115. $objMsg = new PersonMsg();
  116. try {
  117. $objMsg->repeal($userId, $args['session_id'], $args['hash']);
  118. } catch (Exception $e) {
  119. Response::error($e->getCode(), $e->getMessage());
  120. }
  121. }
  122. /**
  123. * 私聊 发送多媒体消息
  124. * @author solu
  125. * @param $args
  126. * @return array
  127. */
  128. public function actionSendFile($args) {
  129. $args = array_merge($args, $_FILES);
  130. $rules = [
  131. 'to_user' => ['string', 'desc' => '群id'],
  132. 'res' => ['array', 'desc' => '资源文件'],
  133. ];
  134. Param::checkParam2($rules, $args);
  135. $data = null;
  136. try {
  137. $msgType = FileUrl::getType($args['res']['type']);
  138. $maxSize = 1000 * 1000; // 默认最大1MB
  139. switch ($msgType) {
  140. case FileUrl::TYPE_IMAGE:
  141. $maxSize = 1000 * 1000; // 图片最大1MB
  142. break;
  143. case FileUrl::TYPE_VIDEO:
  144. $maxSize = 3000 * 1000; // 视频最大3MB
  145. break;
  146. case FileUrl::TYPE_AUDIO:
  147. $maxSize = 2000 * 1000; // 音频最大2MB
  148. break;
  149. default:
  150. Response::error(CODE_PARAM_ERROR, 'unknown type:' . $args['res']['type']);
  151. }
  152. $size = filesize($args['res']['tmp_name']);
  153. if ($size > $maxSize) {
  154. Response::error(CODE_PARAM_ERROR, "file is too big. size:{$size}");
  155. }
  156. $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  157. $url = awsReplaceImg($url);
  158. $msg = Utils::encodeRC4($url);
  159. $userId = User::getUserId();
  160. $objSession = new Session();
  161. $sessionId = Session::getPersonSessionId($userId, $args['to_user']);
  162. $data = $objSession->sendPersonMsg($userId, $sessionId, $msgType, $msg);
  163. } catch (Exception $e) {
  164. Response::error($e->getCode(), $e->getMessage());
  165. }
  166. return $data;
  167. }
  168. }