use serde::{Deserialize, Serialize};
use crate::{
error::{CommonError, CommonResponse},
wechat::WxApiRequestBuilder,
SdkResult,
};
#[derive(Debug, Serialize, Deserialize)]
pub struct IndustryPost {
pub industry_id1: String,
pub industry_id2: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IndustryInfo {
pub primary_industry: IndustryItem,
pub secondary_industry: IndustryItem,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IndustryItem {
first_class: String,
second_class: String,
}
#[derive(Serialize, Deserialize, Debug)]
pub struct AddTplResponse {
pub template_id: Option<String>,
pub errcode: i32,
pub errmsg: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateList {
pub template_list: Vec<TemplateItem>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct TemplateItem {
pub template_id: String,
pub title: String,
pub primary_industry: String,
pub deputy_industry: String,
pub content: String,
pub example: String,
}
pub struct TemplateModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
impl<'a, T: WxApiRequestBuilder> TemplateModule<'a, T> {
pub async fn api_set_industry(&self, form: IndustryPost) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!(form))
.send()
.await?
.json()
.await?;
msg.into()
}
pub async fn get_industry(&self) -> SdkResult<IndustryInfo> {
let base_url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
let sdk = self.0;
let res: CommonResponse<IndustryInfo> =
sdk.wx_get(base_url).await?.send().await?.json().await?;
res.into()
}
pub async fn api_add_template(&self, template_id_short: String) -> SdkResult<AddTplResponse> {
let base_url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template";
let sdk = self.0;
let msg: AddTplResponse = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({
"template_id_short": template_id_short
}))
.send()
.await?
.json()
.await?;
Ok(msg)
}
pub async fn get_all_private_template(&self) -> SdkResult<TemplateList> {
let base_url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template";
let sdk = self.0;
let res: CommonResponse<TemplateList> =
sdk.wx_get(base_url).await?.send().await?.json().await?;
res.into()
}
pub async fn del_private_template(&self, template_id: String) -> SdkResult<()> {
let base_url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template";
let sdk = self.0;
let msg: CommonError = sdk
.wx_post(base_url)
.await?
.json(&serde_json::json!({ "template_id": template_id }))
.send()
.await?
.json()
.await?;
msg.into()
}
}