Skip to main content

bpi_rs/login/
exit.rs

1// 退出登录 (Web端)
2//
3// [文档](https://socialsisteryi.github.io/bilibili-API-collect/docs/login/exit.html#退出登录-web端)
4
5use crate::BilibiliRequest;
6use crate::BpiResult;
7use crate::login::LoginClient;
8use serde::{Deserialize, Serialize};
9
10const LOGOUT_WEB_ENDPOINT: &str = "https://passport.bilibili.com/login/exit/v2";
11
12/// 退出登录成功后的数据体
13
14#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct LogoutData {
16    /// 重定向 URL
17    #[serde(rename = "redirectUrl")]
18    pub redirect_url: String,
19}
20
21/// 退出登录响应结构体
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct LogoutResponse {
24    /// 返回码
25    /// - 0:成功
26    /// - 2202:csrf 请求非法
27    pub code: i32,
28    /// 返回状态,成功为 true
29    pub status: bool,
30    /// 时间戳
31    pub ts: u64,
32    /// 错误信息(成功时不存在)
33    pub message: Option<String>,
34    /// 有效时的 data
35    pub data: Option<LogoutData>,
36}
37
38/// Parameters for web logout.
39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct LogoutWebParams {
41    gourl: String,
42}
43
44impl Default for LogoutWebParams {
45    fn default() -> Self {
46        Self {
47            gourl: "javascript:history.go(-1)".to_string(),
48        }
49    }
50}
51
52impl LogoutWebParams {
53    pub fn new() -> Self {
54        Self::default()
55    }
56
57    pub fn gourl(mut self, gourl: impl Into<String>) -> Self {
58        self.gourl = gourl.into();
59        self
60    }
61
62    fn form_pairs(&self, csrf: &str) -> Vec<(&'static str, String)> {
63        vec![
64            ("biliCSRF", csrf.to_string()),
65            ("gourl", self.gourl.clone()),
66        ]
67    }
68}
69
70impl<'a> LoginClient<'a> {
71    /// Logs out the current web session and returns the canonical payload result.
72    pub async fn logout(&self, params: LogoutWebParams) -> BpiResult<LogoutData> {
73        let csrf = self.client.csrf()?;
74        let form = params.form_pairs(&csrf);
75
76        self.client
77            .post(LOGOUT_WEB_ENDPOINT)
78            .form(&form)
79            .send_bpi_payload("login.logout")
80            .await
81    }
82}
83
84#[cfg(test)]
85mod tests {}