PersonController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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',
  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. return $objSession->getMsgList($session_id, $client_hash, $load_type, 0);
  66. }
  67. /**
  68. * 发送私聊消息
  69. * @author solu
  70. * @param $args
  71. * @return array
  72. */
  73. public function actionSendMsg($args) {
  74. $rules = [
  75. 'session_id' => ['string', 'desc' => '会话id'],
  76. 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
  77. 'msg' => ['string', 'desc' => '内容'],
  78. ];
  79. Param::checkParam2($rules, $args);
  80. $userId = User::getUserId();
  81. $objSession = new Session();
  82. $data = [];
  83. try {
  84. $data = $objSession->sendPersonMsg($userId, $args['session_id'], $args['msg_type'], $args['msg']);
  85. } catch (Exception $e) {
  86. Response::error($e->getCode(), $e->getMessage());
  87. }
  88. return $data;
  89. }
  90. /**
  91. * 撤销消息
  92. * @author solu
  93. * @param $args
  94. */
  95. public function actionRepealMsg($args) {
  96. $rules = [
  97. 'session_id' => ['string', 'desc' => '会话id'],
  98. 'hash' => ['string', 'desc' => '消息hash'],
  99. ];
  100. Param::checkParam2($rules, $args);
  101. $userId = User::getUserId();
  102. $objMsg = new PersonMsg();
  103. try {
  104. $objMsg->repeal($userId, $args['session_id'], $args['hash']);
  105. } catch (Exception $e) {
  106. Response::error($e->getCode(), $e->getMessage());
  107. }
  108. }
  109. }