SessionController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 'miniUnRead',
  16. ];
  17. public function __construct() {
  18. parent::__construct(true);
  19. }
  20. /**
  21. * 返回会话列表
  22. * @author benzhan
  23. */
  24. public function actionList($args) {
  25. $rules = [
  26. // 'update_time_int' => ['int', 'nullable' => true, 'desc' => '最后更新时间']
  27. 'current_group_id' => ['int', 'nullable' => true, 'desc' => '当前群id'],
  28. ];
  29. Param::checkParam2($rules, $args);
  30. $user_id = User::getUserId();
  31. // $update_time_int = (int) $args['update_time_int'];
  32. $where = compact('user_id');
  33. $keyWord = [
  34. '_field' => 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute, read_num',
  35. // '_sortKey' => 'pin_time_int DESC, update_time_int DESC',
  36. '_limit' => 500, // 最多加载500个会话
  37. // '_debug' => 1
  38. ];
  39. $objSession = new Session();
  40. // if ($update_time_int) {
  41. // // 只加载新更新的
  42. // $keyWord['_where'] = "update_time_int > {$update_time_int}";
  43. // }
  44. $currentGroupId = intval($args['current_group_id']);
  45. $currentGroupId && $where['is_group'] = 0; // 群拉人列表过滤群session
  46. $list = $objSession->objTable->getAll($where, $keyWord);
  47. $list = $objSession->fillSession($user_id, $list, $currentGroupId);
  48. return $list;
  49. }
  50. private function _updateState($session_id, $newData) {
  51. $user_id = User::getUserId();
  52. $objSession = new Session();
  53. try {
  54. // 私聊
  55. if ($session_id != intval($session_id)) {
  56. $objSession->checkPersonSession($user_id, $session_id);
  57. } else {
  58. $objSession->checkGroupSession($user_id, $session_id);
  59. }
  60. $newData['update_time_int'] = microtime(true) * 1000;
  61. $objSession->updateState($session_id, $newData, compact('user_id'));
  62. } catch (Exception $ex) {
  63. Response::error($ex->getCode(), $ex->getMessage());
  64. }
  65. }
  66. private function _delete($session_id) {
  67. $user_id = User::getUserId();
  68. $objSession = new Session();
  69. try {
  70. // 私聊
  71. if ($session_id != intval($session_id)) {
  72. $objSession->checkPersonSession($user_id, $session_id);
  73. } else {
  74. $objSession->checkGroupSession($user_id, $session_id);
  75. }
  76. $objSession->objTable->delObject(compact('user_id', 'session_id'));
  77. } catch (Exception $ex) {
  78. // Response::error($ex->getCode(), $ex->getMessage());
  79. }
  80. }
  81. // /**
  82. // * 设置已读
  83. // * @author benzhan
  84. // */
  85. // public function actionSetRead($args) {
  86. // $rules = [
  87. // 'session_id' => ['string', 'desc' => '会话id'],
  88. // 'hash' => ['string', 'desc' => '消息Hash值'],
  89. // ];
  90. // Param::checkParam2($rules, $args);
  91. //
  92. // $this->_updateState($args['session_id'], ['read_hash' => $args['hash']]);
  93. // }
  94. /**
  95. * 静音
  96. * @author benzhan
  97. */
  98. public function actionMute($args) {
  99. $rules = [
  100. 'session_id' => ['string', 'desc' => '会话id'],
  101. ];
  102. Param::checkParam2($rules, $args);
  103. $this->_updateState($args['session_id'], ['is_mute' => 1]);
  104. }
  105. /**
  106. * 取消静音
  107. * @author benzhan
  108. */
  109. public function actionUnMute($args) {
  110. $rules = [
  111. 'session_id' => ['string', 'desc' => '会话id'],
  112. ];
  113. Param::checkParam2($rules, $args);
  114. $this->_updateState($args['session_id'], ['is_mute' => 0]);
  115. }
  116. /**
  117. * 置顶
  118. * @author benzhan
  119. */
  120. public function actionPin($args) {
  121. $rules = [
  122. 'session_id' => ['string', 'desc' => '会话id'],
  123. ];
  124. Param::checkParam2($rules, $args);
  125. $newData = ['is_pin' => 1, 'pin_time_int' => time()];
  126. $this->_updateState($args['session_id'], $newData);
  127. return $newData;
  128. }
  129. /**
  130. * 取消置顶
  131. * @author benzhan
  132. */
  133. public function actionUnPin($args) {
  134. $rules = [
  135. 'session_id' => ['string', 'desc' => '会话id'],
  136. ];
  137. Param::checkParam2($rules, $args);
  138. $newData = ['is_pin' => 0, 'pin_time_int' => 0];
  139. $this->_updateState($args['session_id'], $newData);
  140. return $newData;
  141. }
  142. /**
  143. * 删除会话
  144. * @author benzhan
  145. */
  146. public function actionDelete($args) {
  147. $rules = [
  148. 'session_id' => ['string', 'desc' => '会话id'],
  149. ];
  150. Param::checkParam2($rules, $args);
  151. $this->_delete($args['session_id']);
  152. }
  153. /**
  154. * 迷你版未读数量
  155. * @author solu
  156. * @param $args
  157. * @return array
  158. */
  159. public function actionMiniUnRead($args) {
  160. $rules = [];
  161. Param::checkParam2($rules, $args);
  162. $user_id = User::getUserId();
  163. $objSession = new Session();
  164. $list = $objSession->objTable->getAll(['user_id' => $user_id, 'is_group' => 0], [
  165. '_field' => 'session_id, read_num',
  166. ]);
  167. $objRedis = dwRedis::init(Eos::REDIS_SERV);
  168. $toUserIds = [];
  169. foreach ($list as $v) {
  170. $toUserIds[] = Session::getToUser($user_id, $v['session_id']);
  171. }
  172. $groupServers = (new GroupInfo())->getGroupServerFromUserIds($toUserIds);
  173. $serverIds = array_keys($groupServers);
  174. $unreadMap = [];
  175. foreach ($list as $v) {
  176. $last = Session::getLastMsgNum($v['session_id'], $objRedis);
  177. $unread = max($last - $v['read_num'], 0);
  178. $toUser = Session::getToUser($user_id, $v['session_id']);
  179. if (in_array($toUser, $serverIds)) {
  180. $unreadMap[$groupServers[$toUser]] += $unread;
  181. } else {
  182. $unreadMap[0] += $unread;
  183. }
  184. }
  185. return $unreadMap;
  186. }
  187. //
  188. // /**
  189. // * 文件上传
  190. // * @author solu
  191. // * @param $args
  192. // * @return array
  193. // */
  194. // public function actionUploadFile($args) {
  195. // $args = array_merge($args, $_FILES);
  196. // $rules = [
  197. // 'res' => ['array', 'desc' => '资源文件'],
  198. // ];
  199. // Param::checkParam2($rules, $args);
  200. //
  201. // $url = '';
  202. // try {
  203. // $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  204. // } catch (Exception $e) {
  205. // Response::error($e->getCode(), $e->getMessage());
  206. // }
  207. //
  208. // return compact('url');
  209. // }
  210. }