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

533 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;
2021-05-21 22:56:42 +08:00
use App\Cache\SocketRoom;
2021-07-08 19:30:03 +08:00
use App\Constants\TalkMode;
2021-07-07 19:43:09 +08:00
use App\Service\UserService;
2021-07-08 19:09:06 +08:00
use App\Support\MessageProducer;
2021-01-28 20:07:14 +08:00
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use App\Middleware\JWTAuthMiddleware;
2021-07-05 21:52:44 +08:00
use App\Model\TalkList;
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\Service\GroupService;
2021-07-08 19:30:03 +08:00
use App\Constants\TalkMessageEvent;
2021-04-22 16:14:34 +08:00
use Psr\Http\Message\ResponseInterface;
2021-01-28 20:07:14 +08:00
/**
* Class GroupController
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/group")
2021-01-28 20:07:14 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
class GroupController extends CController
{
/**
* @Inject
* @var GroupService
*/
private $groupService;
/**
* 创建群组
* @RequestMapping(path="create", methods="post")
*
2021-04-22 16:14:34 +08:00
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('创建群聊失败,请稍后再试!');
2021-01-28 20:07:14 +08:00
}
// 加入聊天室
$friend_ids[] = $user_id;
foreach ($friend_ids as $uid) {
2021-05-21 22:56:42 +08:00
SocketRoom::getInstance()->addRoomMember(strval($data['group_id']), $uid);
2021-01-28 20:07:14 +08:00
}
2021-07-08 19:09:06 +08:00
MessageProducer::publish(
2021-07-08 19:30:03 +08:00
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
2021-07-08 19:09:06 +08:00
'sender_id' => $user_id,
'receiver_id' => (int)$data['group_id'],
2021-07-08 19:30:03 +08:00
'talk_type' => TalkMode::GROUP_CHAT,
2021-07-08 19:09:06 +08:00
'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")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('群组解散失败!');
2021-01-28 20:07:14 +08:00
}
2021-05-21 22:56:42 +08:00
SocketRoom::getInstance()->delRoom($params['group_id']);
2021-01-28 20:07:14 +08:00
2021-04-22 16:54:01 +08:00
// ... TODO 推送群消息(预留)
2021-01-28 20:07:14 +08:00
return $this->response->success([], '群组解散成功...');
}
/**
* 邀请好友加入群组接口
* @RequestMapping(path="invite", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('邀请好友加入群聊失败!');
2021-01-28 20:07:14 +08:00
}
// 加入聊天室
foreach ($uids as $uid) {
2021-05-21 22:56:42 +08:00
SocketRoom::getInstance()->addRoomMember($params['group_id'], $uid);
2021-01-28 20:07:14 +08:00
}
2021-07-08 19:09:06 +08:00
MessageProducer::publish(
2021-07-08 19:30:03 +08:00
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
2021-07-08 19:09:06 +08:00
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
2021-07-08 19:30:03 +08:00
'talk_type' => TalkMode::GROUP_CHAT,
2021-07-08 19:09:06 +08:00
'record_id' => $record_id
])
);
2021-01-28 20:07:14 +08:00
return $this->response->success([], '好友已成功加入群聊...');
}
/**
* 退出群组接口
* @RequestMapping(path="secede", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('退出群组失败!');
2021-01-28 20:07:14 +08:00
}
// 移出聊天室
2021-05-21 22:56:42 +08:00
SocketRoom::getInstance()->delRoomMember($params['group_id'], $user_id);
2021-01-28 20:07:14 +08:00
2021-07-08 19:09:06 +08:00
MessageProducer::publish(
2021-07-08 19:30:03 +08:00
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
2021-07-08 19:09:06 +08:00
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
2021-07-08 19:30:03 +08:00
'talk_type' => TalkMode::GROUP_CHAT,
2021-07-08 19:09:06 +08:00
'record_id' => $record_id
])
);
2021-01-28 20:07:14 +08:00
return $this->response->success([], '已成功退出群组...');
}
/**
* 编辑群组信息
* @RequestMapping(path="edit", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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-04-22 16:54:01 +08:00
// ... TODO 推送消息通知(预留)
2021-01-28 20:07:14 +08:00
return $result
? $this->response->success([], '群组信息修改成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('群组信息修改失败!');
2021-01-28 20:07:14 +08:00
}
/**
* 移除指定成员(管理员权限)
* @RequestMapping(path="remove-members", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('群聊用户移除失败!');
2021-01-28 20:07:14 +08:00
}
[$isTrue, $record_id] = $this->groupService->removeMember($params['group_id'], $user_id, $params['members_ids']);
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('群聊用户移除失败!');
2021-01-28 20:07:14 +08:00
}
// 移出聊天室
foreach ($params['members_ids'] as $uid) {
2021-05-21 22:56:42 +08:00
SocketRoom::getInstance()->delRoomMember($params['group_id'], strval($uid));
2021-01-28 20:07:14 +08:00
}
2021-07-08 19:09:06 +08:00
MessageProducer::publish(
2021-07-08 19:30:03 +08:00
MessageProducer::create(TalkMessageEvent::EVENT_TALK, [
2021-07-08 19:09:06 +08:00
'sender_id' => $user_id,
'receiver_id' => (int)$params['group_id'],
2021-07-08 19:30:03 +08:00
'talk_type' => TalkMode::GROUP_CHAT,
2021-07-08 19:09:06 +08:00
'record_id' => $record_id
])
);
2021-01-28 20:07:14 +08:00
return $this->response->success([], '已成功退出群组...');
}
/**
* 获取群信息接口
* @RequestMapping(path="detail", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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,
2021-07-05 21:52:44 +08:00
'profile' => $groupInfo->profile,
2021-03-26 22:03:11 +08:00
'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),
2021-07-08 19:30:03 +08:00
'is_disturb' => (int)TalkList::where('user_id', $user_id)->where('receiver_id', $group_id)->where('talk_type', TalkMode::GROUP_CHAT)->value('is_disturb'),
2021-03-26 22:03:11 +08:00
'notice' => $notice ? $notice->toArray() : []
2021-01-28 20:07:14 +08:00
]);
}
/**
* 设置群名片
* @RequestMapping(path="set-group-card", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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([], '群名片修改成功...')
2021-05-13 18:01:34 +08:00
: $this->response->error('群名片修改失败!');
2021-01-28 20:07:14 +08:00
}
/**
* 获取可邀请加入群组的好友列表
* @RequestMapping(path="invite-friends", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
2021-07-07 19:43:09 +08:00
public function getInviteFriends(UserService $service)
2021-01-28 20:07:14 +08:00
{
$group_id = $this->request->input('group_id', 0);
2021-07-07 19:43:09 +08:00
$friends = $service->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")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
public function getGroups()
{
return $this->response->success(
$this->groupService->getGroups($this->uid())
);
}
/**
* 获取群组成员列表
* @RequestMapping(path="members", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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-05-13 18:01:34 +08:00
return $this->response->fail('非法操作!');
2021-01-28 20:07:14 +08:00
}
$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-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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-05-13 18:01:34 +08:00
return $this->response->fail('非管理员禁止操作!');
2021-01-28 20:07:14 +08:00
}
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")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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-05-13 18:01:34 +08:00
return $this->response->fail('非管理员禁止操作!');
2021-01-28 20:07:14 +08:00
}
// 判断是否是新增数据
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) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('添加群公告信息失败!');
2021-01-28 20:07:14 +08:00
}
2021-04-22 16:54:01 +08:00
// ... TODO 推送群消息(预留)
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([], '修改群公告信息成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('修改群公告信息失败!');
2021-01-28 20:07:14 +08:00
}
/**
* 删除群公告(软删除)
* @RequestMapping(path="delete-notice", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
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-05-13 18:01:34 +08:00
return $this->response->fail('非法操作!');
2021-01-28 20:07:14 +08:00
}
$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([], '公告删除成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('公告删除失败!');
2021-01-28 20:07:14 +08:00
}
}