wechat-test-account.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package channel
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "message-pusher/common"
  8. "message-pusher/model"
  9. "net/http"
  10. "time"
  11. )
  12. type wechatTestAccountResponse struct {
  13. ErrorCode int `json:"errcode"`
  14. ErrorMessage string `json:"errmsg"`
  15. AccessToken string `json:"access_token"`
  16. ExpiresIn int `json:"expires_in"`
  17. }
  18. type WeChatTestAccountTokenStoreItem struct {
  19. AppID string
  20. AppSecret string
  21. AccessToken string
  22. }
  23. func (i *WeChatTestAccountTokenStoreItem) Key() string {
  24. return i.AppID + i.AppSecret
  25. }
  26. func (i *WeChatTestAccountTokenStoreItem) IsShared() bool {
  27. var count int64 = 0
  28. model.DB.Model(&model.Channel{}).Where("type = ? and app_id = ? and secret = ?", model.TypeWeChatTestAccount, i.AppID, i.AppSecret).Count(&count)
  29. return count > 1
  30. }
  31. func (i *WeChatTestAccountTokenStoreItem) IsFilled() bool {
  32. return i.AppID != "" && i.AppSecret != ""
  33. }
  34. func (i *WeChatTestAccountTokenStoreItem) Token() string {
  35. return i.AccessToken
  36. }
  37. func (i *WeChatTestAccountTokenStoreItem) Refresh() {
  38. // https://developers.weixin.qq.com/doc/offiaccount/Basic_Information/Get_access_token.html
  39. client := http.Client{
  40. Timeout: 5 * time.Second,
  41. }
  42. req, err := http.NewRequest("GET", fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s",
  43. i.AppID, i.AppSecret), nil)
  44. if err != nil {
  45. common.SysError(err.Error())
  46. return
  47. }
  48. responseData, err := client.Do(req)
  49. if err != nil {
  50. common.SysError("failed to refresh access token: " + err.Error())
  51. return
  52. }
  53. defer responseData.Body.Close()
  54. var res wechatTestAccountResponse
  55. err = json.NewDecoder(responseData.Body).Decode(&res)
  56. if err != nil {
  57. common.SysError("failed to decode wechatTestAccountResponse: " + err.Error())
  58. return
  59. }
  60. if res.ErrorCode != 0 {
  61. common.SysError(res.ErrorMessage)
  62. return
  63. }
  64. i.AccessToken = res.AccessToken
  65. common.SysLog("access token refreshed")
  66. }
  67. type wechatTestMessageRequest struct {
  68. ToUser string `json:"touser"`
  69. TemplateId string `json:"template_id"`
  70. URL string `json:"url"`
  71. Data struct {
  72. Text struct {
  73. Value string `json:"value"`
  74. } `json:"text"`
  75. } `json:"data"`
  76. }
  77. type wechatTestMessageResponse struct {
  78. ErrorCode int `json:"errcode"`
  79. ErrorMessage string `json:"errmsg"`
  80. }
  81. func SendWeChatTestMessage(message *model.Message, user *model.User, channel_ *model.Channel) error {
  82. values := wechatTestMessageRequest{
  83. ToUser: channel_.AccountId,
  84. TemplateId: channel_.Other,
  85. URL: "",
  86. }
  87. if message.To != "" {
  88. values.ToUser = message.To
  89. }
  90. values.Data.Text.Value = message.Description
  91. values.URL = message.URL
  92. jsonData, err := json.Marshal(values)
  93. if err != nil {
  94. return err
  95. }
  96. key := fmt.Sprintf("%s%s", channel_.AppId, channel_.Secret)
  97. accessToken := TokenStoreGetToken(key)
  98. resp, err := http.Post(fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s", accessToken), "application/json",
  99. bytes.NewBuffer(jsonData))
  100. if err != nil {
  101. return err
  102. }
  103. var res wechatTestMessageResponse
  104. err = json.NewDecoder(resp.Body).Decode(&res)
  105. if err != nil {
  106. return err
  107. }
  108. if res.ErrorCode != 0 {
  109. return errors.New(res.ErrorMessage)
  110. }
  111. return nil
  112. }