GroupController.php 30 KB

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