fix: 创建订单前查重-防止同一用户同一套餐重复pending订单

This commit is contained in:
2026-06-02 11:38:23 +08:00
parent 526830d9de
commit bcd9bc81e4

View File

@@ -674,6 +674,21 @@ pub async fn payment_page(
new_no
}
}
} else {
// 查重:用户是否已有同套餐的待支付订单,有则复用
let existing: Option<String> = 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));
@@ -681,6 +696,7 @@ pub async fn payment_page(
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")