hyperf-chat/app/Controller/Api/V1/GroupController.php

539 lines
17 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
/**
*
* This is my open source code, please do not use it for commercial applications.
*
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
namespace App\Controller\Api\V1;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
use Hyperf\Amqp\Producer;
use App\Model\UsersFriend;
use App\Model\UsersChatList;
use App\Model\Group\Group;
use App\Model\Group\GroupMember;
use App\Model\Group\GroupNotice;
2021-01-28 20:07:14 +08:00
use App\Amqp\Producer\ChatMessageProducer;
use App\Service\SocketRoomService;
use App\Service\GroupService;
use App\Constants\SocketConstants;
/**
* Class GroupController
*
* @Controller(path="/api/v1/group")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
class GroupController extends CController
{
/**
* @Inject
* @var GroupService
*/
private $groupService;
/**
* @Inject
* @var Producer
*/
private $producer;
/**
* @Inject
* @var SocketRoomService
*/
private $socketRoomService;
/**
* 创建群组
*
* @RequestMapping(path="create", methods="post")
*
* @return mixed
*/
public function create()
{
$params = $this->request->inputs(['group_name', 'uids']);
$this->validate($params, [
'group_name' => 'required',
2021-03-26 22:03:11 +08:00
'uids' => 'required|ids'
2021-01-28 20:07:14 +08:00
]);
$friend_ids = parse_ids($params['uids']);
$user_id = $this->uid();
[$isTrue, $data] = $this->groupService->create($user_id, [
2021-03-26 22:03:11 +08:00
'name' => $params['group_name'],
'avatar' => $params['avatar'] ?? '',
2021-01-28 20:07:14 +08:00
'profile' => $params['group_profile'] ?? ''
], $friend_ids);
if (!$isTrue) {
return $this->response->fail('创建群聊失败,请稍后再试...');
}
// 加入聊天室
$friend_ids[] = $user_id;
foreach ($friend_ids as $uid) {
$this->socketRoomService->addRoomMember($uid, $data['group_id']);
}
2021-03-26 22:03:11 +08:00
// ... 消息推送队列
2021-01-28 20:07:14 +08:00
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
2021-03-26 22:03:11 +08:00
'sender' => $user_id, // 发送者ID
'receive' => (int)$data['group_id'], // 接收者ID
'source' => 2, // 接收者类型[1:好友;2:群组;]
'record_id' => (int)$data['record_id']
2021-01-28 20:07:14 +08:00
])
);
return $this->response->success([
'group_id' => $data['group_id']
]);
}
/**
* 解散群组接口
*
* @RequestMapping(path="dismiss", methods="post")
*/
public function dismiss()
{
$params = $this->request->inputs(['group_id']);
$this->validate($params, [
'group_id' => 'required|integer'
]);
$isTrue = $this->groupService->dismiss($params['group_id'], $this->uid());
if (!$isTrue) {
return $this->response->fail('群组解散失败...');
}
$this->socketRoomService->delRoom($params['group_id']);
2021-03-26 22:03:11 +08:00
// ... 推送群消息(预留)
2021-01-28 20:07:14 +08:00
return $this->response->success([], '群组解散成功...');
}
/**
* 邀请好友加入群组接口
*
* @RequestMapping(path="invite", methods="post")
*/
public function invite()
{
$params = $this->request->inputs(['group_id', 'uids']);
$this->validate($params, [
'group_id' => 'required|integer',
2021-03-26 22:03:11 +08:00
'uids' => 'required|ids'
2021-01-28 20:07:14 +08:00
]);
$uids = parse_ids($params['uids']);
$user_id = $this->uid();
[$isTrue, $record_id] = $this->groupService->invite($user_id, $params['group_id'], $uids);
if (!$isTrue) {
return $this->response->fail('邀请好友加入群聊失败...');
}
// 加入聊天室
foreach ($uids as $uid) {
$this->socketRoomService->addRoomMember($uid, $params['group_id']);
}
// ...消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
2021-03-26 22:03:11 +08:00
'sender' => $user_id, // 发送者ID
'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, // 接收者类型[1:好友;2:群组;]
2021-01-28 20:07:14 +08:00
'record_id' => $record_id
])
);
return $this->response->success([], '好友已成功加入群聊...');
}
/**
* 退出群组接口
*
* @RequestMapping(path="secede", methods="post")
*/
public function secede()
{
$params = $this->request->inputs(['group_id']);
$this->validate($params, [
'group_id' => 'required|integer'
]);
$user_id = $this->uid();
[$isTrue, $record_id] = $this->groupService->quit($user_id, $params['group_id']);
if (!$isTrue) {
return $this->response->fail('退出群组失败...');
}
// 移出聊天室
$this->socketRoomService->delRoomMember($params['group_id'], $user_id);
// ...消息推送队列
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
2021-03-26 22:03:11 +08:00
'sender' => $user_id, // 发送者ID
'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, // 接收者类型[1:好友;2:群组;]
2021-01-28 20:07:14 +08:00
'record_id' => $record_id
])
);
return $this->response->success([], '已成功退出群组...');
}
/**
* 编辑群组信息
*
* @RequestMapping(path="edit", methods="post")
*/
public function editDetail()
{
2021-03-26 22:03:11 +08:00
$params = $this->request->inputs(['group_id', 'group_name', 'profile', 'avatar']);
2021-01-28 20:07:14 +08:00
$this->validate($params, [
2021-03-26 22:03:11 +08:00
'group_id' => 'required|integer',
2021-01-28 20:07:14 +08:00
'group_name' => 'required|max:30',
2021-03-26 22:03:11 +08:00
'profile' => 'present|max:100',
'avatar' => 'present|url'
2021-01-28 20:07:14 +08:00
]);
2021-03-26 22:03:11 +08:00
$result = Group::where('id', $params['group_id'])->where('creator_id', $this->uid())->update([
2021-01-28 20:07:14 +08:00
'group_name' => $params['group_name'],
2021-03-26 22:03:11 +08:00
'profile' => $params['profile'],
'avatar' => $params['avatar']
2021-01-28 20:07:14 +08:00
]);
2021-03-26 22:03:11 +08:00
// ... 推送消息通知(预留)
2021-01-28 20:07:14 +08:00
return $result
? $this->response->success([], '群组信息修改成功...')
: $this->response->fail('群组信息修改失败...');
}
/**
* 移除指定成员(管理员权限)
*
* @RequestMapping(path="remove-members", methods="post")
*/
public function removeMembers()
{
$params = $this->request->inputs(['group_id', 'members_ids']);
$this->validate($params, [
2021-03-26 22:03:11 +08:00
'group_id' => 'required|integer',
'members_ids' => 'required|ids'
2021-01-28 20:07:14 +08:00
]);
2021-03-26 22:03:11 +08:00
$params['members_ids'] = parse_ids($params['members_ids']);
2021-01-28 20:07:14 +08:00
$user_id = $this->uid();
if (in_array($user_id, $params['members_ids'])) {
return $this->response->fail('群聊用户移除失败...');
}
[$isTrue, $record_id] = $this->groupService->removeMember($params['group_id'], $user_id, $params['members_ids']);
if (!$isTrue) {
return $this->response->fail('群聊用户移除失败...');
}
// 移出聊天室
foreach ($params['members_ids'] as $uid) {
$this->socketRoomService->delRoomMember($params['group_id'], $uid);
}
2021-03-26 22:03:11 +08:00
// ... 消息推送队列
2021-01-28 20:07:14 +08:00
$this->producer->produce(
new ChatMessageProducer(SocketConstants::EVENT_TALK, [
2021-03-26 22:03:11 +08:00
'sender' => $user_id, // 发送者ID
'receive' => (int)$params['group_id'], // 接收者ID
'source' => 2, // 接收者类型[1:好友;2:群组;]
2021-01-28 20:07:14 +08:00
'record_id' => $record_id
])
);
return $this->response->success([], '已成功退出群组...');
}
/**
* 获取群信息接口
*
* @RequestMapping(path="detail", methods="get")
*/
public function detail()
{
$group_id = $this->request->input('group_id', 0);
2021-03-26 22:03:11 +08:00
$user_id = $this->uid();
2021-01-28 20:07:14 +08:00
2021-03-26 22:03:11 +08:00
$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',
2021-03-26 22:03:11 +08:00
'group.profile',
'group.avatar',
'group.created_at',
2021-01-28 20:07:14 +08:00
'users.nickname'
]);
if (!$groupInfo) {
2021-03-26 22:03:11 +08:00
return $this->response->success();
2021-01-28 20:07:14 +08:00
}
$notice = GroupNotice::where('group_id', $group_id)
2021-01-28 20:07:14 +08:00
->where('is_delete', 0)
->orderBy('id', 'desc')
->first(['title', 'content']);
return $this->response->success([
2021-03-26 22:03:11 +08:00
'group_id' => $groupInfo->id,
'group_name' => $groupInfo->group_name,
'group_profile' => $groupInfo->profile,
'avatar' => $groupInfo->avatar,
'created_at' => $groupInfo->created_at,
'is_manager' => $groupInfo->creator_id == $user_id,
2021-01-28 20:07:14 +08:00
'manager_nickname' => $groupInfo->nickname,
2021-03-26 22:03:11 +08:00
'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() : []
2021-01-28 20:07:14 +08:00
]);
}
/**
* 设置群名片
*
* @RequestMapping(path="set-group-card", methods="post")
*/
2021-03-26 22:03:11 +08:00
public function editGroupCard()
2021-01-28 20:07:14 +08:00
{
$params = $this->request->inputs(['group_id', 'visit_card']);
$this->validate($params, [
2021-03-26 22:03:11 +08:00
'group_id' => 'required|integer',
2021-01-28 20:07:14 +08:00
'visit_card' => 'required|max:20'
]);
$isTrue = GroupMember::where('group_id', $params['group_id'])
2021-01-28 20:07:14 +08:00
->where('user_id', $this->uid())
2021-03-26 22:03:11 +08:00
->update(['user_card' => $params['visit_card']]);
2021-01-28 20:07:14 +08:00
return $isTrue
? $this->response->success([], '群名片修改成功...')
: $this->response->error('群名片修改失败...');
}
/**
* 获取可邀请加入群组的好友列表
*
* @RequestMapping(path="invite-friends", methods="get")
*/
public function getInviteFriends()
{
$group_id = $this->request->input('group_id', 0);
$friends = UsersFriend::getUserFriends($this->uid());
2021-01-28 20:07:14 +08:00
if ($group_id > 0 && $friends) {
if ($ids = GroupMember::getGroupMemberIds($group_id)) {
2021-01-28 20:07:14 +08:00
foreach ($friends as $k => $item) {
if (in_array($item['id'], $ids)) unset($friends[$k]);
}
}
$friends = array_values($friends);
}
return $this->response->success($friends);
}
/**
* 获取群组列表
*
* @RequestMapping(path="list", methods="get")
*/
public function getGroups()
{
return $this->response->success(
$this->groupService->getGroups($this->uid())
);
}
/**
* 获取群组成员列表
*
* @RequestMapping(path="members", methods="get")
*/
public function getGroupMembers()
{
$user_id = $this->uid();
2021-01-28 20:07:14 +08:00
$group_id = $this->request->input('group_id', 0);
// 判断用户是否是群成员
if (!Group::isMember($group_id, $user_id)) {
2021-01-28 20:07:14 +08:00
return $this->response->fail('非法操作...');
}
$members = GroupMember::select([
2021-03-26 22:03:11 +08:00
'group_member.id',
'group_member.leader',
'group_member.user_card',
'group_member.user_id',
'users.avatar',
'users.nickname',
'users.gender',
2021-01-28 20:07:14 +08:00
'users.motto',
])
->leftJoin('users', 'users.id', '=', 'group_member.user_id')
2021-01-28 20:07:14 +08:00
->where([
['group_member.group_id', '=', $group_id],
2021-03-26 22:03:11 +08:00
['group_member.is_quit', '=', 0],
])->orderBy('leader', 'desc')->get()->toArray();
2021-01-28 20:07:14 +08:00
return $this->response->success($members);
}
/**
* 获取群组公告列表
*
* @RequestMapping(path="notices", methods="get")
*/
2021-03-26 22:03:11 +08:00
public function getGroupNotice()
2021-01-28 20:07:14 +08:00
{
$user_id = $this->uid();
2021-01-28 20:07:14 +08:00
$group_id = $this->request->input('group_id', 0);
// 判断用户是否是群成员
if (!Group::isMember($group_id, $user_id)) {
2021-01-28 20:07:14 +08:00
return $this->response->fail('非管理员禁止操作...');
}
2021-03-26 22:03:11 +08:00
$rows = GroupNotice::leftJoin('users', 'users.id', '=', 'group_notice.creator_id')
2021-01-28 20:07:14 +08:00
->where([
['group_notice.group_id', '=', $group_id],
['group_notice.is_delete', '=', 0]
2021-01-28 20:07:14 +08:00
])
2021-03-26 22:03:11 +08:00
->orderBy('group_notice.is_top', 'desc')
->orderBy('group_notice.updated_at', 'desc')
2021-01-28 20:07:14 +08:00
->get([
'group_notice.id',
2021-03-26 22:03:11 +08:00
'group_notice.creator_id',
'group_notice.title',
'group_notice.content',
2021-03-26 22:03:11 +08:00
'group_notice.is_top',
'group_notice.is_confirm',
'group_notice.confirm_users',
'group_notice.created_at',
'group_notice.updated_at',
2021-03-26 22:03:11 +08:00
'users.avatar',
'users.nickname',
2021-01-28 20:07:14 +08:00
])->toArray();
return $this->response->success($rows);
}
/**
* 创建/编辑群公告
*
* @RequestMapping(path="edit-notice", methods="post")
*/
public function editNotice()
{
2021-03-26 22:03:11 +08:00
$params = $this->request->inputs(['group_id', 'notice_id', 'title', 'content', 'is_top', 'is_confirm']);
2021-01-28 20:07:14 +08:00
$this->validate($params, [
2021-03-26 22:03:11 +08:00
'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'
2021-01-28 20:07:14 +08:00
]);
$user_id = $this->uid();
// 判断用户是否是管理员
if (!Group::isManager($user_id, $params['group_id'])) {
2021-01-28 20:07:14 +08:00
return $this->response->fail('非管理员禁止操作...');
}
// 判断是否是新增数据
2021-03-26 22:03:11 +08:00
if (empty($params['notice_id'])) {
$result = GroupNotice::create([
2021-03-26 22:03:11 +08:00
'group_id' => $params['group_id'],
'creator_id' => $user_id,
'title' => $params['title'],
'content' => $params['content'],
'is_top' => $params['is_top'],
'is_confirm' => $params['is_confirm'],
2021-01-28 20:07:14 +08:00
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
if (!$result) {
return $this->response->fail('添加群公告信息失败...');
}
2021-03-26 22:03:11 +08:00
// ... 推送群消息(预留)
2021-01-28 20:07:14 +08:00
return $this->response->success([], '添加群公告信息成功...');
}
2021-03-26 22:03:11 +08:00
$result = GroupNotice::where('id', $params['notice_id'])->update([
'title' => $params['title'],
'content' => $params['content'],
'is_top' => $params['is_top'],
2021-01-28 20:07:14 +08:00
'updated_at' => date('Y-m-d H:i:s')
]);
return $result
? $this->response->success([], '修改群公告信息成功...')
: $this->response->fail('修改群公告信息成功...');
}
/**
* 删除群公告(软删除)
*
* @RequestMapping(path="delete-notice", methods="post")
*/
public function deleteNotice()
{
$params = $this->request->inputs(['group_id', 'notice_id']);
$this->validate($params, [
2021-03-26 22:03:11 +08:00
'group_id' => 'required|integer',
2021-01-28 20:07:14 +08:00
'notice_id' => 'required|integer'
]);
$user_id = $this->uid();
// 判断用户是否是管理员
if (!Group::isManager($user_id, $params['group_id'])) {
2021-01-28 20:07:14 +08:00
return $this->response->fail('非法操作...');
}
$result = GroupNotice::where('id', $params['notice_id'])
2021-01-28 20:07:14 +08:00
->where('group_id', $params['group_id'])
->update([
2021-03-26 22:03:11 +08:00
'is_delete' => 1,
2021-01-28 20:07:14 +08:00
'deleted_at' => date('Y-m-d H:i:s')
]);
return $result
? $this->response->success([], '公告删除成功...')
: $this->response->fail('公告删除失败...');
}
}