From 4d9f8dca8fb39e5a38815dd165b6fd5e066c3b0f Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Sat, 2 Mar 2024 15:48:03 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=82=E6=95=B0=20?= =?UTF-8?q?=E5=85=81=E8=AE=B8=E5=85=B3=E9=97=ADipinfo=E8=8E=B7=E5=8F=96ip?= =?UTF-8?q?=E5=9C=B0=E7=90=86=E4=BD=8D=E7=BD=AE=E6=9C=8D=E5=8A=A1=20?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=A7=81.env.example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + config/params.php | 1 + utils/IPLocation.php | 10 +++++++++- views/user/info.php | 5 ++--- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index 1033e44..ca888ee 100644 --- a/.env.example +++ b/.env.example @@ -18,4 +18,5 @@ TURNSTILE_SECRET=1x0000000000000000000000000000000AA # Turnstile Secret Key # Te COOKIE_VALIDATION_KEY= # Cookie Validation Key # IP地理位置配置 +ENABLE_IPINFO= # 是否启用ipinfo.io [boolean] IPINFO_TOKEN= # ipinfo.io token \ No newline at end of file diff --git a/config/params.php b/config/params.php index 80f1f95..63f13a4 100644 --- a/config/params.php +++ b/config/params.php @@ -19,5 +19,6 @@ return [ 'siteKey' => $_ENV['TURNSTILE_SITE_KEY'], 'secret' => $_ENV['TURNSTILE_SECRET'], ], + 'enableIpInfo' => $_ENV['ENABLE_IPINFO'] === 'true', 'ipinfoToken' => $_ENV['IPINFO_TOKEN'], ]; diff --git a/utils/IPLocation.php b/utils/IPLocation.php index 9b78b55..d0a4ee0 100644 --- a/utils/IPLocation.php +++ b/utils/IPLocation.php @@ -10,14 +10,22 @@ use Yii; class IPLocation { private IPinfo $client; + private bool $is_disabled = true; public function __construct() { - $this->client = new IPinfo(Yii::$app->params['ipinfoToken']); + $status = Yii::$app->params['enableIpInfo']; + if($status){ + $this->is_disabled = false; + $this->client = new IPinfo(Yii::$app->params['ipinfoToken']); + } } public static function getDetails(string $ip): ?Details { $instance = new self(); + if($instance->is_disabled){ + return null; + } try { return $instance->client->getDetails($ip); } catch (IPinfoException $e) { diff --git a/views/user/info.php b/views/user/info.php index 451b944..1d37009 100644 --- a/views/user/info.php +++ b/views/user/info.php @@ -14,7 +14,7 @@ FontAwesomeAsset::register($this); $this->registerCssFile('@web/css/user-info.css'); $details = IPLocation::getDetails($model->last_login_ip); if (is_null($details)) { - echo ''; + echo ''; } ?> @@ -54,8 +54,7 @@ if (is_null($details)) {

上次登录IP

last_login_ip) ?> - (bogon ? ('Bogon IP') : ($details->city . ', ' . $details->country))) ?> - ) + bogon ? ('Bogon IP') : ($details->city . ', ' . $details->country)) . ')') ?>