bpi_rs/wallet/
info.rs

1use serde::{ Deserialize, Serialize };
2use serde_json::json;
3
4use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
5
6/// 用户钱包数据
7#[derive(Debug, Clone, Deserialize, Serialize)]
8#[serde(rename_all = "camelCase")]
9pub struct UserWallet {
10    /// 用户 mid
11    pub mid: i64,
12    /// 总计 B 币
13    pub total_bp: f64,
14    /// 默认 B 币
15    pub default_bp: f64,
16    /// iOS B 币
17    pub ios_bp: f64,
18    /// 优惠券余额
19    pub coupon_balance: f64,
20    /// 可用 B 币
21    pub available_bp: f64,
22    /// 不可用 B 币
23    pub unavailable_bp: f64,
24    /// 不可用原因
25    pub unavailable_reason: String,
26    /// 提示信息
27    pub tip: String,
28    /// 需要显示类余额, 1
29    pub need_show_class_balance: i64,
30}
31
32impl BpiClient {
33    /// 获取用户钱包信息
34    ///
35    /// 文档: https://socialsisteryi.github.io/bilibili-API-collect/docs/wallet/info.html#获取用户钱包信息
36    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}