use serde::{ Deserialize, Serialize };
use serde_json::json;
use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UserWallet {
pub mid: i64,
pub total_bp: f64,
pub default_bp: f64,
pub ios_bp: f64,
pub coupon_balance: f64,
pub available_bp: f64,
pub unavailable_bp: f64,
pub unavailable_reason: String,
pub tip: String,
pub need_show_class_balance: i64,
}
impl BpiClient {
pub async fn wallet_info(&self) -> Result<BpiResponse<UserWallet>, BpiError> {
let csrf = self.csrf()?;
let timestamp = chrono::Utc::now().timestamp_millis();
let body =
json!({
"csrf": csrf,
"platformType": 3,
"timestamp": timestamp,
"traceId": timestamp,
"version": "1.0",
});
self
.post("https://pay.bilibili.com/paywallet/wallet/getUserWallet")
.json(&body)
.send_bpi("获取用户钱包").await
}
}
#[cfg(test)]
mod tests {
use super::*;
use tracing::info;
#[tokio::test]
async fn test_get_user_wallet() {
let bpi = BpiClient::new();
let resp = bpi.wallet_info().await;
info!("响应: {:?}", resp);
assert!(resp.is_ok());
if let Ok(data) = resp {
info!("用户mid: {}", data.data.unwrap().mid);
}
}
}