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