Fix: 强制Flex横排三列显示,修复GeoLite2本地库返回英文省市名改为中文翻译
This commit is contained in:
@@ -68,17 +68,42 @@ class UserController extends Controller
|
||||
$data['login_ip'] = $targetUser->login_ip; // 假设表中存在 login_ip 记录本次IP,若无则使用 last_ip 退化
|
||||
|
||||
// 解析归属地 (防崩溃处理:检查服务提供者是否已安装)
|
||||
// GeoLite2 本地 .mmdb 仅返回英文,这里使用内置翻译表转为中文
|
||||
$locationMap = [
|
||||
'China' => '中国', 'Beijing' => '北京', 'Shanghai' => '上海',
|
||||
'Tianjin' => '天津', 'Chongqing' => '重庆', 'Hebei' => '河北',
|
||||
'Shanxi' => '山西', 'Liaoning' => '辽宁', 'Jilin' => '吉林',
|
||||
'Heilongjiang' => '黑龙江', 'Jiangsu' => '江苏', 'Zhejiang' => '浙江',
|
||||
'Anhui' => '安徽', 'Fujian' => '福建', 'Jiangxi' => '江西',
|
||||
'Shandong' => '山东', 'Henan' => '河南', 'Hubei' => '湖北',
|
||||
'Hunan' => '湖南', 'Guangdong' => '广东', 'Hainan' => '海南',
|
||||
'Sichuan' => '四川', 'Guizhou' => '贵州', 'Yunnan' => '云南',
|
||||
'Shaanxi' => '陕西', 'Gansu' => '甘肃', 'Qinghai' => '青海',
|
||||
'Taiwan' => '台湾', 'Inner Mongolia' => '内蒙古', 'Guangxi' => '广西',
|
||||
'Tibet' => '西藏', 'Ningxia' => '宁夏', 'Xinjiang' => '新疆',
|
||||
'Hong Kong' => '香港', 'Macau' => '澳门',
|
||||
'United States' => '美国', 'Japan' => '日本', 'South Korea' => '韩国',
|
||||
'United Kingdom' => '英国', 'France' => '法国', 'Germany' => '德国',
|
||||
'Russia' => '俄罗斯', 'Canada' => '加拿大', 'Australia' => '澳大利亚',
|
||||
'Singapore' => '新加坡',
|
||||
];
|
||||
$tr = fn(string $name): string => $locationMap[$name] ?? $name;
|
||||
|
||||
$ipToLookup = $targetUser->login_ip ?: $targetUser->last_ip;
|
||||
if ($ipToLookup) {
|
||||
if (class_exists('\Stevebauman\Location\Facades\Location')) {
|
||||
try {
|
||||
$position = Location::get($ipToLookup);
|
||||
if ($position) {
|
||||
$isChinaOrLocal = in_array($position->countryCode ?? $position->isoCode ?? '', ['CN', 'TW', 'HK', 'MO']) || $position->countryName === 'Local';
|
||||
$data['location'] = $isChinaOrLocal ?
|
||||
trim(($position->regionName ?? '') . ' ' . ($position->cityName ?? '')) :
|
||||
($position->countryName ?? '未知区域');
|
||||
|
||||
$isChinaOrLocal = in_array($position->countryCode ?? '', ['CN', 'TW', 'HK', 'MO'])
|
||||
|| $position->countryName === 'Local';
|
||||
if ($isChinaOrLocal) {
|
||||
$region = $tr($position->regionName ?? '');
|
||||
$city = $tr($position->cityName ?? '');
|
||||
$data['location'] = trim($region . ($region !== $city ? ' ' . $city : ''));
|
||||
} else {
|
||||
$data['location'] = $tr($position->countryName ?? '未知区域');
|
||||
}
|
||||
if (empty($data['location'])) {
|
||||
$data['location'] = '未知区域';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user