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

327 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;
use App\Cache\UnreadTalk;
2021-07-05 21:52:44 +08:00
use App\Constants\TalkMsgType;
use App\Constants\TalkType;
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;
2021-07-05 21:52:44 +08:00
use App\Model\TalkList;
use App\Model\Group\Group;
2020-11-09 22:59:25 +08:00
use App\Service\TalkService;
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;
/**
* 获取用户对话列表
* @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)) {
$this->talkService->updateUnreadTalkList($user_id, $list);
2020-11-09 22:59:25 +08:00
}
2021-05-22 21:45:18 +08:00
return $this->response->success($this->talkService->talks($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
*/
2020-11-12 22:41:18 +08:00
public function create()
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-06 23:32:14 +08:00
if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) {
2021-07-05 21:52:44 +08:00
return $this->response->fail('暂不属于好友关系或群聊成员,无法进行聊天!');
2020-11-09 22:59:25 +08:00
}
2021-07-05 21:52:44 +08:00
$result = TalkList::addItem($user_id, $params['receiver_id'], $params['talk_type']);
2021-07-06 23:32:14 +08:00
if (!$result) return $this->response->fail('创建失败!');
2020-11-09 22:59:25 +08:00
$data = [
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-04-20 16:30:57 +08:00
'is_top' => 0,
2021-07-05 21:52:44 +08:00
'is_disturb' => 0,
'is_online' => 1,
'avatar' => '',
2021-04-20 16:30:57 +08:00
'name' => '',
2020-11-09 22:59:25 +08:00
'remark_name' => '',
2021-04-20 16:30:57 +08:00
'unread_num' => 0,
2021-07-05 21:52:44 +08:00
'msg_text' => '',
2021-04-20 16:30:57 +08:00
'updated_at' => date('Y-m-d H:i:s')
2020-11-09 22:59:25 +08:00
];
2021-07-05 21:52:44 +08:00
if ($result['talk_type'] == TalkType::PRIVATE_CHAT) {
$userInfo = User::where('id', $user_id)->first(['nickname', 'avatar']);
2021-04-20 16:30:57 +08:00
$data['avatar'] = $userInfo->avatar;
2021-07-05 21:52:44 +08:00
$data['name'] = $userInfo->nickname;
$data['unread_num'] = UnreadTalk::getInstance()->read($data['receiver_id'], $user_id);
} else if ($result['talk_type'] == TalkType::GROUP_CHAT) {
$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-05 21:52:44 +08:00
return TalkList::delItem($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-05 21:52:44 +08:00
return TalkList::topItem($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-05 21:52:44 +08:00
$isTrue = TalkList::setNotDisturb($this->uid(), $params['receiver_id'], $params['talk_type'], $params['is_disturb']);
2020-11-11 22:09:23 +08:00
return $isTrue
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-05 21:52:44 +08:00
if ($params['talk_type'] == TalkType::PRIVATE_CHAT) {
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-06 23:32:14 +08:00
if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) {
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-06 23:32:14 +08:00
if (!UserRelation::isFriendOrGroupMember($user_id, $params['receiver_id'], $params['talk_type'])) {
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 = [
TalkMsgType::TEXT_MESSAGE,
TalkMsgType::FILE_MESSAGE,
TalkMsgType::FORWARD_MESSAGE,
TalkMsgType::CODE_MESSAGE
];
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-12 22:41:18 +08:00
* 搜索聊天记录(待开发)
2021-07-05 21:52:44 +08:00
* @RequestMapping(path="records-search", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function searchChatRecords()
{
2021-07-05 21:52:44 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
/**
2020-11-12 22:41:18 +08:00
* 获取聊天记录上下文数据(待开发)
2021-07-05 21:52:44 +08:00
* @RequestMapping(path="records-context", methods="get")
2021-04-22 16:14:34 +08:00
*
* @return ResponseInterface
2020-11-09 22:59:25 +08:00
*/
public function getRecordsContext()
{
2020-11-21 19:53:01 +08:00
return $this->response->success();
2020-11-09 22:59:25 +08:00
}
2020-11-04 11:57:16 +08:00
}