Skip to main content

bpi_rs/login/member_center/
vip_info.rs

1//! 查询大会员状态
2//!
3//! [文档](https://socialsisteryi.github.io/bilibili-API-collect/docs/login/member_center.html#查询大会员状态)
4
5use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8/// 大会员信息体
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct VipInfo {
11    /// 我的mid
12    pub mid: u64,
13    /// 大会员类型
14    /// - 0:无
15    /// - 1:月度
16    /// - 2:年度
17    pub vip_type: u8,
18    /// 大会员状态
19    /// - 1:正常
20    /// - 2:IP频繁更换,服务被冻结
21    /// - 3:大会员账号风险过高,功能锁定
22    pub vip_status: u8,
23    /// 大会员到期时间(时间戳,毫秒)
24    pub vip_due_date: u64,
25    /// 是否已购买大会员
26    /// - 0:未购买
27    /// - 1:已购买
28    pub vip_pay_type: u8,
29    /// 作用尚不明确
30    pub theme_type: u8,
31}
32
33impl BpiClient {
34    /// 查询大会员状态
35    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}