import { http, Method } from '@/utils/request.js'; import storage from "@/utils/storage.js"; import api from "@/config/api.js"; /** * 获取当前地址天气(使用腾讯地图获取当前经纬度坐标) * @param {Object} params 查询参数 * @param {number} params.latitude 纬度 * @param {number} params.longitude 经度 * @param {string} params.type 非必填 查询天气类型,取值: * now[默认] 实时天气预报 * future 未来天气预报(默认获取当天和未来3天的天气信息) * hours 未来24小时天气预报(起始时间为当前时间的前一个小时) * @param {string} params.get_md 非必填 未来预报天数,仅在type=future时生效,取值: * 0 [默认]当天加未来3天的天气情况 * 1 当天加未来6天的天气情况 * @param {string} params.added_fields 非必填 附加字段,取值: * alarm 预警信息,仅在type=now时生效。 * index 生活指数信息,仅在type=future时生效 * air 空气质量信息 * @returns {Promise} 返回天气查询结果 * status: number - 状态码,0为正常,其它为异常 * message: string - 对status的描述 * result: object - 天气查询结果 * realtime: object - 实时天气信息,type=now时返回 * forecast: array - 预报天气信息,type=future时返回 */ export function getWeatherInfo(params = {}) { const { latitude, longitude, type = 'now', added_fields = '' } = params; // 腾讯地图天气API的key const TENCENT_MAP_KEY = '2N6BZ-VX5LJ-4GCFA-DGPXT-F4ZNF-7CB5D'; // 构建location参数:纬度,经度 const location = `${latitude},${longitude}`; // 判断是否为H5环境,H5环境使用代理避免跨域 let baseUrl; // #ifdef H5 baseUrl = '/tencent-map-api'; // #endif // #ifndef H5 baseUrl = 'https://apis.map.qq.com'; // #endif // 构建完整的URL let url = `${baseUrl}/ws/weather/v1?key=${TENCENT_MAP_KEY}&location=${location}`; // 添加可选参数 if (type) { url += `&type=${type}`; } if (added_fields) { url += `&added_fields=${added_fields}`; } console.log('天气API请求URL:', url); return new Promise((resolve, reject) => { uni.request({ url: url, method: 'GET', success: (res) => { console.log('天气API响应:', res); if (res.statusCode === 200 && res.data.status === 0) { resolve(res.data); } else { reject(res.data || { message: '天气查询失败' }); } }, fail: (err) => { console.error('天气API请求失败:', err); reject(err); } }); }); } /** * 获取uni-app定位信息 * @returns {Promise} 返回定位结果 */ export function getLocation() { return new Promise((resolve, reject) => { uni.getLocation({ type: 'gcj02', // 返回国测局坐标,适用于腾讯地图 success: (res) => { resolve({ latitude: res.latitude, longitude: res.longitude, accuracy: res.accuracy }); }, fail: (err) => { reject(err); } }); }); }