Fix: 修正ip2region调用方式,改用getIpInfo()兼容各版本,并正确传入xdb路径
This commit is contained in:
@@ -71,24 +71,29 @@ class UserController extends Controller
|
|||||||
$ipToLookup = $targetUser->login_ip ?: $targetUser->last_ip;
|
$ipToLookup = $targetUser->login_ip ?: $targetUser->last_ip;
|
||||||
if ($ipToLookup) {
|
if ($ipToLookup) {
|
||||||
try {
|
try {
|
||||||
$ip2r = new \Ip2Region(database_path('ip2region/ip2region.xdb'));
|
$xdbPath = database_path('ip2region/ip2region.xdb');
|
||||||
$raw = $ip2r->search($ipToLookup); // 格式: 国家|省份|城市|运营商
|
// 第一个参数为缓存策略,第二个为自定义 IPv4 数据库路径
|
||||||
|
$ip2r = new \Ip2Region('file', $xdbPath);
|
||||||
|
$info = $ip2r->getIpInfo($ipToLookup);
|
||||||
|
|
||||||
if ($raw) {
|
if ($info) {
|
||||||
$parts = explode('|', $raw);
|
$country = $info['country'] ?? '';
|
||||||
// 去掉 "0" 占位符并组合省市
|
$province = $info['province'] ?? '';
|
||||||
$parts = array_filter($parts, fn($p) => $p !== '0' && $p !== '');
|
$city = $info['city'] ?? '';
|
||||||
$parts = array_values($parts);
|
|
||||||
$country = $parts[0] ?? '';
|
// 过滤掉占位符 "0"
|
||||||
$region = $parts[1] ?? '';
|
$province = ($province === '0') ? '' : $province;
|
||||||
$city = $parts[2] ?? '';
|
$city = ($city === '0') ? '' : $city;
|
||||||
|
|
||||||
if ($country === '中国') {
|
if ($country === '中国') {
|
||||||
// 国内:显示 省份 城市(如果省市不同则都显示)
|
$data['location'] = trim($province . ($province !== $city ? ' ' . $city : ''));
|
||||||
$data['location'] = trim($region . ($region !== $city ? ' ' . $city : ''));
|
|
||||||
} else {
|
} else {
|
||||||
$data['location'] = $country ?: '未知区域';
|
$data['location'] = $country ?: '未知区域';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (empty($data['location'])) {
|
||||||
|
$data['location'] = '未知区域';
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
$data['location'] = '未知区域';
|
$data['location'] = '未知区域';
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user