登录接口响应统一使用 ApiResponse 包装格式
This commit is contained in:
@@ -28,12 +28,18 @@ struct LoginRequest {
|
|||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录响应
|
// 统一响应包装
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct LoginResponse {
|
struct ApiResponse<T> {
|
||||||
success: bool,
|
success: bool,
|
||||||
token: Option<String>,
|
|
||||||
message: String,
|
message: String,
|
||||||
|
data: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录业务数据
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct LoginData {
|
||||||
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// JWT Claims
|
// JWT Claims
|
||||||
@@ -87,7 +93,7 @@ async fn main() {
|
|||||||
async fn login_handler(
|
async fn login_handler(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(payload): Json<LoginRequest>,
|
Json(payload): Json<LoginRequest>,
|
||||||
) -> (StatusCode, Json<LoginResponse>) {
|
) -> (StatusCode, Json<ApiResponse<LoginData>>) {
|
||||||
info!("Login attempt for user: {}", payload.username);
|
info!("Login attempt for user: {}", payload.username);
|
||||||
|
|
||||||
// 查询用户账号与密码
|
// 查询用户账号与密码
|
||||||
@@ -115,10 +121,10 @@ async fn login_handler(
|
|||||||
|
|
||||||
(
|
(
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: true,
|
success: true,
|
||||||
token: Some(token),
|
|
||||||
message: "Login successful".to_string(),
|
message: "Login successful".to_string(),
|
||||||
|
data: Some(LoginData { token }),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -126,10 +132,10 @@ async fn login_handler(
|
|||||||
warn!("Invalid password for user {}", payload.username);
|
warn!("Invalid password for user {}", payload.username);
|
||||||
(
|
(
|
||||||
StatusCode::UNAUTHORIZED,
|
StatusCode::UNAUTHORIZED,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: false,
|
success: false,
|
||||||
token: None,
|
|
||||||
message: "Invalid credentials".to_string(),
|
message: "Invalid credentials".to_string(),
|
||||||
|
data: None,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -137,10 +143,10 @@ async fn login_handler(
|
|||||||
warn!("Password verification error: {:?}", e);
|
warn!("Password verification error: {:?}", e);
|
||||||
(
|
(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: false,
|
success: false,
|
||||||
token: None,
|
|
||||||
message: "Internal error".to_string(),
|
message: "Internal error".to_string(),
|
||||||
|
data: None,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,12 +28,18 @@ struct LoginRequest {
|
|||||||
password: String,
|
password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 登录响应
|
// 统一响应包装
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct LoginResponse {
|
struct ApiResponse<T> {
|
||||||
success: bool,
|
success: bool,
|
||||||
token: Option<String>,
|
|
||||||
message: String,
|
message: String,
|
||||||
|
data: Option<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 登录业务数据
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct LoginData {
|
||||||
|
token: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
// JWT Claims
|
// JWT Claims
|
||||||
@@ -87,7 +93,7 @@ async fn main() {
|
|||||||
async fn login_handler(
|
async fn login_handler(
|
||||||
State(state): State<Arc<AppState>>,
|
State(state): State<Arc<AppState>>,
|
||||||
Json(payload): Json<LoginRequest>,
|
Json(payload): Json<LoginRequest>,
|
||||||
) -> (StatusCode, Json<LoginResponse>) {
|
) -> (StatusCode, Json<ApiResponse<LoginData>>) {
|
||||||
info!("Login attempt for email: {}", payload.email);
|
info!("Login attempt for email: {}", payload.email);
|
||||||
|
|
||||||
// 查询用户邮箱与密码
|
// 查询用户邮箱与密码
|
||||||
@@ -115,10 +121,10 @@ async fn login_handler(
|
|||||||
|
|
||||||
(
|
(
|
||||||
StatusCode::OK,
|
StatusCode::OK,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: true,
|
success: true,
|
||||||
token: Some(token),
|
|
||||||
message: "Login successful".to_string(),
|
message: "Login successful".to_string(),
|
||||||
|
data: Some(LoginData { token }),
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -126,10 +132,10 @@ async fn login_handler(
|
|||||||
warn!("Invalid password for email {}", payload.email);
|
warn!("Invalid password for email {}", payload.email);
|
||||||
(
|
(
|
||||||
StatusCode::UNAUTHORIZED,
|
StatusCode::UNAUTHORIZED,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: false,
|
success: false,
|
||||||
token: None,
|
|
||||||
message: "Invalid credentials".to_string(),
|
message: "Invalid credentials".to_string(),
|
||||||
|
data: None,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -137,10 +143,10 @@ async fn login_handler(
|
|||||||
warn!("Password verification error: {:?}", e);
|
warn!("Password verification error: {:?}", e);
|
||||||
(
|
(
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
Json(LoginResponse {
|
Json(ApiResponse {
|
||||||
success: false,
|
success: false,
|
||||||
token: None,
|
|
||||||
message: "Internal error".to_string(),
|
message: "Internal error".to_string(),
|
||||||
|
data: None,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user