增加极验模块

This commit is contained in:
李志强 2026-02-26 17:38:11 +08:00
parent f80e1f71d4
commit 050d1adc0b
6 changed files with 273 additions and 90 deletions

View File

@ -287,4 +287,92 @@ class LoginController extends BaseController
'data' => $loginInfo
]);
}
/**
* 获取极验3.0的id和key
* @return Json
*/
public function getGeetest3Infos()
{
// 定义你需要的 label 列表
$targetLabels = [
'geetest3ID',
'geetest3KEY'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
->whereIn('label', $targetLabels) // 仅筛选指定的 label
->field('label, value')
->select();
$this->logSuccess('站点设置管理', '查看极验3.0的id和key', [], $this->getAdminUserInfo());
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $legalInfos->toArray()
]);
}
/**
* 获取极验4.0的id和key
* @return Json
*/
public function getGeetest4Infos()
{
// 定义你需要的 label 列表
$targetLabels = [
'geetest4ID',
'geetest4KEY'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
->whereIn('label', $targetLabels) // 仅筛选指定的 label
->field('label, value')
->select()
->toArray();
// 转换为前端需要的格式
$data = [];
foreach ($legalInfos as $item) {
if ($item['label'] === 'geetest4ID') {
$data['captcha_id'] = $item['value'];
}
if ($item['label'] === 'geetest4KEY') {
$data['captcha_key'] = $item['value'];
}
}
$this->logSuccess('站点设置管理', '查看极验4.0的id和key', [], $this->getAdminUserInfo());
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $data
]);
}
/**
* 判断是否开启验证
* @return Json
*/
public function getOpenVerify()
{
// 定义你需要的 label 列表
$targetLabels = [
'openVerify',
'verifyModel'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
->whereIn('label', $targetLabels) // 仅筛选指定的 label
->field('label, value')
->select();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => $legalInfos->toArray()
]);
}
}

View File

@ -77,8 +77,10 @@ class SiteSettingsController extends BaseController
$targetLabels = [
'openVerify',
'verifyModel',
'geetestID',
'geetestKEY'
'geetest3ID',
'geetest3KEY',
'geetest4ID',
'geetest4KEY'
];
$legalInfos = SystemSiteSettings::where('delete_time', null)
@ -102,7 +104,7 @@ class SiteSettingsController extends BaseController
public function saveVerifyInfos()
{
$params = $this->request->param();
$allowedLabels = ['openVerify', 'verifyModel', 'geetestID', 'geetestKEY'];
$allowedLabels = ['openVerify', 'verifyModel', 'geetest3ID', 'geetest3KEY', 'geetest4ID', 'geetest4KEY'];
foreach ($allowedLabels as $label) {
if (isset($params[$label])) {

View File

@ -13,3 +13,9 @@ Route::post('loginInfo', 'app\\admin\\controller\\LoginController@loginInfo');
Route::post('login', 'app\\admin\\controller\\LoginController@login');
Route::post('logout', 'app\\admin\\controller\\LoginController@logout');
Route::get('user/info', 'app\\admin\\controller\\LoginController@userInfo');
// 极验路由
Route::get('login/getGeetest3Infos', 'app\\admin\\controller\\LoginController@getGeetest3Infos');
Route::get('login/getGeetest4Infos', 'app\\admin\\controller\\LoginController@getGeetest4Infos');
Route::get('login/getOpenVerify', 'app\\admin\\controller\\LoginController@getOpenVerify');

86
app/utils/Geetest.php Normal file
View File

@ -0,0 +1,86 @@
<?php
namespace app\utils;
// 引入极验 SDK 的核心类
use geetest\GeetestLib;
use app\model\System\SystemSiteSettings;
class Geetest
{
/**
* 极验 3.0 ID KEY
* 优先从系统配置表 SystemSiteSettings 中读取 geetest3ID / geetest3KEY
*/
private $captchaId;
private $privateKey;
public function __construct()
{
// 默认值可以为空,避免硬编码
$this->captchaId = null;
$this->privateKey = null;
try {
// 从系统设置表中读取极验3.0配置
$settings = SystemSiteSettings::where('delete_time', null)
->whereIn('label', ['geetest3ID', 'geetest3KEY'])
->column('value', 'label');
if (!empty($settings['geetest3ID']) && !empty($settings['geetest3KEY'])) {
$this->captchaId = $settings['geetest3ID'];
$this->privateKey = $settings['geetest3KEY'];
}
} catch (\Throwable $e) {
// 读取配置失败时,不抛出致命错误,留给上层决定是否启用极验
error_log('加载极验配置失败: ' . $e->getMessage());
}
}
/**
* 初始化极验验证(前端获取验证参数)
* @return string JSON 字符串
*/
public function init()
{
$gtLib = new GeetestLib($this->captchaId, $this->privateKey);
// 用户唯一标识可传用户ID、IP等不能为空
$userId = request()->ip();
// 预处理验证
$status = $gtLib->pre_process($userId);
// 保存状态到 session
session('gtserver', $status);
session('gt_user_id', $userId);
// 返回前端需要的验证参数
return $gtLib->get_response_str();
}
/**
* 验证前端提交的极验数据
* @param array $data 前端提交的 geetest_challenge/validate/seccode
* @return bool 验证结果
*/
public function verify(array $data)
{
$gtLib = new GeetestLib($this->captchaId, $this->privateKey);
$userId = session('gt_user_id');
$status = session('gtserver');
if ($status == 1) {
// 正常模式验证
$result = $gtLib->success_validate(
$data['geetest_challenge'],
$data['geetest_validate'],
$data['geetest_seccode'],
$userId
);
} else {
// 宕机模式验证
$result = $gtLib->fail_validate(
$data['geetest_challenge'],
$data['geetest_validate'],
$data['geetest_seccode']
);
}
return $result;
}
}

View File

@ -34,7 +34,8 @@
},
"autoload": {
"psr-4": {
"app\\": "app"
"app\\": "app",
"geetest\\": "extend/geetest/"
},
"psr-0": {
"": "extend/"

172
composer.lock generated
View File

@ -608,6 +608,91 @@
},
"time": "2021-10-29T13:26:27+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.33.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493",
"shasum": ""
},
"require": {
"ext-iconv": "*",
"php": ">=7.2"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/polyfill",
"name": "symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-12-23T08:48:59+00:00"
},
{
"name": "topthink/framework",
"version": "v8.1.4",
@ -1037,91 +1122,6 @@
],
"time": "2024-09-25T14:21:43+00:00"
},
{
"name": "symfony/polyfill-mbstring",
"version": "v1.33.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-mbstring.git",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/6d857f4d76bd4b343eac26d6b539585d2bc56493",
"reference": "6d857f4d76bd4b343eac26d6b539585d2bc56493",
"shasum": ""
},
"require": {
"ext-iconv": "*",
"php": ">=7.2"
},
"provide": {
"ext-mbstring": "*"
},
"suggest": {
"ext-mbstring": "For best performance"
},
"type": "library",
"extra": {
"thanks": {
"url": "https://github.com/symfony/polyfill",
"name": "symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Mbstring\\": ""
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill for the Mbstring extension",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"mbstring",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-mbstring/tree/v1.33.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://github.com/nicolas-grekas",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2024-12-23T08:48:59+00:00"
},
{
"name": "symfony/var-dumper",
"version": "v6.4.32",
@ -1319,5 +1319,5 @@
"php": ">=8.0.0"
},
"platform-dev": {},
"plugin-api-version": "2.9.0"
"plugin-api-version": "2.6.0"
}