debug
This commit is contained in:
98
code/app/api/BaseController.php
Normal file
98
code/app/api/BaseController.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\api;
|
||||
|
||||
use ddl\DDLManage;
|
||||
use think\App;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\View;
|
||||
use think\Validate;
|
||||
|
||||
/**
|
||||
* 控制器基础类
|
||||
*/
|
||||
abstract class BaseController
|
||||
{
|
||||
/**
|
||||
* Request实例
|
||||
* @var \think\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* 应用实例
|
||||
* @var \think\App
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* 是否批量验证
|
||||
* @var bool
|
||||
*/
|
||||
protected $batchValidate = false;
|
||||
|
||||
/**
|
||||
* 控制器中间件
|
||||
* @var array
|
||||
*/
|
||||
protected $middleware = [];
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @access public
|
||||
* @param App $app 应用对象
|
||||
*/
|
||||
public function __construct(App $app)
|
||||
{
|
||||
|
||||
$this->app = $app;
|
||||
$this->request = $this->app->request;
|
||||
|
||||
// 控制器初始化
|
||||
$this->initialize();
|
||||
|
||||
// View::assign('')
|
||||
}
|
||||
|
||||
// 初始化
|
||||
protected function initialize() {}
|
||||
|
||||
/**
|
||||
* 验证数据
|
||||
* @access protected
|
||||
* @param array $data 数据
|
||||
* @param string|array $validate 验证器名或者验证规则数组
|
||||
* @param array $message 提示信息
|
||||
* @param bool $batch 是否批量验证
|
||||
* @return array|string|true
|
||||
* @throws ValidateException
|
||||
*/
|
||||
protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false)
|
||||
{
|
||||
if (is_array($validate)) {
|
||||
$v = new Validate();
|
||||
$v->rule($validate);
|
||||
} else {
|
||||
if (strpos($validate, '.')) {
|
||||
// 支持场景
|
||||
[$validate, $scene] = explode('.', $validate);
|
||||
}
|
||||
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
|
||||
$v = new $class();
|
||||
if (!empty($scene)) {
|
||||
$v->scene($scene);
|
||||
}
|
||||
}
|
||||
|
||||
$v->message($message);
|
||||
|
||||
// 是否批量验证
|
||||
if ($batch || $this->batchValidate) {
|
||||
$v->batch(true);
|
||||
}
|
||||
|
||||
return $v->failException(true)->check($data);
|
||||
}
|
||||
}
|
||||
2
code/app/api/common.php
Normal file
2
code/app/api/common.php
Normal file
@@ -0,0 +1,2 @@
|
||||
<?php
|
||||
// 这是系统自动生成的公共文件
|
||||
33
code/app/api/config/router.php
Normal file
33
code/app/api/config/router.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use app\api\controller\Novel;
|
||||
use app\api\controller\Statis;
|
||||
use think\facade\Route;
|
||||
|
||||
|
||||
Route::post("/publish", [Statis::class, 'publish'])
|
||||
// ->middleware(\think\middleware\Throttle::class, [
|
||||
// 'visit_rate' => '1/m',
|
||||
// 'key' => '__CONTROLLER__/__ACTION__/__IP__',
|
||||
// ])
|
||||
->name('Statis@publish');
|
||||
|
||||
|
||||
Route::get("/search", [Novel::class, 'search'])
|
||||
// ->middleware(\think\middleware\Throttle::class, [
|
||||
// 'visit_rate' => '1/m',
|
||||
// 'key' => '__CONTROLLER__/__ACTION__/__IP__',
|
||||
// ])
|
||||
->name('Novel@search');;
|
||||
|
||||
|
||||
|
||||
|
||||
Route::get("/test", [Statis::class, 'test'])
|
||||
// ->middleware(\think\middleware\Throttle::class, [
|
||||
// 'visit_rate' => '1/m',
|
||||
// 'key' => '__CONTROLLER__/__ACTION__/__IP__',
|
||||
// ])
|
||||
->name('Statis@test');
|
||||
12
code/app/api/config/throttle.php
Normal file
12
code/app/api/config/throttle.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use think\middleware\Throttle;
|
||||
use think\Response;
|
||||
|
||||
return [
|
||||
'prefix' => 'api:throttle_',
|
||||
'visit_method' => ["GET", "POST", "PUT", "DELETE", "HEAD"],
|
||||
'visit_fail_response' => function (Throttle $throttle, $request, $wait_seconds) {
|
||||
return Response::create('抱歉,请求过于频繁, 请 ' . $wait_seconds . ' 秒后重新操作 。')->code(429);
|
||||
},
|
||||
];
|
||||
44
code/app/api/controller/Statis.php
Normal file
44
code/app/api/controller/Statis.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
use app\api\BaseController;
|
||||
use app\model\CategoryModel;
|
||||
use app\model\NovelClicksModel;
|
||||
use app\model\NovelModel;
|
||||
use DateTime;
|
||||
use db\mongo\MongoBase;
|
||||
use ddl\DDLManage;
|
||||
use Exception;
|
||||
use think\Request;
|
||||
use think\Response;
|
||||
|
||||
class Statis extends BaseController
|
||||
{
|
||||
/**
|
||||
* novel statis
|
||||
*
|
||||
* @param Request $Request
|
||||
* @return Response
|
||||
*/
|
||||
public function publish(Request $Request): Response
|
||||
{
|
||||
$intNId = $Request->post('n_id/d');
|
||||
|
||||
if ($intNId <= 0) {
|
||||
return Response::create('非法请求!')->code(400);
|
||||
}
|
||||
|
||||
$NovelClicksModel = NovelClicksModel::getInstance();
|
||||
|
||||
try {
|
||||
$NovelClicksModel->addStatis($intNId);
|
||||
} catch (\Exception $e) {
|
||||
// throw $e;
|
||||
}
|
||||
|
||||
return Response::create()->code(204);
|
||||
}
|
||||
}
|
||||
5
code/app/api/event.php
Normal file
5
code/app/api/event.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
// 这是系统自动生成的event定义文件
|
||||
return [
|
||||
|
||||
];
|
||||
5
code/app/api/middleware.php
Normal file
5
code/app/api/middleware.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
// 这是系统自动生成的middleware定义文件
|
||||
return [
|
||||
\think\middleware\AllowCrossDomain::class,
|
||||
];
|
||||
Reference in New Issue
Block a user