use std::fs::File;
pub const WECHAT_DEFAULT_USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3534.4 Safari/537.36";
pub const API_DOMAIN: &str = "https://api.weixin.qq.com";
mod errors;
mod message;
mod token;
pub use errors::*;
pub use message::*;
pub use token::*;
pub type WechatResult<T> = Result<T, WechatError>;
#[inline]
pub fn json_decode(data: &str) -> WechatResult<serde_json::Value> {
let obj: serde_json::Value = match serde_json::from_str(data) {
Ok(decoded) => decoded,
Err(ref e) => {
return Err(WechatError::custom(-3, format!("Json decode error: {}", e)));
}
};
let dic = obj.as_object().unwrap();
let code = if dic.contains_key("errcode") {
"errcode"
} else {
"code"
};
let code = obj[code].as_i64().unwrap_or_default();
if code != 0 {
let msg: String = if dic.contains_key("msg") {
obj["msg"].to_string()
} else {
obj["errmsg"].to_string()
};
return Err(WechatError::custom(code as i32, msg));
}
println!("obj====={:?}", obj);
Ok(obj)
}
#[derive(Debug, Clone)]
pub enum PlatformType {
OfficialAccount, OpenPlatfrom, MiniProgram, }
#[derive(Debug, Clone)]
pub struct WechatConfig {
pub app_id: String, pub offical_appid: String, pub secret: String, pub token: String, pub platform: PlatformType, pub mch_id: String, pub private_key: String, pub certificate: String, pub secret_key: String, pub notify_url: String,
pub refund_notify_url: String,
}
impl WechatConfig {
pub fn load(params: serde_json::Value) -> WechatResult<WechatConfig> {
let _conf = WechatConfig {
app_id: params["app-id"].as_str().unwrap_or_default().to_string(),
offical_appid: params["offical-appid"]
.as_str()
.unwrap_or_default()
.to_string(),
secret: params["secret"].as_str().unwrap_or_default().to_string(),
token: params["token"].as_str().unwrap_or_default().to_string(),
platform: PlatformType::MiniProgram,
mch_id: params["mch_id"].as_str().unwrap_or_default().to_string(),
private_key: params["private_key"]
.as_str()
.unwrap_or_default()
.to_string(),
certificate: params["certificate"]
.as_str()
.unwrap_or_default()
.to_string(),
secret_key: params["secret_key"]
.as_str()
.unwrap_or_default()
.to_string(),
notify_url: params["notify-url"]
.as_str()
.unwrap_or_default()
.to_string(),
refund_notify_url: if params["refund-notify-url"].as_str().is_none() {
params["notify-url"]
.as_str()
.unwrap_or_default()
.to_string()
} else {
params["refund-notify-url"]
.as_str()
.unwrap_or_default()
.to_string()
},
};
Ok(_conf)
}
pub fn load_yaml(conf_path: &str) -> WechatResult<WechatConfig> {
use yaml_rust::yaml;
let mut f = match File::open(conf_path) {
Ok(f) => f,
Err(e) => {
return Err(WechatError::custom(4004, format!("{}", e)));
}
};
let mut s = String::new();
use std::io::Read;
match f.read_to_string(&mut s) {
Ok(s) => s,
Err(e) => {
return Err(WechatError::custom(
4004,
format!("Error Reading file: {}", e),
));
}
};
let docs = yaml::YamlLoader::load_from_str(&s).unwrap();
let _yaml_doc = &docs[0];
Ok(WechatConfig::default())
}
}
impl Default for WechatConfig {
fn default() -> Self {
WechatConfig {
app_id: String::new(),
offical_appid: String::new(),
secret: String::new(),
token: String::new(),
platform: PlatformType::MiniProgram,
mch_id: "".to_string(),
private_key: "".to_string(),
certificate: "".to_string(),
secret_key: "".to_string(),
notify_url: "".to_string(),
refund_notify_url: "".to_string(),
}
}
}