GroupController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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. $this->_checkGroupAdmin($group_id);
  430. $objUserGroup = new UserGroup();
  431. $objUserGroup->setBlock($group_id, $user_id, 1);
  432. $eventData = [
  433. 'type' => 'block',
  434. 'group_id' => $group_id,
  435. 'from' => User::getUserId(),
  436. 'to' => $user_id,
  437. 'timestamp' => Session::getMS(),
  438. ];
  439. ThirdApi::pushGroupEvent($group_id, $eventData);
  440. }
  441. /**
  442. * 解禁用户
  443. * @author solu
  444. */
  445. public function actionUnblockUser($args) {
  446. $rules = [
  447. 'group_id' => ['int', 'desc' => '群组id'],
  448. 'block_id' => ['int', 'desc' => '用户id'],
  449. ];
  450. Param::checkParam2($rules, $args);
  451. $group_id = (int) $args['group_id'];
  452. $user_id = (int) $args['block_id'];
  453. $this->_checkGroupAdmin($group_id);
  454. $objUserGroup = new UserGroup();
  455. $objUserGroup->setBlock($group_id, $user_id, 0);
  456. $eventData = [
  457. 'type' => 'unblock',
  458. 'group_id' => $group_id,
  459. 'from' => User::getUserId(),
  460. 'to' => $user_id,
  461. 'timestamp' => Session::getMS(),
  462. ];
  463. ThirdApi::pushGroupEvent($group_id, $eventData);
  464. }
  465. private function _checkGroupAdmin($group_id) {
  466. $owner = User::getUserId();
  467. $objUserGroup = new UserGroup();
  468. if (!$objUserGroup->isAdmin($group_id, $owner)) {
  469. Response::error(CODE_NO_PERMITION);
  470. }
  471. }
  472. /**
  473. * 更新群名称(只能一次
  474. * @author solu
  475. * @param $args
  476. */
  477. public function actionChangeName($args) {
  478. $rules = [
  479. 'group_id' => ['int', 'desc' => '群id'],
  480. 'name' => ['string', 'reg' => '/^[a-z0-9\.]+/i', 'desc' => '新群名'],
  481. ];
  482. Param::checkParam2($rules, $args);
  483. $objGroup = new GroupInfo();
  484. $userId = User::getUserId();
  485. $groupId = (int)$args['group_id'];
  486. if (is_numeric($args['name'])) {
  487. Response::error(CODE_PARAM_ERROR, 'name can not be pure numbers');
  488. }
  489. $groupInfo = $objGroup->objTable->getRow(['group_id' => $groupId]);
  490. if (!$groupInfo) {
  491. Response::error(CODE_PARAM_ERROR, 'group not exists');
  492. }
  493. // 已修改过群名称
  494. if ($groupInfo['group_id'] != $groupInfo['group_name']) {
  495. Response::error(CODE_PARAM_ERROR, 'only one change group name');
  496. }
  497. if ($objGroup->objTable->getRow(['group_name' => $args['name']])) {
  498. Response::error(CODE_PARAM_ERROR, 'group name exists');
  499. }
  500. $data = [
  501. 'group_name' => htmlentities($args['name']),
  502. ];
  503. try {
  504. $objGroup->setData($userId, $args['group_id'], $data);
  505. } catch (Exception $e) {
  506. Response::error($e->getCode(), $e->getMessage());
  507. }
  508. ThirdApi::pushGroupEvent($args['group_id'], [
  509. 'type' => 'update',
  510. 'from' => $userId,
  511. ]);
  512. }
  513. /**
  514. * 更新群公告
  515. * @author solu
  516. * @param $args
  517. */
  518. public function actionChangeNotice($args) {
  519. $rules = [
  520. 'group_id' => ['int', 'desc' => '群id'],
  521. 'notice' => ['string', 'desc' => '新公告'],
  522. ];
  523. Param::checkParam2($rules, $args);
  524. $objGroup = new GroupInfo();
  525. $userId = User::getUserId();
  526. $data = [
  527. 'group_notice' => htmlentities($args['notice']),
  528. ];
  529. try {
  530. $objGroup->setData($userId, $args['group_id'], $data);
  531. } catch (Exception $e) {
  532. Response::error($e->getCode(), $e->getMessage());
  533. }
  534. ThirdApi::pushGroupEvent($args['group_id'], [
  535. 'type' => 'update',
  536. 'from' => $userId,
  537. ]);
  538. }
  539. /**
  540. * 更新群图标
  541. * @author solu
  542. * @param $args
  543. */
  544. public function actionChangeCover($args) {
  545. $args = array_merge($args, $_FILES);
  546. $rules = [
  547. 'group_id' => ['int', 'desc' => '群id'],
  548. 'cover_photo' => ['array', 'desc' => '头像文件'],
  549. ];
  550. Param::checkParam2($rules, $args);
  551. $file = $args['cover_photo'];
  552. $cover_photo = '';
  553. try {
  554. $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type'], true);
  555. } catch (Exception $e) {
  556. Response::error($e->getCode(), $e->getMessage());
  557. }
  558. $objGroup = new GroupInfo();
  559. $userId = User::getUserId();
  560. $data = [
  561. 'cover_photo' => $cover_photo,
  562. ];
  563. try {
  564. $objGroup->setData($userId, $args['group_id'], $data);
  565. } catch (Exception $e) {
  566. Response::error($e->getCode(), $e->getMessage());
  567. }
  568. ThirdApi::pushGroupEvent($args['group_id'], [
  569. 'type' => 'update',
  570. 'from' => $userId,
  571. ]);
  572. }
  573. /**
  574. * 更新群标题
  575. * @author solu
  576. * @param $args
  577. */
  578. public function actionChangeTitle($args) {
  579. $rules = [
  580. 'group_id' => ['int', 'desc' => '群id'],
  581. 'title' => ['string', 'desc' => '新标题'],
  582. ];
  583. Param::checkParam2($rules, $args);
  584. $objGroup = new GroupInfo();
  585. $userId = User::getUserId();
  586. $data = [
  587. 'group_title' => htmlentities($args['title']),
  588. ];
  589. try {
  590. $objGroup->setData($userId, $args['group_id'], $data);
  591. } catch (Exception $e) {
  592. Response::error($e->getCode(), $e->getMessage());
  593. }
  594. ThirdApi::pushGroupEvent($args['group_id'], [
  595. 'type' => 'update',
  596. 'from' => $userId,
  597. ]);
  598. GroupInfo::setGroupNameById($args['group_id'], $data['group_title']);
  599. }
  600. /**
  601. * 批量邀请用户加入群
  602. * @author solu
  603. * @param $args
  604. * @return array
  605. */
  606. public function actionInvites($args) {
  607. $rules = [
  608. 'group_id' => ['int', 'desc' => '群id'],
  609. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  610. ];
  611. Param::checkParam2($rules, $args);
  612. $groupId = (int)$args['group_id'];
  613. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  614. return intval($v) > 0;
  615. });
  616. $adminId = User::getUserId();
  617. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  618. Response::error(CODE_NO_PERMITION, 'no permission');
  619. }
  620. $count = count($userIds);
  621. $success = 0;
  622. $objGroupInfo = new GroupInfo();
  623. foreach ($userIds as $userId) {
  624. try {
  625. $objGroupInfo->joinGroup($userId, $groupId);
  626. $success += 1;
  627. } catch (Exception $e) {}
  628. }
  629. return compact('count', 'success');
  630. }
  631. /**
  632. * 批量移除群用户
  633. * @author solu
  634. * @param $args
  635. * @return array
  636. */
  637. public function actionRemoves($args) {
  638. $rules = [
  639. 'group_id' => ['int', 'desc' => '群id'],
  640. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  641. ];
  642. Param::checkParam2($rules, $args);
  643. $groupId = (int)$args['group_id'];
  644. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  645. return intval($v) > 0;
  646. });
  647. $adminId = User::getUserId();
  648. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  649. Response::error(CODE_NO_PERMITION, 'no permission');
  650. }
  651. $count = count($userIds);
  652. $success = 0;
  653. $objGroupInfo = new GroupInfo();
  654. $group = $objGroupInfo->objTable->getRow(['group_id' => $groupId]);
  655. if (!$group) {
  656. Response::error(CODE_PARAM_ERROR, 'group not exists');
  657. }
  658. foreach ($userIds as $userId) {
  659. try {
  660. $objGroupInfo->leaveGroup($userId, $groupId, $group);
  661. $success += 1;
  662. } catch (Exception $e) {}
  663. }
  664. return compact('count', 'success');
  665. }
  666. /**
  667. * 撤销消息
  668. * @author solu
  669. * @param $args
  670. */
  671. public function actionRepealMsg($args) {
  672. $rules = [
  673. 'group_id' => ['string', 'desc' => '群id'],
  674. 'hash' => ['string', 'desc' => '消息hash'],
  675. ];
  676. Param::checkParam2($rules, $args);
  677. $userId = User::getUserId();
  678. $objMsg = new GroupMsg();
  679. try {
  680. $objMsg->repeal($userId, $args['group_id'], $args['hash']);
  681. } catch (Exception $e) {
  682. Response::error($e->getCode(), $e->getMessage());
  683. }
  684. }
  685. /**
  686. * 添加管理员
  687. * @author solu
  688. * @param $args
  689. */
  690. public function actionAddAdmin($args) {
  691. $rules = [
  692. 'group_id' => ['int', 'desc' => '群id'],
  693. 'user_ids' => ['string', 'desc' => '用户id , 分割'],
  694. ];
  695. Param::checkParam2($rules, $args);
  696. $creator = User::getUserId();
  697. $userIds = array_map('intval', explode(',', $args['user_ids']));
  698. try {
  699. (new GroupInfo())->addAdmin($args['group_id'], $creator, $userIds);
  700. } catch (Exception $e) {
  701. Response::error($e->getCode(), $e->getMessage());
  702. }
  703. }
  704. /**
  705. * 移除管理员
  706. * @author solu
  707. * @param $args
  708. */
  709. public function actionRemoveAdmin($args) {
  710. $rules = [
  711. 'group_id' => ['int', 'desc' => '群id'],
  712. 'admin_id' => ['int', 'desc' => '管理员id'],
  713. ];
  714. Param::checkParam2($rules, $args);
  715. $creator = User::getUserId();
  716. try {
  717. (new GroupInfo())->removeAdmin($args['group_id'], $creator, $args['admin_id']);
  718. } catch (Exception $e) {
  719. Response::error($e->getCode(), $e->getMessage());
  720. }
  721. }
  722. /**
  723. * 搜索群成员信息
  724. * @author solu
  725. * @param $args
  726. * @return array
  727. */
  728. public function actionMemberSearch($args) {
  729. $rules = [
  730. 'keyword' => ['string', 'desc' => '关键字'],
  731. 'group_id' => ['int', 'desc' => '群id'],
  732. ];
  733. Param::checkParam2($rules, $args);
  734. return (new GroupInfo())->memberSearch($args['group_id'], $args['keyword']);
  735. }
  736. /**
  737. * 转移群主
  738. * @author solu
  739. * @param $args
  740. */
  741. public function actionChangeCreator($args) {
  742. $rules = [
  743. 'group_id' => ['int', 'desc' => '群id'],
  744. 'new_creator' => ['int', 'desc' => '新群主id'],
  745. ];
  746. Param::checkParam2($rules, $args);
  747. $userId = User::getUserId();
  748. try {
  749. (new GroupInfo())->changeCreator($args['group_id'], $userId, $args['new_creator']);
  750. } catch (Exception $e) {
  751. Response::error($e->getCode(), $e->getMessage());
  752. }
  753. }
  754. /**
  755. * 热门群推荐
  756. * @author benzhan
  757. */
  758. public function actionHotList($args) {
  759. $rules = [];
  760. Param::checkParam2($rules, $args);
  761. $objGroup = new GroupInfo();
  762. return $objGroup->getHotList();
  763. }
  764. }