subscribe.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package subscribe
  2. import (
  3. "fmt"
  4. "github.com/silenceper/wechat/v2/miniprogram/context"
  5. "github.com/silenceper/wechat/v2/util"
  6. )
  7. const (
  8. //发送订阅消息
  9. //https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
  10. subscribeSendURL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send"
  11. // 获取当前帐号下的个人模板列表
  12. // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
  13. getTemplateURL = "https://api.weixin.qq.com/wxaapi/newtmpl/gettemplate"
  14. // 统一服务消息
  15. // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html
  16. uniformMessageSend = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send"
  17. )
  18. // Subscribe 订阅消息
  19. type Subscribe struct {
  20. *context.Context
  21. }
  22. // NewSubscribe 实例化
  23. func NewSubscribe(ctx *context.Context) *Subscribe {
  24. return &Subscribe{Context: ctx}
  25. }
  26. // Message 订阅消息请求参数
  27. type Message struct {
  28. ToUser string `json:"touser"` //必选,接收者(用户)的 openid
  29. TemplateID string `json:"template_id"` //必选,所需下发的订阅模板id
  30. Page string `json:"page"` //可选,点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
  31. Data map[string]*DataItem `json:"data"` //必选, 模板内容
  32. MiniprogramState string `json:"miniprogram_state"` //可选,跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
  33. Lang string `json:"lang"` //入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN
  34. }
  35. //DataItem 模版内某个 .DATA 的值
  36. type DataItem struct {
  37. Value string `json:"value"`
  38. Color string `json:"color"`
  39. }
  40. //TemplateItem template item
  41. type TemplateItem struct {
  42. PriTmplID string `json:"priTmplId"`
  43. Title string `json:"title"`
  44. Content string `json:"content"`
  45. Example string `json:"example"`
  46. Type int64 `json:"type"`
  47. }
  48. //TemplateList template list
  49. type TemplateList struct {
  50. util.CommonError
  51. Data []TemplateItem `json:"data"`
  52. }
  53. // Send 发送订阅消息
  54. func (s *Subscribe) Send(msg *Message) (err error) {
  55. var accessToken string
  56. accessToken, err = s.GetAccessToken()
  57. if err != nil {
  58. return
  59. }
  60. uri := fmt.Sprintf("%s?access_token=%s", subscribeSendURL, accessToken)
  61. response, err := util.PostJSON(uri, msg)
  62. if err != nil {
  63. return
  64. }
  65. return util.DecodeWithCommonError(response, "Send")
  66. }
  67. //ListTemplates 获取当前帐号下的个人模板列表
  68. // https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.getTemplateList.html
  69. func (s *Subscribe) ListTemplates() (*TemplateList, error) {
  70. accessToken, err := s.GetAccessToken()
  71. if err != nil {
  72. return nil, err
  73. }
  74. uri := fmt.Sprintf("%s?access_token=%s", getTemplateURL, accessToken)
  75. response, err := util.HTTPGet(uri)
  76. if err != nil {
  77. return nil, err
  78. }
  79. templateList := TemplateList{}
  80. err = util.DecodeWithError(response, &templateList, "ListTemplates")
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &templateList, nil
  85. }
  86. // UniformMessage 统一服务消息
  87. type UniformMessage struct {
  88. ToUser string `json:"touser"`
  89. WeappTemplateMsg struct {
  90. TemplateID string `json:"template_id"`
  91. Page string `json:"page"`
  92. FormID string `json:"form_id"`
  93. Data map[string]*DataItem `json:"data"`
  94. EmphasisKeyword string `json:"emphasis_keyword"`
  95. } `json:"weapp_template_msg"`
  96. MpTemplateMsg struct {
  97. Appid string `json:"appid"`
  98. TemplateID string `json:"template_id"`
  99. URL string `json:"url"`
  100. Miniprogram struct {
  101. Appid string `json:"appid"`
  102. Pagepath string `json:"pagepath"`
  103. } `json:"miniprogram"`
  104. Data map[string]*DataItem `json:"data"`
  105. } `json:"mp_template_msg"`
  106. }
  107. // UniformSend 发送统一服务消息
  108. func (s *Subscribe) UniformSend(msg *UniformMessage) (err error) {
  109. var accessToken string
  110. accessToken, err = s.GetAccessToken()
  111. if err != nil {
  112. return
  113. }
  114. uri := fmt.Sprintf("%s?access_token=%s", uniformMessageSend, accessToken)
  115. response, err := util.PostJSON(uri, msg)
  116. if err != nil {
  117. return
  118. }
  119. return util.DecodeWithCommonError(response, "UniformSend")
  120. }