GroupController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?php
  2. /**
  3. * 群相关Api
  4. * @author benzhan
  5. */
  6. class GroupController extends BaseController {
  7. protected $ajaxLoginActions = [
  8. 'sendMsg',
  9. 'sendImageMsg',
  10. 'pinMsg',
  11. 'unpinMsg',
  12. 'getFriends',
  13. 'create',
  14. 'join',
  15. 'leave',
  16. 'blockUser',
  17. 'unblockUser',
  18. 'invites',
  19. 'removes',
  20. 'changeName',
  21. 'changeTitle',
  22. 'changeNotice',
  23. 'changeCover',
  24. ];
  25. public function __construct() {
  26. parent::__construct(true);
  27. }
  28. /**
  29. * 群信息【不需要登录】
  30. * @author benzhan
  31. */
  32. public function actionInfo($args) {
  33. $rules = [
  34. 'group_id' => ['int', 'desc' => '群id'],
  35. ];
  36. Param::checkParam2($rules, $args);
  37. $objGroupInfo = new GroupInfo();
  38. $_field = 'group_id, group_name, group_title, group_notice, cover_photo, member_num, creator';
  39. $group = $objGroupInfo->objTable->getRow($args, compact('_field'));
  40. $group && $group['invite_url'] = GroupInfo::genInviteUrl($group['group_name']);
  41. $_field = 'user_id, is_admin, is_block';
  42. $objUserGroup = new UserGroup();
  43. $groupUsers = $objUserGroup->objTable->getAll(['group_id' => $args['group_id'], 'state' => 1], compact('_field'));
  44. $user_ids = [];
  45. $blockList = [];
  46. $adminList = [];
  47. foreach ($groupUsers as $_u) {
  48. $_uid = intval($_u['user_id']);
  49. $user_ids[] = $_uid;
  50. $_u['is_block'] && $blockList[] = $_uid;
  51. $_u['is_admin'] && $adminList[] = $_uid;
  52. }
  53. // 获取pin的消息
  54. $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
  55. $where2 = [
  56. 'group_id' => $args['group_id'],
  57. 'is_pin' => 1,
  58. '_field' => '`from`, hash, msg, msg_type'
  59. ];
  60. $pinMsg = $objGroupMsg->getRow($where2);
  61. $inList = in_array($pinMsg['from'], $user_ids);
  62. $user_ids[] = $pinMsg['from'];
  63. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  64. $_field = 'user_id, user_name, nick_name, cover_photo';
  65. $members = $objUserInfo->getAll(['user_id' => $user_ids], compact('_field'));
  66. $members = arrayFormatKey($members, 'user_id');
  67. // 置顶消息用户信息
  68. $pinMsg = array_merge($pinMsg, arrayFilter($members[$pinMsg['from']], ['user_name', 'nick_name', 'cover_photo']));
  69. if (!$inList) {
  70. unset($members[$pinMsg['from']]);
  71. }
  72. $members = array_values($members);
  73. $user_id = User::getUserId();
  74. $sessionInfo = [];
  75. if ($user_id) {
  76. $objSession = new Session();
  77. $sessionInfo = $objSession->objTable->getRow(['user_id' => $user_id, 'session_id' => $args['group_id']], ['_field' => 'is_pin, pin_time_int, is_mute']);
  78. }
  79. return compact('group', 'members', 'pinMsg', 'sessionInfo', 'blockList', 'adminList');
  80. }
  81. /**
  82. * 群聊 获取最新消息【不需要登录】
  83. * @author benzhan
  84. */
  85. public function actionNewMsg($args) {
  86. $rules = [
  87. 'group_id' => ['string', 'desc' => '群id'],
  88. 'client_hash' => ['string', 'nullable' => true, 'desc' => '客户端上的最新消息的hash'],
  89. ];
  90. Param::checkParam2($rules, $args);
  91. $load_type = 0;
  92. return $this->_msg($args['group_id'], $load_type, $args['client_hash']);
  93. }
  94. /**
  95. * 群聊 获取历史消息【不需要登录】
  96. * @author benzhan
  97. */
  98. public function actionHistoryMsg($args) {
  99. $rules = [
  100. 'group_id' => ['string', 'desc' => '群id'],
  101. 'client_hash' => ['string', 'desc' => '客户端上的最旧消息的hash'],
  102. ];
  103. Param::checkParam2($rules, $args);
  104. $load_type = -1;
  105. return $this->_msg($args['group_id'], $load_type, $args['client_hash']);
  106. }
  107. /**
  108. * 获取消息列表
  109. * @param $session_id string 会话id
  110. * @param $load_type int 加载方式:-1:历史消息, 0:最新消息
  111. * @param $client_hash string 客户端的hash
  112. *
  113. * @return array
  114. */
  115. private function _msg($session_id, $load_type, $client_hash) {
  116. $where = compact('session_id');
  117. $keyWord = [
  118. '_field' => 'is_group, read_hash',
  119. ];
  120. $objSession = new Session();
  121. $row = $objSession->objTable->getRow($where, $keyWord);
  122. if (!$row) {
  123. Response::error(CODE_NO_PERMITION, 'session is not found');
  124. } else if (!$row['is_group']) {
  125. Response::error(CODE_NO_PERMITION, 'session is not group');
  126. }
  127. return $objSession->getMsgList($session_id, $client_hash, $load_type, 1);
  128. }
  129. /**
  130. * 发送群聊消息
  131. * @author solu
  132. * @param $args
  133. * @return array
  134. */
  135. public function actionSendMsg($args) {
  136. $rules = [
  137. 'group_id' => ['string', 'desc' => '群id'],
  138. 'msg_type' => ['int', 'nullable' => true, 'default' => 0, 'desc' => '消息类型:0:文本,1:图片,2:视频'],
  139. 'msg' => ['string', 'desc' => '内容'],
  140. ];
  141. Param::checkParam2($rules, $args);
  142. $userId = User::getUserId();
  143. $objSession = new Session();
  144. $data = [];
  145. try {
  146. $data = $objSession->sendGroupMsg($userId, $args['group_id'], $args['msg_type'], $args['msg']);
  147. } catch (Exception $e) {
  148. Response::error($e->getCode(), $e->getMessage());
  149. }
  150. return $data;
  151. }
  152. public function actionSendImageMsg($args) {
  153. return $this->actionSendFile($args);
  154. }
  155. /**
  156. * 群聊 发送多媒体消息
  157. * @author solu
  158. * @param $args
  159. * @return array
  160. */
  161. public function actionSendFile($args) {
  162. $args = array_merge($args, $_FILES);
  163. $rules = [
  164. 'group_id' => ['string', 'desc' => '群id'],
  165. 'res' => ['array', 'desc' => '资源文件'],
  166. ];
  167. Param::checkParam2($rules, $args);
  168. $data = null;
  169. try {
  170. $msgType = FileUrl::getType($args['res']['type']);
  171. $maxSize = 1000 * 1000; // 默认最大1MB
  172. switch ($msgType) {
  173. case FileUrl::TYPE_IMAGE:
  174. $maxSize = 1000 * 1000; // 图片最大1MB
  175. break;
  176. case FileUrl::TYPE_VIDEO:
  177. $maxSize = 3000 * 1000; // 视频最大3MB
  178. break;
  179. case FileUrl::TYPE_AUDIO:
  180. $maxSize = 2000 * 1000; // 音频最大2MB
  181. break;
  182. default:
  183. Response::error(CODE_PARAM_ERROR, 'unknown type:' . $args['res']['type']);
  184. }
  185. $size = filesize($args['res']['tmp_name']);
  186. if ($size > $maxSize) {
  187. Response::error(CODE_PARAM_ERROR, "file is too big. size:{$size}");
  188. }
  189. $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  190. $msg = Utils::encodeRC4($url);
  191. $userId = User::getUserId();
  192. $objSession = new Session();
  193. $data = $objSession->sendGroupMsg($userId, $args['group_id'], $msgType, $msg);
  194. } catch (Exception $e) {
  195. Response::error($e->getCode(), $e->getMessage());
  196. }
  197. return $data;
  198. }
  199. /**
  200. * 群聊 置顶消息
  201. * @author solu
  202. * @param $args
  203. */
  204. public function actionPinMsg($args) {
  205. $rules = [
  206. 'group_id' => ['string', 'desc' => '群id'],
  207. 'hash' => ['string', 'desc' => '消息hash'],
  208. ];
  209. Param::checkParam2($rules, $args);
  210. $group_id = (int) $args['group_id'];
  211. $this->_checkGroupAdmin($group_id);
  212. $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
  213. $_field = '`from`, hash, msg, msg_type';
  214. $row = $objGroupMsg->getRow($args, compact('_field'));
  215. if (!$row) {
  216. Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
  217. }
  218. $where2 = [
  219. 'group_id' => $group_id,
  220. 'is_pin' => 1,
  221. ];
  222. // 取消老的pin
  223. $objGroupMsg->updateObject(['is_pin' => 0], $where2);
  224. $objGroupMsg->updateObject(['is_pin' => 1], $args);
  225. $objUser = new TableHelper('user_info', 'dw_chat');
  226. $user = $objUser->getRow(['user_id' => $row['from']]);
  227. $row = array_merge($row, arrayFilter($user, ['user_name', 'nick_name', 'cover_photo']));
  228. // 给群组发消息有pinMsg
  229. ThirdApi::pushGroupEvent($group_id, [
  230. 'type' => 'pin_msg',
  231. 'group_id' => $group_id,
  232. 'pinMsg' => $row,
  233. ]);
  234. return $row;
  235. }
  236. /**
  237. * 群聊 取消置顶消息
  238. * @author solu
  239. * @param $args
  240. */
  241. public function actionUnpinMsg($args) {
  242. $rules = [
  243. 'group_id' => ['string', 'desc' => '群id'],
  244. 'hash' => ['string', 'desc' => '消息hash'],
  245. ];
  246. Param::checkParam2($rules, $args);
  247. $group_id = (int) $args['group_id'];
  248. $this->_checkGroupAdmin($group_id);
  249. $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
  250. $count = $objGroupMsg->getCount($args);
  251. if (!$count) {
  252. Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
  253. }
  254. // 取消老的pin
  255. $objGroupMsg->updateObject(['is_pin' => 0], $args);
  256. // 给群组发消息有人加入了
  257. ThirdApi::pushGroupEvent($group_id, [
  258. 'type' => 'unpin_msg',
  259. 'group_id' => $group_id,
  260. 'hash' => $args['hash'],
  261. ]);
  262. return $args['hash'];
  263. }
  264. /**
  265. * 获取朋友
  266. * @author benzhan
  267. */
  268. public function actionGetFriends($args) {
  269. Param::checkParam2([], $args);
  270. $user_id = User::getUserId();
  271. $is_group = 0;
  272. $where = compact('user_id', 'is_group');
  273. $keyWord = [
  274. '_field' => 'session_id',
  275. '_sort' => 'update_time_int DESC',
  276. '_limit' => 500, // 最多加载500个会话
  277. ];
  278. $objSession = new Session();
  279. $session_ids = $objSession->objTable->getCol($where, $keyWord);
  280. $friend_ids = array_map(function($session_id) use ($user_id) {
  281. return str_replace(['-', $user_id], ['', ''], $session_id);
  282. }, $session_ids);
  283. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  284. $_field = 'user_id, user_name, nick_name, cover_photo';
  285. $users = $objUserInfo->getAll(['user_id' => $friend_ids], compact('_field'));
  286. return $users;
  287. }
  288. // 只过滤出真正的朋友
  289. private function _getFriendList($userId, $friend_id_list) {
  290. $friend_ids = explode(',', $friend_id_list);
  291. $map = [];
  292. foreach ($friend_ids as $friend_id) {
  293. $friend_id = (int) $friend_id;
  294. $sessionId = Session::getPersonSessionId($userId, $friend_id);
  295. $map[$sessionId] = $friend_id;
  296. }
  297. if (!$map) {
  298. return [];
  299. }
  300. $where = [
  301. 'user_id' => $userId,
  302. 'session_id' => array_keys($map),
  303. ];
  304. $_field = 'session_id';
  305. $objSession = new Session();
  306. $sessionIds = $objSession->objTable->getCol($where, compact('_field'));
  307. if (!$sessionIds) return [];
  308. $friend_ids = [];
  309. foreach ($sessionIds as $sessionId) {
  310. $friend_ids[] = $map[$sessionId];
  311. }
  312. return $friend_ids;
  313. }
  314. /**
  315. * 创建群组
  316. * @author benzhan
  317. */
  318. public function actionCreate($args) {
  319. $rules = [
  320. 'group_title' => ['string', 'desc' => '群名称'],
  321. 'cover_photo' => ['string', 'nullable' => true, 'desc' => '群logo'],
  322. 'user_id_list' => ['string', 'nullable' => true, 'desc' => '好友id列表用,隔开'],
  323. ];
  324. Param::checkParam2($rules, $args);
  325. $user_id_list = arrayPop($args, 'user_id_list');
  326. $creator = User::getUserId();
  327. $objGroupInfo = new GroupInfo();
  328. $num = $objGroupInfo->objTable->getCount(compact('creator'));
  329. if ($num >= 10) {
  330. Response::error(CODE_NO_PERMITION, "create group num >= {$num}");
  331. }
  332. $args['creator'] = User::getUserId();
  333. $args['member_num'] = 0;
  334. $args['create_time'] = $args['update_time'] = NOW;
  335. $objGroupInfo->objTable->addObject($args);
  336. $group_id = $objGroupInfo->objTable->getInsertId();
  337. // 默认加群
  338. $this->actionJoin(compact('group_id'));
  339. // 设置为管理员
  340. $objUserGroup = new UserGroup();
  341. $data = [
  342. 'is_admin' => 1,
  343. 'state' => UserGroup::STATE_IN_GROUP,
  344. ];
  345. $objUserGroup->setData($group_id, $creator, $data);
  346. $friend_ids = $this->_getFriendList($creator, $user_id_list);
  347. if ($friend_ids) {
  348. $objGroupInfo->appendToGroup($friend_ids, $group_id);
  349. }
  350. $objSession = new Session();
  351. $_field = 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute';
  352. $sesion = $objSession->objTable->getRow(['session_id' => $group_id], compact('_field'));
  353. $sesion['name'] = $args['group_title'];
  354. $sesion['cover_photo'] = $args['cover_photo'];
  355. return $sesion;
  356. }
  357. /**
  358. * 加入群组
  359. * @author solu
  360. */
  361. public function actionJoin($args) {
  362. $rules = [
  363. 'group_id' => ['int', 'desc' => '群组id'],
  364. ];
  365. Param::checkParam2($rules, $args);
  366. $userId = User::getUserId();
  367. try {
  368. (new GroupInfo())->joinGroup($userId, $args['group_id']);
  369. } catch (Exception $e) {
  370. Response::error($e->getCode(), $e->getMessage());
  371. }
  372. }
  373. /**
  374. * 离开群组
  375. * @author solu
  376. */
  377. public function actionLeave($args) {
  378. $rules = [
  379. 'group_id' => ['int', 'desc' => '群组id'],
  380. ];
  381. Param::checkParam2($rules, $args);
  382. $userId = User::getUserId();
  383. $account = User::getUserName();
  384. try {
  385. (new GroupInfo())->leaveGroup($userId, $args['group_id'], $account);
  386. } catch (Exception $e) {
  387. Response::error($e->getCode(), $e->getMessage());
  388. }
  389. }
  390. /**
  391. * 封禁用户
  392. * @author benzhan
  393. */
  394. public function actionBlockUser($args) {
  395. $rules = [
  396. 'group_id' => ['int', 'desc' => '群组id'],
  397. 'block_id' => ['int', 'desc' => '用户id'],
  398. ];
  399. Param::checkParam2($rules, $args);
  400. $group_id = (int) $args['group_id'];
  401. $user_id = (int) $args['block_id'];
  402. $this->_checkGroupAdmin($group_id);
  403. $objUserGroup = new UserGroup();
  404. $objUserGroup->setBlock($group_id, $user_id, 1);
  405. $eventData = [
  406. 'type' => 'block',
  407. 'group_id' => $group_id,
  408. 'from' => User::getUserId(),
  409. 'to' => $user_id,
  410. 'timestamp' => Session::getMS(),
  411. ];
  412. ThirdApi::pushGroupEvent($group_id, $eventData);
  413. }
  414. /**
  415. * 解禁用户
  416. * @author solu
  417. */
  418. public function actionUnblockUser($args) {
  419. $rules = [
  420. 'group_id' => ['int', 'desc' => '群组id'],
  421. 'block_id' => ['int', 'desc' => '用户id'],
  422. ];
  423. Param::checkParam2($rules, $args);
  424. $group_id = (int) $args['group_id'];
  425. $user_id = (int) $args['block_id'];
  426. $this->_checkGroupAdmin($group_id);
  427. $objUserGroup = new UserGroup();
  428. $objUserGroup->setBlock($group_id, $user_id, 0);
  429. $eventData = [
  430. 'type' => 'unblock',
  431. 'group_id' => $group_id,
  432. 'from' => User::getUserId(),
  433. 'to' => $user_id,
  434. 'timestamp' => Session::getMS(),
  435. ];
  436. ThirdApi::pushGroupEvent($group_id, $eventData);
  437. }
  438. private function _checkGroupAdmin($group_id) {
  439. $owner = User::getUserId();
  440. $objUserGroup = new UserGroup();
  441. if (!$objUserGroup->isAdmin($group_id, $owner)) {
  442. Response::error(CODE_NO_PERMITION);
  443. }
  444. }
  445. /**
  446. * 更新群名称(只能一次
  447. * @author solu
  448. * @param $args
  449. */
  450. public function actionChangeName($args) {
  451. $rules = [
  452. 'group_id' => ['int', 'desc' => '群id'],
  453. 'name' => ['string', 'reg' => '/^[a-z0-9\.]+/i', 'desc' => '新群名'],
  454. ];
  455. Param::checkParam2($rules, $args);
  456. $objGroup = new GroupInfo();
  457. $userId = User::getUserId();
  458. $groupId = (int)$args['group_id'];
  459. if (is_numeric($args['name'])) {
  460. Response::error(CODE_PARAM_ERROR, 'name can not be pure numbers');
  461. }
  462. $groupInfo = $objGroup->objTable->getRow(['group_id' => $groupId]);
  463. if (!$groupInfo) {
  464. Response::error(CODE_PARAM_ERROR, 'group not exists');
  465. }
  466. // 已修改过群名称
  467. if ($groupInfo['group_id'] != $groupInfo['group_name']) {
  468. Response::error(CODE_PARAM_ERROR, 'only one change group name');
  469. }
  470. if ($objGroup->objTable->getRow(['group_name' => $args['name']])) {
  471. Response::error(CODE_PARAM_ERROR, 'group name exists');
  472. }
  473. $data = [
  474. 'group_name' => htmlentities($args['name']),
  475. ];
  476. try {
  477. $objGroup->setData($userId, $args['group_id'], $data);
  478. } catch (Exception $e) {
  479. Response::error($e->getCode(), $e->getMessage());
  480. }
  481. ThirdApi::pushGroupEvent($args['group_id'], [
  482. 'type' => 'update',
  483. 'from' => $userId,
  484. ]);
  485. }
  486. /**
  487. * 更新群公告
  488. * @author solu
  489. * @param $args
  490. */
  491. public function actionChangeNotice($args) {
  492. $rules = [
  493. 'group_id' => ['int', 'desc' => '群id'],
  494. 'notice' => ['string', 'desc' => '新公告'],
  495. ];
  496. Param::checkParam2($rules, $args);
  497. $objGroup = new GroupInfo();
  498. $userId = User::getUserId();
  499. $data = [
  500. 'group_notice' => htmlentities($args['notice']),
  501. ];
  502. try {
  503. $objGroup->setData($userId, $args['group_id'], $data);
  504. } catch (Exception $e) {
  505. Response::error($e->getCode(), $e->getMessage());
  506. }
  507. ThirdApi::pushGroupEvent($args['group_id'], [
  508. 'type' => 'update',
  509. 'from' => $userId,
  510. ]);
  511. }
  512. /**
  513. * 更新群图标
  514. * @author solu
  515. * @param $args
  516. */
  517. public function actionChangeCover($args) {
  518. $args = array_merge($args, $_FILES);
  519. $rules = [
  520. 'group_id' => ['int', 'desc' => '群id'],
  521. 'cover_photo' => ['array', 'desc' => '头像文件'],
  522. ];
  523. Param::checkParam2($rules, $args);
  524. $file = $args['cover_photo'];
  525. $cover_photo = '';
  526. try {
  527. $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type']);
  528. } catch (Exception $e) {
  529. Response::error($e->getCode(), $e->getMessage());
  530. }
  531. $objGroup = new GroupInfo();
  532. $userId = User::getUserId();
  533. $data = [
  534. 'cover_photo' => $cover_photo,
  535. ];
  536. try {
  537. $objGroup->setData($userId, $args['group_id'], $data);
  538. } catch (Exception $e) {
  539. Response::error($e->getCode(), $e->getMessage());
  540. }
  541. ThirdApi::pushGroupEvent($args['group_id'], [
  542. 'type' => 'update',
  543. 'from' => $userId,
  544. ]);
  545. }
  546. /**
  547. * 更新群标题
  548. * @author solu
  549. * @param $args
  550. */
  551. public function actionChangeTitle($args) {
  552. $rules = [
  553. 'group_id' => ['int', 'desc' => '群id'],
  554. 'title' => ['string', 'desc' => '新标题'],
  555. ];
  556. Param::checkParam2($rules, $args);
  557. $objGroup = new GroupInfo();
  558. $userId = User::getUserId();
  559. $data = [
  560. 'group_title' => htmlentities($args['title']),
  561. ];
  562. try {
  563. $objGroup->setData($userId, $args['group_id'], $data);
  564. } catch (Exception $e) {
  565. Response::error($e->getCode(), $e->getMessage());
  566. }
  567. ThirdApi::pushGroupEvent($args['group_id'], [
  568. 'type' => 'update',
  569. 'from' => $userId,
  570. ]);
  571. }
  572. /**
  573. * 批量邀请用户加入群
  574. * @author solu
  575. * @param $args
  576. * @return array
  577. */
  578. public function actionInvites($args) {
  579. $rules = [
  580. 'group_id' => ['int', 'desc' => '群id'],
  581. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  582. ];
  583. Param::checkParam2($rules, $args);
  584. $groupId = (int)$args['group_id'];
  585. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  586. return intval($v) > 0;
  587. });
  588. $adminId = User::getUserId();
  589. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  590. Response::error(CODE_NO_PERMITION, 'no permission');
  591. }
  592. $count = count($userIds);
  593. $success = 0;
  594. $objGroupInfo = new GroupInfo();
  595. $group = $objGroupInfo->objTable->getRow(['group_id' => $groupId]);
  596. if (!$group) {
  597. Response::error(CODE_PARAM_ERROR, 'group not exists');
  598. }
  599. foreach ($userIds as $userId) {
  600. try {
  601. $objGroupInfo->joinGroup($userId, $groupId, $group);
  602. $success += 1;
  603. } catch (Exception $e) {}
  604. }
  605. return compact('count', 'success');
  606. }
  607. /**
  608. * 批量移除群用户
  609. * @author solu
  610. * @param $args
  611. * @return array
  612. */
  613. public function actionRemoves($args) {
  614. $rules = [
  615. 'group_id' => ['int', 'desc' => '群id'],
  616. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  617. ];
  618. Param::checkParam2($rules, $args);
  619. $groupId = (int)$args['group_id'];
  620. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  621. return intval($v) > 0;
  622. });
  623. $adminId = User::getUserId();
  624. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  625. Response::error(CODE_NO_PERMITION, 'no permission');
  626. }
  627. $count = count($userIds);
  628. $success = 0;
  629. $objGroupInfo = new GroupInfo();
  630. $group = $objGroupInfo->objTable->getRow(['group_id' => $groupId]);
  631. if (!$group) {
  632. Response::error(CODE_PARAM_ERROR, 'group not exists');
  633. }
  634. foreach ($userIds as $userId) {
  635. try {
  636. $objGroupInfo->leaveGroup($userId, $groupId, $group);
  637. $success += 1;
  638. } catch (Exception $e) {}
  639. }
  640. return compact('count', 'success');
  641. }
  642. /**
  643. * 撤销消息
  644. * @author solu
  645. * @param $args
  646. */
  647. public function actionRepealMsg($args) {
  648. $rules = [
  649. 'group_id' => ['string', 'desc' => '群id'],
  650. 'hash' => ['string', 'desc' => '消息hash'],
  651. ];
  652. Param::checkParam2($rules, $args);
  653. $userId = User::getUserId();
  654. $objMsg = new GroupMsg();
  655. try {
  656. $objMsg->repeal($userId, $args['group_id'], $args['hash']);
  657. } catch (Exception $e) {
  658. Response::error($e->getCode(), $e->getMessage());
  659. }
  660. }
  661. //
  662. // public function actionTest($args) {
  663. // $this->tpl->display('test');
  664. // }
  665. }