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> {
37 let csrf = self.csrf()?;
38
39 let timestamp = chrono::Utc::now().timestamp_millis();
40
41 let body =
42 json!({
43 "csrf": csrf,
44 "platformType": 3,
45 "timestamp": timestamp,
46 "traceId": timestamp,
47 "version": "1.0",
48 });
49
50 self
51 .post("https://pay.bilibili.com/paywallet/wallet/getUserWallet")
52 .json(&body)
53 .send_bpi("获取用户钱包").await
54 }
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60 use tracing::info;
61
62 #[tokio::test]
63 async fn test_get_user_wallet() {
64 let bpi = BpiClient::new();
65 let resp = bpi.wallet_info().await;
66 info!("响应: {:?}", resp);
67 assert!(resp.is_ok());
68 if let Ok(data) = resp {
69 info!("用户mid: {}", data.data.unwrap().mid);
70 }
71 }
72}