weixin_rust/agent/
mod.rs

1pub mod tools;
2use crate::{mail_list, media};
3use crate::push_message;
4use crate::customer;
5use serde::Deserialize;
6use serde::Serialize;
7
8#[derive(Debug)]
9pub struct Agent{
10    pub id:u32,
11    corpid:String,
12    agent_secret:String,
13    access_token:String,
14    invalid_time:u64,
15}
16
17
18
19#[derive(Debug,Serialize,Deserialize)]
20struct WxResponse{
21    errcode:u32,
22    errmsg:String,
23}
24
25impl Agent{
26
27    ///构建一个agent对象
28    pub fn new(id:u32,corpid:String,agent_secret:String)->Self{
29        Agent{
30            id,
31            corpid,
32            agent_secret,
33            access_token:String::from(""),
34            invalid_time:0u64,
35        }
36    }
37
38
39    pub fn get_access_token(&self)->&str{
40        &self.access_token
41    }
42
43    /*
44    判断是否需要刷新token
45     */
46    /// 刷新token
47    /// # 提示
48    /// 不是每次都请求微信接口,只有当access_token为""时,或有效时间过了,才会请求
49    pub fn fresh_token(&mut self)->&Self{
50        let now_second=tools::get_current_second();
51        if 0==self.access_token.trim().len() || self.invalid_time<=now_second{//如果token已经失效
52            let result=self.get_token();
53            match result {
54                Ok(r)=>{
55                    if 0==r["errcode"].as_u64().unwrap() {
56                        self.access_token=r["access_token"].as_str().unwrap().to_string();
57                        self.invalid_time=tools::get_current_second()+r["expires_in"].as_u64().unwrap();
58                    }else {
59                        panic!("{}",&r["errmsg"])
60                    }
61                },
62                Err(err)=>{
63                    panic!("{:?}",err)
64                }
65            }
66        }
67        return self;
68    }
69
70    /*
71      gettoken
72      根据corpid和应用secret获取access_token
73     */
74    ///根据corpid和应用secret获取access_token
75    /// # 接口地址
76    ///https://qyapi.weixin.qq.com/cgi-bin/gettoken
77    fn get_token(&mut self)->Result<serde_json::Value, reqwest::Error>{
78        let url = format!(
79            "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}",
80            &self.corpid, &self.agent_secret
81        );
82        tools::get_json(url)
83    }
84
85
86    /*
87      获取通讯录中用户服务对象
88     */
89    ///获取通讯录中用户服务对象
90    /// # 提示
91    /// 该方法会新建一个agent给userservice,这样会导致access_token在各个服务对像中不一至。
92    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
93    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
94    /// # rust继承做法
95    /// ```
96    /// use weixin_rust::agent;
97    /// use weixin_rust::agent::Agent;
98    /// use weixin_rust::mail_list::user::UserService;
99    ///
100    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
101    /// let mut user_service=UserService{agent};//注意agent所有权转移了哦
102    ///
103    /// user_service.agent.get_user_info("code...");//后面只能这样操作喽
104    /// ```
105    pub fn get_user_service(&self)->mail_list::user::UserService{
106        mail_list::user::UserService{agent:Agent{
107            id:self.id,
108            corpid:String::from(&self.corpid),
109            agent_secret:String::from(&self.agent_secret),
110            access_token:String::from(&self.access_token),
111            invalid_time:self.invalid_time,
112        }}
113    }
114
115    /*
116      获取通讯录中部门服务对象
117     */
118    ///获取通讯录中部门服务对象
119    /// # 提示
120    /// 该方法会新建一个agent给deptservice,这样会导致access_token在各个服务对像中不一至。
121    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
122    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
123    /// # rust继承做法
124    /// ```
125    /// use weixin_rust::agent;
126    /// use weixin_rust::agent::Agent;
127    /// use weixin_rust::mail_list::dept::DeptService;
128    ///
129    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
130    /// let mut dept_service=DeptService{agent};//注意agent所有权转移了哦
131    /// dept_service.agent.get_user_info("code...");//后面只能这样操作喽
132    /// ```
133    pub fn get_dept_service(&self)->mail_list::dept::DeptService{
134        mail_list::dept::DeptService{agent:Agent{
135            id:self.id,
136            corpid:String::from(&self.corpid),
137            agent_secret:String::from(&self.agent_secret),
138            access_token:String::from(&self.access_token),
139            invalid_time:self.invalid_time,
140        }}
141    }
142
143    /*
144      获取通讯录中标签服务对象
145     */
146    ///获取通讯录中标签服务对象
147    /// # 提示
148    /// 该方法会新建一个agent给tagservice,这样会导致access_token在各个服务对像中不一至。
149    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
150    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
151    /// # rust继承做法
152    /// ```
153    /// use weixin_rust::agent;
154    /// use weixin_rust::agent::Agent;
155    /// use weixin_rust::mail_list::tag::TagService;
156    ///
157    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
158    /// let mut tag_service=TagService{agent};//注意agent所有权转移了哦
159    ///
160    /// tag_service.agent.get_user_info("code...");//后面只能这样操作喽
161    /// ```
162    pub fn get_tag_service(&self)->mail_list::tag::TagService{
163        mail_list::tag::TagService{agent:Agent{
164            id:self.id,
165            corpid:String::from(&self.corpid),
166            agent_secret:String::from(&self.agent_secret),
167            access_token:String::from(&self.access_token),
168            invalid_time:self.invalid_time,
169        }}
170    }
171
172    /*
173      获取通讯录中异步服务对象
174     */
175    ///获取通讯录中异步服务对象
176    /// # 提示
177    /// 该方法会新建一个agent给syncservice,这样会导致access_token在各个服务对像中不一至。
178    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
179    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
180    /// # rust继承做法
181    /// ```
182    /// use weixin_rust::agent;
183    /// use weixin_rust::agent::Agent;
184    /// use weixin_rust::mail_list::sync::SyncService;
185    ///
186    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
187    /// let mut sync_service=SyncService{agent};//注意agent所有权转移了哦
188    ///
189    /// sync_service.agent.get_user_info("code...");//后面只能这样操作喽
190    /// ```
191    pub fn get_sync_service(&self)->mail_list::sync::SyncService{
192        mail_list::sync::SyncService{agent:Agent{
193            id:self.id,
194            corpid:String::from(&self.corpid),
195            agent_secret:String::from(&self.agent_secret),
196            access_token:String::from(&self.access_token),
197            invalid_time:self.invalid_time,
198        }}
199    }
200
201    /*
202      获取通讯录中标签服务对象
203     */
204    ///获取通讯录中消息服务对象
205    /// # 提示
206    /// 该方法会新建一个agent给messageservice,这样会导致access_token在各个服务对像中不一至。
207    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
208    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
209    /// # rust继承做法
210    /// ```
211    /// use weixin_rust::agent;
212    /// use weixin_rust::agent::Agent;
213    /// use weixin_rust::push_message::MessageService;
214    ///
215    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
216    /// let mut msg_service=MessageService{agent};//注意agent所有权转移了哦
217    ///
218    /// msg_service.agent.get_user_info("code...");//后面只能这样操作喽
219    /// ```
220    pub fn get_message_service(&self)->push_message::MessageService{
221        push_message::MessageService{agent:Agent{
222            id:self.id,
223            corpid:String::from(&self.corpid),
224            agent_secret:String::from(&self.agent_secret),
225            access_token:String::from(&self.access_token),
226            invalid_time:self.invalid_time,
227        }}
228    }
229
230    /*
231      获取通讯录中标签服务对象
232     */
233    ///获取通讯录中客户服务对象
234    /// # 提示
235    /// 该方法会新建一个agent给customerservice,这样会导致access_token在各个服务对像中不一至。
236    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
237    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
238    /// # rust继承做法
239    /// ```
240    /// use weixin_rust::agent;
241    /// use weixin_rust::agent::Agent;
242    /// use weixin_rust::customer::CustomService;
243    ///
244    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
245    /// let mut cus_service=CustomService{agent};//注意agent所有权转移了哦
246    ///
247    /// cus_service.agent.get_user_info("code...");//后面只能这样操作喽
248    ///
249    /// ```
250    pub fn get_customer_service(&self)->customer::CustomerService{
251        customer::CustomerService{agent:Agent{
252            id:self.id,
253            corpid:String::from(&self.corpid),
254            agent_secret:String::from(&self.agent_secret),
255            access_token:String::from(&self.access_token),
256            invalid_time:self.invalid_time,
257        }}
258    }
259
260
261    ///获取媒体上传服务对象
262    /// # 提示
263    /// 该方法会新建一个agent给mediaservice,这样会导致access_token在各个服务对像中不一至。
264    /// 虽然有点不严谨,不过影响不大,要怪只能怪rust对开发设计不太友好,只注重语言设计
265    /// 而真正的rust风格中继承都是采用组合的方式实现的,这样agent的所有权会转移到user_service中,
266    /// # rust继承做法
267    /// ```
268    /// use weixin_rust::agent;
269    /// use weixin_rust::agent::Agent;
270    /// use weixin_rust::media::MediaService;
271    ///
272    /// let mut agent=Agent::new{corpid:"".to_string(),agent_secret:"".to_string()};
273    /// let mut media_service=MediaService{agent};//注意agent所有权转移了哦
274    ///
275    /// media_service.agent.get_user_info("code...");//后面只能这样操作喽
276    /// ```
277    pub fn get_media_service(&self)->media::MediaService{
278        media::MediaService{agent:Agent{
279            id:self.id,
280            corpid:String::from(&self.corpid),
281            agent_secret:String::from(&self.agent_secret),
282            access_token:String::from(&self.access_token),
283            invalid_time:self.invalid_time,
284        }}
285    }
286
287
288    /*
289       获取访问用户身份
290       #url
291       https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo
292     */
293    /// 获取访问用户身份
294    /// # 微信接口
295    /// https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo
296    pub fn get_user_info(&mut self,code:&str)->Result<serde_json::Value, reqwest::Error>{
297        self.fresh_token();
298        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserinfo?access_token={}&code={}",self.get_access_token(),code);
299        tools::get_json(url)
300    }
301
302    /*
303       获取访问用户敏感信息
304       #url
305       https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail
306     */
307    /// 获取访问用户敏感信息
308    /// # 微信接口
309    /// https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail
310    pub fn get_user_detail(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
311        self.fresh_token();
312        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail?access_token={}",self.get_access_token());
313        tools::post_json(url,Some(String::from(json_params)))
314    }
315
316
317    /*
318    get_api_domain_ip
319    获取企业微信API域名IP段
320    */
321    /// 获取企业微信API域名IP段
322    /// # 微信接口
323    /// https://qyapi.weixin.qq.com/cgi-bin/get_api_domain_ip
324    pub fn get_api_domain_ip(&mut self)-> Result<serde_json::Value, reqwest::Error>{
325        self.fresh_token();
326        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/get_api_domain_ip?access_token={}",self.access_token);
327        tools::get_json(url)
328    }
329
330    /*
331     openuserid_to_userid
332     将代开发应用或第三方应用获取的密文open_userid转换为明文userid。
333    */
334    /// 将代开发应用或第三方应用获取的密文open_userid转换为明文userid
335    /// # 微信接口
336    /// https://qyapi.weixin.qq.com/cgi-bin/batch/openuserid_to_userid
337    pub fn openuserid_to_userid(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
338        self.fresh_token();
339        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/batch/openuserid_to_userid?access_token={}",self.access_token);
340        tools::post_json(url,Some(String::from(json_params)))
341    }
342
343    /*
344     convert_tmp_external_userid
345     tmp_external_userid的转换
346    */
347    /// tmp_external_userid的转换
348    /// # 微信接口
349    /// https://qyapi.weixin.qq.com/cgi-bin/idconvert/convert_tmp_external_userid
350    pub fn convert_tmp_external_userid(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
351        self.fresh_token();
352        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/idconvert/convert_tmp_external_userid?access_token={}",self.access_token);
353        tools::post_json(url,Some(String::from(json_params)))
354    }
355
356    /*
357     get_perm_list
358     获取应用的可见范围
359    */
360    ///获取应用的可见范围
361    /// # 接口地址
362    ///https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/agent/get_perm_list
363    pub fn get_perm_list(&mut self)->Result<serde_json::Value, reqwest::Error>{
364        self.fresh_token();
365        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/agent/get_perm_list?access_token={}",self.access_token);
366        tools::post_json(url,None)
367    }
368
369    /*
370     linkedcorp/user/get
371     获取互联企业成员详细信息
372    */
373    /// 获取互联企业成员详细信息
374    /// # 微信接口
375    /// https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/get
376    pub fn get_agent_user(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
377        self.fresh_token();
378        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/get?access_token={}",self.access_token);
379        tools::post_json(url,Some(String::from(json_params)))
380    }
381
382    /*
383     linkedcorp/user/simplelist
384     获取互联企业部门成员
385    */
386    /// 获取互联企业部门成员
387    /// # 微信接口
388    /// https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/simplelist
389    pub fn get_agent_simplelist(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
390        self.fresh_token();
391        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/simplelist?access_token={}",self.access_token);
392        tools::post_json(url,Some(String::from(json_params)))
393    }
394
395    /*
396     linkedcorp/user/list
397     获取互联企业部门成员详情
398    */
399    /// 获取互联企业部门成员详情
400    /// # 微信接口
401    /// https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/list
402    pub fn list_agent_user(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
403        self.fresh_token();
404        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/user/list?access_token={}",self.access_token);
405        tools::post_json(url,Some(String::from(json_params)))
406    }
407
408    /*
409     linkedcorp/user/list
410     获取互联企业部门列表
411     # address
412     https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/department/list
413    */
414    /// 获取互联企业部门列表
415    /// # 微信接口
416    /// https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/department/list
417    pub fn list_agent_dept(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
418        self.fresh_token();
419        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/linkedcorp/department/list?access_token={}",self.access_token);
420        tools::post_json(url,Some(String::from(json_params)))
421    }
422
423    /*
424      获取上下游信息
425     */
426    /// 获取上下游信息
427    /// # 微信接口
428    /// https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/get_chain_list
429    pub fn get_chain_list(&mut self)-> Result<serde_json::Value, reqwest::Error>{
430        self.fresh_token();
431        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/corpgroup/corp/get_chain_list?access_token={}",self.access_token);
432        tools::get_json(url)
433    }
434
435    /*
436      获取指定的应用详情
437      #url
438      https://qyapi.weixin.qq.com/cgi-bin/agent/get
439     */
440    /// 获取指定的应用详情
441    /// # 微信接口
442    /// https://qyapi.weixin.qq.com/cgi-bin/agent/get
443    pub fn get_agent(&mut self,agentid:&str)-> Result<serde_json::Value, reqwest::Error>{
444        self.fresh_token();
445        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token={}&agentid={}",self.access_token,agentid);
446        tools::get_json(url)
447    }
448
449    /*
450     设置应用
451     #url
452     https://qyapi.weixin.qq.com/cgi-bin/agent/set
453     */
454    /// 设置应用
455    /// # 微信接口
456    /// https://qyapi.weixin.qq.com/cgi-bin/agent/set
457    pub fn setup_agent(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
458        self.fresh_token();
459        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/agent/set?access_token={}",self.access_token);
460        tools::post_json(url,Some(String::from(json_params)))
461    }
462
463    /*
464     设置应用在工作台展示的模版
465     #url
466     https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template
467     */
468    /// 设置应用在工作台展示的模版
469    /// # 微信接口
470    /// https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template
471    pub fn set_workbench_template(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
472        self.fresh_token();
473        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/agent/set_workbench_template?access_token={}",self.access_token);
474        tools::post_json(url,Some(String::from(json_params)))
475    }
476
477    /*
478     创建菜单
479     #url
480     https://qyapi.weixin.qq.com/cgi-bin/menu/create
481     */
482    /// 创建菜单
483    /// # 微信接口
484    /// https://qyapi.weixin.qq.com/cgi-bin/menu/create
485    pub fn create_menu(&mut self,json_params:&str)->Result<serde_json::Value, reqwest::Error>{
486        self.fresh_token();
487        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token={}",self.access_token);
488        tools::post_json(url,Some(String::from(json_params)))
489    }
490
491    /*
492     获取菜单
493     #url
494     https://qyapi.weixin.qq.com/cgi-bin/menu/get
495     */
496    /// 获取菜单
497    /// # 微信接口
498    /// https://qyapi.weixin.qq.com/cgi-bin/menu/get
499    pub fn get_menu(&mut self,agentid:&str)->Result<serde_json::Value, reqwest::Error>{
500        self.fresh_token();
501        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/menu/get?access_token={}&agentid={}",self.access_token,agentid);
502        tools::get_json(url)
503    }
504
505    /*
506     删除菜单
507     #url
508     https://qyapi.weixin.qq.com/cgi-bin/menu/get
509     */
510    /// 删除菜单
511    /// # 微信接口
512    ///  https://qyapi.weixin.qq.com/cgi-bin/menu/get
513    pub fn delete_menu(&mut self,agentid:&str)->Result<serde_json::Value, reqwest::Error>{
514        self.fresh_token();
515        let url=format!("https://qyapi.weixin.qq.com/cgi-bin/menu/delete?access_token={}&agentid={}",self.access_token,agentid);
516        tools::get_json(url)
517    }
518
519
520
521}