123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951 |
- <?php
- /**
- * 群相关Api
- * @author benzhan
- */
- class GroupController extends BaseController {
- protected $ajaxLoginActions = [
- 'sendMsg',
- 'sendImageMsg',
- 'pinMsg',
- 'unpinMsg',
- 'getFriends',
- 'create',
- 'join',
- 'leave',
- 'blockUser',
- 'unblockUser',
- 'invites',
- 'removes',
- 'changeName',
- 'changeTitle',
- 'changeNotice',
- 'changeCover',
- 'addAdmin',
- 'removeAdmin',
- 'auth',
- 'changeCreator',
- ];
- public function __construct() {
- parent::__construct(true);
- }
- /**
- * 群信息【不需要登录】
- * @author benzhan
- */
- public function actionInfo($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- ];
- Param::checkParam2($rules, $args);
- $user_id = User::getUserId();
- $objGroupInfo = new GroupInfo();
- $_field = 'group_id, group_name, group_title, group_notice, cover_photo, member_num, creator, url, server_id';
- $group = $objGroupInfo->objTable->getRow($args, compact('_field'));
- $group && $group['invite_url'] = GroupInfo::genInviteUrl($group['group_name']);
- $group['increase_num'] = 0;
- $_field = 'user_id, is_admin, is_block, join_time';
- $objUserGroup = new UserGroup();
- $groupUsers = $objUserGroup->objTable->getAll(['group_id' => $args['group_id'], 'state' => 1], ['_field' => $_field, '_sortKey' => 'join_time ASC', '_limit' => 100]);
- $joinMap = arrayFormatKey($groupUsers, 'user_id', 'join_time');
- $user_ids = [];
- $blockList = [];
- $adminList = [];
- // 把自己加进去
- if (!in_array($user_id, array_keys($joinMap))) {
- $_row = $objUserGroup->objTable->getRow(['user_id' => $user_id, 'group_id' => $args['group_id'], 'state' => 1], ['_field' => $_field]);
- if ($_row) {
- $groupUsers[] = $_row;
- $joinMap[$user_id] = $_row['join_time'];
- }
- }
- foreach ($groupUsers as $_u) {
- $_uid = intval($_u['user_id']);
- $user_ids[] = $_uid;
- $_u['is_block'] && $blockList[] = $_uid;
- $_u['is_admin'] && $adminList[] = $_uid;
- }
- // 获取pin的消息
- $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
- $where2 = [
- 'group_id' => $args['group_id'],
- 'is_pin' => 1,
- '_field' => '`from`, hash, msg, msg_type'
- ];
- $pinMsg = $objGroupMsg->getRow($where2);
- // $inList = in_array($pinMsg['from'], $user_ids);
- $user_ids[] = $pinMsg['from'];
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $_field = 'user_id, user_name, nick_name, cover_photo';
- $members = $objUserInfo->getAll(['user_id' => $user_ids], compact('_field'));
- coverReplaceArrImage($members, 'cover_photo');
- $members = arrayFormatKey($members, 'user_id');
- $joinTimes = [];
- $adminNum = 0;
- $newMembers = [];
- foreach ($groupUsers as $_u) {
- $_uid = intval($_u['user_id']);
- if ($members[$_uid]) {
- $joinTime = strtotime($joinMap[$_uid]);
- $joinTime < 0 && $joinTime = time();
- if ($_u['is_block']) {
- $members[$_uid]['is_block'] = (int) $_u['is_block'];
- }
- if ($_u['is_admin']) {
- $members[$_uid]['is_admin'] = (int) $_u['is_admin'];
- $joinTime = $adminNum++;
- if ($group['creator'] == $_uid) {
- $members[$_uid]['is_admin'] = 2;
- $joinTime = -1;
- }
- }
- $newMembers[] = $members[$_uid];
- $joinTimes[$_uid] = $joinTime;
- }
- }
- // 置顶消息用户信息
- $pinMsg = array_merge($pinMsg, arrayFilter($members[$pinMsg['from']], ['user_name', 'nick_name', 'cover_photo']));
- $members = $newMembers;
- array_multisort($joinTimes, SORT_ASC, $members);
- $sessionInfo = null;
- if ($user_id) {
- $objSession = new Session();
- $sessionInfo = $objSession->objTable->getRow(['user_id' => $user_id, 'session_id' => $args['group_id']], ['_field' => 'is_pin, pin_time_int, is_mute']);
- }
- $objGroupEos = new TableHelper('group_eos', 'dw_chat');
- $eosInfo = $objGroupEos->getRow($args);
- return compact('group', 'eosInfo', 'members', 'pinMsg', 'sessionInfo', 'blockList', 'adminList');
- }
- /**
- * 群聊 获取最新消息【不需要登录】
- * @author benzhan
- */
- public function actionNewMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'],
- ];
- Param::checkParam2($rules, $args);
- $load_type = 0;
- return $this->_msg($args['group_id'], $load_type, $args['client_hash']);
- }
- /**
- * 群聊 获取历史消息【不需要登录】
- * @author benzhan
- */
- public function actionHistoryMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'],
- ];
- Param::checkParam2($rules, $args);
- $load_type = -1;
- return $this->_msg($args['group_id'], $load_type, $args['client_hash']);
- }
- /**
- * 获取消息列表
- * @param $session_id string 会话id
- * @param $load_type int 加载方式:-1:历史消息, 0:最新消息
- * @param $client_hash string 客户端的hash
- *
- * @return array
- */
- private function _msg($session_id, $load_type, $client_hash) {
- $userId = User::getUserId();
- $where = compact('session_id');
- if ($userId) {
- $where['user_id'] = $userId;
- }
- $keyWord = [
- '_field' => 'is_group, read_hash, read_num',
- // '_debug' => 1
- ];
- $objSession = new Session();
- $row = $objSession->objTable->getRow($where, $keyWord);
- // if (!$row) {
- // Response::error(CODE_NO_PERMITION, 'session is not found');
- // } else if (!$row['is_group']) {
- // Response::error(CODE_NO_PERMITION, 'session is not group');
- // }
- $data = $objSession->getMsgList($session_id, $client_hash, $load_type, 1);
- if ($userId && $row) {
- $end = end($data['list']);
- $lastNum = $end['msg_num'];
- // var_dump($lastNum, $row);exit;
- if ($lastNum > $row['read_num']) {
- $objSession->updateState($session_id, ['read_num' => $lastNum], ['user_id' => $userId]);
- }
- }
- $data['list'] = array_filter($data['list'], function ($v) {
- return $v['state'] == 1;
- });
- $data['list'] = array_values($data['list']);
- return $data;
- }
- /**
- * 发送群聊消息
- * @author solu
- * @param $args
- * @return array
- */
- public function actionSendMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
- 'msg' => ['string', 'desc' => '内容'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- $objSession = new Session();
- $data = [];
- try {
- $data = $objSession->sendGroupMsg($userId, $args['group_id'], $args['msg_type'], $args['msg']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- return $data;
- }
- public function actionSendImageMsg($args) {
- return $this->actionSendFile($args);
- }
- /**
- * 群聊 发送多媒体消息
- * @author solu
- * @param $args
- * @return array
- */
- public function actionSendFile($args) {
- $args = array_merge($args, $_FILES);
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'res' => ['array', 'desc' => '资源文件'],
- ];
- Param::checkParam2($rules, $args);
- $data = null;
- try {
- $msgType = FileUrl::getType($args['res']['type']);
- $maxSize = 1000 * 1000; // 默认最大1MB
- switch ($msgType) {
- case FileUrl::TYPE_IMAGE:
- $maxSize = 1000 * 1000; // 图片最大1MB
- break;
- case FileUrl::TYPE_VIDEO:
- $maxSize = 3000 * 1000; // 视频最大3MB
- break;
- case FileUrl::TYPE_AUDIO:
- $maxSize = 2000 * 1000; // 音频最大2MB
- break;
- default:
- Response::error(CODE_PARAM_ERROR, 'unknown type:' . $args['res']['type']);
- }
- $size = filesize($args['res']['tmp_name']);
- if ($size > $maxSize) {
- Response::error(CODE_PARAM_ERROR, "file is too big. size:{$size}");
- }
- $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
- $url = awsReplaceImg($url);
- $msg = Utils::encodeRC4($url);
- $userId = User::getUserId();
- $objSession = new Session();
- $data = $objSession->sendGroupMsg($userId, $args['group_id'], $msgType, $msg);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- return $data;
- }
- /**
- * 群聊 置顶消息
- * @author solu
- * @param $args
- */
- public function actionPinMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'hash' => ['string', 'desc' => '消息hash'],
- ];
- Param::checkParam2($rules, $args);
- $group_id = (int) $args['group_id'];
- $this->_checkGroupAdmin($group_id);
- $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
- $_field = '`from`, hash, msg, msg_type';
- $row = $objGroupMsg->getRow($args, compact('_field'));
- if (!$row) {
- Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
- }
- $where2 = [
- 'group_id' => $group_id,
- 'is_pin' => 1,
- ];
- // 取消老的pin
- $objGroupMsg->updateObject(['is_pin' => 0], $where2);
- $objGroupMsg->updateObject(['is_pin' => 1], $args);
- $objUser = new TableHelper('user_info', 'dw_chat');
- $user = $objUser->getRow(['user_id' => $row['from']]);
- $row = array_merge($row, arrayFilter($user, ['user_name', 'nick_name', 'cover_photo']));
- // 给群组发消息有pinMsg
- ThirdApi::pushGroupEvent($group_id, [
- 'type' => 'pin_msg',
- 'group_id' => $group_id,
- 'pinMsg' => $row,
- ]);
- return $row;
- }
- /**
- * 群聊 取消置顶消息
- * @author solu
- * @param $args
- */
- public function actionUnpinMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'hash' => ['string', 'desc' => '消息hash'],
- ];
- Param::checkParam2($rules, $args);
- $group_id = (int) $args['group_id'];
- $this->_checkGroupAdmin($group_id);
- $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
- $count = $objGroupMsg->getCount($args);
- if (!$count) {
- Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
- }
- // 取消老的pin
- $objGroupMsg->updateObject(['is_pin' => 0], $args);
- // 给群组发消息有人加入了
- ThirdApi::pushGroupEvent($group_id, [
- 'type' => 'unpin_msg',
- 'group_id' => $group_id,
- 'hash' => $args['hash'],
- ]);
- return $args['hash'];
- }
- /**
- * 获取朋友
- * @author benzhan
- */
- public function actionGetFriends($args) {
- Param::checkParam2([], $args);
- $user_id = User::getUserId();
- $is_group = 0;
- $where = compact('user_id', 'is_group');
- $keyWord = [
- '_field' => 'session_id',
- '_sort' => 'update_time_int DESC',
- '_limit' => 500, // 最多加载500个会话
- ];
- $objSession = new Session();
- $session_ids = $objSession->objTable->getCol($where, $keyWord);
- $friend_ids = array_map(function($session_id) use ($user_id) {
- return str_replace(['-', $user_id], ['', ''], $session_id);
- }, $session_ids);
- $objUserInfo = new TableHelper('user_info', 'dw_chat');
- $_field = 'user_id, user_name, nick_name, cover_photo';
- $users = $objUserInfo->getAll(['user_id' => $friend_ids], compact('_field'));
- return $users;
- }
- // 只过滤出真正的朋友
- private function _getFriendList($userId, $friend_id_list) {
- $friend_ids = explode(',', $friend_id_list);
- $map = [];
- foreach ($friend_ids as $friend_id) {
- $friend_id = (int) $friend_id;
- $sessionId = Session::getPersonSessionId($userId, $friend_id);
- $map[$sessionId] = $friend_id;
- }
- if (!$map) {
- return [];
- }
- $where = [
- 'user_id' => $userId,
- 'session_id' => array_keys($map),
- ];
- $_field = 'session_id';
- $objSession = new Session();
- $sessionIds = $objSession->objTable->getCol($where, compact('_field'));
- if (!$sessionIds) return [];
- $friend_ids = [];
- foreach ($sessionIds as $sessionId) {
- $friend_ids[] = $map[$sessionId];
- }
- return $friend_ids;
- }
- /**
- * 创建群组
- * @author benzhan
- */
- public function actionCreate($args) {
- $rules = [
- 'group_title' => ['string', 'desc' => '群名称'],
- 'cover_photo' => ['string', 'nullable' => true, 'desc' => '群logo'],
- 'user_id_list' => ['string', 'nullable' => true, 'desc' => '好友id列表用,隔开'],
- ];
- Param::checkParam2($rules, $args);
- $user_id_list = arrayPop($args, 'user_id_list');
- $creator = User::getUserId();
- $objGroupInfo = new GroupInfo();
- $num = $objGroupInfo->objTable->getCount(compact('creator'));
- if ($num >= 100) {
- Response::error(CODE_NO_PERMITION, "create group num >= {$num}");
- }
- $args['creator'] = User::getUserId();
- $args['server_id'] = $args['creator'];
- $memberNum = 0;
- $args['member_num'] = $memberNum;
- $args['create_time'] = $args['update_time'] = NOW;
- $objGroupInfo->objTable->addObject($args);
- $group_id = $objGroupInfo->objTable->getInsertId();
- // 设置group_name
- $objGroupInfo->objTable->updateObject(['group_name' => $group_id], ['group_id' => $group_id]);
- // 默认加群
- $this->actionJoin(compact('group_id'));
- $memberNum++;
- // 设置为管理员
- $objUserGroup = new UserGroup();
- $data = [
- 'is_admin' => 1,
- 'state' => UserGroup::STATE_IN_GROUP,
- ];
- $objUserGroup->setData($group_id, $creator, $data);
- $friend_ids = $this->_getFriendList($creator, $user_id_list);
- if ($friend_ids) {
- $objGroupInfo->appendToGroup($friend_ids, $group_id, $memberNum);
- }
- $objSession = new Session();
- $_field = 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute';
- $sesion = $objSession->objTable->getRow(['session_id' => $group_id], compact('_field'));
- $sesion['name'] = $args['group_title'];
- $sesion['cover_photo'] = $args['cover_photo'];
- return $sesion;
- }
- /**
- * 加入群组
- * @author solu
- */
- public function actionJoin($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群组id'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- try {
- (new GroupInfo())->joinGroup($userId, $args['group_id']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 离开群组
- * @author solu
- */
- public function actionLeave($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群组id'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- try {
- (new GroupInfo())->leaveGroup($userId, $args['group_id']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 封禁用户
- * @author benzhan
- */
- public function actionBlockUser($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群组id'],
- 'block_id' => ['int', 'desc' => '用户id'],
- ];
- Param::checkParam2($rules, $args);
- $group_id = (int) $args['group_id'];
- $user_id = (int) $args['block_id'];
- $this->_checkGroupAdmin($group_id);
- $objUserGroup = new UserGroup();
- $objUserGroup->setBlock($group_id, $user_id, 1);
- $eventData = [
- 'type' => 'block',
- 'group_id' => $group_id,
- 'from' => User::getUserId(),
- 'to' => $user_id,
- 'timestamp' => Session::getMS(),
- ];
- ThirdApi::pushGroupEvent($group_id, $eventData);
- }
- /**
- * 解禁用户
- * @author solu
- */
- public function actionUnblockUser($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群组id'],
- 'block_id' => ['int', 'desc' => '用户id'],
- ];
- Param::checkParam2($rules, $args);
- $group_id = (int) $args['group_id'];
- $user_id = (int) $args['block_id'];
- $this->_checkGroupAdmin($group_id);
- $objUserGroup = new UserGroup();
- $objUserGroup->setBlock($group_id, $user_id, 0);
- $eventData = [
- 'type' => 'unblock',
- 'group_id' => $group_id,
- 'from' => User::getUserId(),
- 'to' => $user_id,
- 'timestamp' => Session::getMS(),
- ];
- ThirdApi::pushGroupEvent($group_id, $eventData);
- }
- private function _checkGroupAdmin($group_id) {
- $owner = User::getUserId();
- $objUserGroup = new UserGroup();
- if (!$objUserGroup->isAdmin($group_id, $owner)) {
- Response::error(CODE_NO_PERMITION);
- }
- }
- /**
- * 更新群名称(只能一次
- * @author solu
- * @param $args
- */
- public function actionChangeName($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'name' => ['string', 'reg' => '/^[a-z0-9\.]+/i', 'desc' => '新群名'],
- ];
- Param::checkParam2($rules, $args);
- $objGroup = new GroupInfo();
- $userId = User::getUserId();
- $groupId = (int)$args['group_id'];
- if (is_numeric($args['name'])) {
- Response::error(CODE_PARAM_ERROR, 'name can not be pure numbers');
- }
- $groupInfo = $objGroup->objTable->getRow(['group_id' => $groupId]);
- if (!$groupInfo) {
- Response::error(CODE_PARAM_ERROR, 'group not exists');
- }
- // 已修改过群名称
- if ($groupInfo['group_id'] != $groupInfo['group_name']) {
- Response::error(CODE_PARAM_ERROR, 'only one change group name');
- }
- if ($objGroup->objTable->getRow(['group_name' => $args['name']])) {
- Response::error(CODE_PARAM_ERROR, 'group name exists');
- }
- $data = [
- 'group_name' => htmlentities($args['name']),
- ];
- try {
- $objGroup->setData($userId, $args['group_id'], $data);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- ThirdApi::pushGroupEvent($args['group_id'], [
- 'type' => 'update',
- 'from' => $userId,
- ]);
- }
- /**
- * 更新群公告
- * @author solu
- * @param $args
- */
- public function actionChangeNotice($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'notice' => ['string', 'desc' => '新公告'],
- ];
- Param::checkParam2($rules, $args);
- $objGroup = new GroupInfo();
- $userId = User::getUserId();
- $data = [
- 'group_notice' => htmlentities($args['notice']),
- ];
- try {
- $objGroup->setData($userId, $args['group_id'], $data);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- ThirdApi::pushGroupEvent($args['group_id'], [
- 'type' => 'update',
- 'from' => $userId,
- ]);
- }
- /**
- * 更新群图标
- * @author solu
- * @param $args
- */
- public function actionChangeCover($args) {
- $args = array_merge($args, $_FILES);
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'cover_photo' => ['array', 'desc' => '头像文件'],
- ];
- Param::checkParam2($rules, $args);
- $file = $args['cover_photo'];
- $cover_photo = '';
- try {
- $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type'], true);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- $objGroup = new GroupInfo();
- $userId = User::getUserId();
- $data = [
- 'cover_photo' => $cover_photo,
- ];
- try {
- $objGroup->setData($userId, $args['group_id'], $data);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- ThirdApi::pushGroupEvent($args['group_id'], [
- 'type' => 'update',
- 'from' => $userId,
- ]);
- }
- /**
- * 更新群标题
- * @author solu
- * @param $args
- */
- public function actionChangeTitle($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'title' => ['string', 'desc' => '新标题'],
- ];
- Param::checkParam2($rules, $args);
- $objGroup = new GroupInfo();
- $userId = User::getUserId();
- $data = [
- 'group_title' => htmlentities($args['title']),
- ];
- try {
- $objGroup->setData($userId, $args['group_id'], $data);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- ThirdApi::pushGroupEvent($args['group_id'], [
- 'type' => 'update',
- 'from' => $userId,
- ]);
- GroupInfo::setGroupNameById($args['group_id'], $data['group_title']);
- }
- /**
- * 批量邀请用户加入群
- * @author solu
- * @param $args
- * @return array
- */
- public function actionInvites($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
- ];
- Param::checkParam2($rules, $args);
- $groupId = (int)$args['group_id'];
- $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
- return intval($v) > 0;
- });
- $adminId = User::getUserId();
- if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
- Response::error(CODE_NO_PERMITION, 'no permission');
- }
- $count = count($userIds);
- $success = 0;
- $objGroupInfo = new GroupInfo();
- foreach ($userIds as $userId) {
- try {
- $objGroupInfo->joinGroup($userId, $groupId);
- $success += 1;
- } catch (Exception $e) {}
- }
- return compact('count', 'success');
- }
- /**
- * 批量移除群用户
- * @author solu
- * @param $args
- * @return array
- */
- public function actionRemoves($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
- ];
- Param::checkParam2($rules, $args);
- $groupId = (int)$args['group_id'];
- $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
- return intval($v) > 0;
- });
- $adminId = User::getUserId();
- if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
- Response::error(CODE_NO_PERMITION, 'no permission');
- }
- $count = count($userIds);
- $success = 0;
- $objGroupInfo = new GroupInfo();
- $group = $objGroupInfo->objTable->getRow(['group_id' => $groupId]);
- if (!$group) {
- Response::error(CODE_PARAM_ERROR, 'group not exists');
- }
- foreach ($userIds as $userId) {
- try {
- $objGroupInfo->leaveGroup($userId, $groupId, $group);
- $success += 1;
- } catch (Exception $e) {}
- }
- return compact('count', 'success');
- }
- /**
- * 撤销消息
- * @author solu
- * @param $args
- */
- public function actionRepealMsg($args) {
- $rules = [
- 'group_id' => ['string', 'desc' => '群id'],
- 'hash' => ['string', 'desc' => '消息hash'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- $objMsg = new GroupMsg();
- try {
- $objMsg->repeal($userId, $args['group_id'], $args['hash']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 添加管理员
- * @author solu
- * @param $args
- */
- public function actionAddAdmin($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'user_ids' => ['string', 'desc' => '用户id , 分割'],
- ];
- Param::checkParam2($rules, $args);
- $creator = User::getUserId();
- $userIds = array_map('intval', explode(',', $args['user_ids']));
- try {
- (new GroupInfo())->addAdmin($args['group_id'], $creator, $userIds);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 移除管理员
- * @author solu
- * @param $args
- */
- public function actionRemoveAdmin($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'admin_id' => ['int', 'desc' => '管理员id'],
- ];
- Param::checkParam2($rules, $args);
- $creator = User::getUserId();
- try {
- (new GroupInfo())->removeAdmin($args['group_id'], $creator, $args['admin_id']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 搜索群成员信息
- * @author solu
- * @param $args
- * @return array
- */
- public function actionMemberSearch($args) {
- $rules = [
- 'keyword' => ['string', 'desc' => '关键字'],
- 'group_id' => ['int', 'desc' => '群id'],
- ];
- Param::checkParam2($rules, $args);
- return (new GroupInfo())->memberSearch($args['group_id'], $args['keyword']);
- }
- /**
- * 转移群主
- * @author solu
- * @param $args
- */
- public function actionChangeCreator($args) {
- $rules = [
- 'group_id' => ['int', 'desc' => '群id'],
- 'new_creator' => ['int', 'desc' => '新群主id'],
- ];
- Param::checkParam2($rules, $args);
- $userId = User::getUserId();
- try {
- (new GroupInfo())->changeCreator($args['group_id'], $userId, $args['new_creator']);
- } catch (Exception $e) {
- Response::error($e->getCode(), $e->getMessage());
- }
- }
- /**
- * 热门群推荐
- * @author benzhan
- */
- public function actionHotList($args) {
- $rules = [];
- Param::checkParam2($rules, $args);
- $objGroup = new GroupInfo();
- $list = $objGroup->objTable->getAll(['is_auth' => 1], ['_limit' => 100]);
- $group_ids = array_column($list, 'group_id');
- $existList = [];
- $userId = User::getUserId();
- if ($userId) {
- $_field = 'group_id';
- $objUserGroup = new UserGroup();
- $where = ['group_id' => $group_ids, 'state' => 1, 'user_id' => $userId];
- $existList = $objUserGroup->objTable->getCol($where, ['_field' => $_field]);
- }
- foreach ($list as $i => $value) {
- $value['is_join'] = in_array($value['group_id'], $existList) ? 1 : 0;
- $list[$i] = $value;
- }
- return $list;
- }
- }
|