-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod.rs
150 lines (133 loc) · 3.67 KB
/
mod.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
use crate::{
client::common::model::Response,
error::{new_api_error, Result},
utils::http::{do_http, PostParameters},
};
use common::model::{Responser, Token};
use reqwest::Method;
use serde::{de::DeserializeOwned, ser::Serialize};
use serde_json::{json, Value};
use std::env;
use tracing::debug;
const BASE_URL: &str = "https://qyapi.weixin.qq.com/cgi-bin";
#[derive(Debug)]
pub struct Client {
pub(crate) corp_id: String,
pub(crate) corp_secret: String,
/// 客户联系专用
pub(crate) custom_contact_secret: Option<String>,
}
impl Client {
pub fn new(
corp_id: String,
corp_secret: String,
custom_contact_secret: Option<String>,
) -> Client {
Self {
corp_id,
corp_secret,
custom_contact_secret,
}
}
pub fn new_from_env() -> Result<Self> {
let custom_contact_secret = if let Ok(ccs) = env::var("CUSTOM_CONTACT_SECRET") {
Some(ccs)
} else {
None
};
Ok(Self {
corp_id: env::var("CORP_ID")?,
corp_secret: env::var("CORP_SECRET")?,
custom_contact_secret,
})
}
async fn access_token(&self) -> Result<String> {
let query_body = json!({
"corpid": self.corp_id,
"corpsecret": self.corp_secret,
});
let resp = do_http(
Method::GET,
&format!("{}/gettoken", BASE_URL),
None,
Some(query_body),
None,
)
.await?;
let data = resp.json::<Response<Token>>().await?;
if data.err_code != 0 {
return Err(new_api_error(data.err_code, data.err_msg));
}
let token = data.data.unwrap();
Ok(token.access_token)
// 测试验证
// let data = resp.text().await?;
// println!("==>> access_token request: {data}");
// Ok(String::new())
}
async fn custom_contact_access_token(&self) -> Result<String> {
let query_body = json!({
"corpid": self.corp_id,
"corpsecret": self.custom_contact_secret,
});
let resp = do_http(
Method::GET,
&format!("{}/gettoken", BASE_URL),
None,
Some(query_body),
None,
)
.await?;
let data = resp.json::<Response<Token>>().await?;
if data.err_code != 0 {
return Err(new_api_error(data.err_code, data.err_msg));
}
let token = data.data.unwrap();
Ok(token.access_token)
}
// http 请求
async fn request<R: Responser + DeserializeOwned + Serialize + Default>(
&self,
method: Method,
url: &str,
body: Option<Value>,
) -> Result<R> {
let body = if let Some(data) = body {
Some(PostParameters::json(data))
} else {
None
};
let resp = do_http(method, url, None, None, body)
.await?
.json::<R>()
.await?;
debug!("\nurl:{url} \nresponse: {}", serde_json::to_string(&resp)?);
if resp.error_code() != 0 {
return Err(new_api_error(resp.error_code(), resp.error_message()));
}
Ok(resp)
}
}
/// 通讯录管理
mod contact;
pub use contact::*;
/// 客户联系管理
mod external_contact;
pub use external_contact::*;
/// 微信客服
mod wechat_custom_service;
pub use wechat_custom_service::*;
/// 身份验证
mod auth;
pub use auth::*;
/// 应用管理
mod app;
pub use app::*;
/// 消息推送
mod message_push;
pub use message_push::*;
/// 事件定义
pub mod event;
// TODO: 其他功能模块接口实现
/// 公共组件
pub mod common;