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 WeCom(WeCom),
17 Applet(Applet),
19 Subscribe(Subscribe),
21 Web,
23 Shop,
25 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 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 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 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 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 fn check(&mut self) -> Result<bool, String>;
152 fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
154 Err(format!("WechatMethod {code} not implemented"))
155 }
156 fn login(&mut self, code: &str) -> Result<JsonValue, String>;
158 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 fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
164 Err(format!("WechatMethod {code} not implemented"))
165 }
166 fn jsapi_ticket(&mut self, url: &str) -> Result<JsonValue, String> {
168 Err(format!("WechatMethod {url} not implemented"))
169 }
170 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}