template.go 4.2 KB

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