diff --git a/app/admin/controller/LoginController.php b/app/admin/controller/LoginController.php index 90c778a..e330ecd 100644 --- a/app/admin/controller/LoginController.php +++ b/app/admin/controller/LoginController.php @@ -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() + ]); + } } diff --git a/app/admin/controller/SiteSettingsController.php b/app/admin/controller/SiteSettingsController.php index 1b9e53f..57cbd66 100644 --- a/app/admin/controller/SiteSettingsController.php +++ b/app/admin/controller/SiteSettingsController.php @@ -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])) { diff --git a/app/admin/route/routes/base.php b/app/admin/route/routes/base.php index afca590..8679f35 100644 --- a/app/admin/route/routes/base.php +++ b/app/admin/route/routes/base.php @@ -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'); + diff --git a/app/utils/Geetest.php b/app/utils/Geetest.php new file mode 100644 index 0000000..85ee834 --- /dev/null +++ b/app/utils/Geetest.php @@ -0,0 +1,86 @@ +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; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index e677309..daf5139 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ }, "autoload": { "psr-4": { - "app\\": "app" + "app\\": "app", + "geetest\\": "extend/geetest/" }, "psr-0": { "": "extend/" diff --git a/composer.lock b/composer.lock index 8749f47..cfc19bc 100644 --- a/composer.lock +++ b/composer.lock @@ -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" }