From 02703de89cc656b761d1a0b405d83e3f4907acf7 Mon Sep 17 00:00:00 2001 From: Milky0217 Date: Sat, 27 Sep 2025 17:29:46 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86get=5Fweather?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E7=8E=B0=E5=9C=A8=E5=8F=AF=E4=BB=A5?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=89=8D=E7=AB=AF=E7=9A=84=E9=9C=80=E6=B1=82?= =?UTF-8?q?=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/db.rs | 2 +- src/main.rs | 34 ++++++++++++++++++++++++++++++++-- src/models.rs | 8 ++++---- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/db.rs b/src/db.rs index 214bf0c..1bfdebc 100644 --- a/src/db.rs +++ b/src/db.rs @@ -155,7 +155,7 @@ pub async fn get_weather_details(pool: &PgPool, weather_id: i32) -> Result, claims: web::ReqData, ) -> impl Responder { - let page = query.get("page").and_then(|v| v.as_i64()).unwrap_or(1) as i32; - let limit = query.get("limit").and_then(|v| v.as_i64()).unwrap_or(10) as i32; + // 同时处理数字和字符串类型的page参数 + let page = match query.get("page") { + Some(v) => { + // 尝试直接解析为数字 + if let Some(num) = v.as_i64() { + num as i32 + } + // 尝试将字符串解析为数字 + else if let Some(s) = v.as_str() { + s.parse().unwrap_or(1) + } + // 解析失败用默认值 + else { + 1 + } + } + None => 1, // 无参数时用默认值 + }; + + // 同理优化limit参数解析 + let limit = match query.get("limit") { + Some(v) => { + if let Some(num) = v.as_i64() { + num as i32 + } else if let Some(s) = v.as_str() { + s.parse().unwrap_or(10) + } else { + 10 + } + } + None => 10, + }; println!("获取天气数据列表, 页码: {}, 每页条数: {}", page, limit); diff --git a/src/models.rs b/src/models.rs index b400079..986372e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -82,8 +82,8 @@ pub struct WeatherDataBrief { #[sqlx(rename = "min")] pub min: i32, - pub longitude: f64, - pub latidute: f64, + pub longitude: String, + pub latitude: String, } // 在models.rs中添加(可放在WeatherDataBrief下方) @@ -151,8 +151,8 @@ pub struct WeatherData { #[sqlx(rename = "inspectiontype")] pub inspection_type: Option, - pub latitude: f64, - pub longitude: f64, + pub latitude: String, + pub longitude: String, #[serde(rename = "lowCloudiness")] #[sqlx(rename = "lowcloudiness")]