fix: 维护模式使用500条临时限额而非完全跳过(防滥用)
This commit is contained in:
18
src/db.rs
18
src/db.rs
@@ -9,25 +9,31 @@ use crate::error::AppError;
|
|||||||
|
|
||||||
// 用于插入weather_data的数据
|
// 用于插入weather_data的数据
|
||||||
pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData, user_id: i32) -> Result<i32, AppError> {
|
pub async fn insert_weather_data(pool: &PgPool, weather_data: &WeatherData, user_id: i32) -> Result<i32, AppError> {
|
||||||
// 维护模式:跳过所有配额检查
|
|
||||||
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 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_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_paid_active {
|
if !is_paid_active {
|
||||||
let current_count = count_user_weather_data(pool, user_id).await?;
|
let current_count = count_user_weather_data(pool, user_id).await?;
|
||||||
let limit: i64 = env::var("FREE_USER_DATA_LIMIT")
|
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())
|
.unwrap_or_else(|_| "20".to_string())
|
||||||
.parse()
|
.parse()
|
||||||
.unwrap_or(20);
|
.unwrap_or(20)
|
||||||
|
};
|
||||||
|
|
||||||
if current_count >= limit {
|
if current_count >= limit {
|
||||||
return Err(AppError::Forbidden("数据条数已达上限,请升级为付费用户".to_string()));
|
return Err(AppError::Forbidden("数据条数已达上限,请升级为付费用户".to_string()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 准备插入数据的 SQL 语句
|
// 准备插入数据的 SQL 语句
|
||||||
let insert_query = r#"
|
let insert_query = r#"
|
||||||
|
|||||||
@@ -1073,17 +1073,22 @@ pub async fn get_user_quota(
|
|||||||
|
|
||||||
let (used, is_paid_active, paid_expires_at) =
|
let (used, is_paid_active, paid_expires_at) =
|
||||||
db::get_user_quota(pool.get_ref(), user_id).await?;
|
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 {
|
} 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!({
|
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||||
"success": true,
|
"success": true,
|
||||||
"data": {
|
"data": {
|
||||||
|
|||||||
Reference in New Issue
Block a user