GroupController.php 24 KB

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