1use serde::{ Deserialize, Serialize };
2use serde_json::json;
3
4use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct UserWallet {
10 pub mid: i64,
12 pub total_bp: f64,
14 pub default_bp: f64,
16 pub ios_bp: f64,
18 pub coupon_balance: f64,
20 pub available_bp: f64,
22 pub unavailable_bp: f64,
24 pub unavailable_reason: String,
26 pub tip: String,
28 pub need_show_class_balance: i64,
30}
31
32impl BpiClient {
33 pub async fn wallet_info(&self) -> Result<BpiResponse<UserWallet>, BpiError> {
38 let csrf = self.csrf()?;
39
40 let timestamp = chrono::Utc::now().timestamp_millis();
41
42 let body =
43 json!({
44 "csrf": csrf,
45 "platformType": 3,
46 "timestamp": timestamp,
47 "traceId": timestamp,
48 "version": "1.0",
49 });
50
51 self
52 .post("https://pay.bilibili.com/paywallet/wallet/getUserWallet")
53 .json(&body)
54 .send_bpi("获取用户钱包").await
55 }
56}
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61 use tracing::info;
62
63 #[tokio::test]
64 async fn test_get_user_wallet() {
65 let bpi = BpiClient::new();
66 let resp = bpi.wallet_info().await;
67 info!("响应: {:?}", resp);
68 assert!(resp.is_ok());
69 if let Ok(data) = resp {
70 info!("用户mid: {}", data.data.unwrap().mid);
71 }
72 }
73}