xcx.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package wx_mp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego"
  6. "bytes"
  7. "fohow.com/libs/tool"
  8. "io/ioutil"
  9. "net/http"
  10. )
  11. type SessionKey struct {
  12. Openid string `json:"openid"` //用户唯一标识
  13. SessionKey string `json:"session_key"` //会话密钥
  14. Unionid string `json:"unionid"` //用户在开放平台的唯一标识符。本字段在满足一定条件的情况下才返回。
  15. }
  16. type SessionKeyError struct {
  17. Errcode int64 `json:"errcode"` //用户唯一标识
  18. Errmsg string `json:"errmsg"` //会话密钥
  19. }
  20. //
  21. //appId :小程序唯一标识
  22. //appSecret: 小程序的 app secret
  23. //code: 登录时获取的 code
  24. func GetXcxSessionKey(appId, appSecret, code string) *SessionKey {
  25. url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", appId, appSecret, code)
  26. resp := tool.HttpCall(url, "GET", nil, nil)
  27. var key *SessionKey
  28. //beego.BeeLogger.Warn("GetXcxSessionKey resp: %s", resp)
  29. if err := json.Unmarshal([]byte(resp), &key); err != nil {
  30. beego.BeeLogger.Error("GetXcxSessionKey parse query error:", err)
  31. }
  32. if key.Openid == "" || key.SessionKey == "" {
  33. var keyError *SessionKeyError
  34. if err := json.Unmarshal([]byte(resp), &keyError); err != nil {
  35. beego.BeeLogger.Error("GetXcxSessionKey parse query error:", err)
  36. }
  37. beego.BeeLogger.Error("GetXcxSessionKey get session key error:%s", keyError)
  38. return nil
  39. }
  40. return key
  41. }
  42. type TemplateMessage struct {
  43. ToUser string `json:"touser"` // 必须, 接受者OpenID
  44. TemplateId string `json:"template_id"` // 必须, 模版ID
  45. URL string `json:"url,omitempty"` // 可选, 用户点击后跳转的URL, 该URL必须处于开发者在公众平台网站中设置的域中
  46. TopColor string `json:"topcolor,omitempty"` // 可选, 整个消息的颜色, 可以不设置
  47. Data json.RawMessage `json:"data"` // 必须, JSON 格式的 []byte, 满足特定的模板需求
  48. }
  49. func (msg *TemplateMessage) ToJson() {
  50. b, err := json.Marshal(msg)
  51. if err != nil {
  52. fmt.Println(err)
  53. return
  54. }
  55. fmt.Println(string(b))
  56. }
  57. type DataKeywordValue struct {
  58. Value string `json:"value"`
  59. Color string `json:"color"`
  60. }
  61. func NewDataKeywordValue(value string, color string) *DataKeywordValue {
  62. if color == "" {
  63. color = "#FFFFFF"
  64. }
  65. return &DataKeywordValue{Value: value, Color: color}
  66. }
  67. type OrderDispatchdMessageData struct {
  68. Amount DataKeywordValue `json:"keyword1"`
  69. Datetime DataKeywordValue `json:"keyword2"`
  70. Reason DataKeywordValue `json:"keyword3"`
  71. Remark DataKeywordValue `json:"keyword4"`
  72. }
  73. type BalanceChangedMessageData struct {
  74. Amount DataKeywordValue `json:"keyword1"`
  75. Datetime DataKeywordValue `json:"keyword2"`
  76. Reason DataKeywordValue `json:"keyword3"`
  77. Remark DataKeywordValue `json:"keyword4"`
  78. }
  79. type ProductLogisticsChangedMessageData struct {
  80. DispatchTime DataKeywordValue `json:"keyword1"`
  81. ProductName DataKeywordValue `json:"keyword2"`
  82. OrderId DataKeywordValue `json:"keyword3"`
  83. ExpressCompany DataKeywordValue `json:"keyword4"`
  84. ExpressOrderNo DataKeywordValue `json:"keyword5"`
  85. Remark DataKeywordValue `json:"keyword6"`
  86. }
  87. type OrderCreatedMessageData struct {
  88. OrderId DataKeywordValue `json:"keyword1"`
  89. Created_at DataKeywordValue `json:"keyword2"`
  90. ProductName DataKeywordValue `json:"keyword3"`
  91. Count DataKeywordValue `json:"keyword4"`
  92. OrderStatus DataKeywordValue `json:"keyword5"`
  93. Remark DataKeywordValue `json:"keyword6"`
  94. }
  95. type SelfUseNotifyMessageData struct {
  96. ProductName DataKeywordValue `json:"keyword1"`
  97. SelfUseCount DataKeywordValue `json:"keyword2"`
  98. WarmTip DataKeywordValue `json:"keyword3"`
  99. ServicePerson DataKeywordValue `json:"keyword4"`
  100. }
  101. type Template3MessageData struct {
  102. Kerword1 DataKeywordValue `json:"keyword1"`
  103. Kerword2 DataKeywordValue `json:"keyword2"`
  104. Kerword3 DataKeywordValue `json:"keyword3"`
  105. }
  106. func SendTemplateMessage(toUserOpenId string, templateId string, page string, formId string, data json.RawMessage, emphasisKeyword string) {
  107. token := GetAccessToken(beego.AppConfig.String("WxFohowXcxAppId"), beego.AppConfig.String("WxFohowXcxAppSecret"))
  108. beego.BeeLogger.Info("TestSendingTemplatemsgController got token: %s", token)
  109. template := `{
  110. "touser": "%s",
  111. "template_id": "%s",
  112. "page": "%s",
  113. "form_id": "%s",
  114. "data": %s,
  115. "emphasis_keyword": "%s"
  116. }`
  117. postData := fmt.Sprintf(template, toUserOpenId, templateId, page, formId, data, emphasisKeyword)
  118. apiUrl := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=%s", token)
  119. var jsonStr = []byte(postData)
  120. req, err := http.NewRequest("POST", apiUrl, bytes.NewBuffer(jsonStr))
  121. req.Header.Set("Content-Type", "application/json")
  122. client := &http.Client{}
  123. resp, err := client.Do(req)
  124. if err != nil {
  125. beego.BeeLogger.Error(err.Error())
  126. }
  127. beego.BeeLogger.Warn("wx_mp.SendTemplateMessage, resp is null : %v", resp == nil)
  128. if resp != nil {
  129. defer resp.Body.Close()
  130. body, _ := ioutil.ReadAll(resp.Body)
  131. fmt.Println("response Body:", string(body))
  132. return
  133. }
  134. beego.BeeLogger.Warn("wx_mp.SendTemplateMessage, finished")
  135. }