Skip to main content

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    /// # 文档
36    /// [查看API文档](https://socialsisteryi.github.io/bilibili-API-collect/docs/wallet/info.html#获取用户钱包信息)
37    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}