Skip to main content

ncm_api_rs/api/
login_cellphone.rs

1use super::Query;
2use crate::error::Result;
3/// 手机号登录
4/// 对应 Node.js module/login_cellphone.js
5use crate::request::{ApiClient, ApiResponse, CryptoType};
6use md5::{Digest, Md5};
7use serde_json::{json, Value};
8
9impl ApiClient {
10    /// 手机号登录
11    /// 对应 /login/cellphone
12    pub async fn login_cellphone(&self, query: &Query) -> Result<ApiResponse> {
13        let mut data = json!({
14            "type": "1",
15            "https": "true",
16            "phone": query.get_or("phone", ""),
17            "countrycode": query.get_or("countrycode", "86"),
18            "remember": "true"
19        });
20
21        if let Some(captcha) = query.get("captcha") {
22            data["captcha"] = Value::String(captcha.to_string());
23            // Node.js 在有 captcha 时同时设置 captcha 字段(上面已设置)
24            // 且 password 字段也设为 captcha 的值
25            data["password"] = Value::String(captcha.to_string());
26        } else {
27            let password = if let Some(md5_pwd) = query.get("md5_password") {
28                md5_pwd.to_string()
29            } else if let Some(pwd) = query.get("password") {
30                format!("{:x}", Md5::digest(pwd.as_bytes()))
31            } else {
32                String::new()
33            };
34            data["password"] = Value::String(password);
35        }
36
37        self.request(
38            "/api/w/login/cellphone",
39            data,
40            query.to_option(CryptoType::Weapi),
41        )
42        .await
43    }
44}