123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- <?php
- class SessionController extends BaseController {
- protected $ajaxLoginActions = [
- 'list',
- 'personMsg',
- 'sendMsg',
- 'mute',
- 'unMute',
- 'pin',
- 'unPin',
- 'miniUnRead',
- ];
- public function __construct() {
- parent::__construct(true);
- }
-
- public function actionList($args) {
- $rules = [
- 'current_group_id' => ['int', 'nullable' => true, 'desc' => '当前群id'],
- ];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- $where = compact('user_id');
- $keyWord = [
- '_field' => 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute, read_num',
- '_limit' => 500,
- ];
- $objSession = new Session();
- $currentGroupId = intval($args['current_group_id']);
- $currentGroupId && $where['is_group'] = 0;
- $list = $objSession->objTable->getAll($where, $keyWord);
- $list = $objSession->fillSession($user_id, $list, $currentGroupId);
- return $list;
- }
- private function _updateState($session_id, $newData) {
- $user_id = User::getUserId();
- $objSession = new Session();
- try {
-
- if ($session_id != intval($session_id)) {
- $objSession->checkPersonSession($user_id, $session_id);
- } else {
- $objSession->checkGroupSession($user_id, $session_id);
- }
- $newData['update_time_int'] = microtime(true) * 1000;
- $objSession->updateState($session_id, $newData, compact('user_id'));
- } catch (Exception $ex) {
- Response::error($ex->getCode(), $ex->getMessage());
- }
- }
- private function _delete($session_id) {
- $user_id = User::getUserId();
- $objSession = new Session();
- try {
-
- if ($session_id != intval($session_id)) {
- $objSession->checkPersonSession($user_id, $session_id);
- } else {
- $objSession->checkGroupSession($user_id, $session_id);
- }
- $objSession->objTable->delObject(compact('user_id', 'session_id'));
- } catch (Exception $ex) {
- }
- }
-
- public function actionMute($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- ];
- Param::checkParam2($rules, $args);
- $this->_updateState($args['session_id'], ['is_mute' => 1]);
- }
-
- public function actionUnMute($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- ];
- Param::checkParam2($rules, $args);
- $this->_updateState($args['session_id'], ['is_mute' => 0]);
- }
-
- public function actionPin($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- ];
- Param::checkParam2($rules, $args);
- $newData = ['is_pin' => 1, 'pin_time_int' => time()];
- $this->_updateState($args['session_id'], $newData);
- return $newData;
- }
-
- public function actionUnPin($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- ];
- Param::checkParam2($rules, $args);
- $newData = ['is_pin' => 0, 'pin_time_int' => 0];
- $this->_updateState($args['session_id'], $newData);
- return $newData;
- }
-
- public function actionDelete($args) {
- $rules = [
- 'session_id' => ['string', 'desc' => '会话id'],
- ];
- Param::checkParam2($rules, $args);
- $this->_delete($args['session_id']);
- }
-
- public function actionMiniUnRead($args) {
- $rules = [];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- $objSession = new Session();
- $list = $objSession->objTable->getAll(['user_id' => $user_id, 'is_group' => 0], [
- '_field' => 'session_id, read_num',
- ]);
- $objRedis = dwRedis::init(Eos::REDIS_SERV);
- $toUserIds = [];
- foreach ($list as $v) {
- $toUserIds[] = Session::getToUser($user_id, $v['session_id']);
- }
- $groupServers = (new GroupInfo())->getGroupServerFromUserIds($toUserIds);
- $serverIds = array_keys($groupServers);
- $unreadMap = [];
- foreach ($list as $v) {
- $last = Session::getLastMsgNum($v['session_id'], $objRedis);
- $unread = max($last - $v['read_num'], 0);
- $toUser = Session::getToUser($user_id, $v['session_id']);
- if (in_array($toUser, $serverIds)) {
- $unreadMap[$groupServers[$toUser]] += $unread;
- } else {
- $unreadMap[0] += $unread;
- }
- }
- return $unreadMap;
- }
- }
|