bpi_rs/login/member_center/
vip_info.rs1use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct VipInfo {
11 pub mid: u64,
13 pub vip_type: u8,
18 pub vip_status: u8,
23 pub vip_due_date: u64,
25 pub vip_pay_type: u8,
29 pub theme_type: u8,
31}
32
33impl BpiClient {
34 pub async fn member_center_vip_info(&self) -> Result<BpiResponse<VipInfo>, BpiError> {
36 let result = self
37 .get("https://api.bilibili.com/x/vip/web/user/info")
38 .send_bpi("查询大会员状态").await?;
39
40 Ok(result)
41 }
42
43 pub async fn is_vip(&self) -> bool {
44 self.member_center_vip_info().await
45 .ok()
46 .and_then(|resp| resp.data)
47 .map(|data2| data2.vip_status == 1 && data2.vip_due_date > 0)
48 .unwrap_or(false)
49 }
50}
51
52#[cfg(test)]
53mod tests {
54 use super::*;
55 use tracing::info;
56
57 #[tokio::test]
58 async fn test_get_vip_info() {
59 let bpi = BpiClient::new();
60
61 match bpi.member_center_vip_info().await {
62 Ok(resp) => {
63 if resp.code == 0 {
64 let data = resp.data.unwrap();
65 info!(
66 "mid: {}, vip_type: {}, vip_status: {}, vip_due_date: {}, vip_pay_type: {}, theme_type: {}",
67 data.mid,
68 data.vip_type,
69 data.vip_status,
70 data.vip_due_date,
71 data.vip_pay_type,
72 data.theme_type
73 );
74 } else {
75 info!("请求失败: code={}, message={}", resp.code, resp.message);
76 }
77 }
78 Err(err) => {
79 panic!("请求出错: {}", err);
80 }
81 }
82 }
83 #[tokio::test]
84 async fn test_is_vip() {
85 let bpi = BpiClient::new();
86
87 match bpi.is_vip().await {
88 true => info!("是大会员"),
89 false => info!("不是大会员"),
90 }
91 }
92}