feat: 统一前后端日志和错误处理
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
// handlers/payment.rs — 支付相关处理器
|
||||
use actix_web::{get, post, web, HttpResponse, Responder};
|
||||
use actix_web::{get, post, web, HttpResponse};
|
||||
use chrono::Utc;
|
||||
use sqlx::postgres::PgPool;
|
||||
use tracing::error;
|
||||
use uuid::Uuid;
|
||||
|
||||
use crate::db;
|
||||
use crate::error::AppError;
|
||||
use crate::models::{Claims, CreateOrderRequest, MockConfirmRequest};
|
||||
|
||||
struct PackageInfo {
|
||||
@@ -45,24 +45,20 @@ pub async fn create_order(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
body: web::Json<CreateOrderRequest>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let user_id = claims.user_id;
|
||||
|
||||
let pkg = match get_package_info(&body.package_type) {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
return HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 400,
|
||||
"errmsg": "无效的套餐类型"
|
||||
}));
|
||||
return Err(AppError::BadRequest("无效的套餐类型".to_string()));
|
||||
}
|
||||
};
|
||||
|
||||
let order_no = Uuid::new_v4().to_string();
|
||||
let expires_at = pkg.days.map(|d| Utc::now() + chrono::Duration::days(d));
|
||||
|
||||
match db::create_payment_order(
|
||||
db::create_payment_order(
|
||||
pool.get_ref(),
|
||||
user_id,
|
||||
&order_no,
|
||||
@@ -70,28 +66,19 @@ pub async fn create_order(
|
||||
pkg.amount,
|
||||
expires_at,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(_) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"order_id": order_no,
|
||||
"package_type": body.package_type,
|
||||
"amount": pkg.amount,
|
||||
"display_amount": pkg.display_amount,
|
||||
"display_name": pkg.display_name,
|
||||
"expires_at": expires_at,
|
||||
}
|
||||
})),
|
||||
Err(e) => {
|
||||
error!("创建订单失败: {}", e);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 500,
|
||||
"errmsg": "创建订单失败,请重试"
|
||||
}))
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"order_id": order_no,
|
||||
"package_type": body.package_type,
|
||||
"amount": pkg.amount,
|
||||
"display_amount": pkg.display_amount,
|
||||
"display_name": pkg.display_name,
|
||||
"expires_at": expires_at,
|
||||
}
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
/// POST /api/payment/mock-confirm
|
||||
@@ -100,26 +87,18 @@ pub async fn mock_confirm(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
body: web::Json<MockConfirmRequest>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let user_id = claims.user_id;
|
||||
|
||||
match db::confirm_payment_order(pool.get_ref(), &body.order_id, user_id).await {
|
||||
Ok(expires_at) => HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"is_paid_active": true,
|
||||
"paid_expires_at": expires_at,
|
||||
}
|
||||
})),
|
||||
Err(e) => {
|
||||
error!("确认支付失败: {}", e);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 400,
|
||||
"errmsg": "支付确认失败,请重试"
|
||||
}))
|
||||
let expires_at = db::confirm_payment_order(pool.get_ref(), &body.order_id, user_id).await?;
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"is_paid_active": true,
|
||||
"paid_expires_at": expires_at,
|
||||
}
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
/// GET /api/user/quota
|
||||
@@ -127,34 +106,23 @@ pub async fn mock_confirm(
|
||||
pub async fn get_user_quota(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let user_id = claims.user_id;
|
||||
|
||||
match db::get_user_quota(pool.get_ref(), user_id).await {
|
||||
Ok((used, is_paid_active, paid_expires_at)) => {
|
||||
let limit: i64 = std::env::var("FREE_USER_DATA_LIMIT")
|
||||
.ok()
|
||||
.and_then(|v| v.parse().ok())
|
||||
.unwrap_or(20);
|
||||
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);
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"used": used,
|
||||
"limit": limit,
|
||||
"unlimited": is_paid_active,
|
||||
"is_paid_active": is_paid_active,
|
||||
"paid_expires_at": paid_expires_at,
|
||||
}
|
||||
}))
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"used": used,
|
||||
"limit": limit,
|
||||
"unlimited": is_paid_active,
|
||||
"is_paid_active": is_paid_active,
|
||||
"paid_expires_at": paid_expires_at,
|
||||
}
|
||||
Err(e) => {
|
||||
error!("获取配额信息失败: {}", e);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 500,
|
||||
"errmsg": "获取配额信息失败"
|
||||
}))
|
||||
}
|
||||
}
|
||||
})))
|
||||
}
|
||||
Reference in New Issue
Block a user