SessionController.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, read_num, update_time_int',
  34. '_sortKey' => '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. private function _delete($session_id) {
  65. $user_id = User::getUserId();
  66. $objSession = new Session();
  67. try {
  68. // 私聊
  69. if ($session_id != intval($session_id)) {
  70. $objSession->checkPersonSession($user_id, $session_id);
  71. } else {
  72. $objSession->checkGroupSession($user_id, $session_id);
  73. }
  74. $objSession->objTable->delObject(compact('user_id', 'session_id'));
  75. } catch (Exception $ex) {
  76. Response::error($ex->getCode(), $ex->getMessage());
  77. }
  78. }
  79. // /**
  80. // * 设置已读
  81. // * @author benzhan
  82. // */
  83. // public function actionSetRead($args) {
  84. // $rules = [
  85. // 'session_id' => ['string', 'desc' => '会话id'],
  86. // 'hash' => ['string', 'desc' => '消息Hash值'],
  87. // ];
  88. // Param::checkParam2($rules, $args);
  89. //
  90. // $this->_updateState($args['session_id'], ['read_hash' => $args['hash']]);
  91. // }
  92. /**
  93. * 静音
  94. * @author benzhan
  95. */
  96. public function actionMute($args) {
  97. $rules = [
  98. 'session_id' => ['string', 'desc' => '会话id'],
  99. ];
  100. Param::checkParam2($rules, $args);
  101. $this->_updateState($args['session_id'], ['is_mute' => 1]);
  102. }
  103. /**
  104. * 取消静音
  105. * @author benzhan
  106. */
  107. public function actionUnMute($args) {
  108. $rules = [
  109. 'session_id' => ['string', 'desc' => '会话id'],
  110. ];
  111. Param::checkParam2($rules, $args);
  112. $this->_updateState($args['session_id'], ['is_mute' => 0]);
  113. }
  114. /**
  115. * 置顶
  116. * @author benzhan
  117. */
  118. public function actionPin($args) {
  119. $rules = [
  120. 'session_id' => ['string', 'desc' => '会话id'],
  121. ];
  122. Param::checkParam2($rules, $args);
  123. $this->_updateState($args['session_id'], ['is_pin' => 1, 'pin_time_int' => time()]);
  124. }
  125. /**
  126. * 取消置顶
  127. * @author benzhan
  128. */
  129. public function actionUnPin($args) {
  130. $rules = [
  131. 'session_id' => ['string', 'desc' => '会话id'],
  132. ];
  133. Param::checkParam2($rules, $args);
  134. $this->_updateState($args['session_id'], ['is_pin' => 0, 'pin_time_int' => 0]);
  135. }
  136. /**
  137. * 删除会话
  138. * @author benzhan
  139. */
  140. public function actionDelete($args) {
  141. $rules = [
  142. 'session_id' => ['string', 'desc' => '会话id'],
  143. ];
  144. Param::checkParam2($rules, $args);
  145. $this->_delete($args['session_id']);
  146. }
  147. //
  148. // /**
  149. // * 文件上传
  150. // * @author solu
  151. // * @param $args
  152. // * @return array
  153. // */
  154. // public function actionUploadFile($args) {
  155. // $args = array_merge($args, $_FILES);
  156. // $rules = [
  157. // 'res' => ['array', 'desc' => '资源文件'],
  158. // ];
  159. // Param::checkParam2($rules, $args);
  160. //
  161. // $url = '';
  162. // try {
  163. // $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  164. // } catch (Exception $e) {
  165. // Response::error($e->getCode(), $e->getMessage());
  166. // }
  167. //
  168. // return compact('url');
  169. // }
  170. }