重构群组相关代码
parent
f60ab0f0c1
commit
619d0234c1
|
@ -68,15 +68,15 @@ class GroupController extends CController
|
||||||
$params = $this->request->inputs(['group_name', 'uids']);
|
$params = $this->request->inputs(['group_name', 'uids']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_name' => 'required',
|
'group_name' => 'required',
|
||||||
'uids' => 'required|ids'
|
'uids' => 'required|ids'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$friend_ids = parse_ids($params['uids']);
|
$friend_ids = parse_ids($params['uids']);
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$user_id = $this->uid();
|
||||||
[$isTrue, $data] = $this->groupService->create($user_id, [
|
[$isTrue, $data] = $this->groupService->create($user_id, [
|
||||||
'name' => $params['group_name'],
|
'name' => $params['group_name'],
|
||||||
'avatar' => $params['avatar'] ?? '',
|
'avatar' => $params['avatar'] ?? '',
|
||||||
'profile' => $params['group_profile'] ?? ''
|
'profile' => $params['group_profile'] ?? ''
|
||||||
], $friend_ids);
|
], $friend_ids);
|
||||||
|
|
||||||
|
@ -90,13 +90,13 @@ class GroupController extends CController
|
||||||
$this->socketRoomService->addRoomMember($uid, $data['group_id']);
|
$this->socketRoomService->addRoomMember($uid, $data['group_id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...消息推送队列
|
// ... 消息推送队列
|
||||||
$this->producer->produce(
|
$this->producer->produce(
|
||||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||||
'sender' => $user_id, //发送者ID
|
'sender' => $user_id, // 发送者ID
|
||||||
'receive' => intval($data['group_id']), //接收者ID
|
'receive' => (int)$data['group_id'], // 接收者ID
|
||||||
'source' => 2, //接收者类型 1:好友;2:群组
|
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||||
'record_id' => intval($data['record_id'])
|
'record_id' => (int)$data['record_id']
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ class GroupController extends CController
|
||||||
|
|
||||||
$this->socketRoomService->delRoom($params['group_id']);
|
$this->socketRoomService->delRoom($params['group_id']);
|
||||||
|
|
||||||
// ... 推送群消息(后期添加)
|
// ... 推送群消息(预留)
|
||||||
|
|
||||||
return $this->response->success([], '群组解散成功...');
|
return $this->response->success([], '群组解散成功...');
|
||||||
}
|
}
|
||||||
|
@ -139,7 +139,7 @@ class GroupController extends CController
|
||||||
$params = $this->request->inputs(['group_id', 'uids']);
|
$params = $this->request->inputs(['group_id', 'uids']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'uids' => 'required|ids'
|
'uids' => 'required|ids'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$uids = parse_ids($params['uids']);
|
$uids = parse_ids($params['uids']);
|
||||||
|
@ -158,9 +158,9 @@ class GroupController extends CController
|
||||||
// ...消息推送队列
|
// ...消息推送队列
|
||||||
$this->producer->produce(
|
$this->producer->produce(
|
||||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||||
'sender' => $user_id, //发送者ID
|
'sender' => $user_id, // 发送者ID
|
||||||
'receive' => intval($params['group_id']), //接收者ID
|
'receive' => (int)$params['group_id'], // 接收者ID
|
||||||
'source' => 2, //接收者类型 1:好友;2:群组
|
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||||
'record_id' => $record_id
|
'record_id' => $record_id
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
@ -192,9 +192,9 @@ class GroupController extends CController
|
||||||
// ...消息推送队列
|
// ...消息推送队列
|
||||||
$this->producer->produce(
|
$this->producer->produce(
|
||||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||||
'sender' => $user_id, //发送者ID
|
'sender' => $user_id, // 发送者ID
|
||||||
'receive' => intval($params['group_id']), //接收者ID
|
'receive' => (int)$params['group_id'], // 接收者ID
|
||||||
'source' => 2, //接收者类型 1:好友;2:群组
|
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||||
'record_id' => $record_id
|
'record_id' => $record_id
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
@ -209,22 +209,21 @@ class GroupController extends CController
|
||||||
*/
|
*/
|
||||||
public function editDetail()
|
public function editDetail()
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['group_id', 'group_name', 'group_profile', 'avatar']);
|
$params = $this->request->inputs(['group_id', 'group_name', 'profile', 'avatar']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'group_name' => 'required|max:30',
|
'group_name' => 'required|max:30',
|
||||||
'group_profile' => 'required|max:100',
|
'profile' => 'present|max:100',
|
||||||
'avatar' => 'present|url'
|
'avatar' => 'present|url'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$result = Group::where('id', $params['group_id'])->where('user_id', $this->uid())->update([
|
$result = Group::where('id', $params['group_id'])->where('creator_id', $this->uid())->update([
|
||||||
'group_name' => $params['group_name'],
|
'group_name' => $params['group_name'],
|
||||||
'group_profile' => $params['group_profile'],
|
'profile' => $params['profile'],
|
||||||
'avatar' => $params['avatar']
|
'avatar' => $params['avatar']
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 推送消息通知
|
// ... 推送消息通知(预留)
|
||||||
// ...
|
|
||||||
|
|
||||||
return $result
|
return $result
|
||||||
? $this->response->success([], '群组信息修改成功...')
|
? $this->response->success([], '群组信息修改成功...')
|
||||||
|
@ -240,10 +239,12 @@ class GroupController extends CController
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['group_id', 'members_ids']);
|
$params = $this->request->inputs(['group_id', 'members_ids']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'members_ids' => 'required|array'
|
'members_ids' => 'required|ids'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
$params['members_ids'] = parse_ids($params['members_ids']);
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$user_id = $this->uid();
|
||||||
if (in_array($user_id, $params['members_ids'])) {
|
if (in_array($user_id, $params['members_ids'])) {
|
||||||
return $this->response->fail('群聊用户移除失败...');
|
return $this->response->fail('群聊用户移除失败...');
|
||||||
|
@ -259,12 +260,12 @@ class GroupController extends CController
|
||||||
$this->socketRoomService->delRoomMember($params['group_id'], $uid);
|
$this->socketRoomService->delRoomMember($params['group_id'], $uid);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ...消息推送队列
|
// ... 消息推送队列
|
||||||
$this->producer->produce(
|
$this->producer->produce(
|
||||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||||
'sender' => $user_id, //发送者ID
|
'sender' => $user_id, // 发送者ID
|
||||||
'receive' => intval($params['group_id']), //接收者ID
|
'receive' => (int)$params['group_id'], // 接收者ID
|
||||||
'source' => 2, //接收者类型 1:好友;2:群组
|
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||||
'record_id' => $record_id
|
'record_id' => $record_id
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
|
@ -280,19 +281,21 @@ class GroupController extends CController
|
||||||
public function detail()
|
public function detail()
|
||||||
{
|
{
|
||||||
$group_id = $this->request->input('group_id', 0);
|
$group_id = $this->request->input('group_id', 0);
|
||||||
|
$user_id = $this->uid();
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.creator_id')
|
||||||
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.user_id')
|
->where('group.id', $group_id)->where('group.is_dismiss', 0)->first([
|
||||||
->where('group.id', $group_id)->where('group.status', 0)->first([
|
'group.id',
|
||||||
'group.id', 'group.user_id',
|
'group.creator_id',
|
||||||
'group.group_name',
|
'group.group_name',
|
||||||
'group.group_profile', 'group.avatar',
|
'group.profile',
|
||||||
|
'group.avatar',
|
||||||
'group.created_at',
|
'group.created_at',
|
||||||
'users.nickname'
|
'users.nickname'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!$groupInfo) {
|
if (!$groupInfo) {
|
||||||
return $this->response->success([]);
|
return $this->response->success();
|
||||||
}
|
}
|
||||||
|
|
||||||
$notice = GroupNotice::where('group_id', $group_id)
|
$notice = GroupNotice::where('group_id', $group_id)
|
||||||
|
@ -301,16 +304,16 @@ class GroupController extends CController
|
||||||
->first(['title', 'content']);
|
->first(['title', 'content']);
|
||||||
|
|
||||||
return $this->response->success([
|
return $this->response->success([
|
||||||
'group_id' => $groupInfo->id,
|
'group_id' => $groupInfo->id,
|
||||||
'group_name' => $groupInfo->group_name,
|
'group_name' => $groupInfo->group_name,
|
||||||
'group_profile' => $groupInfo->group_profile,
|
'group_profile' => $groupInfo->profile,
|
||||||
'avatar' => $groupInfo->avatar,
|
'avatar' => $groupInfo->avatar,
|
||||||
'created_at' => $groupInfo->created_at,
|
'created_at' => $groupInfo->created_at,
|
||||||
'is_manager' => $groupInfo->user_id == $user_id,
|
'is_manager' => $groupInfo->creator_id == $user_id,
|
||||||
'manager_nickname' => $groupInfo->nickname,
|
'manager_nickname' => $groupInfo->nickname,
|
||||||
'visit_card' => GroupMember::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,
|
'not_disturb' => UsersChatList::where('uid', $user_id)->where('group_id', $group_id)->where('type', 2)->value('not_disturb') ?? 0,
|
||||||
'notice' => $notice ? $notice->toArray() : []
|
'notice' => $notice ? $notice->toArray() : []
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -319,18 +322,17 @@ class GroupController extends CController
|
||||||
*
|
*
|
||||||
* @RequestMapping(path="set-group-card", methods="post")
|
* @RequestMapping(path="set-group-card", methods="post")
|
||||||
*/
|
*/
|
||||||
public function setGroupCard()
|
public function editGroupCard()
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['group_id', 'visit_card']);
|
$params = $this->request->inputs(['group_id', 'visit_card']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'visit_card' => 'required|max:20'
|
'visit_card' => 'required|max:20'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$isTrue = GroupMember::where('group_id', $params['group_id'])
|
$isTrue = GroupMember::where('group_id', $params['group_id'])
|
||||||
->where('user_id', $this->uid())
|
->where('user_id', $this->uid())
|
||||||
->where('status', 0)
|
->update(['user_card' => $params['visit_card']]);
|
||||||
->update(['visit_card' => $params['visit_card']]);
|
|
||||||
|
|
||||||
return $isTrue
|
return $isTrue
|
||||||
? $this->response->success([], '群名片修改成功...')
|
? $this->response->success([], '群名片修改成功...')
|
||||||
|
@ -387,15 +389,20 @@ class GroupController extends CController
|
||||||
}
|
}
|
||||||
|
|
||||||
$members = GroupMember::select([
|
$members = GroupMember::select([
|
||||||
'group_member.id', 'group_member.group_owner as is_manager', 'group_member.visit_card',
|
'group_member.id',
|
||||||
'group_member.user_id', 'users.avatar', 'users.nickname', 'users.gender',
|
'group_member.leader',
|
||||||
|
'group_member.user_card',
|
||||||
|
'group_member.user_id',
|
||||||
|
'users.avatar',
|
||||||
|
'users.nickname',
|
||||||
|
'users.gender',
|
||||||
'users.motto',
|
'users.motto',
|
||||||
])
|
])
|
||||||
->leftJoin('users', 'users.id', '=', 'group_member.user_id')
|
->leftJoin('users', 'users.id', '=', 'group_member.user_id')
|
||||||
->where([
|
->where([
|
||||||
['group_member.group_id', '=', $group_id],
|
['group_member.group_id', '=', $group_id],
|
||||||
['group_member.status', '=', 0],
|
['group_member.is_quit', '=', 0],
|
||||||
])->orderBy('is_manager', 'desc')->get()->toArray();
|
])->orderBy('leader', 'desc')->get()->toArray();
|
||||||
|
|
||||||
return $this->response->success($members);
|
return $this->response->success($members);
|
||||||
}
|
}
|
||||||
|
@ -405,7 +412,7 @@ class GroupController extends CController
|
||||||
*
|
*
|
||||||
* @RequestMapping(path="notices", methods="get")
|
* @RequestMapping(path="notices", methods="get")
|
||||||
*/
|
*/
|
||||||
public function getGroupNotices()
|
public function getGroupNotice()
|
||||||
{
|
{
|
||||||
$user_id = $this->uid();
|
$user_id = $this->uid();
|
||||||
$group_id = $this->request->input('group_id', 0);
|
$group_id = $this->request->input('group_id', 0);
|
||||||
|
@ -415,20 +422,25 @@ class GroupController extends CController
|
||||||
return $this->response->fail('非管理员禁止操作...');
|
return $this->response->fail('非管理员禁止操作...');
|
||||||
}
|
}
|
||||||
|
|
||||||
$rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.user_id')
|
$rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.creator_id')
|
||||||
->where([
|
->where([
|
||||||
['group_notice.group_id', '=', $group_id],
|
['group_notice.group_id', '=', $group_id],
|
||||||
['group_notice.is_delete', '=', 0]
|
['group_notice.is_delete', '=', 0]
|
||||||
])
|
])
|
||||||
->orderBy('group_notice.id', 'desc')
|
->orderBy('group_notice.is_top', 'desc')
|
||||||
|
->orderBy('group_notice.updated_at', 'desc')
|
||||||
->get([
|
->get([
|
||||||
'group_notice.id',
|
'group_notice.id',
|
||||||
'group_notice.user_id',
|
'group_notice.creator_id',
|
||||||
'group_notice.title',
|
'group_notice.title',
|
||||||
'group_notice.content',
|
'group_notice.content',
|
||||||
|
'group_notice.is_top',
|
||||||
|
'group_notice.is_confirm',
|
||||||
|
'group_notice.confirm_users',
|
||||||
'group_notice.created_at',
|
'group_notice.created_at',
|
||||||
'group_notice.updated_at',
|
'group_notice.updated_at',
|
||||||
'users.avatar', 'users.nickname',
|
'users.avatar',
|
||||||
|
'users.nickname',
|
||||||
])->toArray();
|
])->toArray();
|
||||||
|
|
||||||
return $this->response->success($rows);
|
return $this->response->success($rows);
|
||||||
|
@ -441,12 +453,14 @@ class GroupController extends CController
|
||||||
*/
|
*/
|
||||||
public function editNotice()
|
public function editNotice()
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['group_id', 'notice_id', 'title', 'content']);
|
$params = $this->request->inputs(['group_id', 'notice_id', 'title', 'content', 'is_top', 'is_confirm']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'notice_id' => 'required|integer',
|
||||||
'notice_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'title' => 'required|max:50',
|
'title' => 'required|max:50',
|
||||||
'content' => 'required'
|
'is_top' => 'integer|in:0,1',
|
||||||
|
'is_confirm' => 'integer|in:0,1',
|
||||||
|
'content' => 'required'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$user_id = $this->uid();
|
$user_id = $this->uid();
|
||||||
|
@ -457,12 +471,14 @@ class GroupController extends CController
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否是新增数据
|
// 判断是否是新增数据
|
||||||
if (empty($data['notice_id'])) {
|
if (empty($params['notice_id'])) {
|
||||||
$result = GroupNotice::create([
|
$result = GroupNotice::create([
|
||||||
'group_id' => $params['group_id'],
|
'group_id' => $params['group_id'],
|
||||||
'title' => $params['title'],
|
'creator_id' => $user_id,
|
||||||
'content' => $params['content'],
|
'title' => $params['title'],
|
||||||
'user_id' => $user_id,
|
'content' => $params['content'],
|
||||||
|
'is_top' => $params['is_top'],
|
||||||
|
'is_confirm' => $params['is_confirm'],
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
'created_at' => date('Y-m-d H:i:s'),
|
||||||
'updated_at' => date('Y-m-d H:i:s')
|
'updated_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
@ -471,13 +487,15 @@ class GroupController extends CController
|
||||||
return $this->response->fail('添加群公告信息失败...');
|
return $this->response->fail('添加群公告信息失败...');
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... 推送群消息
|
// ... 推送群消息(预留)
|
||||||
|
|
||||||
return $this->response->success([], '添加群公告信息成功...');
|
return $this->response->success([], '添加群公告信息成功...');
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = GroupNotice::where('id', $data['notice_id'])->update([
|
$result = GroupNotice::where('id', $params['notice_id'])->update([
|
||||||
'title' => $data['title'],
|
'title' => $params['title'],
|
||||||
'content' => $data['content'],
|
'content' => $params['content'],
|
||||||
|
'is_top' => $params['is_top'],
|
||||||
'updated_at' => date('Y-m-d H:i:s')
|
'updated_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -495,7 +513,7 @@ class GroupController extends CController
|
||||||
{
|
{
|
||||||
$params = $this->request->inputs(['group_id', 'notice_id']);
|
$params = $this->request->inputs(['group_id', 'notice_id']);
|
||||||
$this->validate($params, [
|
$this->validate($params, [
|
||||||
'group_id' => 'required|integer',
|
'group_id' => 'required|integer',
|
||||||
'notice_id' => 'required|integer'
|
'notice_id' => 'required|integer'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
@ -509,7 +527,7 @@ class GroupController extends CController
|
||||||
$result = GroupNotice::where('id', $params['notice_id'])
|
$result = GroupNotice::where('id', $params['notice_id'])
|
||||||
->where('group_id', $params['group_id'])
|
->where('group_id', $params['group_id'])
|
||||||
->update([
|
->update([
|
||||||
'is_delete' => 1,
|
'is_delete' => 1,
|
||||||
'deleted_at' => date('Y-m-d H:i:s')
|
'deleted_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
|
@ -27,6 +27,10 @@ class ValidatorFactoryResolvedListener implements ListenerInterface
|
||||||
|
|
||||||
// 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5])
|
// 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5])
|
||||||
$validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) {
|
$validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) {
|
||||||
|
if (!is_string($value)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$arr = explode(',', $value);
|
$arr = explode(',', $value);
|
||||||
foreach ($arr as $id) {
|
foreach ($arr as $id) {
|
||||||
if (!check_int($id)) return false;
|
if (!check_int($id)) return false;
|
||||||
|
|
|
@ -59,9 +59,9 @@ class Group extends BaseModel
|
||||||
*/
|
*/
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'creator_id' => 'integer',
|
'creator_id' => 'integer',
|
||||||
'max_num' => 'integer',
|
'max_num' => 'integer',
|
||||||
'is_overt' => 'integer',
|
'is_overt' => 'integer',
|
||||||
'is_mute' => 'integer',
|
'is_mute' => 'integer',
|
||||||
'is_dismiss' => 'integer',
|
'is_dismiss' => 'integer',
|
||||||
'created_at' => 'datetime'
|
'created_at' => 'datetime'
|
||||||
];
|
];
|
||||||
|
@ -84,7 +84,7 @@ class Group extends BaseModel
|
||||||
*/
|
*/
|
||||||
public static function isManager(int $user_id, int $group_id, $leader = 2)
|
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();
|
return self::where('id', $group_id)->where('creator_id', $user_id)->exists();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -51,11 +51,11 @@ class GroupMember extends BaseModel
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'group_id' => 'integer',
|
'group_id' => 'integer',
|
||||||
'user_id' => 'integer',
|
'user_id' => 'integer',
|
||||||
'leader' => 'integer',
|
'leader' => 'integer',
|
||||||
'is_mute' => 'integer',
|
'is_mute' => 'integer',
|
||||||
'is_quit' => 'integer',
|
'is_quit' => 'integer',
|
||||||
'created_at' => 'datetime',
|
'created_at' => 'datetime',
|
||||||
'deleted_at' => 'datetime',
|
'deleted_at' => 'datetime',
|
||||||
];
|
];
|
||||||
|
@ -80,7 +80,7 @@ class GroupMember extends BaseModel
|
||||||
*/
|
*/
|
||||||
public static function visitCard(int $user_id, int $group_id)
|
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') ?? "";
|
return self::where('group_id', $group_id)->where('user_id', $user_id)->value('user_card') ?? "";
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -57,10 +57,10 @@ class GroupNotice extends BaseModel
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
protected $casts = [
|
protected $casts = [
|
||||||
'group_id' => 'integer',
|
'group_id' => 'integer',
|
||||||
'creator_id' => 'integer',
|
'creator_id' => 'integer',
|
||||||
'is_top' => 'integer',
|
'is_top' => 'integer',
|
||||||
'is_delete' => 'integer',
|
'is_delete' => 'integer',
|
||||||
'is_confirm' => 'integer',
|
'is_confirm' => 'integer',
|
||||||
'created_at' => 'datetime',
|
'created_at' => 'datetime',
|
||||||
'updated_at' => 'datetime',
|
'updated_at' => 'datetime',
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
<?php
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Service;
|
namespace App\Service;
|
||||||
|
|
||||||
|
@ -13,6 +14,7 @@ use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class GroupService
|
* Class GroupService
|
||||||
|
*
|
||||||
* @package App\Service
|
* @package App\Service
|
||||||
*/
|
*/
|
||||||
class GroupService extends BaseService
|
class GroupService extends BaseService
|
||||||
|
@ -25,9 +27,7 @@ class GroupService extends BaseService
|
||||||
*/
|
*/
|
||||||
public function getGroups(int $user_id): array
|
public function getGroups(int $user_id): array
|
||||||
{
|
{
|
||||||
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id', 'inner', [
|
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id')
|
||||||
['group.is_dismiss', '=', 0]
|
|
||||||
])
|
|
||||||
->where([
|
->where([
|
||||||
['group_member.user_id', '=', $user_id],
|
['group_member.user_id', '=', $user_id],
|
||||||
['group_member.is_quit', '=', 0]
|
['group_member.is_quit', '=', 0]
|
||||||
|
@ -37,17 +37,17 @@ class GroupService extends BaseService
|
||||||
'group.id',
|
'group.id',
|
||||||
'group.group_name',
|
'group.group_name',
|
||||||
'group.avatar',
|
'group.avatar',
|
||||||
'group.group_profile',
|
'group.profile',
|
||||||
'group.leader',
|
'group_member.leader',
|
||||||
])->toArray();
|
])->toArray();
|
||||||
|
|
||||||
|
$arr = UsersChatList::where([
|
||||||
|
['uid', '=', $user_id],
|
||||||
|
['type', '=', 2],
|
||||||
|
])->get(['group_id', 'not_disturb'])->keyBy('group_id')->toArray();
|
||||||
|
|
||||||
foreach ($items as $key => $item) {
|
foreach ($items as $key => $item) {
|
||||||
// 是否消息免打扰
|
$items[$key]['not_disturb'] = $arr[$item['id']] ? $arr[$item['id']]['not_disturb'] : 0;
|
||||||
$items[$key]['not_disturb'] = UsersChatList::where([
|
|
||||||
['uid', '=', $user_id],
|
|
||||||
['type', '=', 2],
|
|
||||||
['group_id', '=', $item['id']]
|
|
||||||
])->value('not_disturb');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $items;
|
return $items;
|
||||||
|
@ -72,32 +72,32 @@ class GroupService extends BaseService
|
||||||
$insRes = Group::create([
|
$insRes = Group::create([
|
||||||
'creator_id' => $user_id,
|
'creator_id' => $user_id,
|
||||||
'group_name' => $group_info['name'],
|
'group_name' => $group_info['name'],
|
||||||
'avatar' => $group_info['avatar'],
|
'avatar' => $group_info['avatar'],
|
||||||
'profile' => $group_info['profile'],
|
'profile' => $group_info['profile'],
|
||||||
'max_num' => Group::MAX_MEMBER_NUM,
|
'max_num' => Group::MAX_MEMBER_NUM,
|
||||||
'is_overt' => 0,
|
'is_overt' => 0,
|
||||||
'is_mute' => 0,
|
'is_mute' => 0,
|
||||||
'is_dismiss' => 0,
|
'is_dismiss' => 0,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
foreach ($friend_ids as $k => $uid) {
|
foreach ($friend_ids as $k => $uid) {
|
||||||
$groupMember[] = [
|
$groupMember[] = [
|
||||||
'group_id' => $insRes->id,
|
'group_id' => $insRes->id,
|
||||||
'user_id' => $uid,
|
'user_id' => $uid,
|
||||||
'leader' => $user_id == $uid ? 2 : 0,
|
'leader' => $user_id == $uid ? 2 : 0,
|
||||||
'is_mute' => 0,
|
'is_mute' => 0,
|
||||||
'is_quit' => 0,
|
'is_quit' => 0,
|
||||||
'user_card' => 0,
|
'user_card' => '',
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
'created_at' => date('Y-m-d H:i:s'),
|
||||||
];
|
];
|
||||||
|
|
||||||
$chatList[] = [
|
$chatList[] = [
|
||||||
'type' => 2,
|
'type' => 2,
|
||||||
'uid' => $uid,
|
'uid' => $uid,
|
||||||
'friend_id' => 0,
|
'friend_id' => 0,
|
||||||
'group_id' => $insRes->id,
|
'group_id' => $insRes->id,
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
'created_at' => date('Y-m-d H:i:s'),
|
||||||
'updated_at' => date('Y-m-d H:i:s')
|
'updated_at' => date('Y-m-d H:i:s')
|
||||||
];
|
];
|
||||||
|
@ -112,18 +112,18 @@ class GroupService extends BaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = ChatRecord::create([
|
$result = ChatRecord::create([
|
||||||
'msg_type' => 3,
|
'msg_type' => 3,
|
||||||
'source' => 2,
|
'source' => 2,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
'receive_id' => $insRes->id,
|
'receive_id' => $insRes->id,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
ChatRecordsInvite::create([
|
ChatRecordsInvite::create([
|
||||||
'record_id' => $result->id,
|
'record_id' => $result->id,
|
||||||
'type' => 1,
|
'type' => 1,
|
||||||
'operate_user_id' => $user_id,
|
'operate_user_id' => $user_id,
|
||||||
'user_ids' => implode(',', $friend_ids)
|
'user_ids' => implode(',', $friend_ids)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
|
@ -139,7 +139,7 @@ class GroupService extends BaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 解散群组
|
* 解散群组(群主权限)
|
||||||
*
|
*
|
||||||
* @param int $group_id 群ID
|
* @param int $group_id 群ID
|
||||||
* @param int $user_id 用户ID
|
* @param int $user_id 用户ID
|
||||||
|
@ -152,10 +152,19 @@ class GroupService extends BaseService
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Group::where('id', $group_id)->where('creator_id', $user_id)->update([
|
DB::transaction(function () use ($group_id, $user_id) {
|
||||||
'is_dismiss' => 1,
|
Group::where('id', $group_id)->where('creator_id', $user_id)->update([
|
||||||
'dismissed_at' => date('Y-m-d H:i:s'),
|
'is_dismiss' => 1,
|
||||||
]) ? true : false;
|
'dismissed_at' => date('Y-m-d H:i:s'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
GroupMember::where('group_id', $group_id)->update([
|
||||||
|
'is_quit' => 1,
|
||||||
|
'deleted_at' => date('Y-m-d H:i:s'),
|
||||||
|
]);
|
||||||
|
}, 2);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -168,9 +177,7 @@ class GroupService extends BaseService
|
||||||
*/
|
*/
|
||||||
public function invite(int $user_id, int $group_id, $friend_ids = [])
|
public function invite(int $user_id, int $group_id, $friend_ids = [])
|
||||||
{
|
{
|
||||||
if (empty($friend_ids)) {
|
if (!$friend_ids) return [false, 0];
|
||||||
return [false, 0];
|
|
||||||
}
|
|
||||||
|
|
||||||
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
|
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
|
||||||
|
|
||||||
|
@ -185,14 +192,14 @@ class GroupService extends BaseService
|
||||||
$chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray();
|
$chatArr = UsersChatList::where('group_id', $group_id)->whereIn('uid', $friend_ids)->get(['id', 'uid', 'status'])->keyBy('uid')->toArray();
|
||||||
|
|
||||||
foreach ($friend_ids as $uid) {
|
foreach ($friend_ids as $uid) {
|
||||||
if (!isset($members[$uid])) {//存在聊天群成员记录
|
if (!isset($members[$uid])) {
|
||||||
$insertArr[] = [
|
$insertArr[] = [
|
||||||
'group_id' => $group_id,
|
'group_id' => $group_id,
|
||||||
'user_id' => $uid,
|
'user_id' => $uid,
|
||||||
'leader' => 0,
|
'leader' => 0,
|
||||||
'is_mute' => 0,
|
'is_mute' => 0,
|
||||||
'is_quit' => 0,
|
'is_quit' => 0,
|
||||||
'user_card' => '',
|
'user_card' => '',
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
];
|
];
|
||||||
} else if ($members[$uid]['status'] == 1) {
|
} else if ($members[$uid]['status'] == 1) {
|
||||||
|
@ -201,11 +208,11 @@ class GroupService extends BaseService
|
||||||
|
|
||||||
if (!isset($chatArr[$uid])) {
|
if (!isset($chatArr[$uid])) {
|
||||||
$insertArr1[] = [
|
$insertArr1[] = [
|
||||||
'type' => 2,
|
'type' => 2,
|
||||||
'uid' => $uid,
|
'uid' => $uid,
|
||||||
'friend_id' => 0,
|
'friend_id' => 0,
|
||||||
'group_id' => $group_id,
|
'group_id' => $group_id,
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
'created_at' => date('Y-m-d H:i:s'),
|
'created_at' => date('Y-m-d H:i:s'),
|
||||||
'updated_at' => date('Y-m-d H:i:s')
|
'updated_at' => date('Y-m-d H:i:s')
|
||||||
];
|
];
|
||||||
|
@ -217,10 +224,10 @@ class GroupService extends BaseService
|
||||||
try {
|
try {
|
||||||
if ($updateArr) {
|
if ($updateArr) {
|
||||||
GroupMember::whereIn('id', $updateArr)->update([
|
GroupMember::whereIn('id', $updateArr)->update([
|
||||||
'leader' => 0,
|
'leader' => 0,
|
||||||
'is_mute' => 0,
|
'is_mute' => 0,
|
||||||
'is_quit' => 0,
|
'is_quit' => 0,
|
||||||
'user_card' => '',
|
'user_card' => '',
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -231,7 +238,7 @@ class GroupService extends BaseService
|
||||||
|
|
||||||
if ($updateArr1) {
|
if ($updateArr1) {
|
||||||
UsersChatList::whereIn('id', $updateArr1)->update([
|
UsersChatList::whereIn('id', $updateArr1)->update([
|
||||||
'status' => 1,
|
'status' => 1,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
@ -241,22 +248,22 @@ class GroupService extends BaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = ChatRecord::create([
|
$result = ChatRecord::create([
|
||||||
'msg_type' => 3,
|
'msg_type' => 3,
|
||||||
'source' => 2,
|
'source' => 2,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
'receive_id' => $group_id,
|
'receive_id' => $group_id,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
ChatRecordsInvite::create([
|
ChatRecordsInvite::create([
|
||||||
'record_id' => $result->id,
|
'record_id' => $result->id,
|
||||||
'type' => 1,
|
'type' => 1,
|
||||||
'operate_user_id' => $user_id,
|
'operate_user_id' => $user_id,
|
||||||
'user_ids' => implode(',', $friend_ids)
|
'user_ids' => implode(',', $friend_ids)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
} catch (\Exception $e) {
|
} catch (Exception $e) {
|
||||||
Db::rollBack();
|
Db::rollBack();
|
||||||
return [false, 0];
|
return [false, 0];
|
||||||
}
|
}
|
||||||
|
@ -266,7 +273,7 @@ class GroupService extends BaseService
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 退出群组
|
* 退出群组(仅普通管理员及群成员)
|
||||||
*
|
*
|
||||||
* @param int $user_id 用户ID
|
* @param int $user_id 用户ID
|
||||||
* @param int $group_id 群组ID
|
* @param int $group_id 群组ID
|
||||||
|
@ -280,30 +287,35 @@ class GroupService extends BaseService
|
||||||
|
|
||||||
Db::beginTransaction();
|
Db::beginTransaction();
|
||||||
try {
|
try {
|
||||||
GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->update([
|
$count = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->where('is_quit', 0)->update([
|
||||||
'is_quit' => 1,
|
'is_quit' => 1,
|
||||||
'deleted_at' => date('Y-m-d H:i:s'),
|
'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]);
|
if ($count == 0) {
|
||||||
|
throw new Exception('更新记录失败...');
|
||||||
|
}
|
||||||
|
|
||||||
$result = ChatRecord::create([
|
$result = ChatRecord::create([
|
||||||
'msg_type' => 3,
|
'msg_type' => 3,
|
||||||
'source' => 2,
|
'source' => 2,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
'receive_id' => $group_id,
|
'receive_id' => $group_id,
|
||||||
'content' => $user_id,
|
'content' => $user_id,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$record_id = $result->id;
|
$record_id = $result->id;
|
||||||
|
|
||||||
ChatRecordsInvite::create([
|
ChatRecordsInvite::create([
|
||||||
'record_id' => $result->id,
|
'record_id' => $result->id,
|
||||||
'type' => 2,
|
'type' => 2,
|
||||||
'operate_user_id' => $user_id,
|
'operate_user_id' => $user_id,
|
||||||
'user_ids' => $user_id
|
'user_ids' => $user_id
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]);
|
||||||
|
|
||||||
Db::commit();
|
Db::commit();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Db::rollBack();
|
Db::rollBack();
|
||||||
|
@ -323,33 +335,40 @@ class GroupService extends BaseService
|
||||||
*/
|
*/
|
||||||
public function removeMember(int $group_id, int $user_id, array $member_ids)
|
public function removeMember(int $group_id, int $user_id, array $member_ids)
|
||||||
{
|
{
|
||||||
if (!Group::isManager($user_id, $group_id, [1, 2])) {
|
if (!Group::isManager($user_id, $group_id)) {
|
||||||
return [false, 0];
|
return [false, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
Db::beginTransaction();
|
Db::beginTransaction();
|
||||||
try {
|
try {
|
||||||
GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->whereIn('leader', [0, 1])->update([
|
$count = GroupMember::where('group_id', $group_id)->whereIn('user_id', $member_ids)->where('is_quit', 0)->update([
|
||||||
'is_quit' => 1,
|
'is_quit' => 1,
|
||||||
'deleted_at' => date('Y-m-d H:i:s'),
|
'deleted_at' => date('Y-m-d H:i:s'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
if ($count == 0) {
|
||||||
|
throw new Exception('更新记录失败...');
|
||||||
|
}
|
||||||
|
|
||||||
$result = ChatRecord::create([
|
$result = ChatRecord::create([
|
||||||
'msg_type' => 3,
|
'msg_type' => 3,
|
||||||
'source' => 2,
|
'source' => 2,
|
||||||
'user_id' => 0,
|
'user_id' => 0,
|
||||||
'receive_id' => $group_id,
|
'receive_id' => $group_id,
|
||||||
'created_at' => date('Y-m-d H:i:s')
|
'created_at' => date('Y-m-d H:i:s')
|
||||||
]);
|
]);
|
||||||
|
|
||||||
ChatRecordsInvite::create([
|
ChatRecordsInvite::create([
|
||||||
'record_id' => $result->id,
|
'record_id' => $result->id,
|
||||||
'type' => 3,
|
'type' => 3,
|
||||||
'operate_user_id' => $user_id,
|
'operate_user_id' => $user_id,
|
||||||
'user_ids' => implode(',', $member_ids)
|
'user_ids' => implode(',', $member_ids)
|
||||||
]);
|
]);
|
||||||
|
|
||||||
UsersChatList::whereIn('uid', $member_ids)->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();
|
Db::commit();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
|
|
|
@ -48,7 +48,7 @@ return [
|
||||||
],
|
],
|
||||||
'settings' => [
|
'settings' => [
|
||||||
'enable_coroutine' => true,
|
'enable_coroutine' => true,
|
||||||
'worker_num' => swoole_cpu_num() * 4,
|
'worker_num' => 1,
|
||||||
'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
|
'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
|
||||||
'open_tcp_nodelay' => true,
|
'open_tcp_nodelay' => true,
|
||||||
'max_coroutine' => 100000,
|
'max_coroutine' => 100000,
|
||||||
|
|
Loading…
Reference in New Issue