bpi_rs/login/member_center/
account.rs

1//! 获取我的信息
2//!
3//! https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/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    /// 文档: https://github.com/SocialSisterYi/bilibili-API-collect/tree/master/docs/login
46    pub async fn member_center_account_info(&self) -> Result<BpiResponse<AccountInfo>, BpiError> {
47        let result = self
48            .get("https://api.bilibili.com/x/member/web/account")
49            .send_bpi("获取我的信息").await?;
50
51        Ok(result)
52    }
53}
54
55#[cfg(test)]
56mod tests {
57    use super::*;
58
59    #[tokio::test]
60    async fn test_get_account_info() {
61        let bpi = BpiClient::new();
62
63        match bpi.member_center_account_info().await {
64            Ok(resp) => {
65                if resp.code == 0 {
66                    let data = resp.data.unwrap();
67                    tracing::info!("获取账号成功: mid={}, uname={}", data.mid, data.uname);
68                } else {
69                    tracing::info!("请求失败: code={}, message={}", resp.code, resp.message);
70                }
71            }
72            Err(err) => {
73                panic!("请求出错: {}", err);
74            }
75        }
76    }
77}