GroupController.php 29 KB

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