SessionController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Class SessionController
  4. * @author Blackbinbin
  5. */
  6. class SessionController extends BaseController {
  7. protected $ajaxLoginActions = [
  8. 'list',
  9. 'personMsg',
  10. 'sendMsg',
  11. 'mute',
  12. 'unMute',
  13. 'pin',
  14. 'unPin',
  15. ];
  16. public function __construct() {
  17. parent::__construct(true);
  18. }
  19. /**
  20. * 返回会话列表
  21. * @author benzhan
  22. */
  23. public function actionList($args) {
  24. $rules = [
  25. // 'update_time_int' => ['int', 'nullable' => true, 'desc' => '最后更新时间']
  26. 'current_group_id' => ['int', 'nullable' => true, 'desc' => '当前群id'],
  27. ];
  28. Param::checkParam2($rules, $args);
  29. $user_id = User::getUserId();
  30. // $update_time_int = (int) $args['update_time_int'];
  31. $where = compact('user_id');
  32. $keyWord = [
  33. '_field' => 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute',
  34. '_sort' => 'pin_time_int DESC, update_time_int DESC',
  35. '_limit' => 500, // 最多加载500个会话
  36. ];
  37. $objSession = new Session();
  38. // if ($update_time_int) {
  39. // // 只加载新更新的
  40. // $keyWord['_where'] = "update_time_int > {$update_time_int}";
  41. // }
  42. $currentGroupId = intval($args['current_group_id']);
  43. $currentGroupId && $where['is_group'] = 0; // 群拉人列表过滤群session
  44. $list = $objSession->objTable->getAll($where, $keyWord);
  45. $list = $objSession->fillSession($user_id, $list, $currentGroupId);
  46. return $list;
  47. }
  48. private function _updateState($session_id, $newData) {
  49. $user_id = User::getUserId();
  50. $objSession = new Session();
  51. try {
  52. // 私聊
  53. if ($session_id != intval($session_id)) {
  54. $objSession->checkPersonSession($user_id, $session_id);
  55. } else {
  56. $objSession->checkGroupSession($user_id, $session_id);
  57. }
  58. $newData['update_time_int'] = microtime(true) * 1000;
  59. $objSession->updateState($session_id, $newData, compact('user_id'));
  60. } catch (Exception $ex) {
  61. Response::error($ex->getCode(), $ex->getMessage());
  62. }
  63. }
  64. // /**
  65. // * 设置已读
  66. // * @author benzhan
  67. // */
  68. // public function actionSetRead($args) {
  69. // $rules = [
  70. // 'session_id' => ['string', 'desc' => '会话id'],
  71. // 'hash' => ['string', 'desc' => '消息Hash值'],
  72. // ];
  73. // Param::checkParam2($rules, $args);
  74. //
  75. // $this->_updateState($args['session_id'], ['read_hash' => $args['hash']]);
  76. // }
  77. /**
  78. * 静音
  79. * @author benzhan
  80. */
  81. public function actionMute($args) {
  82. $rules = [
  83. 'session_id' => ['string', 'desc' => '会话id'],
  84. ];
  85. Param::checkParam2($rules, $args);
  86. $this->_updateState($args['session_id'], ['is_mute' => 1]);
  87. }
  88. /**
  89. * 取消静音
  90. * @author benzhan
  91. */
  92. public function actionUnMute($args) {
  93. $rules = [
  94. 'session_id' => ['string', 'desc' => '会话id'],
  95. ];
  96. Param::checkParam2($rules, $args);
  97. $this->_updateState($args['session_id'], ['is_mute' => 0]);
  98. }
  99. /**
  100. * 置顶
  101. * @author benzhan
  102. */
  103. public function actionPin($args) {
  104. $rules = [
  105. 'session_id' => ['string', 'desc' => '会话id'],
  106. ];
  107. Param::checkParam2($rules, $args);
  108. $this->_updateState($args['session_id'], ['is_pin' => 1]);
  109. }
  110. /**
  111. * 取消置顶
  112. * @author benzhan
  113. */
  114. public function actionUnPin($args) {
  115. $rules = [
  116. 'session_id' => ['string', 'desc' => '会话id'],
  117. ];
  118. Param::checkParam2($rules, $args);
  119. $this->_updateState($args['session_id'], ['is_pin' => 0]);
  120. }
  121. /**
  122. * 删除会话
  123. * @author benzhan
  124. */
  125. public function actionDelete($args) {
  126. $rules = [
  127. 'session_id' => ['string', 'desc' => '会话id'],
  128. ];
  129. Param::checkParam2($rules, $args);
  130. // $this->_updateState($args['session_id'], ['is_delete' => 1]);
  131. }
  132. //
  133. // /**
  134. // * 文件上传
  135. // * @author solu
  136. // * @param $args
  137. // * @return array
  138. // */
  139. // public function actionUploadFile($args) {
  140. // $args = array_merge($args, $_FILES);
  141. // $rules = [
  142. // 'res' => ['array', 'desc' => '资源文件'],
  143. // ];
  144. // Param::checkParam2($rules, $args);
  145. //
  146. // $url = '';
  147. // try {
  148. // $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  149. // } catch (Exception $e) {
  150. // Response::error($e->getCode(), $e->getMessage());
  151. // }
  152. //
  153. // return compact('url');
  154. // }
  155. }