1 Star 0 Fork 0

h79 / gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
request.go 3.83 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-09-13 21:08 . structs
/*
*
- https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
第一类 模板消息
- 1 设置所属行业
- 2 获取设置的行业信息
- 3 获得模板ID
- 4 获取模板列表
- 5 删除模板
- 6 发送模板消息
*/
package template
import (
"encoding/json"
"fmt"
"gitee.com/h79/gothird/token"
"gitee.com/h79/gothird/weixin/consts"
"gitee.com/h79/gothird/weixin/offia"
"gitee.com/h79/gothird/weixin/response"
"gitee.com/h79/goutils/common/http"
)
// 设置所属行业,每月可修改行业1次
// 数据示例如下:
//
// {
// "industry_id1":"1",
// "industry_id2":"4"
// }
//
// POST https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=ACCESS_TOKEN
func SetIndustry(api *token.Api, req *offia.IndustryReq) error {
uri := fmt.Sprintf("%s/cgi-bin/template/api_set_industry?", consts.ApiPrefixUrl)
buf, err := json.Marshal(&req)
if err != nil {
return err
}
res := response.Response{}
return api.Request("POST", uri, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
}
// 获取设置的行业信息
// GET https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=ACCESS_TOKEN
// ErrorIf
// {
// "primary_industry":{"first_class":"运输与仓储","second_class":"快递"},
// "secondary_industry":{"first_class":"IT科技","second_class":"互联网|电子商务"}
// }
func GetIndustry(api *token.Api) (*offia.IndustryS, error) {
uri := fmt.Sprintf("%s/cgi-bin/template/get_industry?", consts.ApiPrefixUrl)
type industryResult struct {
response.Response
offia.IndustryS
}
res := industryResult{}
er := api.Request("GET", uri, nil, func(hp *http.Http, body []byte) error {
if err := json.Unmarshal(body, &res); err != nil {
return err
}
return res.ReturnIf(api)
})
return &res.IndustryS, er
}
// 获得模板ID
// POST https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=ACCESS_TOKEN
func GetId(api *token.Api, tmShortId string) (string, error) {
uri := fmt.Sprintf("%s/cgi-bin/template/api_add_template?", consts.ApiPrefixUrl)
type idReq struct {
ShortId string `json:"template_id_short"`
}
req := idReq{
ShortId: tmShortId,
}
buf, err := json.Marshal(&req)
if err != nil {
return "", err
}
type idResult struct {
response.Response
Id string `json:"template_id"`
}
res := idResult{}
er := api.Request("POST", uri, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
return res.Id, er
}
// 获取模板列表
// GET https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN
func GetList(api *token.Api) ([]offia.TMInfo, error) {
uri := fmt.Sprintf("%s/cgi-bin/template/get_all_private_template?", consts.ApiPrefixUrl)
type listResult struct {
response.Response
TMList []offia.TMInfo `json:"template_list"`
}
res := listResult{}
er := api.Request("GET", uri, nil, func(hp *http.Http, body []byte) error {
if err := json.Unmarshal(body, &res); err != nil {
return err
}
return res.ReturnIf(api)
})
return res.TMList, er
}
// 删除模板
// POST https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN
// {
// "template_id" : "Dyvp3-Ff0cnail_CDSzk1fIc6-9lOkxsQE7exTJbwUE"
// }
func Delete(api *token.Api, tmId string) error {
uri := fmt.Sprintf("%s/cgi-bin/template/del_private_template?", consts.ApiPrefixUrl)
type delReq struct {
Id string `json:"template_id"`
}
req := delReq{
Id: tmId,
}
buf, err := json.Marshal(&req)
if err != nil {
return err
}
res := response.Response{}
return api.Request("POST", uri, buf, func(hp *http.Http, body []byte) error {
if er := json.Unmarshal(body, &res); er != nil {
return er
}
return res.ReturnIf(api)
})
}
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.101

搜索帮助