重构群组相关数据表结构,及修改代码

main
gzydong 2021-03-25 17:32:36 +08:00
parent bbd582acf7
commit f60ab0f0c1
18 changed files with 526 additions and 498 deletions

View File

@ -20,7 +20,7 @@ use App\Middleware\JWTAuthMiddleware;
use App\Model\Article\ArticleAnnex;
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsFile;
use App\Model\Group\UsersGroup;
use App\Model\Group\Group;
use App\Service\UploadService;
use Hyperf\HttpServer\Contract\ResponseInterface;
@ -64,7 +64,7 @@ class DownloadController extends CController
return $this->response->fail('非法请求...');
}
} else {
if (!UsersGroup::isMember($recordsInfo->receive_id, $user_id)) {
if (!Group::isMember($recordsInfo->receive_id, $user_id)) {
return $this->response->fail('非法请求...');
}
}

View File

@ -20,9 +20,9 @@ use App\Middleware\JWTAuthMiddleware;
use Hyperf\Amqp\Producer;
use App\Model\UsersFriend;
use App\Model\UsersChatList;
use App\Model\Group\UsersGroup;
use App\Model\Group\UsersGroupMember;
use App\Model\Group\UsersGroupNotice;
use App\Model\Group\Group;
use App\Model\Group\GroupMember;
use App\Model\Group\GroupNotice;
use App\Amqp\Producer\ChatMessageProducer;
use App\Service\SocketRoomService;
use App\Service\GroupService;
@ -217,7 +217,7 @@ class GroupController extends CController
'avatar' => 'present|url'
]);
$result = UsersGroup::where('id', $params['group_id'])->where('user_id', $this->uid())->update([
$result = Group::where('id', $params['group_id'])->where('user_id', $this->uid())->update([
'group_name' => $params['group_name'],
'group_profile' => $params['group_profile'],
'avatar' => $params['avatar']
@ -282,12 +282,12 @@ class GroupController extends CController
$group_id = $this->request->input('group_id', 0);
$user_id = $this->uid();
$groupInfo = UsersGroup::leftJoin('users', 'users.id', '=', 'users_group.user_id')
->where('users_group.id', $group_id)->where('users_group.status', 0)->first([
'users_group.id', 'users_group.user_id',
'users_group.group_name',
'users_group.group_profile', 'users_group.avatar',
'users_group.created_at',
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.user_id')
->where('group.id', $group_id)->where('group.status', 0)->first([
'group.id', 'group.user_id',
'group.group_name',
'group.group_profile', 'group.avatar',
'group.created_at',
'users.nickname'
]);
@ -295,7 +295,7 @@ class GroupController extends CController
return $this->response->success([]);
}
$notice = UsersGroupNotice::where('group_id', $group_id)
$notice = GroupNotice::where('group_id', $group_id)
->where('is_delete', 0)
->orderBy('id', 'desc')
->first(['title', 'content']);
@ -308,7 +308,7 @@ class GroupController extends CController
'created_at' => $groupInfo->created_at,
'is_manager' => $groupInfo->user_id == $user_id,
'manager_nickname' => $groupInfo->nickname,
'visit_card' => UsersGroupMember::visitCard($user_id, $group_id),
'visit_card' => GroupMember::visitCard($user_id, $group_id),
'not_disturb' => UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->where('type', 2)->value('not_disturb') ?? 0,
'notice' => $notice ? $notice->toArray() : []
]);
@ -327,7 +327,7 @@ class GroupController extends CController
'visit_card' => 'required|max:20'
]);
$isTrue = UsersGroupMember::where('group_id', $params['group_id'])
$isTrue = GroupMember::where('group_id', $params['group_id'])
->where('user_id', $this->uid())
->where('status', 0)
->update(['visit_card' => $params['visit_card']]);
@ -347,7 +347,7 @@ class GroupController extends CController
$group_id = $this->request->input('group_id', 0);
$friends = UsersFriend::getUserFriends($this->uid());
if ($group_id > 0 && $friends) {
if ($ids = UsersGroupMember::getGroupMemberIds($group_id)) {
if ($ids = GroupMember::getGroupMemberIds($group_id)) {
foreach ($friends as $k => $item) {
if (in_array($item['id'], $ids)) unset($friends[$k]);
}
@ -382,19 +382,19 @@ class GroupController extends CController
$group_id = $this->request->input('group_id', 0);
// 判断用户是否是群成员
if (!UsersGroup::isMember($group_id, $user_id)) {
if (!Group::isMember($group_id, $user_id)) {
return $this->response->fail('非法操作...');
}
$members = UsersGroupMember::select([
'users_group_member.id', 'users_group_member.group_owner as is_manager', 'users_group_member.visit_card',
'users_group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender',
$members = GroupMember::select([
'group_member.id', 'group_member.group_owner as is_manager', 'group_member.visit_card',
'group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender',
'users.motto',
])
->leftJoin('users', 'users.id', '=', 'users_group_member.user_id')
->leftJoin('users', 'users.id', '=', 'group_member.user_id')
->where([
['users_group_member.group_id', '=', $group_id],
['users_group_member.status', '=', 0],
['group_member.group_id', '=', $group_id],
['group_member.status', '=', 0],
])->orderBy('is_manager', 'desc')->get()->toArray();
return $this->response->success($members);
@ -411,23 +411,23 @@ class GroupController extends CController
$group_id = $this->request->input('group_id', 0);
// 判断用户是否是群成员
if (!UsersGroup::isMember($group_id, $user_id)) {
if (!Group::isMember($group_id, $user_id)) {
return $this->response->fail('非管理员禁止操作...');
}
$rows = UsersGroupNotice::leftJoin('users', 'users.id', '=', 'users_group_notice.user_id')
$rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.user_id')
->where([
['users_group_notice.group_id', '=', $group_id],
['users_group_notice.is_delete', '=', 0]
['group_notice.group_id', '=', $group_id],
['group_notice.is_delete', '=', 0]
])
->orderBy('users_group_notice.id', 'desc')
->orderBy('group_notice.id', 'desc')
->get([
'users_group_notice.id',
'users_group_notice.user_id',
'users_group_notice.title',
'users_group_notice.content',
'users_group_notice.created_at',
'users_group_notice.updated_at',
'group_notice.id',
'group_notice.user_id',
'group_notice.title',
'group_notice.content',
'group_notice.created_at',
'group_notice.updated_at',
'users.avatar', 'users.nickname',
])->toArray();
@ -452,13 +452,13 @@ class GroupController extends CController
$user_id = $this->uid();
// 判断用户是否是管理员
if (!UsersGroup::isManager($user_id, $params['group_id'])) {
if (!Group::isManager($user_id, $params['group_id'])) {
return $this->response->fail('非管理员禁止操作...');
}
// 判断是否是新增数据
if (empty($data['notice_id'])) {
$result = UsersGroupNotice::create([
$result = GroupNotice::create([
'group_id' => $params['group_id'],
'title' => $params['title'],
'content' => $params['content'],
@ -475,7 +475,7 @@ class GroupController extends CController
return $this->response->success([], '添加群公告信息成功...');
}
$result = UsersGroupNotice::where('id', $data['notice_id'])->update([
$result = GroupNotice::where('id', $data['notice_id'])->update([
'title' => $data['title'],
'content' => $data['content'],
'updated_at' => date('Y-m-d H:i:s')
@ -502,11 +502,11 @@ class GroupController extends CController
$user_id = $this->uid();
// 判断用户是否是管理员
if (!UsersGroup::isManager($user_id, $params['group_id'])) {
if (!Group::isManager($user_id, $params['group_id'])) {
return $this->response->fail('非法操作...');
}
$result = UsersGroupNotice::where('id', $params['notice_id'])
$result = GroupNotice::where('id', $params['notice_id'])
->where('group_id', $params['group_id'])
->update([
'is_delete' => 1,

View File

@ -24,7 +24,7 @@ use App\Model\FileSplitUpload;
use App\Model\User;
use App\Model\UsersChatList;
use App\Model\UsersFriend;
use App\Model\Group\UsersGroup;
use App\Model\Group\Group;
use App\Service\TalkService;
use App\Service\UploadService;
use App\Amqp\Producer\ChatMessageProducer;
@ -101,7 +101,7 @@ class TalkController extends CController
return $this->response->fail('暂不属于好友关系,无法进行聊天...');
}
} else {
if (!UsersGroup::isMember($params['receive_id'], $user_id)) {
if (!Group::isMember($params['receive_id'], $user_id)) {
return $this->response->fail('暂不属于群成员,无法进行群聊...');
}
}
@ -133,7 +133,7 @@ class TalkController extends CController
$data['name'] = $userInfo->nickname;
$data['avatar'] = $userInfo->avatar;
} else if ($result['type'] == 2) {
$groupInfo = UsersGroup::where('id', $result['group_id'])->first(['group_name', 'avatar']);
$groupInfo = Group::where('id', $result['group_id'])->first(['group_name', 'avatar']);
$data['name'] = $groupInfo->group_name;
$data['avatar'] = $groupInfo->avatar;
}
@ -366,7 +366,7 @@ class TalkController extends CController
$limit = 30;
// 判断是否属于群成员
if ($params['source'] == 2 && UsersGroup::isMember($params['receive_id'], $user_id) == false) {
if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) {
return $this->response->success([
'rows' => [],
'record_id' => 0,
@ -428,7 +428,7 @@ class TalkController extends CController
$limit = 30;
// 判断是否属于群成员
if ($params['source'] == 2 && UsersGroup::isMember($params['receive_id'], $user_id) == false) {
if ($params['source'] == 2 && Group::isMember($params['receive_id'], $user_id) == false) {
return $this->response->success([
'rows' => [],
'record_id' => 0,

View File

@ -27,7 +27,7 @@ use Phper666\JWTAuth\JWT;
use App\Service\SocketClientService;
use App\Service\MessageHandleService;
use App\Service\SocketRoomService;
use App\Model\Group\UsersGroupMember;
use App\Model\Group\GroupMember;
use App\Amqp\Producer\ChatMessageProducer;
use App\Support\SocketIOParser;
@ -99,7 +99,7 @@ class WebSocketController implements OnMessageInterface, OnOpenInterface, OnClos
$this->socketClientService->bindRelation($request->fd, $userInfo['user_id']);
// 加入群聊
$groupIds = UsersGroupMember::getUserGroupIds($userInfo['user_id']);
$groupIds = GroupMember::getUserGroupIds($userInfo['user_id']);
foreach ($groupIds as $group_id) {
$this->socketRoomService->addRoomMember($userInfo['user_id'], $group_id);
}

112
app/Model/Group/Group.php Normal file
View File

@ -0,0 +1,112 @@
<?php
declare (strict_types=1);
namespace App\Model\Group;
use App\Model\BaseModel;
/**
* 聊天群组数据表模型
*
* @property integer $id 群ID
* @property integer $creator_id 群主ID
* @property string $group_name 群名称
* @property string $profile 群简介
* @property integer $avatar 群头像
* @property integer $max_num 最大群成员数量
* @property integer $is_overt 是否公开可见[0:;1:;]
* @property integer $is_mute 是否全员禁言 [0:;1:;],提示:不包含群主或管理员
* @property integer $is_dismiss 是否已解散[0:;1:;]
* @property string $created_at 创建时间
* @property string $dismissed_at 解散时间
*
* @package App\Model\Group
*/
class Group extends BaseModel
{
// 最大成员数量
const MAX_MEMBER_NUM = 200;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'group';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'creator_id',
'group_name',
'profile',
'avatar',
'max_num',
'is_overt',
'is_mute',
'is_dismiss',
'created_at',
'dismissed_at',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'creator_id' => 'integer',
'max_num' => 'integer',
'is_overt' => 'integer',
'is_mute' => 'integer',
'is_dismiss' => 'integer',
'created_at' => 'datetime'
];
/**
* 获取群聊成员
*/
public function members()
{
return $this->hasMany(GroupMember::class, 'group_id', 'id');
}
/**
* 判断用户是否是管理员
*
* @param int $user_id 用户ID
* @param int $group_id 群ID
* @param int|array $leader 管理员类型[0:普通成员;1:管理员;2:群主;]
* @return bool
*/
public static function isManager(int $user_id, int $group_id, $leader = 2)
{
return self::where('id', $group_id)->where('creator_id', $user_id)->where('leader', $leader)->exists();
}
/**
* 判断群组是否已解散
*
* @param int $group_id 群ID
* @return bool
*/
public static function isDismiss(int $group_id)
{
return self::where('id', $group_id)->where('is_dismiss', 1)->exists();
}
/**
* 判断用户是否是群成员
*
* @param int $group_id 群ID
* @param int $user_id 用户ID
* @return bool
*/
public static function isMember(int $group_id, int $user_id)
{
return GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->exists();
}
}

View File

@ -0,0 +1,96 @@
<?php
declare (strict_types=1);
namespace App\Model\Group;
use App\Model\BaseModel;
/**
* 聊天群组[成员]数据表模型
*
* @property integer $id 群成员ID
* @property integer $group_id 群组ID
* @property integer $user_id 用户ID
* @property integer $leader 成员属性[0:普通成员;1:管理员;2:群主;]
* @property integer $is_mute 是否禁言[0:;1:;]
* @property integer $is_quit 是否退群[0:;1:;]
* @property string $user_card 群名片
* @property string $created_at 入群时间
* @property string $deleted_at 退群时间
*
* @package App\Model\Group
*/
class GroupMember extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'group_member';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'group_id',
'user_id',
'leader',
'is_mute',
'is_quit',
'user_card',
'created_at',
'deleted_at',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'group_id' => 'integer',
'user_id' => 'integer',
'leader' => 'integer',
'is_mute' => 'integer',
'is_quit' => 'integer',
'created_at' => 'datetime',
'deleted_at' => 'datetime',
];
/**
* 获取聊天群成员ID
*
* @param int $group_id 群聊ID
* @return array
*/
public static function getGroupMemberIds(int $group_id)
{
return self::where('group_id', $group_id)->where('is_quit', 0)->pluck('user_id')->toArray() ?? [];
}
/**
* 获取用户的群名片
*
* @param int $user_id 用户ID
* @param int $group_id 群ID
* @return string
*/
public static function visitCard(int $user_id, int $group_id)
{
return self::where('group_id', $group_id)->where('user_id', $user_id)->value('visit_card') ?? "";
}
/**
* 获取用户的所有群ID
*
* @param int $user_id 用户ID
* @return array
*/
public static function getUserGroupIds(int $user_id)
{
return self::where('user_id', $user_id)->where('is_quit', 0)->pluck('group_id')->toArray() ?? [];
}
}

View File

@ -1,5 +1,4 @@
<?php
declare (strict_types=1);
namespace App\Model\Group;
@ -7,28 +6,31 @@ namespace App\Model\Group;
use App\Model\BaseModel;
/**
* 用户群组[公告消息]数据表模型
* 聊天群组[公告消息]数据表模型
*
* @property int $id 群公告ID
* @property int $group_id ID
* @property int $user_id 发布者ID
* @property integer $id 群公告ID
* @property integer $group_id ID
* @property integer $creator_id 创建者用户ID
* @property string $title 公告标题
* @property string $content 公告内容
* @property int $is_delete 是否删除[0:;1:]
* @property string $created_at 发布时间
* @property string $updated_at 修改时间
* @property integer $is_top 是否置顶[0:;1:;]
* @property integer $is_delete 是否删除[0:;1:;]
* @property integer $is_confirm 是否需群成员确认公告[0:;1:;]
* @property array $confirm_users 已确认成员
* @property string $created_at 创建时间
* @property string $updated_at 更新时间
* @property string $deleted_at 删除时间
*
* @package App\Model\Group
*/
class UsersGroupNotice extends BaseModel
class GroupNotice extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users_group_notice';
protected $table = 'group_notice';
/**
* The attributes that are mass assignable.
@ -37,10 +39,13 @@ class UsersGroupNotice extends BaseModel
*/
protected $fillable = [
'group_id',
'user_id',
'creator_id',
'title',
'content',
'is_top',
'is_delete',
'is_confirm',
'confirm_users',
'created_at',
'updated_at',
'deleted_at'
@ -52,11 +57,13 @@ class UsersGroupNotice extends BaseModel
* @var array
*/
protected $casts = [
'id' => 'integer',
'group_id' => 'integer',
'user_id' => 'integer',
'creator_id' => 'integer',
'is_top' => 'integer',
'is_delete' => 'integer',
'is_confirm' => 'integer',
'created_at' => 'datetime',
'updated_at' => 'datetime'
'updated_at' => 'datetime',
'deleted_at' => 'datetime',
];
}

View File

@ -1,88 +0,0 @@
<?php
declare (strict_types=1);
namespace App\Model\Group;
use App\Model\BaseModel;
/**
* 用户群组数据表模型
*
* @property int $id 群ID
* @property int $user_id 群组ID
* @property string $group_name 群名称
* @property string $group_profile 群简介
* @property int $status 群状态
* @property string $avatar 群头像
* @property string $created_at 创建时间
*
* @package App\Model\Group
*/
class UsersGroup extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users_group';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'user_id',
'group_name',
'group_profile',
'status',
'avatar',
'created_at',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'user_id' => 'integer',
'status' => 'integer',
'created_at' => 'datetime'
];
/**
* 获取群聊成员
*/
public function members()
{
return $this->hasMany(UsersGroupMember::class, 'group_id', 'id');
}
/**
* 判断用户是否是管理员
*
* @param int $user_id 用户ID
* @param int $group_id 群ID
* @return mixed
*/
public static function isManager(int $user_id, int $group_id)
{
return self::where('id', $group_id)->where('user_id', $user_id)->exists();
}
/**
* 判断用户是否是群成员
*
* @param int $group_id 群ID
* @param int $user_id 用户ID
* @return bool
*/
public static function isMember(int $group_id, int $user_id)
{
return UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('status', 0)->exists();
}
}

View File

@ -1,85 +0,0 @@
<?php
declare (strict_types=1);
namespace App\Model\Group;
use App\Model\BaseModel;
/**
* 用户群组[成员]数据表模型
*
* @property int $id 群成员ID
* @property int $group_id 群组ID
* @property int $user_id 用户ID
* @property int $group_owner 是否群主[0:;1:;]
* @property int $status 退群状态[0:正常状态;1:已退群;]
* @property string $visit_card 用户群名片
* @property string $created_at 入群时间
*
* @package App\Model\Group
*/
class UsersGroupMember extends BaseModel
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'users_group_member';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
'group_id' => 'integer',
'user_id' => 'integer',
'group_owner' => 'integer',
'status' => 'integer',
'created_at' => 'datetime'
];
/**
* 获取聊天群成员ID
*
* @param int $group_id 群聊ID
* @return mixed
*/
public static function getGroupMemberIds(int $group_id)
{
return self::where('group_id', $group_id)->where('status', 0)->pluck('user_id')->toArray();
}
/**
* 获取用户的群名片
*
* @param int $user_id 用户ID
* @param int $group_id 群ID
* @return mixed
*/
public static function visitCard(int $user_id, int $group_id)
{
return self::where('group_id', $group_id)->where('user_id', $user_id)->value('visit_card');
}
/**
* 获取用户的所有群ID
*
* @param int $user_id
* @return array
*/
public static function getUserGroupIds(int $user_id)
{
return self::where('user_id', $user_id)->where('status', 0)->pluck('group_id')->toArray();
}
}

View File

@ -5,7 +5,7 @@ namespace App\Service;
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsFile;
use App\Model\EmoticonDetail;
use App\Model\Group\UsersGroup;
use App\Model\Group\Group;
use App\Model\UsersEmoticon;
/**
@ -107,7 +107,7 @@ class EmoticonService extends BaseService
return [false, []];
}
} else {
if (!UsersGroup::isMember($result->receive_id, $user_id)) {
if (!Group::isMember($result->receive_id, $user_id)) {
return [false, []];
}
}

View File

@ -5,8 +5,8 @@ namespace App\Service;
use App\Cache\LastMsgCache;
use App\Model\Chat\ChatRecord;
use App\Model\Chat\ChatRecordsInvite;
use App\Model\Group\UsersGroup;
use App\Model\Group\UsersGroupMember;
use App\Model\Group\Group;
use App\Model\Group\GroupMember;
use App\Model\UsersChatList;
use Hyperf\DbConnection\Db;
use Exception;
@ -25,26 +25,23 @@ class GroupService extends BaseService
*/
public function getGroups(int $user_id): array
{
$fields = [
'users_group.id', 'users_group.group_name', 'users_group.avatar',
'users_group.group_profile', 'users_group.user_id as group_user_id'
];
$items = UsersGroupMember::join('users_group', 'users_group.id', '=', 'users_group_member.group_id')
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id', 'inner', [
['group.is_dismiss', '=', 0]
])
->where([
['users_group_member.user_id', '=', $user_id],
['users_group_member.status', '=', 0]
['group_member.user_id', '=', $user_id],
['group_member.is_quit', '=', 0]
])
->orderBy('id', 'desc')
->get($fields)->toArray();
->get([
'group.id',
'group.group_name',
'group.avatar',
'group.group_profile',
'group.leader',
])->toArray();
foreach ($items as $key => $item) {
// 判断当前用户是否是群主
$items[$key]['isGroupLeader'] = $item['group_user_id'] == $user_id;
//删除无关字段
unset($items[$key]['group_user_id']);
// 是否消息免打扰
$items[$key]['not_disturb'] = UsersChatList::where([
['uid', '=', $user_id],
@ -72,25 +69,26 @@ class GroupService extends BaseService
Db::beginTransaction();
try {
$insRes = UsersGroup::create([
'user_id' => $user_id,
$insRes = Group::create([
'creator_id' => $user_id,
'group_name' => $group_info['name'],
'avatar' => $group_info['avatar'],
'group_profile' => $group_info['profile'],
'status' => 0,
'profile' => $group_info['profile'],
'max_num' => Group::MAX_MEMBER_NUM,
'is_overt' => 0,
'is_mute' => 0,
'is_dismiss' => 0,
'created_at' => date('Y-m-d H:i:s')
]);
if (!$insRes) {
throw new Exception('创建群失败');
}
foreach ($friend_ids as $k => $uid) {
$groupMember[] = [
'group_id' => $insRes->id,
'user_id' => $uid,
'group_owner' => $user_id == $uid ? 1 : 0,
'status' => 0,
'leader' => $user_id == $uid ? 2 : 0,
'is_mute' => 0,
'is_quit' => 0,
'user_card' => 0,
'created_at' => date('Y-m-d H:i:s'),
];
@ -105,7 +103,7 @@ class GroupService extends BaseService
];
}
if (!Db::table('users_group_member')->insert($groupMember)) {
if (!Db::table('group_member')->insert($groupMember)) {
throw new Exception('创建群成员信息失败');
}
@ -121,10 +119,6 @@ class GroupService extends BaseService
'created_at' => date('Y-m-d H:i:s')
]);
if (!$result) {
throw new Exception('创建群成员的聊天列表失败');
}
ChatRecordsInvite::create([
'record_id' => $result->id,
'type' => 1,
@ -135,7 +129,6 @@ class GroupService extends BaseService
Db::commit();
} catch (Exception $e) {
Db::rollBack();
logger()->error($e);
return [false, 0];
}
@ -154,26 +147,15 @@ class GroupService extends BaseService
*/
public function dismiss(int $group_id, int $user_id)
{
if (!UsersGroup::where('id', $group_id)->where('status', 0)->exists()) {
$group = Group::where('id', $group_id)->first(['creator_id', 'is_dismiss']);
if (!$group || $group->creator_id != $user_id || $group->is_dismiss == 1) {
return false;
}
//判断执行者是否属于群主
if (!UsersGroup::isManager($user_id, $group_id)) {
return false;
}
Db::beginTransaction();
try {
UsersGroup::where('id', $group_id)->update(['status' => 1]);
UsersGroupMember::where('group_id', $group_id)->update(['status' => 1]);
Db::commit();
} catch (Exception $e) {
Db::rollBack();
return false;
}
return true;
return Group::where('id', $group_id)->where('creator_id', $user_id)->update([
'is_dismiss' => 1,
'dismissed_at' => date('Y-m-d H:i:s'),
]) ? true : false;
}
/**
@ -186,31 +168,47 @@ class GroupService extends BaseService
*/
public function invite(int $user_id, int $group_id, $friend_ids = [])
{
$info = UsersGroupMember::select(['id', 'status'])->where('group_id', $group_id)->where('user_id', $user_id)->first();
//判断主动邀请方是否属于聊天群成员
if (!$info && $info->status == 1) {
if (empty($friend_ids)) {
return [false, 0];
}
if (empty($friend_ids)) {
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
// 判断主动邀请方是否属于聊天群成员
if (!$info && $info->is_quit == 1) {
return [false, 0];
}
$updateArr = $insertArr = $updateArr1 = $insertArr1 = [];
$members = UsersGroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'status'])->keyBy('user_id')->toArray();
$members = GroupMember::where('group_id', $group_id)->whereIn('user_id', $friend_ids)->get(['id', 'user_id', 'is_quit'])->keyBy('user_id')->toArray();
$chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray();
foreach ($friend_ids as $uid) {
if (!isset($members[$uid])) {//存在聊天群成员记录
$insertArr[] = ['group_id' => $group_id, 'user_id' => $uid, 'group_owner' => 0, 'status' => 0, 'created_at' => date('Y-m-d H:i:s')];
$insertArr[] = [
'group_id' => $group_id,
'user_id' => $uid,
'leader' => 0,
'is_mute' => 0,
'is_quit' => 0,
'user_card' => '',
'created_at' => date('Y-m-d H:i:s')
];
} else if ($members[$uid]['status'] == 1) {
$updateArr[] = $members[$uid]['id'];
}
if (!isset($chatArr[$uid])) {
$insertArr1[] = ['type' => 2, 'uid' => $uid, 'friend_id' => 0, 'group_id' => $group_id, 'status' => 1, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
$insertArr1[] = [
'type' => 2,
'uid' => $uid,
'friend_id' => 0,
'group_id' => $group_id,
'status' => 1,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
} else if ($chatArr[$uid]['status'] == 0) {
$updateArr1[] = $chatArr[$uid]['id'];
}
@ -218,15 +216,24 @@ class GroupService extends BaseService
try {
if ($updateArr) {
UsersGroupMember::whereIn('id', $updateArr)->update(['status' => 0]);
GroupMember::whereIn('id', $updateArr)->update([
'leader' => 0,
'is_mute' => 0,
'is_quit' => 0,
'user_card' => '',
'created_at' => date('Y-m-d H:i:s')
]);
}
if ($insertArr) {
Db::table('users_group_member')->insert($insertArr);
Db::table('group_member')->insert($insertArr);
}
if ($updateArr1) {
UsersChatList::whereIn('id', $updateArr1)->update(['status' => 1, 'created_at' => date('Y-m-d H:i:s')]);
UsersChatList::whereIn('id', $updateArr1)->update([
'status' => 1,
'created_at' => date('Y-m-d H:i:s')
]);
}
if ($insertArr1) {
@ -241,17 +248,13 @@ class GroupService extends BaseService
'created_at' => date('Y-m-d H:i:s')
]);
if (!$result) throw new Exception('添加群通知记录失败1');
$result2 = ChatRecordsInvite::create([
ChatRecordsInvite::create([
'record_id' => $result->id,
'type' => 1,
'operate_user_id' => $user_id,
'user_ids' => implode(',', $friend_ids)
]);
if (!$result2) throw new Exception('添加群通知记录失败2');
Db::commit();
} catch (\Exception $e) {
Db::rollBack();
@ -271,13 +274,18 @@ class GroupService extends BaseService
*/
public function quit(int $user_id, int $group_id)
{
$record_id = 0;
if (Group::isManager($user_id, $group_id)) {
return [false, 0];
}
Db::beginTransaction();
try {
$res = UsersGroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('group_owner', 0)->update(['status' => 1]);
if ($res) {
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]);
GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]);
$result = ChatRecord::create([
'msg_type' => 3,
'source' => 2,
@ -287,26 +295,15 @@ class GroupService extends BaseService
'created_at' => date('Y-m-d H:i:s')
]);
if (!$result) {
throw new Exception('添加群通知记录失败 : quitGroupChat');
}
$record_id = $result->id;
$result2 = ChatRecordsInvite::create([
ChatRecordsInvite::create([
'record_id' => $result->id,
'type' => 2,
'operate_user_id' => $user_id,
'user_ids' => $user_id
]);
if (!$result2) {
throw new Exception('添加群通知记录失败2 : quitGroupChat');
}
UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
$record_id = $result->id;
}
Db::commit();
} catch (Exception $e) {
Db::rollBack();
@ -326,16 +323,16 @@ class GroupService extends BaseService
*/
public function removeMember(int $group_id, int $user_id, array $member_ids)
{
if (!UsersGroup::isManager($user_id, $group_id)) {
if (!Group::isManager($user_id, $group_id, [1, 2])) {
return [false, 0];
}
Db::beginTransaction();
try {
//更新用户状态
if (!UsersGroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->where('group_owner', 0)->update(['status' => 1])) {
throw new Exception('修改群成员状态失败');
}
GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->whereIn('leader', [0, 1])->update([
'is_quit' => 1,
'deleted_at' => date('Y-m-d H:i:s'),
]);
$result = ChatRecord::create([
'msg_type' => 3,
@ -345,24 +342,14 @@ class GroupService extends BaseService
'created_at' => date('Y-m-d H:i:s')
]);
if (!$result) {
throw new Exception('添加群通知记录失败1');
}
$result2 = ChatRecordsInvite::create([
ChatRecordsInvite::create([
'record_id' => $result->id,
'type' => 3,
'operate_user_id' => $user_id,
'user_ids' => implode(',', $member_ids)
]);
if (!$result2) {
throw new Exception('添加群通知记录失败2');
}
foreach ($member_ids as $member_id) {
UsersChatList::where('uid', $member_id)->where('group_id', $group_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
}
UsersChatList::whereIn('uid', $member_ids)->where('group_id', $group_id)->update(['status' => 0, 'updated_at' => date('Y-m-d H:i:s')]);
Db::commit();
} catch (Exception $e) {

View File

@ -11,7 +11,7 @@ use App\Amqp\Producer\ChatMessageProducer;
use App\Cache\LastMsgCache;
use App\Cache\UnreadTalkCache;
use App\Model\Chat\ChatRecord;
use App\Model\Group\UsersGroup;
use App\Model\Group\Group;
use App\Model\UsersFriend;
class MessageHandleService
@ -62,7 +62,7 @@ class MessageHandleService
}
} else if ($data['source_type'] == 2) {//群聊
//判断是否属于群成员
if (!UsersGroup::isMember((int)$data['receive_user'], (int)$data['send_user'])) {
if (!Group::isMember((int)$data['receive_user'], (int)$data['send_user'])) {
return;
}
}

View File

@ -10,7 +10,7 @@ use App\Model\Chat\ChatRecordsCode;
use App\Model\Chat\ChatRecordsFile;
use App\Model\Chat\ChatRecordsForward;
use App\Model\Chat\ChatRecordsInvite;
use App\Model\Group\UsersGroup;
use App\Model\Group\Group;
use App\Model\User;
use App\Model\UsersChatList;
use App\Model\UsersFriend;
@ -38,17 +38,14 @@ class TalkService extends BaseService
$rows = UsersChatList::from('users_chat_list as list')
->leftJoin('users', 'users.id', '=', 'list.friend_id')
->leftJoin('users_group as group', 'group.id', '=', 'list.group_id')
->leftJoin('group', 'group.id', '=', 'list.group_id')
->where('list.uid', $user_id)
->where('list.status', 1)
->orderBy('updated_at', 'desc')
->get($filed)
->toArray();
if (!$rows) {
return [];
}
if (!$rows) return [];
$socketFDService = make(SocketClientService::class);
$runIdAll = $socketFDService->getServerRunIdAll();
@ -306,7 +303,7 @@ class TalkService extends BaseService
//判断是否有权限查看
if ($result->source == 1 && ($result->user_id != $user_id && $result->receive_id != $user_id)) {
return [];
} else if ($result->source == 2 && !UsersGroup::isMember($result->receive_id, $user_id)) {
} else if ($result->source == 2 && !Group::isMember($result->receive_id, $user_id)) {
return [];
}
@ -357,7 +354,7 @@ class TalkService extends BaseService
}
// 判读是否属于群消息并且判断是否是群成员
if ($source == 2 && !UsersGroup::isMember($receive_id, $user_id)) {
if ($source == 2 && !Group::isMember($receive_id, $user_id)) {
return false;
}
@ -394,7 +391,7 @@ class TalkService extends BaseService
return [false, '非法操作', []];
}
} else if ($result->source == 2) {
if (!UsersGroup::isMember($result->receive_id, $user_id)) {
if (!Group::isMember($result->receive_id, $user_id)) {
return [false, '非法操作', []];
}
}
@ -426,7 +423,7 @@ class TalkService extends BaseService
return [];
}
} else if ($result->source == 2) {
if (!UsersGroup::isMember($result->receive_id, $user_id)) {
if (!Group::isMember($result->receive_id, $user_id)) {
return [];
}
}
@ -516,7 +513,7 @@ class TalkService extends BaseService
//验证是否有权限转发
if ($source == 2) {//群聊消息
//判断是否是群聊成员
if (!UsersGroup::isMember($receive_id, $user_id)) {
if (!Group::isMember($receive_id, $user_id)) {
return false;
}

View File

@ -0,0 +1,46 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateGroupTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('group', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('群ID');
$table->unsignedInteger('creator_id')->default(0)->comment('创建者ID(群主ID)');
$table->string('group_name', 30)->default('')->charset('utf8mb4')->comment('群名称');
$table->string('profile', 100)->default('')->comment('群介绍');
$table->string('avatar', 200)->default('')->comment('群头像');
$table->unsignedSmallInteger('max_num')->default(200)->comment('最大群成员数量');
$table->tinyInteger('is_overt')->default(0)->comment('是否公开可见[0:否;1:是;]');
$table->tinyInteger('is_mute')->default(0)->comment('是否全员禁言 [0:否;1:是;],提示:不包含群主或管理员');
$table->tinyInteger('is_dismiss')->default(0)->comment('是否已解散[0:否;1:是;]');
$table->dateTime('created_at')->nullable()->comment('创建时间');
$table->dateTime('dismissed_at')->nullable()->comment('解散时间');
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB';
$table->index(['group_id', 'is_delete', 'is_top', 'updated_at'], 'idx_group_id_is_delete_is_top_updated_at');
});
$prefix = config('databases.default.prefix');
DB::statement("ALTER TABLE `{$prefix}group` comment '聊天群组表'");
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('group');
}
}

View File

@ -1,52 +0,0 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateUsersGroupTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users_group', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('群ID');
$table->unsignedInteger('user_id')->default(0)->comment('用户ID');
$table->string('group_name', 30)->default('')->charset('utf8mb4')->comment('群名称');
$table->string('group_profile', 100)->default('')->comment('群介绍');
$table->tinyInteger('status')->default(0)->comment('群状态[0:正常;1:已解散]');
$table->string('avatar', 255)->default('')->comment('群头像');
//预留字段
//$table->string('invite_mode', 255)->default(0)->comment('邀请方式[0:全员邀请;1:全员邀请需群主确认;2:仅群主可邀请;]');
$table->dateTime('created_at')->nullable()->comment('创建时间');
});
// 预留表(后期邀请入群需同意后进入)
/*
Schema::create('users_group_invite', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('自增ID');
$table->unsignedInteger('record_id')->default(0)->comment('消息记录ID');
$table->unsignedInteger('group_id')->default(0)->comment('群ID');
$table->unsignedInteger('user_id', 30)->default(0)->comment('用户ID');
$table->tinyInteger('status')->default(0)->comment('邀请状态[0:等待同意;1:已同意;2:拒绝邀请]');
$table->dateTime('created_at')->nullable()->comment('创建时间');
});
*/
$prefix = config('databases.default.prefix');
DB::statement("ALTER TABLE `{$prefix}users_group` comment '用户聊天群'");
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users_group');
}
}

View File

@ -0,0 +1,45 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateGroupMemberTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('group_member', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('群成员ID');
$table->unsignedInteger('group_id')->default(0)->comment('群ID');
$table->unsignedInteger('user_id')->default(0)->comment('用户ID');
$table->tinyInteger('leader')->comment('成员属性[0:普通成员;1:管理员;2:群主;]');
$table->tinyInteger('is_mute')->default(0)->comment('是否禁言[0:否;1:是;]');
$table->tinyInteger('is_quit')->default(0)->comment('是否退群[0:否;1:是;]');
$table->string('user_card', 20)->default('')->comment('群名片');
$table->dateTime('created_at')->nullable()->comment('入群时间');
$table->dateTime('deleted_at')->nullable()->comment('退群时间');
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB';
$table->unique(['group_id', 'user_id'], 'uk_group_id_user_id');
$table->index(['user_id', 'group_id'], 'idx_user_id_group_id');
});
$prefix = config('databases.default.prefix');
DB::statement("ALTER TABLE `{$prefix}group_member` comment '聊天群组成员表'");
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('group_member');
}
}

View File

@ -1,41 +0,0 @@
<?php
use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateUsersGroupMemberTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('users_group_member', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('自增ID');
$table->unsignedInteger('group_id')->default(0)->comment('群ID');
$table->unsignedInteger('user_id')->default(0)->comment('用户ID');
$table->tinyInteger('group_owner')->nullable()->comment('是否为群主[0:否;1:是]');
$table->tinyInteger('status')->default(0)->comment('退群状态[0:正常状态;1:已退群]');
$table->string('visit_card', 20)->default('')->comment('用户群名片');
$table->dateTime('created_at')->nullable()->comment('入群时间');
$table->charset = 'utf8';
$table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB';
$table->index(['group_id', 'status'], 'idx_group_id_status');
});
$prefix = config('databases.default.prefix');
DB::statement("ALTER TABLE `{$prefix}users_group_member` comment '群聊成员'");
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('users_group_member');
}
}

View File

@ -4,7 +4,8 @@ use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;
class CreateUsersGroupNoticeTable extends Migration
class CreateGroupNoticeTable extends Migration
{
/**
* Run the migrations.
@ -12,12 +13,15 @@ class CreateUsersGroupNoticeTable extends Migration
public function up(): void
{
Schema::create('users_group_notice', function (Blueprint $table) {
$table->unsignedInteger('id', true)->comment('公告ID');
$table->unsignedInteger('group_id')->default(0)->comment('群ID');
$table->unsignedInteger('user_id')->default(0)->comment('创建者用户ID');
$table->string('title', 30)->default('')->charset('utf8mb4')->comment('公告标题');
$table->unsignedInteger('id', true)->comment('公告ID');
$table->unsignedInteger('group_id')->default(0)->comment('群ID');
$table->unsignedInteger('creator_id')->default(0)->comment('创建者用户ID');
$table->string('title', 50)->default('')->charset('utf8mb4')->comment('公告标题');
$table->text('content')->charset('utf8mb4')->comment('公告内容');
$table->tinyInteger('is_delete')->default(0)->comment('是否删除[0:否;1:已删除]');
$table->tinyInteger('is_top')->default(0)->comment('是否置顶[0:否;1:是;]');
$table->tinyInteger('is_delete')->default(0)->comment('是否删除[0:否;1:是;]');
$table->tinyInteger('is_confirm')->default(0)->comment('是否需群成员确认公告[0:否;1:是;]');
$table->json('confirm_users')->nullable()->comment('已确认成员');
$table->dateTime('created_at')->nullable()->comment('创建时间');
$table->dateTime('updated_at')->nullable()->comment('更新时间');
$table->dateTime('deleted_at')->nullable()->comment('删除时间');
@ -26,11 +30,11 @@ class CreateUsersGroupNoticeTable extends Migration
$table->collation = 'utf8_general_ci';
$table->engine = 'InnoDB';
$table->index(['group_id'], 'idx_group_id');
$table->index(['group_id', 'is_delete', 'is_top', 'updated_at'], 'idx_group_id_is_delete_is_top_updated_at');
});
$prefix = config('databases.default.prefix');
DB::statement("ALTER TABLE `{$prefix}users_group_notice` comment '群组公告表'");
DB::statement("ALTER TABLE `{$prefix}group_notice` comment '群组公告表'");
}
/**
@ -38,6 +42,6 @@ class CreateUsersGroupNoticeTable extends Migration
*/
public function down(): void
{
Schema::dropIfExists('users_group_notice');
Schema::dropIfExists('group_notice');
}
}