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, BpiClient, BpiError, BpiResponse};
6use serde::{Deserialize, Serialize};
7
8/// 退出登录成功后的数据体
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct LogoutData {
11    /// 重定向 URL
12    #[serde(rename = "redirectUrl")]
13    pub redirect_url: String,
14}
15
16/// 退出登录响应结构体
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct LogoutResponse {
19    /// 返回码
20    /// - 0:成功
21    /// - 2202:csrf 请求非法
22    pub code: i32,
23    /// 返回状态,成功为 true
24    pub status: bool,
25    /// 时间戳
26    pub ts: u64,
27    /// 错误信息(成功时不存在)
28    pub message: Option<String>,
29    /// 有效时的 data
30    pub data: Option<LogoutData>,
31}
32
33impl BpiClient {
34    /// 退出登录 (Web端)
35    ///
36    /// # 参数
37    /// - `gourl`:成功后跳转的 URL,可选,默认 `javascript:history.go(-1)`
38    pub async fn logout_web(
39        &self,
40        gourl: Option<&str>,
41    ) -> Result<BpiResponse<LogoutData>, BpiError> {
42        let csrf = self.csrf()?;
43
44        let form = vec![
45            ("biliCSRF", csrf),
46            (
47                "gourl",
48                gourl.unwrap_or("javascript:history.go(-1)").to_string(),
49            ),
50        ];
51
52        let result = self
53            .post("https://passport.bilibili.com/login/exit/v2")
54            .form(&form)
55            .send_bpi("退出登录 (Web端)")
56            .await?;
57
58        Ok(result)
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65
66    #[tokio::test]
67    async fn test_logout_web() -> Result<(), Box<BpiError>> {
68        // let bpi = BpiClient::new();
69        //
70        // match bpi.logout_web(None).await {
71        //     Ok(resp) => {
72        //         if resp.code == 0 {
73        //             let data = resp.data.unwrap();
74        //             tracing::info!("退出登录成功,重定向 URL: {}", data.redirect_url);
75        //         } else {
76        //             tracing::info!(
77        //                 "退出登录失败: code={}, message={:?}",
78        //                 resp.code,
79        //                 resp.message
80        //             );
81        //         }
82        //     }
83        //     Err(err) => {
84        //         panic!("请求出错: {}", err);
85        //     }
86        // }
87        Ok(())
88    }
89}