br_wechat/
lib.rs

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};
8
9pub mod wecom;
10pub mod applet;
11pub mod config;
12
13static ACCOUNT: Lazy<RwLock<HashMap<String, Config>>> = Lazy::new(|| {
14    RwLock::new(HashMap::new())
15});
16
17
18#[derive(Clone)]
19pub enum Wechat {
20    /// 企业微信
21    WeCom(WeCom),
22    /// 小程序
23    Applet(Applet),
24    /// 公众号
25    Subscribe,
26    /// 网站
27    Web,
28    /// 小商店
29    Shop,
30    /// 开放平台
31    Open,
32    None,
33}
34
35impl Wechat {
36    pub fn new(config: Config) -> Self {
37        {
38            let mut data = ACCOUNT.write().unwrap();
39            if data.get(&config.appid.clone()).is_none() {
40                data.insert(config.appid.clone(), config.clone());
41            }
42        }
43        let t = ACCOUNT.read().unwrap();
44        let account = t.get(&config.appid).unwrap().clone();
45
46        match account.channel {
47            Channel::WeCom => Wechat::WeCom(WeCom {
48                appid: account.appid,
49                agent_id: account.agent_id,
50                secret: account.secret,
51                access_token: account.access_token,
52                expires_in: account.expires_in,
53            }),
54            Channel::Applet => Wechat::Applet(Applet {
55                appid: account.appid,
56                secret: account.secret,
57                access_token: account.access_token,
58                expires_in: account.expires_in,
59            }),
60            _ => Wechat::None,
61        }
62    }
63    /// 企业微信获取部门
64    pub fn get_department(&mut self) -> JsonValue {
65        match self {
66            Wechat::WeCom(e) => e.get_department(),
67            Wechat::Applet(_) => array![],
68            _ => array![]
69        }
70    }
71    /// 企业微信获取部门成员
72    pub fn get_simplelist(&mut self, department_id: &str) -> JsonValue {
73        match self {
74            Wechat::WeCom(e) => e.get_simplelist(department_id),
75            Wechat::Applet(_) => array![],
76            _ => array![]
77        }
78    }
79    /// 通过user_id获取open_id
80    pub fn convert_to_openid(&mut self, userid: &str) -> JsonValue {
81        match self {
82            Wechat::WeCom(e) => e.convert_to_openid(userid),
83            Wechat::Applet(_) => array![],
84            _ => array![]
85        }
86    }
87    /// 企业微信获取部门成员详情
88    pub fn get_simplelist_details(&mut self, department_id: &str) -> JsonValue {
89        match self {
90            Wechat::WeCom(e) => e.get_simplelist_details(department_id),
91            Wechat::Applet(_) => array![],
92            _ => array![]
93        }
94    }
95}
96impl WechatMethod for Wechat {
97    fn check(&mut self) -> bool {
98        match self {
99            Wechat::WeCom(e) => e.check(),
100            Wechat::Applet(e) => e.check(),
101            _ => false
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            _ => Err("暂未开通".to_string())
116        }
117    }
118
119    fn get_phone_number(&mut self, iv: &str, encrypted_data: &str, session_key: &str) -> Result<JsonValue, String> {
120        match self {
121            Wechat::Applet(e) => e.get_phone_number(iv, encrypted_data, session_key),
122            _ => Err("暂未开通".to_string())
123        }
124    }
125    fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String> {
126        match self {
127            Wechat::WeCom(e) => e.getuserinfo(code),
128            Wechat::Applet(e) => e.getuserinfo(code),
129            _ => Err("暂未开通".to_string())
130        }
131    }
132}
133pub trait WechatMethod {
134    /// 服务状态检测
135    fn check(&mut self) -> bool;
136    /// 小程序登录
137    fn code2_session(&mut self, code: &str) -> Result<JsonValue, String> {
138        Err(format!("WechatMethod {code} not implemented"))
139    }
140    /// 登录
141    fn login(&mut self, code: &str) -> Result<JsonValue, String>;
142    /// 小程序获取手机号
143    fn get_phone_number(&mut self, iv: &str, _encrypted_data: &str, _session_key: &str) -> Result<JsonValue, String> {
144        Err(format!("WechatMethod {iv} not implemented"))
145    }
146    /// code 获取用户信息 企业微信只能拿到userid
147    fn getuserinfo(&mut self, code: &str) -> Result<JsonValue, String>;
148}