Skip to main content

wx_sdk/mp/
template.rs

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    /// 设置所属行业
52    /// 设置行业可在微信公众平台后台完成,每月可修改行业1次,帐号仅可使用所属行业中相关的模板,为方便第三方开发者,提供通过接口调用的方式来修改账号所属行业,具体如下:
53    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    /// 获取设置的行业信息
69    /// 获取帐号设置的行业信息。可登录微信公众平台,在公众号后台中查看行业信息。为方便第三方开发者,提供通过接口调用的方式来获取帐号所设置的行业信息,具体如下:
70    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    /// 获得模板ID
80    /// 从行业模板库选择模板到帐号后台,获得模板ID的过程可在微信公众平台后台完成。为方便第三方开发者,提供通过接口调用的方式来获取模板ID,具体如下:
81    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    /// 获取模板列表
99    /// 获取已添加至帐号下所有模板列表,可在微信公众平台后台中查看模板列表信息。为方便第三方开发者,提供通过接口调用的方式来获取帐号下所有模板信息,具体如下:
100    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    /// 删除模板
110    /// 删除模板可在微信公众平台后台完成,为方便第三方开发者,提供通过接口调用的方式来删除某帐号下的模板,具体如下:
111    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}