1mod 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#[derive(Clone)]
25pub struct AuthApi {
26 client: MpClient,
27}
28
29impl AuthApi {
30 pub fn new(client: MpClient) -> Self {
32 Self { client }
33 }
34
35 pub fn client(&self) -> &MpClient {
37 &self.client
38 }
39
40 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 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 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 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 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 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 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 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}