1use serde::{Deserialize, Serialize};
2
3use crate::{
4 error::{CommonError, CommonResponse},
5 wechat::WxApiRequestBuilder,
6 SdkResult,
7};
8
9#[derive(Debug, Serialize, Deserialize)]
10pub struct IndustryPost {
11 pub industry_id1: String,
12 pub industry_id2: String,
13}
14
15#[derive(Debug, Serialize, Deserialize)]
16pub struct IndustryInfo {
17 pub primary_industry: IndustryItem,
18 pub secondary_industry: IndustryItem,
19}
20
21#[derive(Debug, Serialize, Deserialize)]
22pub struct IndustryItem {
23 first_class: String,
24 second_class: String,
25}
26
27#[derive(Serialize, Deserialize, Debug)]
28pub struct AddTplResponse {
29 pub template_id: Option<String>,
30 pub errcode: i32,
31 pub errmsg: String,
32}
33
34#[derive(Debug, Serialize, Deserialize)]
35pub struct TemplateList {
36 pub template_list: Vec<TemplateItem>,
37}
38
39#[derive(Debug, Serialize, Deserialize)]
40pub struct TemplateItem {
41 pub template_id: String,
42 pub title: String,
43 pub primary_industry: String,
44 pub deputy_industry: String,
45 pub content: String,
46 pub example: String,
47}
48
49pub struct TemplateModule<'a, T: WxApiRequestBuilder>(pub(crate) &'a T);
50impl<'a, T: WxApiRequestBuilder> TemplateModule<'a, T> {
51 pub async fn api_set_industry(&self, form: IndustryPost) -> SdkResult<()> {
54 let base_url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry";
55 let sdk = self.0;
56 let msg: CommonError = sdk
57 .wx_post(base_url)
58 .await?
59 .json(&serde_json::json!(form))
60 .send()
61 .await?
62 .json()
63 .await?;
64
65 msg.into()
66 }
67
68 pub async fn get_industry(&self) -> SdkResult<IndustryInfo> {
71 let base_url = "https://api.weixin.qq.com/cgi-bin/template/get_industry";
72 let sdk = self.0;
73 let res: CommonResponse<IndustryInfo> =
74 sdk.wx_get(base_url).await?.send().await?.json().await?;
75
76 res.into()
77 }
78
79 pub async fn api_add_template(&self, template_id_short: String) -> SdkResult<AddTplResponse> {
82 let base_url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template";
83 let sdk = self.0;
84 let msg: AddTplResponse = sdk
85 .wx_post(base_url)
86 .await?
87 .json(&serde_json::json!({
88 "template_id_short": template_id_short
89 }))
90 .send()
91 .await?
92 .json()
93 .await?;
94
95 Ok(msg)
96 }
97
98 pub async fn get_all_private_template(&self) -> SdkResult<TemplateList> {
101 let base_url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template";
102 let sdk = self.0;
103 let res: CommonResponse<TemplateList> =
104 sdk.wx_get(base_url).await?.send().await?.json().await?;
105
106 res.into()
107 }
108
109 pub async fn del_private_template(&self, template_id: String) -> SdkResult<()> {
112 let base_url = "https://api.weixin.qq.com/cgi-bin/template/del_private_template";
113 let sdk = self.0;
114 let msg: CommonError = sdk
115 .wx_post(base_url)
116 .await?
117 .json(&serde_json::json!({ "template_id": template_id }))
118 .send()
119 .await?
120 .json()
121 .await?;
122
123 msg.into()
124 }
125}