Skip to content

Commit

Permalink
feat: 🎸 插件接口请求添加验证码验证支持
Browse files Browse the repository at this point in the history
  • Loading branch information
aoaostar committed Oct 7, 2022
1 parent f9a0f42 commit 4aa5297
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 8 deletions.
7 changes: 6 additions & 1 deletion app/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,11 @@ function npm_cdn($file = '')
return $config_get . $file;
}

function captcha_api()
{
return (string)url('/api/captcha');
}

if (!function_exists('str_starts_with')) {
function str_starts_with($str, $start)
{
Expand Down Expand Up @@ -477,4 +482,4 @@ function get_enabled_oauth_mode()
}
return $arr;
}
}
}
6 changes: 3 additions & 3 deletions app/controller/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace app\controller;


use think\facade\View;

class Plugin extends Base
Expand All @@ -13,13 +12,14 @@ public function api()
$method = plugin_method_get();
$class = '\\plugin\\' . plugin_class_get($alias) . '\\App';
if (!class_exists($class)) {
return msg("error", "该Api不存在");
return error('该Api不存在');
}
$app = new $class();

if (!method_exists($app, $method)) {
return msg("error", "该Api不存在该方法");
return error('该Api不存在该方法');
}

return $app->$method();
}

Expand Down
30 changes: 30 additions & 0 deletions app/middleware/RateLimit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare (strict_types=1);

namespace app\middleware;


use plugin\CheckCaptcha;
use think\facade\Validate;

class RateLimit
{
public function handle($request, \Closure $next)
{

$class = '\\plugin\\' . plugin_class_get(plugin_alias_get()) . '\\App';

if (new $class() instanceof CheckCaptcha && $class::CHECK_CAPTCHA) {
try {
Validate::failException(true)->check([
'captcha' => request()->param('captcha')
], [
'captcha|验证码' => 'require|captcha'
]);
} catch (\Exception $e) {
return error($e->getMessage());
}
}
return $next($request);
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"ext-iconv": "*",
"ext-openssl": "*",
"firebase/php-jwt": "^6.2",
"ext-fileinfo": "*"
"ext-fileinfo": "*",
"topthink/think-captcha": "^3.0"
},
"require-dev": {
"symfony/var-dumper": "^4.2",
Expand Down
39 changes: 39 additions & 0 deletions config/captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
// +----------------------------------------------------------------------
// | Captcha配置文件
// +----------------------------------------------------------------------

return [
//验证码位数
'length' => 5,
// 验证码字符集合
'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
// 验证码过期时间
'expire' => 1800,
// 是否使用中文验证码
'useZh' => false,
// 是否使用算术验证码
'math' => true,
// 是否使用背景图
'useImgBg' => false,
//验证码字符大小
'fontSize' => 25,
// 是否使用混淆曲线
'useCurve' => true,
//是否添加杂点
'useNoise' => true,
// 验证码字体 不设置则随机
'fontttf' => '',
//背景颜色
'bg' => [243, 251, 254],
// 验证码图片高度
'imageH' => 0,
// 验证码图片宽度
'imageW' => 0,

// 添加额外的验证码设置
// verify => [
// 'length'=>4,
// ...
//],
];
9 changes: 9 additions & 0 deletions plugin/CheckCaptcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php


namespace plugin;

interface CheckCaptcha
{
const CHECK_CAPTCHA = true;
}
5 changes: 3 additions & 2 deletions route/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

use think\facade\Route;



Route::group('api', function () {
Route::any('plugins', 'Plugin/all');
Route::any('categories', 'Category/all');
Route::any('record', 'Plugin/record');
Route::any('captcha', function (){
return captcha();
});
})->prefix('api.');


Expand Down
3 changes: 2 additions & 1 deletion route/plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use think\facade\Route;

Route::rule('api/:alias/[:method]', "Plugin/api")
->middleware(\app\middleware\PluginCheck::class, true);
->middleware(\app\middleware\PluginCheck::class, true)
->middleware(\app\middleware\RateLimit::class);


Route::get(':alias/logo', 'Plugin/logo');
Expand Down

0 comments on commit 4aa5297

Please # to comment.