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

149 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Controller\Api\V1\Talk;
use App\Constants\TalkMessageType;
use App\Constants\TalkModeConstant;
use App\Controller\Api\V1\CController;
use App\Service\Group\GroupMemberService;
use App\Service\TalkListService;
use App\Service\TalkService;
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;
/**
* Class RecordController
*
* @Controller(prefix="/api/v1/talk/records")
* @Middleware(JWTAuthMiddleware::class)
*
* @package App\Controller\Api\V1\Talk
*/
class RecordsController extends CController
{
/**
* @Inject
* @var TalkService
*/
public $talkService;
/**
* @Inject
* @var TalkListService
*/
public $talkListService;
/**
* 获取对话面板中的聊天记录
*
* @RequestMapping(path="", methods="get")
*/
public function records(): ResponseInterface
{
$params = $this->request->inputs(['talk_type', 'receiver_id', 'record_id']);
$this->validate($params, [
'talk_type' => 'required|in:1,2',
'receiver_id' => 'required|integer|min:1',
'record_id' => 'required|integer|min:0',
]);
$user_id = $this->uid();
if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember((int)$params['receiver_id'], $user_id)) {
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
}
$limit = 30;
$result = $this->talkService->getChatRecords(
$user_id,
intval($params['receiver_id']),
intval($params['talk_type']),
intval($params['record_id']),
$limit
);
return $this->response->success([
'rows' => $result,
'record_id' => $result ? end($result)['id'] : 0,
'limit' => $limit
]);
}
/**
* 查询聊天历史记录
*
* @RequestMapping(path="history", methods="get")
*/
public function history(): ResponseInterface
{
$params = $this->request->inputs(['talk_type', 'receiver_id', 'record_id', 'msg_type']);
$this->validate($params, [
'talk_type' => 'required|in:1,2',
'receiver_id' => 'required|integer|min:1',
'record_id' => 'required|integer|min:0',
'msg_type' => 'required|integer',
]);
$user_id = $this->uid();
if ($params['talk_type'] == TalkModeConstant::GROUP_CHAT && !di()->get(GroupMemberService::class)->isMember((int)$params['receiver_id'], $user_id)) {
return $this->response->fail('暂不属于好友关系或群聊成员,无法查看聊天记录!');
}
$types = [
TalkMessageType::TEXT_MESSAGE,
TalkMessageType::FILE_MESSAGE,
TalkMessageType::FORWARD_MESSAGE,
TalkMessageType::CODE_MESSAGE,
TalkMessageType::VOTE_MESSAGE
];
if (in_array($params['msg_type'], $types)) {
$msg_type = [$params['msg_type']];
} else {
$msg_type = $types;
}
$limit = 30;
$result = $this->talkService->getChatRecords(
$user_id,
(int)$params['receiver_id'],
(int)$params['talk_type'],
(int)$params['record_id'],
$limit,
$msg_type
);
return $this->response->success([
'rows' => $result,
'record_id' => $result ? end($result)['id'] : 0,
'limit' => $limit
]);
}
/**
* 获取转发记录详情
*
* @RequestMapping(path="forward", methods="get")
*/
public function forwards(): ResponseInterface
{
$params = $this->request->inputs(['record_id']);
$this->validate($params, [
'record_id' => 'required|integer|min:1'
]);
$rows = $this->talkService->getForwardRecords($this->uid(), intval($params['record_id']));
return $this->response->success(['rows' => $rows]);
}
}