B2B网络软件

标题: 苹果iOS脚本天气信息查询工具 [打印本页]

作者: YYPOST群发软件    时间: 1 小时前
标题: 苹果iOS脚本天气信息查询工具

苹果iOS脚本天气信息查询工具
苹果iOS脚本天气信息查询工具 B2B网络软件


  1. /**
  2. * AIWROK软件苹果iOS - 天气信息查询工具
  3. * 交流QQ群711841924 | 苹果内测QQ群648461709
  4. *
  5. * 功能特点:
  6. * 1. 支持多个城市查询
  7. * 2. 自动重试机制
  8. * 3. 友好的格式化输出
  9. * 4. 完整的错误处理
  10. */

  11. // =============================================================================
  12. // 配置区域
  13. // =============================================================================

  14. var WEATHER_CONFIG = {
  15.     // 默认查询城市(可以修改为你所在的城市)
  16.     defaultCity: "北京",
  17.    
  18.     // API超时时间(毫秒)
  19.     timeout: 5000,
  20.    
  21.     // 最大重试次数
  22.     maxRetries: 3
  23. };

  24. // =============================================================================
  25. // 核心功能函数
  26. // =============================================================================

  27. /**
  28. * 获取指定城市的天气信息
  29. * @param {String} city - 城市名称
  30. * @returns {Object|null} 天气数据对象或null
  31. */
  32. function getWeatherInfo(city) {
  33.     printl("🌤️ 正在查询 [" + city + "] 的天气信息...");
  34.    
  35.     var http = new OkHttp();
  36.    
  37.     // 设置超时时间
  38.     http.setTimeout(WEATHER_CONFIG.timeout, WEATHER_CONFIG.timeout, WEATHER_CONFIG.timeout);
  39.    
  40.     // 设置请求头
  41.     http.setHeader("User-Agent", "AIWROK-Weather/1.0");
  42.     http.setHeader("Accept", "application/json");
  43.     http.setCharset("UTF-8");
  44.    
  45.     // 使用免费的天气API(Open-Meteo,无需API Key)
  46.     // 注意:这里需要先获取城市坐标,为简化示例,我们使用固定坐标
  47.     var coordinates = getCityCoordinates(city);
  48.    
  49.     if (!coordinates) {
  50.         printl("❌ 未找到城市 [" + city + "] 的坐标信息");
  51.         return null;
  52.     }
  53.    
  54.     var apiUrl = "https://api.open-meteo.com/v1/forecast?latitude=" +
  55.                  coordinates.lat + "&longitude=" + coordinates.lon +
  56.                  "¤t=temperature_2m,relative_humidity_2m,apparent_temperature," +
  57.                  "weather_code,wind_speed_10m&daily=temperature_2m_max,temperature_2m_min" +
  58.                  "&timezone=auto&forecast_days=3";
  59.    
  60.     try {
  61.         printl("📡 发送请求到天气服务器...");
  62.         var startTime = new Date().getTime();
  63.         var response = http.get(apiUrl);
  64.         var endTime = new Date().getTime();
  65.         var duration = endTime - startTime;
  66.         
  67.         if (!response || response.length === 0) {
  68.             printl("❌ 服务器返回空响应");
  69.             return null;
  70.         }
  71.         
  72.         printl("⏱️ 响应时间: " + duration + "ms");
  73.         
  74.         // 解析JSON响应
  75.         var weatherData = JSON.parse(response);
  76.         
  77.         if (!weatherData || !weatherData.current) {
  78.             printl("❌ 响应数据格式异常");
  79.             return null;
  80.         }
  81.         
  82.         return {
  83.             city: city,
  84.             current: weatherData.current,
  85.             daily: weatherData.daily,
  86.             fetchTime: new Date().toLocaleString()
  87.         };
  88.         
  89.     } catch (error) {
  90.         var errorMsg = error.toString();
  91.         printl("❌ 请求失败: " + errorMsg.substring(0, 100));
  92.         
  93.         if (errorMsg.indexOf('timeout') > -1) {
  94.             printl("  原因: 连接超时,请检查网络");
  95.         } else if (errorMsg.indexOf('SSL') > -1) {
  96.             printl("  原因: SSL证书问题");
  97.         }
  98.         
  99.         return null;
  100.     }
  101. }

  102. /**
  103. * 获取城市坐标(简化版,仅包含常见城市)
  104. * @param {String} city - 城市名称
  105. * @returns {Object|null} 包含lat和lon的对象
  106. */
  107. function getCityCoordinates(city) {
  108.     var cityMap = {
  109.         "北京": { lat: 39.9042, lon: 116.4074 },
  110.         "上海": { lat: 31.2304, lon: 121.4737 },
  111.         "广州": { lat: 23.1291, lon: 113.2644 },
  112.         "深圳": { lat: 22.5431, lon: 114.0579 },
  113.         "成都": { lat: 30.5728, lon: 104.0668 },
  114.         "杭州": { lat: 30.2741, lon: 120.1551 },
  115.         "武汉": { lat: 30.5928, lon: 114.3055 },
  116.         "西安": { lat: 34.3416, lon: 108.9398 },
  117.         "南京": { lat: 32.0603, lon: 118.7969 },
  118.         "重庆": { lat: 29.4316, lon: 106.9123 }
  119.     };
  120.    
  121.     return cityMap[city] || null;
  122. }

  123. /**
  124. * 格式化天气代码为中文描述
  125. * @param {Number} code - WMO天气代码
  126. * @returns {String} 天气描述
  127. */
  128. function formatWeatherCode(code) {
  129.     var weatherMap = {
  130.         0: "晴朗 ☀️",
  131.         1: "主要晴朗 🌤️",
  132.         2: "部分多云 ⛅",
  133.         3: "阴天 ☁️",
  134.         45: "雾 🌫️",
  135.         48: "雾凇 🌫️",
  136.         51: "毛毛雨 🌦️",
  137.         53: "中度毛毛雨 🌧️",
  138.         55: "大毛毛雨 🌧️",
  139.         61: "小雨 🌧️",
  140.         63: "中雨 🌧️",
  141.         65: "大雨 🌧️",
  142.         71: "小雪 ❄️",
  143.         73: "中雪 ❄️",
  144.         75: "大雪 ❄️",
  145.         95: "雷雨 ⛈️",
  146.         96: "雷阵雨伴冰雹 ⛈️",
  147.         99: "强雷阵雨伴冰雹 ⛈️"
  148.     };
  149.    
  150.     return weatherMap[code] || "未知天气 (" + code + ")";
  151. }

  152. /**
  153. * 格式化并显示天气信息
  154. * @param {Object} weatherData - 天气数据对象
  155. */
  156. function displayWeatherInfo(weatherData) {
  157.     if (!weatherData) {
  158.         printl("❌ 没有可显示的天气数据");
  159.         return;
  160.     }
  161.    
  162.     printl("\n" + "=".repeat(50));
  163.     printl("🌍 天气报告 - " + weatherData.city);
  164.     printl("📅 查询时间: " + weatherData.fetchTime);
  165.     printl("=".repeat(50));
  166.    
  167.     // 当前天气
  168.     var current = weatherData.current;
  169.     printl("\n📊 当前天气:");
  170.     printl("  温度: " + current.temperature_2m + "°C");
  171.     printl("  体感温度: " + current.apparent_temperature + "°C");
  172.     printl("  湿度: " + current.relative_humidity_2m + "%");
  173.     printl("  风速: " + current.wind_speed_10m + " km/h");
  174.     printl("  天气状况: " + formatWeatherCode(current.weather_code));
  175.    
  176.     // 未来几天预报
  177.     if (weatherData.daily && weatherData.daily.time) {
  178.         printl("\n📅 未来天气预报:");
  179.         var times = weatherData.daily.time;
  180.         var maxTemps = weatherData.daily.temperature_2m_max;
  181.         var minTemps = weatherData.daily.temperature_2m_min;
  182.         
  183.         for (var i = 0; i < Math.min(times.length, 3); i++) {
  184.             var dateStr = times[i];
  185.             var maxTemp = maxTemps[i];
  186.             var minTemp = minTemps[i];
  187.             
  188.             printl("  " + dateStr + ": " + minTemp + "°C ~ " + maxTemp + "°C");
  189.         }
  190.     }
  191.    
  192.     printl("=".repeat(50) + "\n");
  193. }

  194. /**
  195. * 带重试机制的天气查询
  196. * @param {String} city - 城市名称
  197. * @param {Number} retries - 剩余重试次数
  198. * @returns {Object|null} 天气数据
  199. */
  200. function getWeatherWithRetry(city, retries) {
  201.     if (retries === undefined) {
  202.         retries = WEATHER_CONFIG.maxRetries;
  203.     }
  204.    
  205.     for (var attempt = 1; attempt <= retries; attempt++) {
  206.         printl("\n--- 第 " + attempt + "/" + retries + " 次尝试 ---");
  207.         
  208.         var result = getWeatherInfo(city);
  209.         
  210.         if (result) {
  211.             printl("✅ 成功获取天气数据");
  212.             return result;
  213.         }
  214.         
  215.         if (attempt < retries) {
  216.             printl("⚠️ 准备重试...");
  217.             // 短暂延迟后重试(注释掉可能不存在的sleep函数)
  218.             // sleep.millisecond(1000);
  219.         }
  220.     }
  221.    
  222.     printl("❌ 经过 " + retries + " 次尝试后仍然失败");
  223.     return null;
  224. }

  225. /**
  226. * 批量查询多个城市天气
  227. * @param {Array} cities - 城市名称数组
  228. */
  229. function queryMultipleCities(cities) {
  230.     printl("\n&#127760; 开始批量查询 " + cities.length + " 个城市的天气\n");
  231.    
  232.     var results = [];
  233.    
  234.     for (var i = 0; i < cities.length; i++) {
  235.         var city = cities[i];
  236.         printl("\n[" + (i + 1) + "/" + cities.length + "] 查询: " + city);
  237.         
  238.         var weatherData = getWeatherWithRetry(city, 2);
  239.         
  240.         if (weatherData) {
  241.             results.push(weatherData);
  242.             displayWeatherInfo(weatherData);
  243.         } else {
  244.             printl("⚠️ 跳过城市: " + city);
  245.         }
  246.         
  247.         // 避免频繁请求(注释掉可能不存在的sleep函数)
  248.         // if (i < cities.length - 1) {
  249.         //     sleep.millisecond(500);
  250.         // }
  251.     }
  252.    
  253.     printl("\n&#128202; 查询完成统计:");
  254.     printl("  总计: " + cities.length + " 个城市");
  255.     printl("  成功: " + results.length + " 个");
  256.     printl("  失败: " + (cities.length - results.length) + " 个");
  257.    
  258.     return results;
  259. }

  260. // =============================================================================
  261. // 主程序入口
  262. // =============================================================================

  263. function main() {
  264.     printl("========================================");
  265.     printl("&#127780;️ AIWROK 天气信息查询工具 v1.0");
  266.     printl("适用于苹果iOS系统");
  267.     printl("========================================\n");
  268.    
  269.     // 示例1: 查询单个城市
  270.     printl("【示例1】查询单个城市天气");
  271.     var singleCityWeather = getWeatherWithRetry(WEATHER_CONFIG.defaultCity);
  272.     if (singleCityWeather) {
  273.         displayWeatherInfo(singleCityWeather);
  274.     }
  275.    
  276.     // 示例2: 批量查询多个城市
  277.     printl("\n【示例2】批量查询多个城市天气");
  278.     var cities = ["北京", "上海", "广州", "深圳"];
  279.     var batchResults = queryMultipleCities(cities);
  280.    
  281.     // 示例3: 查询用户自定义城市
  282.     printl("\n【示例3】查询其他城市");
  283.     var otherCities = ["成都", "杭州", "武汉"];
  284.     queryMultipleCities(otherCities);
  285.    
  286.     printl("\n========================================");
  287.     printl("✅ 所有查询任务完成");
  288.     printl("========================================");
  289. }

  290. // 执行主程序
  291. main();
复制代码







欢迎光临 B2B网络软件 (http://bbs.niubt.cn/) Powered by Discuz! X3.2