From bcd9bc81e42454badc062aa6b5d19eea82f81eb7 Mon Sep 17 00:00:00 2001 From: milky0217 Date: Tue, 2 Jun 2026 11:38:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=88=9B=E5=BB=BA=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E5=89=8D=E6=9F=A5=E9=87=8D-=E9=98=B2=E6=AD=A2=E5=90=8C?= =?UTF-8?q?=E4=B8=80=E7=94=A8=E6=88=B7=E5=90=8C=E4=B8=80=E5=A5=97=E9=A4=90?= =?UTF-8?q?=E9=87=8D=E5=A4=8Dpending=E8=AE=A2=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/handlers/payment.rs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/handlers/payment.rs b/src/handlers/payment.rs index 3885ece..a55a3d4 100644 --- a/src/handlers/payment.rs +++ b/src/handlers/payment.rs @@ -675,12 +675,28 @@ pub async fn payment_page( } } } else { - let new_no = Uuid::new_v4().to_string(); - let expires_at = pkg.days.map(|d| Utc::now() + chrono::Duration::days(d)); - db::create_payment_order( - pool.get_ref(), claims.user_id, &new_no, &query.package_, pkg.amount, expires_at, - ).await?; - new_no + // 查重:用户是否已有同套餐的待支付订单,有则复用 + let existing: Option = sqlx::query_scalar( + r#"SELECT order_no FROM payment_orders + WHERE user_id = $1 AND package_type = $2 AND status = 'pending' + ORDER BY created_at DESC LIMIT 1"#, + ) + .bind(claims.user_id) + .bind(&query.package_) + .fetch_optional(pool.get_ref()) + .await + .map_err(|e| AppError::Database(format!("查询已有订单失败: {}", e)))?; + + if let Some(no) = existing { + no + } else { + let new_no = Uuid::new_v4().to_string(); + let expires_at = pkg.days.map(|d| Utc::now() + chrono::Duration::days(d)); + db::create_payment_order( + pool.get_ref(), claims.user_id, &new_no, &query.package_, pkg.amount, expires_at, + ).await?; + new_no + } }; let base_url = std::env::var("APP_BASE_URL")