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

179 lines
5.8 KiB
PHP
Raw Normal View History

2021-01-28 20:07:14 +08:00
<?php
namespace App\Controller\Api\V1;
2021-07-20 23:12:18 +08:00
use App\Constants\TalkModeConstant;
2021-07-28 23:08:07 +08:00
use App\Service\Group\GroupMemberService;
2021-07-10 00:18:40 +08:00
use App\Service\TalkListService;
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\GroupNotice;
2021-07-28 23:08:07 +08:00
use App\Service\Group\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;
2021-07-28 23:08:07 +08:00
/**
* @inject
* @var GroupMemberService
*/
private $groupMemberService;
2021-01-28 20:07:14 +08:00
/**
* 创建群组
* @RequestMapping(path="create", methods="post")
*/
2021-09-11 12:41:28 +08:00
public function create(): ResponseInterface
2021-01-28 20:07:14 +08:00
{
$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-09-11 12:41:28 +08:00
public function dismiss(): ResponseInterface
2021-01-28 20:07:14 +08:00
{
$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-09-11 12:41:28 +08:00
public function invite(): ResponseInterface
2021-01-28 20:07:14 +08:00
{
$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-09-11 12:41:28 +08:00
public function secede(): ResponseInterface
2021-01-28 20:07:14 +08:00
{
$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-09-11 12:41:28 +08:00
public function editDetail(): ResponseInterface
2021-01-28 20:07:14 +08:00
{
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="detail", methods="get")
*/
2021-09-11 12:41:28 +08:00
public function detail(TalkListService $service): ResponseInterface
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-07-28 23:08:07 +08:00
'visit_card' => $this->groupMemberService->getVisitCard($group_id, $user_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
]);
}
}