WxTemplate.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package Server
  2. import (
  3. "Wx_MP/logs"
  4. "encoding/json"
  5. "fmt"
  6. _ "github.com/astaxie/beego/cache/redis"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. // 发送模板消息
  12. // reqdata := "{\"first\":{\"value\":\"温度超上限报警\", \"color\":\"#ff450a\"}, \"keyword1\":{\"value\":\"温度超上限报警\", \"color\":\"#ff450a\"}, \"keyword2\":{\"value\":\"数据测试主机23455[温度探头1] 温度超上限报警\", \"color\":\"#056cff\"}, \"remark\":{\"value\":\"温度超高3.2℃ 报警发生时间:2021/12/24 09:07:45 已持续79分钟,请尽快处理!\", \"color\":\"#ffcb0a\"}}"
  13. // lib.Wx_templatepost ("o5EKB1buEEsyDP6u-6H3H326T4no","kKb7YVgGAzbcPc9g0XkNDCrJ4MUZvHQ8nVazJE-Gjgc","index?wid=bar",reqdata)
  14. func Wx_templatepost(openid string, templateid string, parameter string, reqdata string) string {
  15. logs.Println("发送微信模板:", " openid:", openid, " templateid:", templateid, " parameter:", parameter, " reqdata:", reqdata)
  16. breakHere:
  17. accessToken := GetAccessToken(false)
  18. url := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken
  19. reqbody := "{\"touser\":\"" + openid + "\" , \"template_id\":\"" + templateid + "\", \"data\": " + reqdata + "}"
  20. //reqbody := "{\"touser\":\"" + openid + "\", \"miniprogram\":{\"appid\":\"wx1f12337d314725e0\",\"path\":\"" + parameter + "\"} , \"template_id\":\"" + templateid + "\", \"data\": " + reqdata + "}"
  21. logs.Println("reqbody:", reqbody)
  22. resp, err := http.Post(url,
  23. "application/x-www-form-urlencoded",
  24. strings.NewReader(string(reqbody)))
  25. if err != nil {
  26. logs.PrintlnE("Wx_templatepost:", err)
  27. return " -> 请求失败" + err.Error()
  28. }
  29. defer resp.Body.Close()
  30. body, err := ioutil.ReadAll(resp.Body)
  31. if err != nil {
  32. logs.PrintlnE("Wx_templatepost:", err)
  33. return " -> 请求失败" + err.Error()
  34. }
  35. logs.Println(openid, " body:", string(body))
  36. if strings.Contains(string(body), "access_token expired") {
  37. accessToken = GetAccessToken(true)
  38. logs.PrintlnE("强制重新获取 微信TOKEY!!")
  39. goto breakHere // 跳转到标签
  40. }
  41. if strings.Contains(string(body), "\"errcode\":0,\"errmsg\":\"ok\"") {
  42. return "发送成功!"
  43. }
  44. return string(body)
  45. }
  46. // 生成参数二维码
  47. func GenerateQRCode(QR_str string) string {
  48. breakHere:
  49. accessToken := GetAccessToken(false)
  50. logs.Println("accessToken:", accessToken)
  51. url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", accessToken)
  52. println("GenerateQRCode QR_str:", QR_str)
  53. reqbody := "{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + QR_str + "\"}}}"
  54. resp, err := http.Post(url,
  55. "application/x-www-form-urlencoded",
  56. strings.NewReader(string(reqbody)))
  57. if err != nil {
  58. logs.Println("GenerateQRCode:", err)
  59. return ""
  60. }
  61. defer resp.Body.Close()
  62. body, err := ioutil.ReadAll(resp.Body)
  63. if err != nil {
  64. logs.PrintlnE("GenerateQRCode:", err)
  65. return ""
  66. }
  67. logs.Println(string(body))
  68. if strings.Contains(string(body), "access_token") {
  69. logs.PrintlnE(" AccessToken 失效!!!")
  70. accessToken = GetAccessToken(true)
  71. goto breakHere // 跳转到标签
  72. return ""
  73. }
  74. var Ms_project map[string]interface{}
  75. err = json.Unmarshal(body, &Ms_project)
  76. if err != nil {
  77. logs.PrintlnE("JSON反序列化失败[GenerateQRCode],err=", err, string(body))
  78. return ""
  79. }
  80. smg, ok := Ms_project["ticket"]
  81. if !ok {
  82. logs.PrintlnE("JSON反序列化失败[GenerateQRCode] ticket 没有!body:", string(body))
  83. return ""
  84. }
  85. logs.Println("二维码 ticket :", smg.(string))
  86. return smg.(string)
  87. }