feat: 添加 POST /api/payment/sync-order 接口,用于强制同步订单会员状态

This commit is contained in:
2026-05-07 16:58:55 +08:00
parent c82bb303f7
commit a05b46dfe2
3 changed files with 40 additions and 1 deletions

View File

@@ -41,6 +41,7 @@ pub use payment::create_order;
pub use payment::generate_code;
pub use payment::get_user_quota;
pub use payment::mock_confirm;
pub use payment::sync_order;
pub use payment::payment_index;
pub use payment::payment_login_status;
pub use payment::payment_page;

View File

@@ -936,6 +936,43 @@ pub async fn mock_confirm(
})))
}
/// POST /api/payment/sync-order — 根据订单号强制同步会员状态
///
/// 用户支付完成后,若前端会员状态未刷新,可调用此接口同步确认订单并获取最新状态。
#[post("/api/payment/sync-order")]
pub async fn sync_order(
pool: web::Data<PgPool>,
claims: web::ReqData<Claims>,
body: web::Json<MockConfirmRequest>,
) -> Result<HttpResponse, AppError> {
let user_id = claims.user_id;
// 尝试确认订单(幂等)
let _ = db::confirm_payment_order(pool.get_ref(), &body.order_id, user_id).await;
// 查询最新状态
let user = sqlx::query_as::<_, (bool, Option<chrono::DateTime<chrono::Utc>>)>(
r#"SELECT is_paid, paid_expires_at FROM users WHERE id = $1"#,
)
.bind(user_id)
.fetch_optional(pool.get_ref())
.await
.map_err(|e| AppError::Database(format!("查询用户失败: {}", e)))?
.ok_or_else(|| AppError::NotFound("用户不存在".to_string()))?;
let (is_paid, paid_expires_at) = user;
let is_paid_active = is_paid && (paid_expires_at.is_none() || paid_expires_at.unwrap() > Utc::now());
Ok(HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": {
"is_paid": is_paid,
"is_paid_active": is_paid_active,
"paid_expires_at": paid_expires_at,
}
})))
}
/// GET /api/user/quota
#[get("/api/user/quota")]
pub async fn get_user_quota(