rust_wechat_weapp/api/
user_info.rs

1use crate::Client;
2use rust_wechat_codegen::ServerResponse;
3use rust_wechat_core::client::ClientTrait;
4use serde::{Deserialize, Serialize};
5
6const GET_PLUGIN_OPEN_PID_URL: &str = "https://api.weixin.qq.com/wxa/getpluginopenpid";
7const CHECK_ENCRYPTED_MSG_URL: &str = "https://api.weixin.qq.com/wxa/business/checkencryptedmsg";
8const GET_PAID_UNIONID_URL: &str = "https://api.weixin.qq.com/wxa/getpaidunionid?";
9
10#[derive(Debug, Serialize, Deserialize, ServerResponse)]
11#[sr(flatten)]
12pub struct PluginOpenPid {
13    pub openpid: String,
14}
15#[derive(Debug, Serialize, Deserialize, ServerResponse)]
16#[sr(flatten)]
17pub struct CheckEncryptedMsg {
18    pub valid: bool,
19    pub create_time: u32,
20}
21#[derive(Debug, Serialize, Deserialize, ServerResponse)]
22#[sr(flatten)]
23pub struct PaidUnionid {
24    pub unionid: String,
25}
26
27impl Client {
28    pub async fn get_plugin_open_pid(&self, code: &str) -> crate::Result<PluginOpenPid> {
29        let url = self.authorized_url(GET_PLUGIN_OPEN_PID_URL).await?;
30        let response: PluginOpenPidResponse = self.json(url, &[("code", code)]).await?;
31
32        Ok(response.data()?)
33    }
34    pub async fn check_encrypted_msg(
35        &self,
36        encrypted_msg_hash: &str,
37    ) -> crate::Result<CheckEncryptedMsg> {
38        let url = self.authorized_url(CHECK_ENCRYPTED_MSG_URL).await?;
39        let response: CheckEncryptedMsgResponse = self
40            .json(url, &[("encrypted_msg_hash", encrypted_msg_hash)])
41            .await?;
42
43        Ok(response.data()?)
44    }
45    pub async fn get_paid_unionid_by_transaction(
46        &self,
47        openid: &str,
48        transaction_id: &str,
49    ) -> crate::Result<PaidUnionid> {
50        let access_token = self.access_token().await?;
51
52        let response: PaidUnionidResponse = self
53            .get(
54                GET_PAID_UNIONID_URL,
55                &[
56                    ("openid", openid),
57                    ("access_token", &access_token),
58                    ("transaction_id", transaction_id),
59                ],
60            )
61            .await?;
62        Ok(response.data()?)
63    }
64    pub async fn get_paid_unionid_by_trade_no(
65        &self,
66        openid: &str,
67        mch_id: &str,
68        out_trade_no: &str,
69    ) -> crate::Result<PaidUnionid> {
70        let access_token = self.access_token().await?;
71
72        let response: PaidUnionidResponse = self
73            .get(
74                GET_PAID_UNIONID_URL,
75                &[
76                    ("openid", openid),
77                    ("access_token", &access_token),
78                    ("mch_id", mch_id),
79                    ("out_trade_no", out_trade_no),
80                ],
81            )
82            .await?;
83        Ok(response.data()?)
84    }
85}