GroupController.php 28 KB

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