#pragma once #include #include /** * @brief WeatherApi类用于封装和风天气API调用 */ class WeatherApi { private: // 和风天气API配置参数 std::string api_host_; // API主机地址 std::string api_key_; // API密钥 std::string default_location_; // 默认城市位置 std::string kid_; // JWT认证ID std::string project_id_; // 项目ID std::string private_key_; // 私钥 // 简单缓存管理 std::unordered_map weather_cache_; // 天气数据缓存 std::unordered_map ip_info_cache_; // IP信息缓存 // 天气代码映射表,将API返回的天气代码转换为中文描述 static std::unordered_map WEATHER_CODE_MAP; /** * @brief 生成JWT令牌(预留接口) * @return JWT令牌字符串 */ std::string GenerateJwtToken(); /** * @brief 封装HTTP GET请求 * @param url 请求URL * @param headers 请求头 * @param response 响应内容 * @return 是否请求成功 */ bool HttpGet(const std::string& url, const std::string& headers, std::string& response); /** * @brief 获取城市信息 * @param location 城市名称 * @return 城市详细信息 */ std::string FetchCityInfo(const std::string& location, const std::string& lang); /** * @brief 获取IP位置信息 * @details 自动获取设备当前IP的地理位置信息,无需指定IP参数 * @return IP对应的地理位置信息 */ std::string GetIpInfo(); public: /** * @brief 构造函数,初始化配置参数 */ WeatherApi(); /** * @brief 获取当前默认城市 */ std::string GetDefaultLocation() const; /** * @brief 析构函数 */ ~WeatherApi() = default; /** * @brief 获取天气信息主函数 * @param location 城市名称 * @param lang 语言设置 * @return 格式化的天气信息 */ std::string GetWeather(const std::string& location, const std::string& lang); /** * @brief 自动检测并设置当前位置 * @details 调用太平洋IP查询API自动检测当前地理位置,并更新默认城市设置 */ void AutoDetectLocation();// 自动检测NVS中是否存在当前位置的城市信息,并设置当前位置 }; /** * @brief 全局函数,用于获取天气信息 * @param location 城市名称 * @param lang 语言设置 * @return 格式化的天气信息 */ extern std::string GetWeatherInfo(const std::string& location, const std::string& lang); /** * @brief 全局函数,用于自动检测并设置当前位置 * @details 供应用程序在网络连接后调用,自动检测地理位置并更新默认城市 */ extern void AutoDetectAndSetLocation(); /** * @brief 全局WeatherApi实例 */ extern WeatherApi g_weather_api;