新增参数

允许关闭ipinfo获取ip地理位置服务
方法见.env.example
This commit is contained in:
Chenx221 2024-03-02 15:48:03 +08:00
parent 179f482c76
commit 4d9f8dca8f
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
4 changed files with 13 additions and 4 deletions

View File

@ -18,4 +18,5 @@ TURNSTILE_SECRET=1x0000000000000000000000000000000AA # Turnstile Secret Key # Te
COOKIE_VALIDATION_KEY= # Cookie Validation Key COOKIE_VALIDATION_KEY= # Cookie Validation Key
# IP地理位置配置 # IP地理位置配置
ENABLE_IPINFO= # 是否启用ipinfo.io [boolean]
IPINFO_TOKEN= # ipinfo.io token IPINFO_TOKEN= # ipinfo.io token

View File

@ -19,5 +19,6 @@ return [
'siteKey' => $_ENV['TURNSTILE_SITE_KEY'], 'siteKey' => $_ENV['TURNSTILE_SITE_KEY'],
'secret' => $_ENV['TURNSTILE_SECRET'], 'secret' => $_ENV['TURNSTILE_SECRET'],
], ],
'enableIpInfo' => $_ENV['ENABLE_IPINFO'] === 'true',
'ipinfoToken' => $_ENV['IPINFO_TOKEN'], 'ipinfoToken' => $_ENV['IPINFO_TOKEN'],
]; ];

View File

@ -10,14 +10,22 @@ use Yii;
class IPLocation class IPLocation
{ {
private IPinfo $client; private IPinfo $client;
private bool $is_disabled = true;
public function __construct() 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 public static function getDetails(string $ip): ?Details
{ {
$instance = new self(); $instance = new self();
if($instance->is_disabled){
return null;
}
try { try {
return $instance->client->getDetails($ip); return $instance->client->getDetails($ip);
} catch (IPinfoException $e) { } catch (IPinfoException $e) {

View File

@ -14,7 +14,7 @@ FontAwesomeAsset::register($this);
$this->registerCssFile('@web/css/user-info.css'); $this->registerCssFile('@web/css/user-info.css');
$details = IPLocation::getDetails($model->last_login_ip); $details = IPLocation::getDetails($model->last_login_ip);
if (is_null($details)) { if (is_null($details)) {
echo '<script>console.log("IP位置信息获取失败")</script>'; echo '<script>console.log("IP位置信息功能停用或存在内部错误")</script>';
} }
?> ?>
@ -54,8 +54,7 @@ if (is_null($details)) {
<p class="user-login-info-title">上次登录IP</p> <p class="user-login-info-title">上次登录IP</p>
<p class="user-login-info-content"> <p class="user-login-info-content">
<?= Html::encode($model->last_login_ip) ?> <?= Html::encode($model->last_login_ip) ?>
(<?= Html::encode(($details === null) ? '' : ($details->bogon ? ('Bogon IP') : ($details->city . ', ' . $details->country))) ?> <?= Html::encode(($details === null) ? '' : '(' . ($details->bogon ? ('Bogon IP') : ($details->city . ', ' . $details->country)) . ')') ?>
)
</p> </p>
</div> </div>
</div> </div>