template.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package template
  2. import (
  3. "encoding/json"
  4. "github.com/yaotian/gowechat/mp/base"
  5. "github.com/yaotian/gowechat/util"
  6. "github.com/yaotian/gowechat/wxcontext"
  7. )
  8. const (
  9. templateSendURL = "https://api.weixin.qq.com/cgi-bin/message/template/send"
  10. templateAddURL = "https://api.weixin.qq.com/cgi-bin/template/api_add_template"
  11. templateAllURL = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template"
  12. templateSetIndustryURL = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry"
  13. templateGetIndustryURL = "https://api.weixin.qq.com/cgi-bin/template/get_industry"
  14. )
  15. //Template 模板消息
  16. type Template struct {
  17. base.MpBase
  18. }
  19. //NewTemplate 实例化
  20. func NewTemplate(context *wxcontext.Context) *Template {
  21. tpl := new(Template)
  22. tpl.Context = context
  23. return tpl
  24. }
  25. //Message 发送的模板消息内容
  26. type Message struct {
  27. ToUser string `json:"touser"` // 必须, 接受者OpenID
  28. TemplateID string `json:"template_id"` // 必须, 模版ID
  29. URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
  30. Color string `json:"color,omitempty"` // 可选, 整个消息的颜色, 可以不设置
  31. Data map[string]*DataItem `json:"data"` // 必须, 模板数据
  32. MiniProgram struct {
  33. AppID string `json:"appid"` //所需跳转到的小程序appid(该小程序appid必须与发模板消息的公众号是绑定关联关系)
  34. PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
  35. } `json:"miniprogram"` //可选,跳转至小程序地址
  36. }
  37. //DataItem 模版内某个 .DATA 的值
  38. type DataItem struct {
  39. Value string `json:"value"`
  40. Color string `json:"color,omitempty"`
  41. }
  42. type resTemplateSend struct {
  43. util.CommonError
  44. MsgID int64 `json:"msgid"`
  45. }
  46. //Send 发送模板消息
  47. func (tpl *Template) Send(msg *Message) (msgID int64, err error) {
  48. response, err := tpl.HTTPPostJSONWithAccessToken(templateSendURL, msg)
  49. if err != nil {
  50. return 0, err
  51. }
  52. var result resTemplateSend
  53. err = json.Unmarshal(response, &result)
  54. if err != nil {
  55. return
  56. }
  57. msgID = result.MsgID
  58. return
  59. }
  60. //IndustryList 行业列表
  61. type IndustryList struct {
  62. PrimaryIndustry *Industry `json:"primary_industry"`
  63. SecondIndustry *Industry `json:"secondary_industry"`
  64. }
  65. //Industry 行业
  66. type Industry struct {
  67. FirstClass string `json:"first_class"`
  68. SecondClass string `json:"second_class"`
  69. }
  70. //Tmpl 模板
  71. type Tmpl struct {
  72. TemplateId string `json:"template_id"`
  73. Title string `json:"title"`
  74. PrimaryIndustry string `json:"primary_industry"`
  75. DeputyIndustry string `json:"deputy_industry"`
  76. }
  77. //TmplList 模板列表
  78. type TmplList struct {
  79. Templates []*Tmpl `json:"template_list"`
  80. }
  81. //AddTemplate 增加一个模板
  82. func (tpl *Template) AddTemplate(templateIDShort string) (templateID string, err error) {
  83. type reqAddTmpl struct {
  84. TemplateIDShort string `json:"template_id_short"`
  85. }
  86. var response []byte
  87. response, err = tpl.HTTPPostJSONWithAccessToken(templateAddURL, reqAddTmpl{TemplateIDShort: templateIDShort})
  88. if err != nil {
  89. return "", err
  90. }
  91. var result Tmpl
  92. err = json.Unmarshal(response, &result)
  93. if err != nil {
  94. return
  95. }
  96. templateID = result.TemplateId
  97. return
  98. }
  99. //GetTemplateList 查询模板列表
  100. func (tpl *Template) GetTemplateList(templateIDShort string) (list TmplList, err error) {
  101. var response []byte
  102. response, err = tpl.HTTPGetWithAccessToken(templateAllURL)
  103. err = json.Unmarshal(response, &list)
  104. return
  105. }
  106. //GetTemplateIndustry 获得模板行业
  107. func (tpl *Template) GetTemplateIndustry() (industryList IndustryList, err error) {
  108. var response []byte
  109. response, err = tpl.HTTPGetWithAccessToken(templateGetIndustryURL)
  110. err = json.Unmarshal(response, &industryList)
  111. return
  112. }
  113. //SetTemplateIndustry 设置模板行业
  114. func (tpl *Template) SetTemplateIndustry(industry1, industry2 int) (err error) {
  115. type reqSetIndustry struct {
  116. Industry1 int `json:"industry_id1"`
  117. Industry2 int `json:"industry_id2"`
  118. }
  119. var req reqSetIndustry
  120. if industry1 > 0 {
  121. req.Industry1 = industry1
  122. }
  123. if industry2 > 0 {
  124. req.Industry2 = industry2
  125. }
  126. _, err = tpl.HTTPPostJSONWithAccessToken(templateSetIndustryURL, req)
  127. return
  128. }