hyperf-chat/app/Controller/Api/V1/Talk/TalkController.php

200 lines
6.7 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2022-01-17 21:06:27 +08:00
declare(strict_types=1);
2021-04-20 16:30:57 +08:00
2022-01-17 21:06:27 +08:00
namespace App\Controller\Api\V1\Talk;
2020-11-04 11:57:16 +08:00
2021-05-20 22:23:48 +08:00
use App\Cache\LastMessage;
2021-07-11 00:12:25 +08:00
use App\Cache\Repository\LockRedis;
2021-07-21 23:31:41 +08:00
use App\Cache\UnreadTalkCache;
2022-01-22 20:08:19 +08:00
use App\Constant\TalkModeConstant;
2022-01-17 21:06:27 +08:00
use App\Controller\Api\V1\CController;
use App\Model\Group\Group;
2022-01-16 14:22:53 +08:00
use App\Model\Talk\TalkSession;
2022-01-17 21:06:27 +08:00
use App\Model\User;
2022-01-22 16:04:54 +08:00
use App\Service\TalkSessionService;
2022-01-17 21:06:27 +08:00
use App\Service\TalkService;
2021-07-12 22:00:48 +08:00
use App\Service\UserFriendService;
2021-07-06 23:32:14 +08:00
use App\Support\UserRelation;
2020-11-29 14:44:11 +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 Psr\Http\Message\ResponseInterface;
2020-11-04 11:57:16 +08:00
2020-11-09 22:59:25 +08:00
/**
* Class TalkController
2021-07-05 21:52:44 +08:00
* @Controller(prefix="/api/v1/talk")
2020-11-09 22:59:25 +08:00
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1
*/
2020-11-04 11:57:16 +08:00
class TalkController extends CController
{
2020-11-09 22:59:25 +08:00
/**
* @Inject
* @var TalkService
*/
public $talkService;
2021-07-08 23:44:43 +08:00
/**
* @Inject
2022-01-22 16:04:54 +08:00
* @var TalkSessionService
2021-07-08 23:44:43 +08:00
*/
public $talkListService;
2020-11-09 22:59:25 +08:00
/**
* 获取用户对话列表
* @RequestMapping(path="list", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2021-09-11 12:41:28 +08:00
public function list(): ResponseInterface
2020-11-09 22:59:25 +08:00
{
$user_id = $this->uid();
// 读取用户的未读消息列表
2021-07-21 23:31:41 +08:00
if ($list = UnreadTalkCache::getInstance()->reads($user_id)) {
2021-07-08 23:44:43 +08:00
foreach ($list as $friend_id => $num) {
2021-07-20 23:12:18 +08:00
$this->talkListService->create($user_id, $friend_id, TalkModeConstant::PRIVATE_CHAT);
2021-07-08 23:44:43 +08:00
}
2020-11-09 22:59:25 +08:00
}
2021-07-08 23:44:43 +08:00
return $this->response->success($this->talkListService->getTalkList($user_id));
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 新增对话列表
2022-01-17 21:06:27 +08:00
*
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="create", methods="post")
*/
2021-09-11 12:41:28 +08:00
public function create(UserFriendService $service): ResponseInterface
2020-11-09 22:59:25 +08:00
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['talk_type', 'receiver_id']);
2020-11-11 22:09:23 +08:00
$this->validate($params, [
2021-07-05 21:52:44 +08:00
'talk_type' => 'required|in:1,2',
'receiver_id' => 'required|integer|min:1'
2020-11-11 22:09:23 +08:00
]);
2020-11-09 22:59:25 +08:00
2020-11-11 22:09:23 +08:00
$user_id = $this->uid();
2021-07-30 23:23:48 +08:00
$string = join('-', [$user_id, $params['receiver_id'], $params['talk_type'], md5($this->request->getHeaderLine('user-agent'))]);
2021-07-21 23:31:41 +08:00
$lock = 'talk:list:' . $string;
// 防止前端并发请求
if (!LockRedis::getInstance()->lock($lock, 60)) {
2021-07-11 00:12:25 +08:00
return $this->response->fail('创建失败!');
}
2021-07-06 23:32:14 +08:00
if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) {
2021-07-11 00:12:25 +08:00
LockRedis::getInstance()->delete($lock);
2021-07-05 21:52:44 +08:00
return $this->response->fail('暂不属于好友关系或群聊成员,无法进行聊天!');
2020-11-09 22:59:25 +08:00
}
2021-07-08 23:44:43 +08:00
$result = $this->talkListService->create($user_id, $params['receiver_id'], $params['talk_type']);
2021-07-11 00:12:25 +08:00
if (!$result) {
LockRedis::getInstance()->delete($lock);
return $this->response->fail('创建失败!');
}
2020-11-09 22:59:25 +08:00
2022-01-16 14:22:53 +08:00
$data = TalkSession::item([
2021-04-20 16:30:57 +08:00
'id' => $result['id'],
2021-07-05 21:52:44 +08:00
'talk_type' => $result['talk_type'],
'receiver_id' => $result['receiver_id'],
2021-07-12 22:00:48 +08:00
]);
2020-11-09 22:59:25 +08:00
2021-07-20 23:12:18 +08:00
if ($result['talk_type'] == TalkModeConstant::PRIVATE_CHAT) {
2021-07-11 00:12:25 +08:00
$userInfo = User::where('id', $data['receiver_id'])->first(['nickname', 'avatar']);
$data['avatar'] = $userInfo->avatar;
$data['name'] = $userInfo->nickname;
2021-07-21 23:31:41 +08:00
$data['unread_num'] = UnreadTalkCache::getInstance()->read($data['receiver_id'], $user_id);
2021-07-12 22:00:48 +08:00
$data['remark_name'] = $service->getFriendRemark($user_id, (int)$data['receiver_id']);
2021-07-20 23:12:18 +08:00
} else if ($result['talk_type'] == TalkModeConstant::GROUP_CHAT) {
2021-07-05 21:52:44 +08:00
$groupInfo = Group::where('id', $data['receiver_id'])->first(['group_name', 'avatar']);
2021-04-20 16:30:57 +08:00
$data['name'] = $groupInfo->group_name;
2020-11-09 22:59:25 +08:00
$data['avatar'] = $groupInfo->avatar;
}
2021-07-05 21:52:44 +08:00
$records = LastMessage::getInstance()->read($result['talk_type'], $user_id, $result['receiver_id']);
2020-11-09 22:59:25 +08:00
if ($records) {
2021-04-20 16:30:57 +08:00
$data['msg_text'] = $records['text'];
2020-11-09 22:59:25 +08:00
$data['updated_at'] = $records['created_at'];
}
2021-07-05 21:52:44 +08:00
return $this->response->success($data);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 删除对话列表
2022-01-17 21:06:27 +08:00
*
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="delete", methods="post")
*/
2021-09-11 12:41:28 +08:00
public function delete(): ResponseInterface
2020-11-09 22:59:25 +08:00
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['list_id']);
$this->validate($params, [
'list_id' => 'required|integer|min:0'
]);
2021-07-08 23:44:43 +08:00
return $this->talkListService->delete($this->uid(), $params['list_id'])
2020-11-11 22:09:23 +08:00
? $this->response->success([], '对话列表删除成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('对话列表删除失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 对话列表置顶
2022-01-17 21:06:27 +08:00
*
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="topping", methods="post")
*/
2021-09-11 12:41:28 +08:00
public function topping(): ResponseInterface
2020-11-09 22:59:25 +08:00
{
2020-11-11 22:09:23 +08:00
$params = $this->request->inputs(['list_id', 'type']);
$this->validate($params, [
'list_id' => 'required|integer|min:0',
2021-04-20 16:30:57 +08:00
'type' => 'required|in:1,2',
2020-11-11 22:09:23 +08:00
]);
2021-07-08 23:44:43 +08:00
return $this->talkListService->top($this->uid(), $params['list_id'], $params['type'] == 1)
2020-11-20 19:17:11 +08:00
? $this->response->success([], '对话列表置顶(或取消置顶)成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('对话列表置顶(或取消置顶)失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 设置消息免打扰状态
2022-01-17 21:06:27 +08:00
*
2021-07-05 23:28:14 +08:00
* @RequestMapping(path="disturb", methods="post")
2020-11-09 22:59:25 +08:00
*/
2021-09-11 12:41:28 +08:00
public function disturb(): ResponseInterface
2020-11-09 22:59:25 +08:00
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['talk_type', 'receiver_id', 'is_disturb']);
2020-11-11 22:09:23 +08:00
$this->validate($params, [
2021-07-05 21:52:44 +08:00
'talk_type' => 'required|in:1,2',
'receiver_id' => 'required|integer|min:1',
'is_disturb' => 'required|in:0,1',
2020-11-11 22:09:23 +08:00
]);
2021-07-08 23:44:43 +08:00
return $this->talkListService->disturb($this->uid(), $params['receiver_id'], $params['talk_type'], $params['is_disturb'])
2020-11-21 22:47:21 +08:00
? $this->response->success([], '免打扰设置成功...')
2021-05-13 18:01:34 +08:00
: $this->response->fail('免打扰设置失败!');
2020-11-09 22:59:25 +08:00
}
/**
2020-11-11 22:09:23 +08:00
* 更新对话列表未读数
2022-01-17 21:06:27 +08:00
* @RequestMapping(path="unread/clear", methods="post")
2020-11-09 22:59:25 +08:00
*/
2021-09-11 12:41:28 +08:00
public function updateUnreadNum(): ResponseInterface
2020-11-09 22:59:25 +08:00
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['talk_type', 'receiver_id']);
2020-11-11 22:09:23 +08:00
$this->validate($params, [
2021-07-05 21:52:44 +08:00
'talk_type' => 'required|in:1,2',
'receiver_id' => 'required|integer|min:1',
2020-11-11 22:09:23 +08:00
]);
2020-11-09 22:59:25 +08:00
2020-11-11 22:09:23 +08:00
// 设置好友消息未读数
2021-07-20 23:12:18 +08:00
if ($params['talk_type'] == TalkModeConstant::PRIVATE_CHAT) {
2021-07-21 23:31:41 +08:00
UnreadTalkCache::getInstance()->reset((int)$params['receiver_id'], $this->uid());
2020-11-11 22:09:23 +08:00
}
2021-05-13 18:01:34 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
2020-11-04 11:57:16 +08:00
}