br_wechat/
lib.rs

1use json::{JsonValue};
2use crate::applet::Applet;
3use crate::wecom::WeCom;
4use crate::config::{Channel, Config};
5use crate::subscribe::Subscribe;
6
7pub mod wecom;
8pub mod applet;
9pub mod config;
10mod subscribe;
11
12
13#[derive(Clone)]
14pub enum Wechat {
15    /// 企业微信
16    WeCom(WeCom),
17    /// 小程序
18    Applet(Applet),
19    /// 公众号
20    Subscribe(Subscribe),
21    /// 网站
22    Web,
23    /// 小商店
24    Shop,
25    /// 开放平台
26    Open,
27    None,
28}
29
30impl Wechat {
31    pub fn new(config: Config) -> Self {
32        match config.channel {
33            Channel::WeCom => Wechat::WeCom(WeCom {
34                appid: config.appid,
35                agent_id: config.agent_id,
36                secret: config.secret,
37                access_token: config.access_token,
38                expires_in: config.expires_in,
39            }),
40            Channel::Applet => Wechat::Applet(Applet {
41                appid: config.appid,
42                secret: config.secret,
43                access_token: config.access_token,
44                expires_in: config.expires_in,
45            }),
46            Channel::Subscribe => Wechat::Subscribe(Subscribe {
47                appid: config.appid,
48                secret: config.secret,
49                access_token: config.access_token,
50                expires_in: config.expires_in,
51                ticket_expires_in: 0,
52                ticket: "".to_string(),
53            }),
54            _ => Wechat::None,
55        }
56    }
57    /// 企业微信获取部门
58    pub fn get_department(&mut self) -> Result<JsonValue, String> {
59        match self {
60            Wechat::WeCom(e) => e.get_department(),
61            _ => Err("暂未开放".to_string()),
62        }
63    }
64    /// 企业微信获取部门成员
65    pub fn get_simplelist(&mut self, department_id: &str)-> Result<JsonValue, String> {
66        match self {
67            Wechat::WeCom(e) => e.get_simplelist(department_id),
68            _ => Err("暂未开放".to_string()),
69        }
70    }
71    /// 通过user_id获取open_id
72    pub fn convert_to_openid(&mut self, userid: &str)-> Result<JsonValue, String> {
73        match self {
74            Wechat::WeCom(e) => e.convert_to_openid(userid),
75            _ => Err("暂未开放".to_string()),
76        }
77    }
78    /// 企业微信获取部门成员详情
79    pub fn get_simplelist_details(&mut self, department_id: &str) -> Result<JsonValue, String> {
80        match self {
81            Wechat::WeCom(e) => e.get_simplelist_details(department_id),
82            _ => Err("暂未开放".to_string()),
83        }
84    }
85}
86impl WechatMethod for Wechat {
87    fn access_token(&mut self) -> Result<JsonValue, String> {
88        match self {
89            Wechat::WeCom(e) => e.access_token(),
90            Wechat::Applet(e) => e.access_token(),
91            Wechat::Subscribe(e) => e.access_token(),
92            _ => Err("暂未开放".to_string()),
93        }
94    }
95
96    fn check(&mut self) -> Result<bool, String> {
97        match self {
98            Wechat::WeCom(e) => e.check(),
99            Wechat::Applet(e) => e.check(),
100            Wechat::Subscribe(e) => e.check(),
101            _ => Err("暂未开放".to_string()),
102        }
103    }
104
105    fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
106        match self {
107            Wechat::Applet(e) => e.code2_session(code),
108            _ => Err("暂未开通".to_string())
109        }
110    }
111
112    fn login(&mut self, code: &str) -> Result<JsonValue, String> {
113        match self {
114            Wechat::Applet(e) => e.login(code),
115            Wechat::Subscribe(e) => e.login(code),
116            _ => Err("暂未开通".to_string())
117        }
118    }
119
120    fn get_phone_number(&mut self, iv: &str, encrypted_data: &str, session_key: &str) -> Result<JsonValue, String> {
121        match self {
122            Wechat::Applet(e) => e.get_phone_number(iv, encrypted_data, session_key),
123            _ => Err("暂未开通".to_string())
124        }
125    }
126    fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
127        match self {
128            Wechat::WeCom(e) => e.getuserinfo(code),
129            Wechat::Applet(e) => e.getuserinfo(code),
130            Wechat::Subscribe(e) => e.getuserinfo(code),
131            _ => Err("暂未开通".to_string())
132        }
133    }
134
135    fn jsapi_ticket(&mut self, url: &str) -> Result<JsonValue, String> {
136        match self {
137            Wechat::Subscribe(e) => e.jsapi_ticket(url),
138            _ => Err("暂未开通".to_string())
139        }
140    }
141    fn send_message(&mut self, template_id: &str, page: &str, touser: &str, data: JsonValue, miniprogram_state: &str, lang: &str) -> Result<JsonValue, String> {
142        match self {
143            Wechat::Applet(e) => e.send_message(template_id, page, touser, data, miniprogram_state, lang),
144            _ => Err("暂未开通".to_string())
145        }
146    }
147}
148pub trait WechatMethod {
149    fn access_token(&mut self) -> Result<JsonValue, String>;
150    /// 服务状态检测
151    fn check(&mut self) -> Result<bool, String>;
152    /// 小程序登录
153    fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
154        Err(format!("WechatMethod {code} not implemented"))
155    }
156    /// 登录
157    fn login(&mut self, code: &str) -> Result<JsonValue, String>;
158    /// 小程序获取手机号
159    fn get_phone_number(&mut self, iv: &str, _encrypted_data: &str, _session_key: &str) -> Result<JsonValue, String> {
160        Err(format!("WechatMethod {iv} not implemented"))
161    }
162    /// code 获取用户信息 企业微信只能拿到userid
163    fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
164        Err(format!("WechatMethod {code} not implemented"))
165    }
166    /// 公众号JS接口的临时票据
167    fn jsapi_ticket(&mut self, url: &str) -> Result<JsonValue, String> {
168        Err(format!("WechatMethod {url} not implemented"))
169    }
170    /// 发送订阅消息
171    /// * template_id 模版ID
172    /// * page 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数
173    /// * touser 接收者(用户)的 openid
174    /// * data 模板内容,格式形如{ "phrase3": { "value": "审核通过" }, "name1": { "value": "订阅" }, "date2": { "value": "2019-12-25 09:42" } }
175    fn send_message(&mut self, template_id: &str, page: &str, touser: &str, _data: JsonValue, _miniprogram_state: &str, _lang: &str) -> Result<JsonValue, String> {
176        Err(format!("sendMessage {template_id} {page} {touser}"))
177    }
178}