2023-01-29 10:26:52 +08:00

82 lines
2.0 KiB
PHP

<?php
namespace app\api\controller;
use app\api\exception\ApiException;
use app\api\service\SearchService;
use app\common\exception\EnumCode;
use app\api\validate\SearchValidate;
use think\Hook;
use think\Request;
class Search
{
protected $searchService;
protected $searchValidate;
public function __construct(SearchService $searchService, SearchValidate $searchValidate)
{
$this->searchService = $searchService;
$this->searchValidate = $searchValidate;
}
/**
* 搜索首页 function
*
* @author dotdotdot <6383846@qq.com>
* @date 2022-08-10
* @return array
*/
public function index()
{
$data = $this->searchService->index();
return json(['code' => EnumCode::Success, 'msg' => 'ok', 'data' => $data]);
}
/**
* 搜索 function
*
* @author dotdotdot <6383846@qq.com>
* @date 2022-08-10
* @param Request $request
* @return array
*/
public function search(Request $request)
{
$actionAllowed = ['method' => 'search_', 'key' => getDecodeTokenData()['uid'], 'period' => 1];
Hook::listen('action_allowed', $actionAllowed);
$params = [
'keywords' => $request->param('keywords', ''),
'page' => $request->param('page', 1),
'limit' => $request->param('limit', 20)
];
if (!$this->searchValidate->scene('search')->check($params)) {
throw new ApiException((string)$this->searchValidate->getError(), EnumCode::ValidateError);
}
$data = $this->searchService->search($params);
return json(['code' => EnumCode::Success, 'msg' => 'ok', 'data' => $data]);
}
/**
* 短剧排行首页 function
*
* @author dotdotdot <6383846@qq.com>
* @date 2022-08-10
* @return array
*/
public function popularRank()
{
$data = $this->searchService->popularRank();
return json(['code' => EnumCode::Success, 'msg' => 'ok', 'data' => $data]);
}
}