fix: 添加后台定时重试 + 管理员强制确认 + 审计日志 + mock守卫

This commit is contained in:
2026-05-25 13:04:19 +08:00
parent af2837e15e
commit 5f3b919e92
7 changed files with 284 additions and 9 deletions

View File

@@ -18,7 +18,7 @@ use auth::jwt_middleware;
use config::AppConfig;
use db::create_pool;
use handlers::{
admin_get_user, admin_update_user_payment, add_favorite, alipay_notify, alipay_pay_page, alipay_refund_notify,
admin_force_confirm_order, admin_get_user, admin_update_user_payment, add_favorite, alipay_notify, alipay_pay_page, alipay_refund_notify,
create_order, delete_weather, generate_code, generate_temp_token_handler, get_current_user_profile,
get_favorites, get_user_quota, get_weather_brief, get_weather_details,
get_user_orders, health_check, login, mock_login, mock_confirm, sync_order, payment_index, payment_login_status, payment_page, payment_success,
@@ -107,6 +107,7 @@ fn create_server_config(
.service(save_user_profile)
.service(admin_get_user)
.service(admin_update_user_payment)
.service(admin_force_confirm_order)
.service(create_order)
.service(mock_confirm)
.service(sync_order)
@@ -231,17 +232,35 @@ async fn main() -> std::io::Result<()> {
let http_client = Client::new();
// 启动时清理所有超过 24 小时的待支付订单
// 启动时清理:过期订单 + 过期登录码
match db::cleanup_expired_pending_orders(&pool, None).await {
Ok(n) => info!("已清理 {} 个过期待支付订单(启动时)", n),
Err(e) => warn!("启动时清理过期待支付订单失败: {}", e),
}
// 启动时清理所有过期的 web 登录码
match db::cleanup_expired_login_codes(&pool).await {
Ok(n) => info!("已清理 {} 个过期的 web 登录码(启动时)", n),
Err(e) => warn!("启动时清理 web 登录码失败: {}", e),
}
// 后台定时扫描待支付订单(每 5 分钟)
let pool_clone = pool.clone();
actix_web::rt::spawn(async move {
let mut interval = actix_web::rt::time::interval(std::time::Duration::from_secs(300));
interval.tick().await; // 跳过立即执行
loop {
interval.tick().await;
tracing::info!("[定时任务] 开始扫描待支付订单...");
match db::check_and_retry_pending_orders(&pool_clone).await {
Ok(confirmed) => {
if confirmed > 0 {
tracing::info!("[定时任务] 自动确认了 {} 个待支付订单", confirmed);
}
}
Err(e) => tracing::warn!("[定时任务] 扫描待支付订单失败: {}", e),
}
}
});
info!("Attempting to start server...");
let is_production = std::env::var("APP_ENV").unwrap_or_else(|_| "development".into()) == "production";