Skip to main content

wechat_mp_auth/
lib.rs

1//! 微信小程序登录 / 用户信息 / 手机号
2//!
3//! 覆盖官方文档:
4//! - [小程序登录](https://developers.weixin.qq.com/miniprogram/dev/server/API/user-login/)
5//! - [用户信息](https://developers.weixin.qq.com/miniprogram/dev/server/API/user-info/)
6
7mod login;
8mod phone;
9mod session;
10mod user_info;
11
12pub use login::{check_session, code2session, reset_session, Code2SessionResponse, ResetSessionResponse};
13pub use phone::{get_phone_number, GetPhoneNumberRequest, PhoneInfo, PhoneNumberResponse, Watermark};
14pub use session::session_signature;
15pub use user_info::{
16    check_encrypted_msg, get_paid_unionid, get_plugin_openpid, get_user_encrypt_key,
17    CheckEncryptedMsgResponse, EncryptKeyInfo, GetPaidUnionidParams, GetUserEncryptKeyResponse,
18    PluginOpenPidResponse,
19};
20
21use wechat_mp_core::MpClient;
22
23/// 登录与用户信息 API 门面
24#[derive(Clone)]
25pub struct AuthApi {
26    client: MpClient,
27}
28
29impl AuthApi {
30    /// 基于已有 `MpClient` 创建
31    pub fn new(client: MpClient) -> Self {
32        Self { client }
33    }
34
35    /// 内部客户端
36    pub fn client(&self) -> &MpClient {
37        &self.client
38    }
39
40    /// 登录凭证校验(code2session)
41    pub async fn code2session(
42        &self,
43        js_code: impl AsRef<str>,
44    ) -> wechat_mp_core::Result<Code2SessionResponse> {
45        code2session(&self.client, js_code.as_ref()).await
46    }
47
48    /// 检验登录态
49    pub async fn check_session(
50        &self,
51        openid: impl AsRef<str>,
52        session_key: impl AsRef<str>,
53    ) -> wechat_mp_core::Result<()> {
54        check_session(&self.client, openid.as_ref(), session_key.as_ref()).await
55    }
56
57    /// 重置登录态
58    pub async fn reset_session(
59        &self,
60        openid: impl AsRef<str>,
61        session_key: impl AsRef<str>,
62    ) -> wechat_mp_core::Result<ResetSessionResponse> {
63        reset_session(&self.client, openid.as_ref(), session_key.as_ref()).await
64    }
65
66    /// 获取手机号
67    pub async fn get_phone_number(
68        &self,
69        code: impl AsRef<str>,
70        openid: Option<&str>,
71    ) -> wechat_mp_core::Result<PhoneNumberResponse> {
72        get_phone_number(
73            &self.client,
74            &GetPhoneNumberRequest {
75                code: code.as_ref().to_string(),
76                openid: openid.map(str::to_string),
77            },
78        )
79        .await
80    }
81
82    /// 获取插件用户 openpid
83    pub async fn get_plugin_openpid(
84        &self,
85        code: impl AsRef<str>,
86    ) -> wechat_mp_core::Result<PluginOpenPidResponse> {
87        get_plugin_openpid(&self.client, code.as_ref()).await
88    }
89
90    /// 检查加密信息
91    pub async fn check_encrypted_msg(
92        &self,
93        encrypted_msg_hash: impl AsRef<str>,
94    ) -> wechat_mp_core::Result<CheckEncryptedMsgResponse> {
95        check_encrypted_msg(&self.client, encrypted_msg_hash.as_ref()).await
96    }
97
98    /// 支付后获取 UnionId
99    pub async fn get_paid_unionid(
100        &self,
101        params: &GetPaidUnionidParams,
102    ) -> wechat_mp_core::Result<String> {
103        get_paid_unionid(&self.client, params).await
104    }
105
106    /// 获取用户 encryptKey
107    pub async fn get_user_encrypt_key(
108        &self,
109        openid: impl AsRef<str>,
110        session_key: impl AsRef<str>,
111    ) -> wechat_mp_core::Result<GetUserEncryptKeyResponse> {
112        get_user_encrypt_key(&self.client, openid.as_ref(), session_key.as_ref()).await
113    }
114}