123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- <?php
- /**
- * 群组相关
- * @author solu
- */
- class GroupInfo extends Model {
- protected $tableName = 'group_info';
- protected $dbKey = 'dw_chat';
- const OFFICIAL_ID = 10000; // 官方群id
- /**
- * 加入群组
- * @author solu
- * @param $userId
- * @param $groupId
- * @param $group
- * @return bool
- * @throws Exception
- */
- public function joinGroup($userId, $groupId, $group = []) {
- $where = ['group_id' => $groupId];
- !$group && $group = $this->objTable->getRow($where);
- if (!$group) {
- throw new Exception('group not exists', CODE_PARAM_ERROR);
- }
- $objUserGroup = new TableHelper('user_group', 'dw_chat');
- if ($objUserGroup->getRow(['user_id' => $userId, 'group_id' => $groupId, 'state' => UserGroup::STATE_IN_GROUP])) {
- throw new Exception('already joined this group', CODE_PARAM_ERROR);
- }
- $objSession = new Session();
- $objUserGroup->autoCommit(false);
- $data = [
- 'user_id' => $userId,
- 'group_id' => $groupId,
- 'join_time' => NOW,
- 'state' => UserGroup::STATE_IN_GROUP,
- ];
- if (!$objUserGroup->replaceObject($data)) {
- throw new Exception('join fail', CODE_DB_ERROR);
- }
- $memberNum = $group['member_num'] + 1;
- $where['member_num'] = $group['member_num'];
- if (!$this->objTable->updateObject(['member_num' => $memberNum], $where)) {
- throw new Exception('update group info fail', CODE_DB_ERROR);
- }
- $sessData = [
- 'user_id' => $userId,
- 'session_id' => $groupId,
- 'is_group' => 1,
- ];
- if (!$objSession->objTable->replaceObject($sessData)) {
- throw new Exception('create session error', CODE_DB_ERROR);
- }
- $objUserGroup->commit();
- // 给自己发消息订阅新群组
- ThirdApi::pushPersonEvent($userId, [
- 'type' => 'join_group',
- 'group_id' => $groupId,
- ]);
- $user_info = User::getUserInfoById($userId);
- // 给群组发消息有人加入了
- ThirdApi::pushGroupEvent($groupId, [
- 'type' => 'join',
- 'group_id' => $groupId,
- 'user_info' => $user_info,
- ]);
- return true;
- }
- /**
- * 加入群组
- * @author solu
- * @param $userId
- * @param $groupId
- * @param $group
- * @return bool
- * @throws Exception
- */
- public function appendToGroup($userIds, $groupId) {
- $userGroupDatas = [];
- $sessDatas = [];
- foreach ($userIds as $userId) {
- $userGroupDatas[] = [
- 'user_id' => $userId,
- 'group_id' => $groupId,
- 'join_time' => NOW,
- 'state' => UserGroup::STATE_IN_GROUP,
- ];
- $sessDatas[] = [
- 'user_id' => $userId,
- 'session_id' => $groupId,
- 'is_group' => 1,
- ];
- }
- $objUserGroup = new UserGroup();
- $objUserGroup->objTable->replaceObjects2($userGroupDatas);
- $objSession = new Session();
- $objSession->objTable->replaceObjects2($sessDatas);
- $where = ['group_id' => $groupId];
- $newData = [
- 'member_num' => count($userIds),
- 'group_name' => $groupId,
- ];
- $this->objTable->updateObject($newData, $where);
- }
- /**
- * 离开群组
- * @author solu
- * @param $userId
- * @param $groupId
- * @param $group
- * @return bool
- * @throws Exception
- */
- public function leaveGroup($userId, $groupId, $group = []) {
- $groupId = (int) $groupId;
- $where = ['group_id' => $groupId];
- !$group && $group = $this->objTable->getRow($where);
- if (!$group) {
- throw new Exception('group not exists', CODE_PARAM_ERROR);
- }
- $objUserGroup = new TableHelper('user_group', 'dw_chat');
- if (!$objUserGroup->getRow(['user_id' => $userId, 'group_id' => $groupId, 'state' => UserGroup::STATE_IN_GROUP])) {
- throw new Exception('not in group', CODE_PARAM_ERROR);
- }
- $objSession = new Session();
- $objUserGroup->autoCommit(false);
- $ugWhere = [
- 'user_id' => $userId,
- 'group_id' => $groupId,
- ];
- if (!$objUserGroup->updateObject(['state' => UserGroup::STATE_LEAVE], $ugWhere)) {
- throw new Exception('leave fail', CODE_DB_ERROR);
- }
- $sql = "UPDATE group_info SET member_num = member_num - 1 WHERE group_id = {$groupId}";
- $this->objDb->update($sql);
- // $memberNum = $group['member_num'] - 1;
- // $where['member_num'] = $group['member_num'];
- // if (!$this->objTable->updateObject(['member_num' => $memberNum], $where)) {
- // throw new Exception('update group info fail', CODE_DB_ERROR);
- // }
- $sessWhere = [
- 'user_id' => $userId,
- 'session_id' => $groupId,
- ];
- if (!$objSession->objTable->delObject($sessWhere)) {
- throw new Exception('delete session error', CODE_DB_ERROR);
- }
- $objUserGroup->commit();
- // 给自己发消息订阅新群组
- ThirdApi::pushPersonEvent($userId, [
- 'type' => 'leave_group',
- 'group_id' => $groupId,
- ]);
- $user_info = User::getUserInfoById($userId);
- // 给群组发消息有人离开了
- ThirdApi::pushGroupEvent($groupId, [
- 'type' => 'leave',
- 'group_id' => $groupId,
- 'user_id' => $userId,
- 'user_info' => $user_info,
- ]);
- return true;
- }
- /**
- * 更新信息
- * @author solu
- * @param $userId
- * @param $groupId
- * @param array $data
- * @return bool|int
- * @throws Exception
- */
- public function setData($userId, $groupId, array $data) {
- if (!(new UserGroup())->isAdmin($groupId, $userId)) {
- throw new Exception('no permission', CODE_NO_PERMITION);
- }
- $data['update_time'] = NOW;
- return $this->objTable->updateObject($data, ['group_id' => $groupId]);
- }
- /**
- * 分享连接
- * @author solu
- * @param string $name
- * @return string
- */
- public static function genInviteUrl($name) {
- if (!$name) {
- return '';
- }
- return sprintf('%s/s/%s', URL_SELF, $name);
- }
- }
|