fix: 维护模式使用500条临时限额而非完全跳过(防滥用)

This commit is contained in:
2026-05-25 13:34:40 +08:00
parent f82dd8d5de
commit fed95531fe
2 changed files with 30 additions and 19 deletions

View File

@@ -9,25 +9,31 @@ use crate::error::AppError;
// 用于插入weather_data的数据
pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData, user_id: i32) -> Result<i32, AppError> {
// 维护模式:跳过所有配额检查
// 配额检查:非付费用户数据条数限制
let user = get_user_by_id(pool, user_id).await?;
let is_paid_active = user.is_paid && user.paid_expires_at.map_or(true, |expires| expires > Utc::now());
// 维护模式:非会员使用更高的临时限额,防止资源滥用
let is_maintenance = std::env::var("PAYMENT_MAINTENANCE_MODE").ok() == Some("true".to_string());
if !is_maintenance {
// 配额检查:非付费用户数据条数限制
let user = get_user_by_id(pool, user_id).await?;
let is_paid_active = user.is_paid && user.paid_expires_at.map_or(true, |expires| expires > Utc::now());
if !is_paid_active {
if !is_paid_active {
let current_count = count_user_weather_data(pool, user_id).await?;
let limit: i64 = env::var("FREE_USER_DATA_LIMIT")
.unwrap_or_else(|_| "20".to_string())
.parse()
.unwrap_or(20);
let limit: i64 = if is_maintenance {
env::var("MAINTENANCE_MODE_DATA_LIMIT")
.unwrap_or_else(|_| "500".to_string())
.parse()
.unwrap_or(500)
} else {
env::var("FREE_USER_DATA_LIMIT")
.unwrap_or_else(|_| "20".to_string())
.parse()
.unwrap_or(20)
};
if current_count >= limit {
return Err(AppError::Forbidden("数据条数已达上限,请升级为付费用户".to_string()));
}
}
}
// 准备插入数据的 SQL 语句
let insert_query = r#"

View File

@@ -1073,17 +1073,22 @@ pub async fn get_user_quota(
let (used, is_paid_active, paid_expires_at) =
db::get_user_quota(pool.get_ref(), user_id).await?;
let limit: i64 = std::env::var("FREE_USER_DATA_LIMIT")
.ok()
.and_then(|v| v.parse().ok())
.unwrap_or(20);
let (unlimited, active) = if is_maintenance {
(true, true)
// 维护模式使用更高的临时限额,但非会员仍然有限制防止滥用
let (limit, unlimited) = if is_maintenance && !is_paid_active {
let ml: i64 = std::env::var("MAINTENANCE_MODE_DATA_LIMIT")
.ok().and_then(|v| v.parse().ok()).unwrap_or(500);
(ml, false)
} else if is_paid_active {
(0, true)
} else {
(is_paid_active, is_paid_active)
let fl: i64 = std::env::var("FREE_USER_DATA_LIMIT")
.ok().and_then(|v| v.parse().ok()).unwrap_or(20);
(fl, false)
};
let active = if is_maintenance { true } else { is_paid_active };
Ok(HttpResponse::Ok().json(serde_json::json!({
"success": true,
"data": {