github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/message/template/template.go (about)

     1  package template
     2  
     3  import (
     4  	"github.com/chanxuehong/wechat/mp/core"
     5  )
     6  
     7  // 设置所属行业.
     8  func SetIndustry(clt *core.Client, industryId1, industryId2 int64) (err error) {
     9  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token="
    10  
    11  	var request = struct {
    12  		IndustryId1 int64 `json:"industry_id1"`
    13  		IndustryId2 int64 `json:"industry_id2"`
    14  	}{
    15  		IndustryId1: industryId1,
    16  		IndustryId2: industryId2,
    17  	}
    18  	var result core.Error
    19  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    20  		return
    21  	}
    22  	if result.ErrCode != core.ErrCodeOK {
    23  		err = &result
    24  		return
    25  	}
    26  	return
    27  }
    28  
    29  type Industry struct {
    30  	FirstClass  string `json:"first_class"`
    31  	SecondClass string `json:"second_class"`
    32  }
    33  
    34  // 获取设置的行业信息
    35  func GetIndustry(clt *core.Client) (primaryIndustry, secondaryIndustry Industry, err error) {
    36  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token="
    37  
    38  	var result struct {
    39  		core.Error
    40  		PrimaryIndustry   Industry `json:"primary_industry"`
    41  		SecondaryIndustry Industry `json:"secondary_industry"`
    42  	}
    43  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
    44  		return
    45  	}
    46  	if result.ErrCode != core.ErrCodeOK {
    47  		err = &result.Error
    48  		return
    49  	}
    50  	primaryIndustry = result.PrimaryIndustry
    51  	secondaryIndustry = result.SecondaryIndustry
    52  	return
    53  }
    54  
    55  // 从行业模板库选择模板添加到账号后台, 并返回模板id.
    56  //
    57  //	templateIdShort: 模板库中模板的编号, 有"TM**"和"OPENTMTM**"等形式.
    58  func AddPrivateTemplate(clt *core.Client, templateIdShort string) (templateId string, err error) {
    59  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token="
    60  
    61  	var request = struct {
    62  		TemplateIdShort string `json:"template_id_short"`
    63  	}{
    64  		TemplateIdShort: templateIdShort,
    65  	}
    66  	var result struct {
    67  		core.Error
    68  		TemplateId string `json:"template_id"`
    69  	}
    70  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
    71  		return
    72  	}
    73  	if result.ErrCode != core.ErrCodeOK {
    74  		err = &result.Error
    75  		return
    76  	}
    77  	templateId = result.TemplateId
    78  	return
    79  }
    80  
    81  // 模板数据结构
    82  //
    83  //	{
    84  //	    "template_id": "iPk5sOIt5X_flOVKn5GrTFpncEYTojx6ddbt8WYoV5s",
    85  //	    "title": "领取奖金提醒",
    86  //	    "primary_industry": "IT科技",
    87  //	    "deputy_industry": "互联网|电子商务",
    88  //	    "content": "{ {result.DATA} }\n\n领奖金额:{ {withdrawMoney.DATA} }\n领奖  时间:{ {withdrawTime.DATA} }\n银行信息:{ {cardInfo.DATA} }\n到账时间:  { {arrivedTime.DATA} }\n{ {remark.DATA} }",
    89  //	    "example": "您已提交领奖申请\n\n领奖金额:xxxx元\n领奖时间:2013-10-10 12:22:22\n银行信息:xx银行(尾号xxxx)\n到账时间:预计xxxxxxx\n\n预计将于xxxx到达您的银行卡"
    90  //	}
    91  type Template struct {
    92  	TemplateId      string `json:"template_id"`
    93  	Title           string `json:"title"`
    94  	PrimaryIndustry string `json:"primary_industry"`
    95  	DeputyIndustry  string `json:"deputy_industry"`
    96  	Content         string `json:"content"`
    97  	Example         string `json:"example"`
    98  }
    99  
   100  // 获取模板列表
   101  func GetAllPrivateTemplate(clt *core.Client) (templateList []Template, err error) {
   102  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token="
   103  
   104  	var result struct {
   105  		core.Error
   106  		TemplateList []Template `json:"template_list"`
   107  	}
   108  	if err = clt.GetJSON(incompleteURL, &result); err != nil {
   109  		return
   110  	}
   111  	if result.ErrCode != core.ErrCodeOK {
   112  		err = &result.Error
   113  		return
   114  	}
   115  	templateList = result.TemplateList
   116  	return
   117  }
   118  
   119  // 删除模板.
   120  func DeletePrivateTemplate(clt *core.Client, templateId string) (err error) {
   121  	const incompleteURL = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token="
   122  
   123  	var request = struct {
   124  		TemplateId string `json:"template_id"`
   125  	}{
   126  		TemplateId: templateId,
   127  	}
   128  	var result core.Error
   129  	if err = clt.PostJSON(incompleteURL, &request, &result); err != nil {
   130  		return
   131  	}
   132  	if result.ErrCode != core.ErrCodeOK {
   133  		err = &result
   134  		return
   135  	}
   136  	return
   137  }