新增参数
允许关闭ipinfo获取ip地理位置服务 方法见.env.example
This commit is contained in:
parent
179f482c76
commit
4d9f8dca8f
@ -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
|
@ -19,5 +19,6 @@ return [
|
||||
'siteKey' => $_ENV['TURNSTILE_SITE_KEY'],
|
||||
'secret' => $_ENV['TURNSTILE_SECRET'],
|
||||
],
|
||||
'enableIpInfo' => $_ENV['ENABLE_IPINFO'] === 'true',
|
||||
'ipinfoToken' => $_ENV['IPINFO_TOKEN'],
|
||||
];
|
||||
|
@ -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) {
|
||||
|
@ -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 '<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-content">
|
||||
<?= 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>
|
||||
</div>
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user