Skip to main content

bpi_rs/login/member_center/
account.rs

1//! 获取我的信息
2//!
3//! [查看 API 文档](https://github.com/Yuelioi/bilibili-API-collect/tree/cfc5fddcc8a94b74d91970bb5b4eaeb349addc47/docs/login/member_center.md)
4
5use crate::{ BilibiliRequest, BpiClient, BpiError, BpiResponse };
6use serde::{ Deserialize, Serialize };
7
8/// Bilibili 账号信息
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct AccountInfo {
11    /// 我的 mid(用户唯一 ID)
12    pub mid: u64,
13
14    /// 我的昵称
15    pub uname: String,
16
17    /// 我的用户名(登录用的 ID,不一定和昵称相同)
18    pub userid: String,
19
20    /// 我的个性签名
21    pub sign: String,
22
23    /// 我的生日(格式:YYYY-MM-DD)
24    pub birthday: String,
25
26    /// 我的性别
27    /// 取值:
28    /// - `"男"`
29    /// - `"女"`
30    /// - `"保密"`
31    pub sex: String,
32
33    /// 是否未设置昵称
34    /// - `false`:已经设置过昵称
35    /// - `true` :未设置过昵称
36    pub nick_free: bool,
37
38    /// 我的会员等级
39    /// 一般是字符串形式的数字,例如 `"0"`、`"6"`
40    pub rank: String,
41}
42
43impl BpiClient {
44    /// 获取我的账号信息
45    /// # 文档
46    /// [查看API文档](https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/login)
47    pub async fn member_center_account_info(&self) -> Result<BpiResponse<AccountInfo>, BpiError> {
48        let result = self
49            .get("https://api.bilibili.com/x/member/web/account")
50            .send_bpi("获取我的信息").await?;
51
52        Ok(result)
53    }
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59
60    #[tokio::test]
61    async fn test_get_account_info() {
62        let bpi = BpiClient::new();
63
64        match bpi.member_center_account_info().await {
65            Ok(resp) => {
66                if resp.code == 0 {
67                    let data = resp.data.unwrap();
68                    tracing::info!("获取账号成功: mid={}, uname={}", data.mid, data.uname);
69                } else {
70                    tracing::info!("请求失败: code={}, message={}", resp.code, resp.message);
71                }
72            }
73            Err(err) => {
74                panic!("请求出错: {}", err);
75            }
76        }
77    }
78}