Skip to content

Commit c513e25

Browse files
committed
增强积分查看功能:支持查看指定用户积分
- 更新聊天室 :p 命令,支持查看指定用户积分 (:p username) 或当前用户积分 (:p) - 重构 get_points API 方法,返回积分和用户名元组,便于显示用户信息 - 更新 UserService 的 get_point 方法,适配新的返回类型 - 优化错误处理和数据解析逻辑,提高API调用的健壮性 此变更扩展了积分查询功能,用户现在可以方便地查看其他用户的积分信息
1 parent 08eeb6e commit c513e25

3 files changed

Lines changed: 42 additions & 21 deletions

File tree

client/src/commands/handlers/chatroom.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -267,21 +267,34 @@ impl ChatroomCommand {
267267
}
268268
}
269269
cmd if cmd.starts_with(":p") => {
270-
let username = self.context.auth.get_user_name().await?;
271-
let result = self.context.client.user.get_point(&username).await;
272-
if result.success {
273-
if let Some(points) = result.data {
274-
println!(
275-
"{} 的积分: {}",
276-
username.green(),
277-
points.to_string().yellow()
278-
);
279-
} else {
280-
println!("{}", "数据为空".red());
270+
let parts: Vec<&str> = cmd.split_whitespace().collect();
271+
if parts.len() > 1 {
272+
let username = parts[1];
273+
let result = self.context.client.user.get_point(username).await;
274+
if result.success {
275+
if let Some((point, name)) = result.data {
276+
println!("{} 的积分: {}", name.green(), point.to_string().yellow());
277+
} else {
278+
println!("{}", "数据为空".red());
279+
}
281280
}
282281
} else {
283-
let error_msg = result.message.unwrap_or_else(|| "未知错误".to_string());
284-
println!("{}: {}", "获取积分失败".red(), error_msg);
282+
let username = self.context.auth.get_user_name().await?;
283+
let result = self.context.client.user.get_point(&username).await;
284+
if result.success {
285+
if let Some((point, name)) = result.data {
286+
println!(
287+
"{} 的积分: {}",
288+
name.green(),
289+
point.to_string().yellow()
290+
);
291+
} else {
292+
println!("{}", "数据为空".red());
293+
}
294+
} else {
295+
let error_msg = result.message.unwrap_or_else(|| "未知错误".to_string());
296+
println!("{}: {}", "获取积分失败".red(), error_msg);
297+
}
285298
}
286299
}
287300
cmd if cmd.starts_with(":bg") => {

src/api/user_api.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,16 +221,24 @@ impl UserApi {
221221
}
222222

223223
/// 积分获取接口
224-
pub async fn get_points(&self, username: &str) -> Result<i32> {
224+
pub async fn get_points(&self, username: &str) -> Result<(i32, String)> {
225225
let token = self.client.get_token().await;
226-
226+
227227
let mut params = HashMap::new();
228228
if let Some(token_value) = token {
229229
params.insert("apiKey".to_string(), token_value);
230230
}
231-
232-
self.client
233-
.get::<i32>(&format!("/user/{}/point", username), Some(params))
234-
.await
231+
232+
let response: Value = self.client
233+
.get(&format!("/user/{}/point", username), Some(params))
234+
.await?;
235+
236+
if let Some(data) = response.get("data") {
237+
let user_point = data.get("userPoint").and_then(|v| v.as_i64()).ok_or(anyhow::anyhow!("userPoint 无效"))? as i32;
238+
let user_name = data.get("userName").and_then(|v| v.as_str()).ok_or(anyhow::anyhow!("userName 无效"))?.to_string();
239+
Ok((user_point, user_name))
240+
} else {
241+
Err(anyhow::anyhow!("缺少 data"))
242+
}
235243
}
236-
}
244+
}

src/services/user_service.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ impl UserService {
8585
}
8686

8787
/// 获取用户积分
88-
pub async fn get_point(&self, username: &str) -> Response<i32> {
88+
pub async fn get_point(&self, username: &str) -> Response<(i32, String)> {
8989
self.call_api(
9090
&format!("获取用户积分: {}", username),
9191
|| self.user_api.get_points(username),

0 commit comments

Comments
 (0)