From c3cddc2f24b97f39a8181c336d74e17d8d575a7e Mon Sep 17 00:00:00 2001 From: kingc2022 Date: Sun, 30 Jul 2023 19:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=AD=BE=E5=88=B0=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/TrafficController.php | 45 ++++++++++++++----- config/captcha.php | 5 +++ package.json | 1 + resources/js/views/Sign.vue | 15 +++++-- yarn.lock | 12 +++++ 5 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 config/captcha.php diff --git a/app/Http/Controllers/Api/TrafficController.php b/app/Http/Controllers/Api/TrafficController.php index 567dea3..236de5b 100644 --- a/app/Http/Controllers/Api/TrafficController.php +++ b/app/Http/Controllers/Api/TrafficController.php @@ -4,6 +4,8 @@ use App\Http\Controllers\Controller; use App\Support\WHMCS; +use GuzzleHttp\Client; +use GuzzleHttp\Exception\GuzzleException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Cache; @@ -103,7 +105,7 @@ public function free() ]); } - public function sign() + public function sign(Request $request) { $user = auth()->user(); @@ -115,19 +117,38 @@ public function sign() return $this->error('您已经签到过了,请明天再来吧。'); } - // 随机 config('settings.sign.min') 到 config('settings.sign.max') - $traffic = rand(config('settings.sign.min'), config('settings.sign.max')); - - if ($traffic !== -1) { - $user->traffic += $traffic; - $user->save(); + $recaptcha = $request->post("recaptcha"); + $http = new Client; + try { + $check = $http->post("https://www.recaptcha.net/recaptcha/api/siteverify", [ + 'form_params' => [ + "secret" => config("captcha.secret"), + "response" => $recaptcha, + ], +// 'verify' => false, + ])->getBody(); + } catch (GuzzleException $e) { + return $this->error("与验证码 API 通信失败"); } - Cache::put('traffic_sign:' . $day . '-' . $user->id, true); + $check = json_decode($check); + if ($check->success) { + // 随机 config('settings.sign.min') 到 config('settings.sign.max') + $traffic = rand(config('settings.sign.min'), config('settings.sign.max')); - return $this->success([ - 'traffic' => $traffic, - 'last_sign_at' => now()->toDateTimeString(), - ]); + if ($traffic !== -1) { + $user->traffic += $traffic; + $user->save(); + } + + Cache::put('traffic_sign:' . $day . '-' . $user->id, true); + + return $this->success([ + 'traffic' => $traffic, + 'last_sign_at' => now()->toDateTimeString(), + ]); + } else { + return $this->error("验证码校验失败"); + } } } diff --git a/config/captcha.php b/config/captcha.php new file mode 100644 index 0000000..16be4d5 --- /dev/null +++ b/config/captcha.php @@ -0,0 +1,5 @@ + env("RECAPTCHA_SECRET"), +]; diff --git a/package.json b/package.json index 9316867..75fd046 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "humanize-plus": "^1.8.2", "vue": "^3.2.36", "vue-axios": "^3.5.2", + "vue-recaptcha": "2.0.3", "vue-router": "^4.0.13" } } diff --git a/resources/js/views/Sign.vue b/resources/js/views/Sign.vue index 260c95d..91b9594 100644 --- a/resources/js/views/Sign.vue +++ b/resources/js/views/Sign.vue @@ -6,7 +6,13 @@

当前流量: {{ traffic.traffic }}GB

今日已签到
- +

完成验证码以签到

+
@@ -14,6 +20,7 @@