GroupController.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  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. $url = (new FileUrl())->getFileUrl($args['res']['tmp_name'], $args['res']['name'], $args['res']['type']);
  254. // 只有图片才需要用ips处理
  255. if ($msgType == FileUrl::TYPE_IMAGE) {
  256. $url = awsReplaceImg($url);
  257. }
  258. $msg = Utils::encodeRC4($url);
  259. $userId = User::getUserId();
  260. $objSession = new Session();
  261. $data = $objSession->sendGroupMsg($userId, $args['group_id'], $msgType, $msg);
  262. } catch (Exception $e) {
  263. Response::error($e->getCode(), $e->getMessage());
  264. }
  265. return $data;
  266. }
  267. /**
  268. * 群聊 置顶消息
  269. * @author solu
  270. * @param $args
  271. */
  272. public function actionPinMsg($args) {
  273. $rules = [
  274. 'group_id' => ['string', 'desc' => '群id'],
  275. 'hash' => ['string', 'desc' => '消息hash'],
  276. ];
  277. Param::checkParam2($rules, $args);
  278. $group_id = (int) $args['group_id'];
  279. $this->_checkGroupAdmin($group_id);
  280. $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
  281. $_field = '`from`, hash, msg, msg_type';
  282. $row = $objGroupMsg->getRow($args, compact('_field'));
  283. if (!$row) {
  284. Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
  285. }
  286. $where2 = [
  287. 'group_id' => $group_id,
  288. 'is_pin' => 1,
  289. ];
  290. // 取消老的pin
  291. $objGroupMsg->updateObject(['is_pin' => 0], $where2);
  292. $objGroupMsg->updateObject(['is_pin' => 1], $args);
  293. $objUser = new TableHelper('user_info', 'dw_chat');
  294. $user = $objUser->getRow(['user_id' => $row['from']]);
  295. $row = array_merge($row, arrayFilter($user, ['user_name', 'nick_name', 'cover_photo']));
  296. // 给群组发消息有pinMsg
  297. ThirdApi::pushGroupEvent($group_id, [
  298. 'type' => 'pin_msg',
  299. 'group_id' => $group_id,
  300. 'pinMsg' => $row,
  301. ]);
  302. return $row;
  303. }
  304. /**
  305. * 群聊 取消置顶消息
  306. * @author solu
  307. * @param $args
  308. */
  309. public function actionUnpinMsg($args) {
  310. $rules = [
  311. 'group_id' => ['string', 'desc' => '群id'],
  312. 'hash' => ['string', 'desc' => '消息hash'],
  313. ];
  314. Param::checkParam2($rules, $args);
  315. $group_id = (int) $args['group_id'];
  316. $this->_checkGroupAdmin($group_id);
  317. $objGroupMsg = new TableHelper('group_msg', 'dw_chat');
  318. $count = $objGroupMsg->getCount($args);
  319. if (!$count) {
  320. Response::error(CODE_NORMAL_ERROR, 'can not find the msg');
  321. }
  322. // 取消老的pin
  323. $objGroupMsg->updateObject(['is_pin' => 0], $args);
  324. // 给群组发消息有人加入了
  325. ThirdApi::pushGroupEvent($group_id, [
  326. 'type' => 'unpin_msg',
  327. 'group_id' => $group_id,
  328. 'hash' => $args['hash'],
  329. ]);
  330. return $args['hash'];
  331. }
  332. /**
  333. * 获取朋友
  334. * @author benzhan
  335. */
  336. public function actionGetFriends($args) {
  337. Param::checkParam2([], $args);
  338. $user_id = User::getUserId();
  339. $is_group = 0;
  340. $where = compact('user_id', 'is_group');
  341. $keyWord = [
  342. '_field' => 'session_id',
  343. '_sort' => 'update_time_int DESC',
  344. '_limit' => 500, // 最多加载500个会话
  345. ];
  346. $objSession = new Session();
  347. $session_ids = $objSession->objTable->getCol($where, $keyWord);
  348. $friend_ids = array_map(function($session_id) use ($user_id) {
  349. return str_replace(['-', $user_id], ['', ''], $session_id);
  350. }, $session_ids);
  351. $objUserInfo = new TableHelper('user_info', 'dw_chat');
  352. $_field = 'user_id, user_name, nick_name, cover_photo';
  353. $users = $objUserInfo->getAll(['user_id' => $friend_ids], compact('_field'));
  354. return $users;
  355. }
  356. // 只过滤出真正的朋友
  357. private function _getFriendList($userId, $friend_id_list) {
  358. $friend_ids = explode(',', $friend_id_list);
  359. $map = [];
  360. foreach ($friend_ids as $friend_id) {
  361. $friend_id = (int) $friend_id;
  362. $sessionId = Session::getPersonSessionId($userId, $friend_id);
  363. $map[$sessionId] = $friend_id;
  364. }
  365. if (!$map) {
  366. return [];
  367. }
  368. $where = [
  369. 'user_id' => $userId,
  370. 'session_id' => array_keys($map),
  371. ];
  372. $_field = 'session_id';
  373. $objSession = new Session();
  374. $sessionIds = $objSession->objTable->getCol($where, compact('_field'));
  375. if (!$sessionIds) return [];
  376. $friend_ids = [];
  377. foreach ($sessionIds as $sessionId) {
  378. $friend_ids[] = $map[$sessionId];
  379. }
  380. return $friend_ids;
  381. }
  382. /**
  383. * 创建群组
  384. * @author benzhan
  385. */
  386. public function actionCreate($args) {
  387. $rules = [
  388. 'group_title' => ['string', 'desc' => '群名称'],
  389. 'cover_photo' => ['string', 'nullable' => true, 'desc' => '群logo'],
  390. 'user_id_list' => ['string', 'nullable' => true, 'desc' => '好友id列表用,隔开'],
  391. ];
  392. Param::checkParam2($rules, $args);
  393. $user_id_list = arrayPop($args, 'user_id_list');
  394. $creator = User::getUserId();
  395. $objGroupInfo = new GroupInfo();
  396. $num = $objGroupInfo->objTable->getCount(compact('creator'));
  397. if ($num >= 100) {
  398. Response::error(CODE_NO_PERMITION, "create group num >= {$num}");
  399. }
  400. $args['creator'] = User::getUserId();
  401. $args['server_id'] = $args['creator'];
  402. $memberNum = 0;
  403. $args['member_num'] = $memberNum;
  404. $args['create_time'] = $args['update_time'] = NOW;
  405. $objGroupInfo->objTable->addObject($args);
  406. $group_id = $objGroupInfo->objTable->getInsertId();
  407. // 设置group_name
  408. $objGroupInfo->objTable->updateObject(['group_name' => $group_id], ['group_id' => $group_id]);
  409. // 默认加群
  410. $this->actionJoin(compact('group_id'));
  411. $memberNum++;
  412. // 设置为管理员
  413. $objUserGroup = new UserGroup();
  414. $data = [
  415. 'is_admin' => 1,
  416. 'state' => UserGroup::STATE_IN_GROUP,
  417. ];
  418. $objUserGroup->setData($group_id, $creator, $data);
  419. $friend_ids = $this->_getFriendList($creator, $user_id_list);
  420. if ($friend_ids) {
  421. $objGroupInfo->appendToGroup($friend_ids, $group_id, $memberNum);
  422. }
  423. $objSession = new Session();
  424. $_field = 'session_id, is_group, read_hash, is_pin, pin_time_int, is_mute';
  425. $sesion = $objSession->objTable->getRow(['session_id' => $group_id], compact('_field'));
  426. $sesion['name'] = $args['group_title'];
  427. $sesion['cover_photo'] = $args['cover_photo'];
  428. return $sesion;
  429. }
  430. /**
  431. * 加入群组
  432. * @author solu
  433. */
  434. public function actionJoin($args) {
  435. $rules = [
  436. 'group_id' => ['int', 'desc' => '群组id'],
  437. ];
  438. Param::checkParam2($rules, $args);
  439. $userId = User::getUserId();
  440. try {
  441. (new GroupInfo())->joinGroup($userId, $args['group_id']);
  442. } catch (Exception $e) {
  443. Response::error($e->getCode(), $e->getMessage());
  444. }
  445. }
  446. /**
  447. * 离开群组
  448. * @author solu
  449. */
  450. public function actionLeave($args) {
  451. $rules = [
  452. 'group_id' => ['int', 'desc' => '群组id'],
  453. ];
  454. Param::checkParam2($rules, $args);
  455. $userId = User::getUserId();
  456. try {
  457. (new GroupInfo())->leaveGroup($userId, $args['group_id']);
  458. } catch (Exception $e) {
  459. Response::error($e->getCode(), $e->getMessage());
  460. }
  461. }
  462. /**
  463. * 封禁用户
  464. * @author benzhan
  465. */
  466. public function actionBlockUser($args) {
  467. $rules = [
  468. 'group_id' => ['int', 'desc' => '群组id'],
  469. 'block_id' => ['int', 'desc' => '用户id'],
  470. ];
  471. Param::checkParam2($rules, $args);
  472. $group_id = (int) $args['group_id'];
  473. $user_id = (int) $args['block_id'];
  474. $this->_checkGroupAdmin($group_id);
  475. $objUserGroup = new UserGroup();
  476. $objUserGroup->setBlock($group_id, $user_id, 1);
  477. $eventData = [
  478. 'type' => 'block',
  479. 'group_id' => $group_id,
  480. 'from' => User::getUserId(),
  481. 'to' => $user_id,
  482. 'timestamp' => Session::getMS(),
  483. ];
  484. ThirdApi::pushGroupEvent($group_id, $eventData);
  485. }
  486. /**
  487. * 解禁用户
  488. * @author solu
  489. */
  490. public function actionUnblockUser($args) {
  491. $rules = [
  492. 'group_id' => ['int', 'desc' => '群组id'],
  493. 'block_id' => ['int', 'desc' => '用户id'],
  494. ];
  495. Param::checkParam2($rules, $args);
  496. $group_id = (int) $args['group_id'];
  497. $user_id = (int) $args['block_id'];
  498. $this->_checkGroupAdmin($group_id);
  499. $objUserGroup = new UserGroup();
  500. $objUserGroup->setBlock($group_id, $user_id, 0);
  501. $eventData = [
  502. 'type' => 'unblock',
  503. 'group_id' => $group_id,
  504. 'from' => User::getUserId(),
  505. 'to' => $user_id,
  506. 'timestamp' => Session::getMS(),
  507. ];
  508. ThirdApi::pushGroupEvent($group_id, $eventData);
  509. }
  510. private function _checkGroupAdmin($group_id) {
  511. $owner = User::getUserId();
  512. $objUserGroup = new UserGroup();
  513. if (!$objUserGroup->isAdmin($group_id, $owner)) {
  514. Response::error(CODE_NO_PERMITION);
  515. }
  516. }
  517. /**
  518. * 更新群名称(只能一次
  519. * @author solu
  520. * @param $args
  521. */
  522. public function actionChangeName($args) {
  523. $rules = [
  524. 'group_id' => ['int', 'desc' => '群id'],
  525. 'name' => ['string', 'reg' => '/^[a-z0-9\.]+/i', 'desc' => '新群名'],
  526. ];
  527. Param::checkParam2($rules, $args);
  528. $objGroup = new GroupInfo();
  529. $userId = User::getUserId();
  530. $groupId = (int)$args['group_id'];
  531. if (is_numeric($args['name'])) {
  532. Response::error(CODE_PARAM_ERROR, 'name can not be pure numbers');
  533. }
  534. $groupInfo = $objGroup->objTable->getRow(['group_id' => $groupId]);
  535. if (!$groupInfo) {
  536. Response::error(CODE_PARAM_ERROR, 'group not exists');
  537. }
  538. // 已修改过群名称
  539. if ($groupInfo['group_id'] != $groupInfo['group_name']) {
  540. Response::error(CODE_PARAM_ERROR, 'only one change group name');
  541. }
  542. if ($objGroup->objTable->getRow(['group_name' => $args['name']])) {
  543. Response::error(CODE_PARAM_ERROR, 'group name exists');
  544. }
  545. $data = [
  546. 'group_name' => htmlentities($args['name']),
  547. ];
  548. try {
  549. $objGroup->setData($userId, $args['group_id'], $data);
  550. } catch (Exception $e) {
  551. Response::error($e->getCode(), $e->getMessage());
  552. }
  553. ThirdApi::pushGroupEvent($args['group_id'], [
  554. 'type' => 'update',
  555. 'from' => $userId,
  556. ]);
  557. }
  558. /**
  559. * 更新群公告
  560. * @author solu
  561. * @param $args
  562. */
  563. public function actionChangeNotice($args) {
  564. $rules = [
  565. 'group_id' => ['int', 'desc' => '群id'],
  566. 'notice' => ['string', 'desc' => '新公告'],
  567. ];
  568. Param::checkParam2($rules, $args);
  569. $objGroup = new GroupInfo();
  570. $userId = User::getUserId();
  571. $data = [
  572. 'group_notice' => htmlentities($args['notice']),
  573. ];
  574. try {
  575. $objGroup->setData($userId, $args['group_id'], $data);
  576. } catch (Exception $e) {
  577. Response::error($e->getCode(), $e->getMessage());
  578. }
  579. ThirdApi::pushGroupEvent($args['group_id'], [
  580. 'type' => 'update',
  581. 'from' => $userId,
  582. ]);
  583. }
  584. /**
  585. * 更新群图标
  586. * @author solu
  587. * @param $args
  588. */
  589. public function actionChangeCover($args) {
  590. $args = array_merge($args, $_FILES);
  591. $rules = [
  592. 'group_id' => ['int', 'desc' => '群id'],
  593. 'cover_photo' => ['array', 'desc' => '头像文件'],
  594. ];
  595. Param::checkParam2($rules, $args);
  596. $file = $args['cover_photo'];
  597. $cover_photo = '';
  598. try {
  599. $cover_photo = (new FileUrl())->getFileUrl($file['tmp_name'], $file['name'], $file['type'], true);
  600. } catch (Exception $e) {
  601. Response::error($e->getCode(), $e->getMessage());
  602. }
  603. $objGroup = new GroupInfo();
  604. $userId = User::getUserId();
  605. $data = [
  606. 'cover_photo' => $cover_photo,
  607. ];
  608. try {
  609. $objGroup->setData($userId, $args['group_id'], $data);
  610. } catch (Exception $e) {
  611. Response::error($e->getCode(), $e->getMessage());
  612. }
  613. ThirdApi::pushGroupEvent($args['group_id'], [
  614. 'type' => 'update',
  615. 'from' => $userId,
  616. ]);
  617. }
  618. /**
  619. * 更新群标题
  620. * @author solu
  621. * @param $args
  622. */
  623. public function actionChangeTitle($args) {
  624. $rules = [
  625. 'group_id' => ['int', 'desc' => '群id'],
  626. 'title' => ['string', 'desc' => '新标题'],
  627. ];
  628. Param::checkParam2($rules, $args);
  629. $objGroup = new GroupInfo();
  630. $userId = User::getUserId();
  631. $data = [
  632. 'group_title' => htmlentities($args['title']),
  633. ];
  634. try {
  635. $objGroup->setData($userId, $args['group_id'], $data);
  636. } catch (Exception $e) {
  637. Response::error($e->getCode(), $e->getMessage());
  638. }
  639. ThirdApi::pushGroupEvent($args['group_id'], [
  640. 'type' => 'update',
  641. 'from' => $userId,
  642. ]);
  643. GroupInfo::setGroupNameById($args['group_id'], $data['group_title']);
  644. }
  645. /**
  646. * 批量邀请用户加入群
  647. * @author solu
  648. * @param $args
  649. * @return array
  650. */
  651. public function actionInvites($args) {
  652. $rules = [
  653. 'group_id' => ['int', 'desc' => '群id'],
  654. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  655. ];
  656. Param::checkParam2($rules, $args);
  657. $groupId = (int)$args['group_id'];
  658. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  659. return intval($v) > 0;
  660. });
  661. $adminId = User::getUserId();
  662. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  663. Response::error(CODE_NO_PERMITION, 'no permission');
  664. }
  665. $count = count($userIds);
  666. $success = 0;
  667. $objGroupInfo = new GroupInfo();
  668. foreach ($userIds as $userId) {
  669. try {
  670. $objGroupInfo->joinGroup($userId, $groupId);
  671. $success += 1;
  672. } catch (Exception $e) {}
  673. }
  674. return compact('count', 'success');
  675. }
  676. /**
  677. * 批量移除群用户
  678. * @author solu
  679. * @param $args
  680. * @return array
  681. */
  682. public function actionRemoves($args) {
  683. $rules = [
  684. 'group_id' => ['int', 'desc' => '群id'],
  685. 'user_ids' => ['string', 'desc' => '用户id 多人,分割'],
  686. ];
  687. Param::checkParam2($rules, $args);
  688. $groupId = (int)$args['group_id'];
  689. $userIds = array_filter(explode(',', $args['user_ids']), function ($v) {
  690. return intval($v) > 0;
  691. });
  692. $adminId = User::getUserId();
  693. if (!(new UserGroup())->isAdmin($groupId, $adminId)) {
  694. Response::error(CODE_NO_PERMITION, 'no permission');
  695. }
  696. $count = count($userIds);
  697. $success = 0;
  698. $objGroupInfo = new GroupInfo();
  699. $group = $objGroupInfo->objTable->getRow(['group_id' => $groupId]);
  700. if (!$group) {
  701. Response::error(CODE_PARAM_ERROR, 'group not exists');
  702. }
  703. foreach ($userIds as $userId) {
  704. try {
  705. $objGroupInfo->leaveGroup($userId, $groupId, $group);
  706. $success += 1;
  707. } catch (Exception $e) {}
  708. }
  709. return compact('count', 'success');
  710. }
  711. /**
  712. * 撤销消息
  713. * @author solu
  714. * @param $args
  715. */
  716. public function actionRepealMsg($args) {
  717. $rules = [
  718. 'group_id' => ['string', 'desc' => '群id'],
  719. 'hash' => ['string', 'desc' => '消息hash'],
  720. ];
  721. Param::checkParam2($rules, $args);
  722. $userId = User::getUserId();
  723. $objMsg = new GroupMsg();
  724. try {
  725. $objMsg->repeal($userId, $args['group_id'], $args['hash']);
  726. } catch (Exception $e) {
  727. Response::error($e->getCode(), $e->getMessage());
  728. }
  729. }
  730. /**
  731. * 添加管理员
  732. * @author solu
  733. * @param $args
  734. */
  735. public function actionAddAdmin($args) {
  736. $rules = [
  737. 'group_id' => ['int', 'desc' => '群id'],
  738. 'user_ids' => ['string', 'desc' => '用户id , 分割'],
  739. ];
  740. Param::checkParam2($rules, $args);
  741. $creator = User::getUserId();
  742. $userIds = array_map('intval', explode(',', $args['user_ids']));
  743. try {
  744. (new GroupInfo())->addAdmin($args['group_id'], $creator, $userIds);
  745. } catch (Exception $e) {
  746. Response::error($e->getCode(), $e->getMessage());
  747. }
  748. }
  749. /**
  750. * 移除管理员
  751. * @author solu
  752. * @param $args
  753. */
  754. public function actionRemoveAdmin($args) {
  755. $rules = [
  756. 'group_id' => ['int', 'desc' => '群id'],
  757. 'admin_id' => ['int', 'desc' => '管理员id'],
  758. ];
  759. Param::checkParam2($rules, $args);
  760. $creator = User::getUserId();
  761. try {
  762. (new GroupInfo())->removeAdmin($args['group_id'], $creator, $args['admin_id']);
  763. } catch (Exception $e) {
  764. Response::error($e->getCode(), $e->getMessage());
  765. }
  766. }
  767. /**
  768. * 搜索群成员信息
  769. * @author solu
  770. * @param $args
  771. * @return array
  772. */
  773. public function actionMemberSearch($args) {
  774. $rules = [
  775. 'keyword' => ['string', 'desc' => '关键字'],
  776. 'group_id' => ['int', 'desc' => '群id'],
  777. ];
  778. Param::checkParam2($rules, $args);
  779. return (new GroupInfo())->memberSearch($args['group_id'], $args['keyword']);
  780. }
  781. /**
  782. * 转移群主
  783. * @author solu
  784. * @param $args
  785. */
  786. public function actionChangeCreator($args) {
  787. $rules = [
  788. 'group_id' => ['int', 'desc' => '群id'],
  789. 'new_creator' => ['int', 'desc' => '新群主id'],
  790. ];
  791. Param::checkParam2($rules, $args);
  792. $userId = User::getUserId();
  793. try {
  794. (new GroupInfo())->changeCreator($args['group_id'], $userId, $args['new_creator']);
  795. } catch (Exception $e) {
  796. Response::error($e->getCode(), $e->getMessage());
  797. }
  798. }
  799. private function array_random_assoc($arr, $num = 1) {
  800. $keys = array_keys($arr);
  801. shuffle($keys);
  802. $r = array();
  803. for ($i = 0; $i < $num; $i++) {
  804. $r[$keys[$i]] = $arr[$keys[$i]];
  805. }
  806. return $r;
  807. }
  808. /**
  809. * 热门群推荐
  810. * @author benzhan
  811. */
  812. public function actionHotList($args) {
  813. $rules = [];
  814. Param::checkParam2($rules, $args);
  815. $objGroup = new GroupInfo();
  816. $list = $objGroup->objTable->getAll(['is_auth' => 1, 'is_public' => 1], ['_limit' => 100]);
  817. $fillNum = 24 - count($list);
  818. if ($fillNum > 0) {
  819. // 找用户自发来补充
  820. $keyWord = ['_limit' => 200, '_sortKey' => 'member_num DESC'];
  821. $list2 = $objGroup->objTable->getAll(['is_auth' => 0, 'is_public' => 1], $keyWord);
  822. // 随机取10个
  823. $list2 = $this->array_random_assoc($list2, 24 - count($list));
  824. $list = array_merge($list, $list2);
  825. }
  826. $group_ids = array_column($list, 'group_id');
  827. $existList = [];
  828. $userId = User::getUserId();
  829. if ($userId) {
  830. $_field = 'group_id';
  831. $objUserGroup = new UserGroup();
  832. $where = ['group_id' => $group_ids, 'state' => 1, 'user_id' => $userId];
  833. $existList = $objUserGroup->objTable->getCol($where, ['_field' => $_field]);
  834. }
  835. foreach ($list as $i => $value) {
  836. $value['is_join'] = in_array($value['group_id'], $existList) ? 1 : 0;
  837. $list[$i] = $value;
  838. }
  839. return $list;
  840. }
  841. }