重构群组相关代码
parent
f60ab0f0c1
commit
619d0234c1
|
@ -94,9 +94,9 @@ class GroupController extends CController
|
|||
$this->producer->produce(
|
||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||
'sender' => $user_id, // 发送者ID
|
||||
'receive' => intval($data['group_id']), //接收者ID
|
||||
'source' => 2, //接收者类型 1:好友;2:群组
|
||||
'record_id' => intval($data['record_id'])
|
||||
'receive' => (int)$data['group_id'], // 接收者ID
|
||||
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||
'record_id' => (int)$data['record_id']
|
||||
])
|
||||
);
|
||||
|
||||
|
@ -124,7 +124,7 @@ class GroupController extends CController
|
|||
|
||||
$this->socketRoomService->delRoom($params['group_id']);
|
||||
|
||||
// ... 推送群消息(后期添加)
|
||||
// ... 推送群消息(预留)
|
||||
|
||||
return $this->response->success([], '群组解散成功...');
|
||||
}
|
||||
|
@ -159,8 +159,8 @@ class GroupController extends CController
|
|||
$this->producer->produce(
|
||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||
'sender' => $user_id, // 发送者ID
|
||||
'receive' => intval($params['group_id']), //接收者ID
|
||||
'source' => 2, //接收者类型 1:好友;2:群组
|
||||
'receive' => (int)$params['group_id'], // 接收者ID
|
||||
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||
'record_id' => $record_id
|
||||
])
|
||||
);
|
||||
|
@ -193,8 +193,8 @@ class GroupController extends CController
|
|||
$this->producer->produce(
|
||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||
'sender' => $user_id, // 发送者ID
|
||||
'receive' => intval($params['group_id']), //接收者ID
|
||||
'source' => 2, //接收者类型 1:好友;2:群组
|
||||
'receive' => (int)$params['group_id'], // 接收者ID
|
||||
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||
'record_id' => $record_id
|
||||
])
|
||||
);
|
||||
|
@ -209,22 +209,21 @@ class GroupController extends CController
|
|||
*/
|
||||
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, [
|
||||
'group_id' => 'required|integer',
|
||||
'group_name' => 'required|max:30',
|
||||
'group_profile' => 'required|max:100',
|
||||
'profile' => 'present|max:100',
|
||||
'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_profile' => $params['group_profile'],
|
||||
'profile' => $params['profile'],
|
||||
'avatar' => $params['avatar']
|
||||
]);
|
||||
|
||||
// 推送消息通知
|
||||
// ...
|
||||
// ... 推送消息通知(预留)
|
||||
|
||||
return $result
|
||||
? $this->response->success([], '群组信息修改成功...')
|
||||
|
@ -241,9 +240,11 @@ class GroupController extends CController
|
|||
$params = $this->request->inputs(['group_id', 'members_ids']);
|
||||
$this->validate($params, [
|
||||
'group_id' => 'required|integer',
|
||||
'members_ids' => 'required|array'
|
||||
'members_ids' => 'required|ids'
|
||||
]);
|
||||
|
||||
$params['members_ids'] = parse_ids($params['members_ids']);
|
||||
|
||||
$user_id = $this->uid();
|
||||
if (in_array($user_id, $params['members_ids'])) {
|
||||
return $this->response->fail('群聊用户移除失败...');
|
||||
|
@ -263,8 +264,8 @@ class GroupController extends CController
|
|||
$this->producer->produce(
|
||||
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
|
||||
'sender' => $user_id, // 发送者ID
|
||||
'receive' => intval($params['group_id']), //接收者ID
|
||||
'source' => 2, //接收者类型 1:好友;2:群组
|
||||
'receive' => (int)$params['group_id'], // 接收者ID
|
||||
'source' => 2, // 接收者类型[1:好友;2:群组;]
|
||||
'record_id' => $record_id
|
||||
])
|
||||
);
|
||||
|
@ -280,19 +281,21 @@ class GroupController extends CController
|
|||
public function detail()
|
||||
{
|
||||
$group_id = $this->request->input('group_id', 0);
|
||||
|
||||
$user_id = $this->uid();
|
||||
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.user_id')
|
||||
->where('group.id', $group_id)->where('group.status', 0)->first([
|
||||
'group.id', 'group.user_id',
|
||||
|
||||
$groupInfo = Group::leftJoin('users', 'users.id', '=', 'group.creator_id')
|
||||
->where('group.id', $group_id)->where('group.is_dismiss', 0)->first([
|
||||
'group.id',
|
||||
'group.creator_id',
|
||||
'group.group_name',
|
||||
'group.group_profile', 'group.avatar',
|
||||
'group.profile',
|
||||
'group.avatar',
|
||||
'group.created_at',
|
||||
'users.nickname'
|
||||
]);
|
||||
|
||||
if (!$groupInfo) {
|
||||
return $this->response->success([]);
|
||||
return $this->response->success();
|
||||
}
|
||||
|
||||
$notice = GroupNotice::where('group_id', $group_id)
|
||||
|
@ -303,10 +306,10 @@ class GroupController extends CController
|
|||
return $this->response->success([
|
||||
'group_id' => $groupInfo->id,
|
||||
'group_name' => $groupInfo->group_name,
|
||||
'group_profile' => $groupInfo->group_profile,
|
||||
'group_profile' => $groupInfo->profile,
|
||||
'avatar' => $groupInfo->avatar,
|
||||
'created_at' => $groupInfo->created_at,
|
||||
'is_manager' => $groupInfo->user_id == $user_id,
|
||||
'is_manager' => $groupInfo->creator_id == $user_id,
|
||||
'manager_nickname' => $groupInfo->nickname,
|
||||
'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,
|
||||
|
@ -319,7 +322,7 @@ class GroupController extends CController
|
|||
*
|
||||
* @RequestMapping(path="set-group-card", methods="post")
|
||||
*/
|
||||
public function setGroupCard()
|
||||
public function editGroupCard()
|
||||
{
|
||||
$params = $this->request->inputs(['group_id', 'visit_card']);
|
||||
$this->validate($params, [
|
||||
|
@ -329,8 +332,7 @@ class GroupController extends CController
|
|||
|
||||
$isTrue = GroupMember::where('group_id', $params['group_id'])
|
||||
->where('user_id', $this->uid())
|
||||
->where('status', 0)
|
||||
->update(['visit_card' => $params['visit_card']]);
|
||||
->update(['user_card' => $params['visit_card']]);
|
||||
|
||||
return $isTrue
|
||||
? $this->response->success([], '群名片修改成功...')
|
||||
|
@ -387,15 +389,20 @@ class GroupController extends CController
|
|||
}
|
||||
|
||||
$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',
|
||||
'group_member.id',
|
||||
'group_member.leader',
|
||||
'group_member.user_card',
|
||||
'group_member.user_id',
|
||||
'users.avatar',
|
||||
'users.nickname',
|
||||
'users.gender',
|
||||
'users.motto',
|
||||
])
|
||||
->leftJoin('users', 'users.id', '=', 'group_member.user_id')
|
||||
->where([
|
||||
['group_member.group_id', '=', $group_id],
|
||||
['group_member.status', '=', 0],
|
||||
])->orderBy('is_manager', 'desc')->get()->toArray();
|
||||
['group_member.is_quit', '=', 0],
|
||||
])->orderBy('leader', 'desc')->get()->toArray();
|
||||
|
||||
return $this->response->success($members);
|
||||
}
|
||||
|
@ -405,7 +412,7 @@ class GroupController extends CController
|
|||
*
|
||||
* @RequestMapping(path="notices", methods="get")
|
||||
*/
|
||||
public function getGroupNotices()
|
||||
public function getGroupNotice()
|
||||
{
|
||||
$user_id = $this->uid();
|
||||
$group_id = $this->request->input('group_id', 0);
|
||||
|
@ -415,20 +422,25 @@ class GroupController extends CController
|
|||
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([
|
||||
['group_notice.group_id', '=', $group_id],
|
||||
['group_notice.is_delete', '=', 0]
|
||||
])
|
||||
->orderBy('group_notice.id', 'desc')
|
||||
->orderBy('group_notice.is_top', 'desc')
|
||||
->orderBy('group_notice.updated_at', 'desc')
|
||||
->get([
|
||||
'group_notice.id',
|
||||
'group_notice.user_id',
|
||||
'group_notice.creator_id',
|
||||
'group_notice.title',
|
||||
'group_notice.content',
|
||||
'group_notice.is_top',
|
||||
'group_notice.is_confirm',
|
||||
'group_notice.confirm_users',
|
||||
'group_notice.created_at',
|
||||
'group_notice.updated_at',
|
||||
'users.avatar', 'users.nickname',
|
||||
'users.avatar',
|
||||
'users.nickname',
|
||||
])->toArray();
|
||||
|
||||
return $this->response->success($rows);
|
||||
|
@ -441,11 +453,13 @@ class GroupController extends CController
|
|||
*/
|
||||
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, [
|
||||
'group_id' => 'required|integer',
|
||||
'notice_id' => 'required|integer',
|
||||
'group_id' => 'required|integer',
|
||||
'title' => 'required|max:50',
|
||||
'is_top' => 'integer|in:0,1',
|
||||
'is_confirm' => 'integer|in:0,1',
|
||||
'content' => 'required'
|
||||
]);
|
||||
|
||||
|
@ -457,12 +471,14 @@ class GroupController extends CController
|
|||
}
|
||||
|
||||
// 判断是否是新增数据
|
||||
if (empty($data['notice_id'])) {
|
||||
if (empty($params['notice_id'])) {
|
||||
$result = GroupNotice::create([
|
||||
'group_id' => $params['group_id'],
|
||||
'creator_id' => $user_id,
|
||||
'title' => $params['title'],
|
||||
'content' => $params['content'],
|
||||
'user_id' => $user_id,
|
||||
'is_top' => $params['is_top'],
|
||||
'is_confirm' => $params['is_confirm'],
|
||||
'created_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->success([], '添加群公告信息成功...');
|
||||
}
|
||||
|
||||
$result = GroupNotice::where('id', $data['notice_id'])->update([
|
||||
'title' => $data['title'],
|
||||
'content' => $data['content'],
|
||||
$result = GroupNotice::where('id', $params['notice_id'])->update([
|
||||
'title' => $params['title'],
|
||||
'content' => $params['content'],
|
||||
'is_top' => $params['is_top'],
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
|
|
|
@ -27,6 +27,10 @@ class ValidatorFactoryResolvedListener implements ListenerInterface
|
|||
|
||||
// 注册了 ids 验证器(验证英文逗号拼接的整形数字字符串 例如:[1,2,3,4,5])
|
||||
$validatorFactory->extend('ids', function ($attribute, $value, $parameters, $validator) {
|
||||
if (!is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$arr = explode(',', $value);
|
||||
foreach ($arr as $id) {
|
||||
if (!check_int($id)) return false;
|
||||
|
|
|
@ -84,7 +84,7 @@ class Group extends BaseModel
|
|||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -80,7 +80,7 @@ class GroupMember extends BaseModel
|
|||
*/
|
||||
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') ?? "";
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
|
@ -13,6 +14,7 @@ use Exception;
|
|||
|
||||
/**
|
||||
* Class GroupService
|
||||
*
|
||||
* @package App\Service
|
||||
*/
|
||||
class GroupService extends BaseService
|
||||
|
@ -25,9 +27,7 @@ class GroupService extends BaseService
|
|||
*/
|
||||
public function getGroups(int $user_id): array
|
||||
{
|
||||
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id', 'inner', [
|
||||
['group.is_dismiss', '=', 0]
|
||||
])
|
||||
$items = GroupMember::join('group', 'group.id', '=', 'group_member.group_id')
|
||||
->where([
|
||||
['group_member.user_id', '=', $user_id],
|
||||
['group_member.is_quit', '=', 0]
|
||||
|
@ -37,17 +37,17 @@ class GroupService extends BaseService
|
|||
'group.id',
|
||||
'group.group_name',
|
||||
'group.avatar',
|
||||
'group.group_profile',
|
||||
'group.leader',
|
||||
'group.profile',
|
||||
'group_member.leader',
|
||||
])->toArray();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
// 是否消息免打扰
|
||||
$items[$key]['not_disturb'] = UsersChatList::where([
|
||||
$arr = UsersChatList::where([
|
||||
['uid', '=', $user_id],
|
||||
['type', '=', 2],
|
||||
['group_id', '=', $item['id']]
|
||||
])->value('not_disturb');
|
||||
])->get(['group_id', 'not_disturb'])->keyBy('group_id')->toArray();
|
||||
|
||||
foreach ($items as $key => $item) {
|
||||
$items[$key]['not_disturb'] = $arr[$item['id']] ? $arr[$item['id']]['not_disturb'] : 0;
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
@ -88,7 +88,7 @@ class GroupService extends BaseService
|
|||
'leader' => $user_id == $uid ? 2 : 0,
|
||||
'is_mute' => 0,
|
||||
'is_quit' => 0,
|
||||
'user_card' => 0,
|
||||
'user_card' => '',
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
|
@ -139,7 +139,7 @@ class GroupService extends BaseService
|
|||
}
|
||||
|
||||
/**
|
||||
* 解散群组
|
||||
* 解散群组(群主权限)
|
||||
*
|
||||
* @param int $group_id 群ID
|
||||
* @param int $user_id 用户ID
|
||||
|
@ -152,10 +152,19 @@ class GroupService extends BaseService
|
|||
return false;
|
||||
}
|
||||
|
||||
return Group::where('id', $group_id)->where('creator_id', $user_id)->update([
|
||||
DB::transaction(function () use ($group_id, $user_id) {
|
||||
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;
|
||||
]);
|
||||
|
||||
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 = [])
|
||||
{
|
||||
if (empty($friend_ids)) {
|
||||
return [false, 0];
|
||||
}
|
||||
if (!$friend_ids) return [false, 0];
|
||||
|
||||
$info = GroupMember::where('group_id', $group_id)->where('user_id', $user_id)->first(['id', 'is_quit']);
|
||||
|
||||
|
@ -185,7 +192,7 @@ class GroupService extends BaseService
|
|||
$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])) {//存在聊天群成员记录
|
||||
if (!isset($members[$uid])) {
|
||||
$insertArr[] = [
|
||||
'group_id' => $group_id,
|
||||
'user_id' => $uid,
|
||||
|
@ -256,7 +263,7 @@ class GroupService extends BaseService
|
|||
]);
|
||||
|
||||
Db::commit();
|
||||
} catch (\Exception $e) {
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
return [false, 0];
|
||||
}
|
||||
|
@ -266,7 +273,7 @@ class GroupService extends BaseService
|
|||
}
|
||||
|
||||
/**
|
||||
* 退出群组
|
||||
* 退出群组(仅普通管理员及群成员)
|
||||
*
|
||||
* @param int $user_id 用户ID
|
||||
* @param int $group_id 群组ID
|
||||
|
@ -280,12 +287,15 @@ class GroupService extends BaseService
|
|||
|
||||
Db::beginTransaction();
|
||||
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,
|
||||
'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([
|
||||
'msg_type' => 3,
|
||||
'source' => 2,
|
||||
|
@ -304,6 +314,8 @@ class GroupService extends BaseService
|
|||
'user_ids' => $user_id
|
||||
]);
|
||||
|
||||
UsersChatList::where('uid', $user_id)->where('type', 2)->where('group_id', $group_id)->update(['status' => 0]);
|
||||
|
||||
Db::commit();
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
|
@ -323,17 +335,21 @@ class GroupService extends BaseService
|
|||
*/
|
||||
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];
|
||||
}
|
||||
|
||||
Db::beginTransaction();
|
||||
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,
|
||||
'deleted_at' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
|
||||
if ($count == 0) {
|
||||
throw new Exception('更新记录失败...');
|
||||
}
|
||||
|
||||
$result = ChatRecord::create([
|
||||
'msg_type' => 3,
|
||||
'source' => 2,
|
||||
|
@ -349,7 +365,10 @@ class GroupService extends BaseService
|
|||
'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();
|
||||
} catch (Exception $e) {
|
||||
|
|
|
@ -48,7 +48,7 @@ return [
|
|||
],
|
||||
'settings' => [
|
||||
'enable_coroutine' => true,
|
||||
'worker_num' => swoole_cpu_num() * 4,
|
||||
'worker_num' => 1,
|
||||
'pid_file' => BASE_PATH . '/runtime/hyperf.pid',
|
||||
'open_tcp_nodelay' => true,
|
||||
'max_coroutine' => 100000,
|
||||
|
|
Loading…
Reference in New Issue