PersonController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * 私聊相关Api
  4. * @author benzhan
  5. */
  6. class PersonController extends BaseController {
  7. protected $ajaxLoginActions = [
  8. 'msg',
  9. 'sendMsg',
  10. 'repealMsg',
  11. ];
  12. public function __construct() {
  13. parent::__construct(true);
  14. }
  15. /**
  16. * 私聊 获取最新消息
  17. * @author benzhan
  18. */
  19. public function actionNewMsg($args) {
  20. $rules = [
  21. 'session_id' => ['string', 'desc' => '会话id'],
  22. 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'],
  23. ];
  24. Param::checkParam2($rules, $args);
  25. $load_type = 0;
  26. return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
  27. }
  28. /**
  29. * 私聊 获取历史消息
  30. * @author benzhan
  31. */
  32. public function actionHistoryMsg($args) {
  33. $rules = [
  34. 'session_id' => ['string', 'desc' => '会话id'],
  35. 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'],
  36. ];
  37. Param::checkParam2($rules, $args);
  38. $load_type = -1;
  39. return $this->_msg($args['session_id'], $load_type, $args['client_hash']);
  40. }
  41. /**
  42. * 获取消息列表
  43. * @param $session_id string 会话id
  44. * @param $load_type int 加载方式:-1:历史消息, 0:最新消息
  45. * @param $client_hash string 客户端的hash
  46. *
  47. * @return array
  48. */
  49. private function _msg($session_id, $load_type, $client_hash) {
  50. $where = compact('session_id');
  51. $keyWord = [
  52. '_field' => 'is_group, read_hash, read_num',
  53. ];
  54. $objSession = new Session();
  55. $row = $objSession->objTable->getRow($where, $keyWord);
  56. if (!$row) {
  57. return [
  58. 'userMap' => null,
  59. 'list' => [],
  60. 'need_clear' => false,
  61. ];
  62. } else if ($row['is_group']) {
  63. Response::error(CODE_NO_PERMITION, 'session is not personal');
  64. }
  65. $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 0);
  66. $userId = User::getUserId();
  67. if ($userId) {
  68. $c = count($data['list']);
  69. $lastNum = $data['list'][$c-1]['msg_num'];
  70. if ($lastNum > $row['read_num']) {
  71. $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]);
  72. }
  73. }
  74. $data['list'] = array_filter($data['list'], function ($v) {
  75. return $v['state'] == 1;
  76. });
  77. return $data;
  78. }
  79. /**
  80. * 发送私聊消息
  81. * @author solu
  82. * @param $args
  83. * @return array
  84. */
  85. public function actionSendMsg($args) {
  86. $rules = [
  87. 'session_id' => ['string', 'desc' => '会话id'],
  88. 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
  89. 'msg' => ['string', 'desc' => '内容'],
  90. ];
  91. Param::checkParam2($rules, $args);
  92. $userId = User::getUserId();
  93. $objSession = new Session();
  94. $data = [];
  95. try {
  96. $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']);
  97. } catch (Exception $e) {
  98. Response::error($e->getCode(), $e->getMessage());
  99. }
  100. return $data;
  101. }
  102. /**
  103. * 撤销消息
  104. * @author solu
  105. * @param $args
  106. */
  107. public function actionRepealMsg($args) {
  108. $rules = [
  109. 'session_id' => ['string', 'desc' => '会话id'],
  110. 'hash' => ['string', 'desc' => '消息hash'],
  111. ];
  112. Param::checkParam2($rules, $args);
  113. $userId = User::getUserId();
  114. $objMsg = new PersonMsg();
  115. try {
  116. $objMsg->repeal($userId, $args['session_id'], $args['hash']);
  117. } catch (Exception $e) {
  118. Response::error($e->getCode(), $e->getMessage());
  119. }
  120. }
  121. }