feat: 统一前后端日志和错误处理
This commit is contained in:
@@ -1,47 +1,37 @@
|
||||
use actix_web::{web, get, put, HttpResponse, Responder};
|
||||
use actix_web::{web, get, put, HttpResponse};
|
||||
use sqlx::postgres::PgPool;
|
||||
use tracing::error;
|
||||
use tracing::info;
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::db;
|
||||
use crate::models::{Claims, ErrorResponse};
|
||||
use crate::error::AppError;
|
||||
use crate::models::Claims;
|
||||
|
||||
#[get("/api/user/profile")]
|
||||
pub async fn get_current_user_profile(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let user_id = claims.user_id;
|
||||
tracing::info!("获取当前用户信息, 用户ID: {}", user_id);
|
||||
info!("获取当前用户信息, 用户ID: {}", user_id);
|
||||
|
||||
match db::get_user_by_id(pool.get_ref(), user_id).await {
|
||||
Ok(user) => {
|
||||
let is_paid_active = user.is_paid &&
|
||||
(user.paid_expires_at.is_none() || user.paid_expires_at.unwrap() > chrono::Utc::now());
|
||||
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"id": user.id,
|
||||
"name": user.name,
|
||||
"nickname": user.nickname,
|
||||
"avatarUrl": user.avatar_url,
|
||||
"is_paid": user.is_paid,
|
||||
"is_paid_active": is_paid_active,
|
||||
"is_admin": user.is_admin,
|
||||
"paid_expires_at": user.paid_expires_at
|
||||
}
|
||||
}))
|
||||
let user = db::get_user_by_id(pool.get_ref(), user_id).await?;
|
||||
let is_paid_active = user.is_paid &&
|
||||
(user.paid_expires_at.is_none() || user.paid_expires_at.unwrap() > chrono::Utc::now());
|
||||
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true,
|
||||
"data": {
|
||||
"id": user.id,
|
||||
"name": user.name,
|
||||
"nickname": user.nickname,
|
||||
"avatarUrl": user.avatar_url,
|
||||
"is_paid": user.is_paid,
|
||||
"is_paid_active": is_paid_active,
|
||||
"is_admin": user.is_admin,
|
||||
"paid_expires_at": user.paid_expires_at
|
||||
}
|
||||
Err(e) => {
|
||||
error!("获取用户信息失败: {}", e);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 500,
|
||||
"errmsg": e
|
||||
}))
|
||||
}
|
||||
}
|
||||
})))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
@@ -55,23 +45,12 @@ pub async fn save_user_profile(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
body: web::Json<SaveUserProfileRequest>,
|
||||
) -> impl Responder {
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let user_id = claims.user_id;
|
||||
tracing::info!("保存用户信息, 用户ID: {}", user_id);
|
||||
info!("保存用户信息, 用户ID: {}", user_id);
|
||||
|
||||
match db::update_user_profile(pool.get_ref(), user_id, &body.nickname, &body.avatar_url).await {
|
||||
Ok(_) => {
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true
|
||||
}))
|
||||
}
|
||||
Err(e) => {
|
||||
error!("保存用户信息失败: {}", e);
|
||||
HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": false,
|
||||
"errcode": 500,
|
||||
"errmsg": e
|
||||
}))
|
||||
}
|
||||
}
|
||||
db::update_user_profile(pool.get_ref(), user_id, &body.nickname, &body.avatar_url).await?;
|
||||
Ok(HttpResponse::Ok().json(serde_json::json!({
|
||||
"success": true
|
||||
})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user