From 34f698ded203db16d011607765f30ae138276584 Mon Sep 17 00:00:00 2001 From: Milky0217 Date: Wed, 25 Mar 2026 16:08:39 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=BD=93=E5=89=8D=E7=94=A8=E6=88=B7=E4=BF=A1=E6=81=AF=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 GET /api/user/profile 接口 - 返回用户付费状态和管理员标识 - 计算付费是否有效(考虑过期时间) --- src/main.rs | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index c6d14ad..aacf1d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -698,6 +698,43 @@ async fn serve_static_files(path: web::Path) -> impl Responder { } } +// 获取当前用户信息(普通用户可用) +#[get("/api/user/profile")] +async fn get_current_user_profile( + pool: web::Data, + claims: web::ReqData, +) -> 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), )