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

315 lines
10 KiB
PHP
Raw Normal View History

2020-11-04 11:57:16 +08:00
<?php
2020-12-26 21:33:40 +08:00
/**
* 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
*/
2021-04-20 16:30:57 +08:00
2020-11-04 11:57:16 +08:00
namespace App\Controller\Api\V1;
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-05-20 22:23:48 +08:00
use App\Cache\UnreadTalk;
2021-07-08 19:30:03 +08:00
use App\Constants\TalkMessageType;
2021-07-20 23:12:18 +08:00
use App\Constants\TalkModeConstant;
2021-07-12 22:00:48 +08:00
use App\Model\Talk\TalkList;
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-11 22:09:23 +08:00
use App\Model\User;
use App\Model\Group\Group;
2020-11-09 22:59:25 +08:00
use App\Service\TalkService;
2021-07-08 23:44:43 +08:00
use App\Service\TalkListService;
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
* @var TalkListService
*/
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
*/
public function list()
{
$user_id = $this->uid();
// 读取用户的未读消息列表
2021-05-20 22:23:48 +08:00
if ($list = UnreadTalk::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
* 新增对话列表
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="create", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2021-07-12 22:00:48 +08:00
public function create(UserFriendService $service)
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-11 00:12:25 +08:00
$lock = 'talk:list:' . $user_id . '-' . $params['receiver_id'] . '-' . $params['talk_type'];
if (!LockRedis::getInstance()->lock($lock, 10)) {
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
2021-07-12 22:00:48 +08:00
$data = TalkList::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;
$data['unread_num'] = UnreadTalk::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
* 删除对话列表
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="delete", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function delete()
{
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
* 对话列表置顶
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="topping", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function topping()
{
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
* 设置消息免打扰状态
2021-07-05 23:28:14 +08:00
* @RequestMapping(path="disturb", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2021-07-05 23:28:14 +08:00
public function disturb()
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
* 更新对话列表未读数
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="update-unread-num", methods="post")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
2020-11-12 22:41:18 +08:00
public function updateUnreadNum()
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-05 21:52:44 +08:00
UnreadTalk::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-12 22:41:18 +08:00
* 获取对话面板中的聊天记录
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getChatRecords()
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['talk_type', 'receiver_id', 'record_id']);
2020-11-12 22:41:18 +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',
'record_id' => 'required|integer|min:0',
2020-11-12 22:41:18 +08:00
]);
$user_id = $this->uid();
2021-07-20 23:12:18 +08:00
if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) {
2021-07-05 21:52:44 +08:00
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
2020-11-12 22:41:18 +08:00
}
2020-11-09 22:59:25 +08:00
2021-07-05 21:52:44 +08:00
$limit = 30;
2020-11-12 22:41:18 +08:00
$result = $this->talkService->getChatRecords(
$user_id,
2021-07-05 21:52:44 +08:00
$params['receiver_id'],
$params['talk_type'],
2020-11-12 22:41:18 +08:00
$params['record_id'],
$limit
);
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => $result,
2020-11-29 14:44:11 +08:00
'record_id' => $result ? end($result)['id'] : 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 获取转发记录详情
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="get-forward-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getForwardRecords()
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['record_id']);
2020-11-12 22:41:18 +08:00
$this->validate($params, [
2021-07-05 21:52:44 +08:00
'record_id' => 'required|integer|min:1'
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
2021-07-05 21:52:44 +08:00
$rows = $this->talkService->getForwardRecords($this->uid(), $params['record_id']);
2020-11-12 22:41:18 +08:00
return $this->response->success(['rows' => $rows]);
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 查询聊天记录
2020-11-09 22:59:25 +08:00
* @RequestMapping(path="find-chat-records", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function findChatRecords()
{
2021-07-05 21:52:44 +08:00
$params = $this->request->inputs(['talk_type', 'receiver_id', 'record_id', 'msg_type']);
2020-11-12 22:41:18 +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',
'record_id' => 'required|integer|min:0',
'msg_type' => 'required|integer',
2020-11-12 22:41:18 +08:00
]);
$user_id = $this->uid();
2021-07-20 23:12:18 +08:00
if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !Group::isMember((int)$params['receiver_id'], $user_id)) {
2021-07-05 21:52:44 +08:00
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
2020-11-12 22:41:18 +08:00
}
2021-07-05 21:52:44 +08:00
$types = [
2021-07-08 19:30:03 +08:00
TalkMessageType::TEXT_MESSAGE,
TalkMessageType::FILE_MESSAGE,
TalkMessageType::FORWARD_MESSAGE,
TalkMessageType::CODE_MESSAGE
2021-07-05 21:52:44 +08:00
];
if (in_array($params['msg_type'], $types)) {
2020-11-12 22:41:18 +08:00
$msg_type = [$params['msg_type']];
} else {
2021-07-05 21:52:44 +08:00
$msg_type = $types;
2020-11-12 22:41:18 +08:00
}
2020-11-09 22:59:25 +08:00
2021-07-05 21:52:44 +08:00
$limit = 30;
2020-11-12 22:41:18 +08:00
$result = $this->talkService->getChatRecords(
$user_id,
2021-07-05 21:52:44 +08:00
$params['receiver_id'],
$params['talk_type'],
2020-11-12 22:41:18 +08:00
$params['record_id'],
$limit,
$msg_type
);
return $this->response->success([
2021-04-20 16:30:57 +08:00
'rows' => $result,
2020-11-29 14:44:11 +08:00
'record_id' => $result ? end($result)['id'] : 0,
2021-04-20 16:30:57 +08:00
'limit' => $limit
2020-11-12 22:41:18 +08:00
]);
2020-11-09 22:59:25 +08:00
}
2020-11-04 11:57:16 +08:00
}