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

398 lines
13 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-07-20 23:12:18 +08:00
use App\Constants\TalkModeConstant;
2021-07-10 00:18:40 +08:00
use App\Service\GroupNoticeService;
use App\Service\TalkListService;
2021-07-07 19:43:09 +08:00
use App\Service\UserService;
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;
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-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
]);
2021-07-10 00:18:40 +08:00
[$isTrue, $group] = $this->groupService->create($this->uid(), [
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'] ?? ''
2021-07-10 00:18:40 +08:00
], parse_ids($params['uids']));
2021-01-28 20:07:14 +08:00
2021-07-10 00:18:40 +08:00
if (!$isTrue) return $this->response->fail('创建群聊失败,请稍后再试!');
2021-01-28 20:07:14 +08:00
return $this->response->success([
2021-07-10 00:18:40 +08:00
'group_id' => $group->id
2021-01-28 20:07:14 +08:00
]);
}
/**
* 解散群组接口
* @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
}
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
]);
2021-07-10 00:18:40 +08:00
$isTrue = $this->groupService->invite($this->uid(), $params['group_id'], parse_ids($params['uids']));
2021-01-28 20:07:14 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('邀请好友加入群聊失败!');
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'
]);
2021-07-10 00:18:40 +08:00
if (!$this->groupService->quit($this->uid(), $params['group_id'])) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('退出群组失败!');
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-07-10 00:18:40 +08:00
return $this->groupService->update($params['group_id'], $this->uid(), $params)
2021-01-28 20:07:14 +08:00
? $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
}
2021-07-10 00:18:40 +08:00
$isTrue = $this->groupService->removeMember($params['group_id'], $user_id, $params['members_ids']);
2021-01-28 20:07:14 +08:00
if (!$isTrue) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('群聊用户移除失败!');
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
*/
2021-07-10 00:18:40 +08:00
public function detail(TalkListService $service)
2021-01-28 20:07:14 +08:00
{
$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'
]);
2021-07-10 00:18:40 +08:00
if (!$groupInfo) return $this->response->success();
2021-01-28 20:07:14 +08:00
2021-07-10 00:18:40 +08:00
$notice = GroupNotice::where('group_id', $group_id)->where('is_delete', 0)->orderBy('id', 'desc')->first(['title', 'content']);
2021-01-28 20:07:14 +08:00
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-20 23:12:18 +08:00
'is_disturb' => (int)$service->isDisturb($user_id, $group_id, TalkModeConstant::GROUP_CHAT),
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'
]);
2021-07-10 00:18:40 +08:00
$isTrue = $this->groupService->updateMemberCard($params['group_id'], $this->uid(), $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(
2021-07-10 00:18:40 +08:00
$this->groupService->getUserGroups($this->uid())
2021-01-28 20:07:14 +08:00
);
}
/**
* 获取群组成员列表
* @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-07-10 00:18:40 +08:00
public function getGroupNotice(GroupNoticeService $service)
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-07-10 00:18:40 +08:00
return $this->response->success($service->lists($group_id));
2021-01-28 20:07:14 +08:00
}
/**
* 创建/编辑群公告
* @RequestMapping(path="edit-notice", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2021-01-28 20:07:14 +08:00
*/
2021-07-10 00:18:40 +08:00
public function editNotice(GroupNoticeService $service)
2021-01-28 20:07:14 +08:00
{
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'])) {
2021-07-10 00:18:40 +08:00
if (!$service->create($user_id, $params)) {
2021-05-13 18:01:34 +08:00
return $this->response->fail('添加群公告信息失败!');
2021-01-28 20:07:14 +08:00
}
return $this->response->success([], '添加群公告信息成功...');
}
2021-07-10 00:18:40 +08:00
return $service->update($params)
2021-01-28 20:07:14 +08:00
? $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
*/
2021-07-10 00:18:40 +08:00
public function deleteNotice(GroupNoticeService $service)
2021-01-28 20:07:14 +08:00
{
$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'
]);
2021-07-10 00:18:40 +08:00
try {
$isTrue = $service->delete($params['notice_id'], $this->uid());
} catch (\Exception $e) {
return $this->response->fail($e->getMessage());
2021-01-28 20:07:14 +08:00
}
2021-07-10 00:18:40 +08:00
return $isTrue
2021-01-28 20:07:14 +08:00
? $this->response->success([], '公告删除成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('公告删除失败!');
2021-01-28 20:07:14 +08:00
}
}