Skip to content

偶现不带 country 参数的请求,会把国内经纬度识别为国外 #100

Description

@sddpljx

美区 Apple ID (或者区域设置为美国)+ 简体中文状态下,经纬度为国内地点,偶尔会出现 URL 不带 country的情况。初步判断可能是 iOS 小组件/watchOS 小组件请求天气时会遇到。

现象

例如在出现请求该位置:32.390,119.499时,下方的 GET 后缀中没有 country 参数,只有 zh-Hans-US

GET /api/v2/weather/zh-Hans-US/32.390/119.499?timezone=Asia/Shanghai&dataSets=forecastHourly,forecastDaily,forecastNextHour,currentWeather

日志:

language: zh-Hans  country: US
CurrentAirQuality  Unsupported country: US
Minutely           Unsupported country: US
colorfulCloudsComparison  无法从彩云天气获取今日的空气质量数据
WeatherKit2.encode.compile  失败=[forecastNextHour must be an object, weatherAlerts must be an object]

可能的原因

parseWeatherKitURL 在缺少 country 查询参数时,用 locale 末段当国家。zh-Hans-USUS

可能的代码修改方法

country` 优先级改为:查询参数 → 坐标推断(CN/HK/MO/TW)→ locale

src/function/parseWeatherKitURL.mjs

             result.latitude = parameters?.latitude;
             result.longitude = parameters?.longitude;
-            result.country = url.searchParams.get("country")?.toUpperCase() || localeCountry;
+            result.country = url.searchParams.get("country")?.toUpperCase() || countryFromCoordinates(parameters?.latitude, parameters?.longitude) || localeCountry;
             result.dataSets = url.searchParams.get("dataSets")?.split(",") || [];
             break;
         }
     }
     Console.info("✅ parseWeatherKitURL", `🟧version: ${result.version} 🟧language: ${result.language} 🟧country: ${result.country}`, `🟧latitude: ${result.latitude} 🟧longitude: ${result.longitude}`);
     return result;
 }
+
+function countryFromCoordinates(latitude, longitude) {
+    const lat = Number(latitude);
+    const lon = Number(longitude);
+    if (!Number.isFinite(lat) || !Number.isFinite(lon)) return;
+    if (lat >= 22.13 && lat <= 22.58 && lon >= 113.82 && lon <= 114.41) return "HK";
+    if (lat >= 22.09 && lat <= 22.25 && lon >= 113.52 && lon <= 113.65) return "MO";
+    if (lat >= 21.7 && lat <= 25.4 && lon >= 119.3 && lon <= 122.1) return "TW";
+    if (lat >= 18 && lat <= 54 && lon >= 73 && lon <= 135) return "CN";
+}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions