GroupController.php 27 KB

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