feat: 添加获取当前用户信息 API
新增 GET /api/user/profile 接口 - 返回用户付费状态和管理员标识 - 计算付费是否有效(考虑过期时间)
This commit is contained in:
40
src/main.rs
40
src/main.rs
@@ -698,6 +698,43 @@ async fn serve_static_files(path: web::Path<String>) -> impl Responder {
|
||||
}
|
||||
}
|
||||
|
||||
// 获取当前用户信息(普通用户可用)
|
||||
#[get("/api/user/profile")]
|
||||
async fn get_current_user_profile(
|
||||
pool: web::Data<PgPool>,
|
||||
claims: web::ReqData<Claims>,
|
||||
) -> impl Responder {
|
||||
info!("获取当前用户信息, 用户ID: {}", claims.user_id);
|
||||
|
||||
match db::get_user_by_id(pool.get_ref(), claims.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,
|
||||
"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
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 创建服务器配置的函数
|
||||
fn create_server_config(
|
||||
pool: PgPool,
|
||||
@@ -724,9 +761,10 @@ fn create_server_config(
|
||||
web::scope("")
|
||||
.wrap(from_fn(jwt_middleware)) // 关键修改:用 from_fn 包装
|
||||
.service(post_weather_data)
|
||||
.service(get_weather_brief) // 新增
|
||||
.service(get_weather_brief)
|
||||
.service(generate_temp_token_handler)
|
||||
.service(delete_weather)
|
||||
.service(get_current_user_profile)
|
||||
.service(admin_get_user)
|
||||
.service(admin_update_user_payment),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user