1use std::collections::HashMap;
2use json::{array, JsonValue};
3use crate::applet::Applet;
4use crate::wecom::WeCom;
5use std::sync::{RwLock};
6use once_cell::sync::Lazy;
7use crate::config::{Channel, Config};
8use crate::subscribe::Subscribe;
9
10pub mod wecom;
11pub mod applet;
12pub mod config;
13mod subscribe;
14
15static ACCOUNT: Lazy<RwLock<HashMap<String, Config>>> = Lazy::new(|| {
16 RwLock::new(HashMap::new())
17});
18
19
20#[derive(Clone)]
21pub enum Wechat {
22 WeCom(WeCom),
24 Applet(Applet),
26 Subscribe(Subscribe),
28 Web,
30 Shop,
32 Open,
34 None,
35}
36
37impl Wechat {
38 pub fn new(config: Config) -> Self {
39 {
40 let mut data = ACCOUNT.write().unwrap();
41 if data.get(&config.appid.clone()).is_none() {
42 data.insert(config.appid.clone(), config.clone());
43 }
44 }
45 let t = ACCOUNT.read().unwrap();
46 let account = t.get(&config.appid).unwrap().clone();
47
48 match account.channel {
49 Channel::WeCom => Wechat::WeCom(WeCom {
50 appid: account.appid,
51 agent_id: account.agent_id,
52 secret: account.secret,
53 access_token: account.access_token,
54 expires_in: account.expires_in,
55 }),
56 Channel::Applet => Wechat::Applet(Applet {
57 appid: account.appid,
58 secret: account.secret,
59 access_token: account.access_token,
60 expires_in: account.expires_in,
61 }),
62 Channel::Subscribe => Wechat::Subscribe(Subscribe {
63 appid: account.appid,
64 secret: account.secret,
65 access_token: account.access_token,
66 expires_in: account.expires_in,
67 ticket_expires_in: 0,
68 ticket: "".to_string(),
69 }),
70 _ => Wechat::None,
71 }
72 }
73 pub fn get_department(&mut self) -> JsonValue {
75 match self {
76 Wechat::WeCom(e) => e.get_department(),
77 Wechat::Applet(_) => array![],
78 _ => array![]
79 }
80 }
81 pub fn get_simplelist(&mut self, department_id: &str) -> JsonValue {
83 match self {
84 Wechat::WeCom(e) => e.get_simplelist(department_id),
85 Wechat::Applet(_) => array![],
86 _ => array![]
87 }
88 }
89 pub fn convert_to_openid(&mut self, userid: &str) -> JsonValue {
91 match self {
92 Wechat::WeCom(e) => e.convert_to_openid(userid),
93 Wechat::Applet(_) => array![],
94 _ => array![]
95 }
96 }
97 pub fn get_simplelist_details(&mut self, department_id: &str) -> JsonValue {
99 match self {
100 Wechat::WeCom(e) => e.get_simplelist_details(department_id),
101 Wechat::Applet(_) => array![],
102 _ => array![]
103 }
104 }
105}
106impl WechatMethod for Wechat {
107 fn check(&mut self) -> Result<bool, String> {
108 match self {
109 Wechat::WeCom(e) => e.check(),
110 Wechat::Applet(e) => e.check(),
111 Wechat::Subscribe(e) => e.check(),
112 _ => Err("暂未开放".to_string()),
113 }
114 }
115
116 fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
117 match self {
118 Wechat::Applet(e) => e.code2_session(code),
119 _ => Err("暂未开通".to_string())
120 }
121 }
122
123 fn login(&mut self, code: &str) -> Result<JsonValue, String> {
124 match self {
125 Wechat::Applet(e) => e.login(code),
126 Wechat::Subscribe(e) => e.login(code),
127 _ => Err("暂未开通".to_string())
128 }
129 }
130
131 fn get_phone_number(&mut self, iv: &str, encrypted_data: &str, session_key: &str) -> Result<JsonValue, String> {
132 match self {
133 Wechat::Applet(e) => e.get_phone_number(iv, encrypted_data, session_key),
134 _ => Err("暂未开通".to_string())
135 }
136 }
137 fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
138 match self {
139 Wechat::WeCom(e) => e.getuserinfo(code),
140 Wechat::Applet(e) => e.getuserinfo(code),
141 Wechat::Subscribe(e) => e.getuserinfo(code),
142 _ => Err("暂未开通".to_string())
143 }
144 }
145
146 fn jsapi_ticket(&mut self, url: &str) -> Result<JsonValue, String> {
147 match self {
148 Wechat::Subscribe(e) => e.jsapi_ticket(url),
149 _ => Err("暂未开通".to_string())
150 }
151 }
152}
153pub trait WechatMethod {
154 fn check(&mut self) -> Result<bool, String>;
156 fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
158 Err(format!("WechatMethod {code} not implemented"))
159 }
160 fn login(&mut self, code: &str) -> Result<JsonValue, String>;
162 fn get_phone_number(&mut self, iv: &str, _encrypted_data: &str, _session_key: &str) -> Result<JsonValue, String> {
164 Err(format!("WechatMethod {iv} not implemented"))
165 }
166 fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
168 Err(format!("WechatMethod {code} not implemented"))
169 }
170 fn jsapi_ticket(&mut self, url: &str) -> Result<JsonValue, String> {
172 Err(format!("WechatMethod {url} not implemented"))
173 }
174}