open_wechat/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::{collections::HashMap, sync::Arc};

use tracing::{event, instrument, Level};

use crate::{
    credential::{AccessTokenBuilder, Credential},
    error::Error::InternalServer,
    response::Response,
    Result,
};

/// 存储微信小程序的 appid 和 secret
#[derive(Debug, Clone)]
pub struct Client {
    inner: Arc<ClientInner>,
}

impl Client {
    pub fn new(app_id: &str, secret: &str) -> Self {
        let client = reqwest::Client::new();

        Self {
            inner: Arc::new(ClientInner {
                app_id: app_id.into(),
                secret: secret.into(),
                client,
            }),
        }
    }

    const AUTHENTICATION: &'static str = "https://api.weixin.qq.com/sns/jscode2session";

    /// 登录凭证校验
    /// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/user-login/code2Session.html
    #[instrument(skip(self))]
    pub async fn login(&self, code: &str) -> Result<Credential> {
        event!(Level::DEBUG, "code: {}", code);

        let mut map: HashMap<&str, &str> = HashMap::new();

        map.insert("appid", &self.inner.app_id);
        map.insert("secret", &self.inner.secret);
        map.insert("js_code", code);
        map.insert("grant_type", "authorization_code");

        let response = self
            .inner
            .client
            .get(Self::AUTHENTICATION)
            .query(&map)
            .send()
            .await?;

        event!(Level::DEBUG, "authentication response: {:#?}", response);

        if response.status().is_success() {
            event!(Level::DEBUG, "get credentials");

            let response = response.json::<Response<Credential>>().await?;

            let credential = response.extract()?;

            event!(Level::DEBUG, "credentials: {:#?}", credential);

            Ok(credential)
        } else {
            Err(InternalServer(response.text().await?))
        }
    }

    const ACCESS_TOKEN: &'static str = "https://api.weixin.qq.com/cgi-bin/token";

    /// 获取小程序全局唯一后台接口调用凭据(access_token)
    /// https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/access-token/auth.getAccessToken.html
    #[instrument(skip(self))]
    pub(crate) async fn get_access_token(&self) -> Result<AccessTokenBuilder> {
        let mut map: HashMap<&str, &str> = HashMap::new();

        map.insert("grant_type", "client_credential");
        map.insert("appid", &self.inner.app_id);
        map.insert("secret", &self.inner.secret);

        let response = self
            .inner
            .client
            .get(Self::ACCESS_TOKEN)
            .query(&map)
            .send()
            .await?;

        event!(Level::DEBUG, "response: {:#?}", response);

        if response.status().is_success() {
            event!(Level::DEBUG, "get access_token");

            let res = response.json::<Response<AccessTokenBuilder>>().await?;

            let builder = res.extract()?;

            event!(Level::DEBUG, "access token builder: {:#?}", builder);

            Ok(builder)
        } else {
            Err(InternalServer(response.text().await?))
        }
    }

    const STABLE_ACCESS_TOKEN: &str = "https://api.weixin.qq.com/cgi-bin/stable_token";

    /// 获取小程序全局唯一后台接口调用凭据(access_token)
    /// https://developers.weixin.qq.com/miniprogram/dev/OpenApiDoc/mp-access-token/getStableAccessToken.html
    #[instrument(skip(self))]
    pub(crate) async fn get_stable_access_token(
        &self,
        force_refresh: Option<bool>,
    ) -> Result<AccessTokenBuilder> {
        let mut map: HashMap<&str, String> = HashMap::new();

        map.insert("grant_type", "client_credential".into());
        map.insert("appid", self.inner.app_id.clone());
        map.insert("secret", self.inner.secret.clone());

        if let Some(force_refresh) = force_refresh {
            map.insert("force_refresh", force_refresh.to_string());
        }

        let response = self
            .inner
            .client
            .post(Self::STABLE_ACCESS_TOKEN)
            .json(&map)
            .send()
            .await?;

        event!(Level::DEBUG, "response: {:#?}", response);

        if response.status().is_success() {
            event!(Level::DEBUG, "get stable access_token");

            let response = response.json::<Response<AccessTokenBuilder>>().await?;

            let builder = response.extract()?;

            event!(Level::DEBUG, "stable access token builder: {:#?}", builder);

            Ok(builder)
        } else {
            Err(InternalServer(response.text().await?))
        }
    }
}

#[derive(Debug)]
struct ClientInner {
    app_id: String,
    secret: String,
    client: reqwest::Client,
}