package Server import ( "Wx_MP/logs" "encoding/json" "fmt" _ "github.com/astaxie/beego/cache/redis" "io/ioutil" "net/http" "strings" ) // 发送模板消息 // 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\"}}" // lib.Wx_templatepost ("o5EKB1buEEsyDP6u-6H3H326T4no","kKb7YVgGAzbcPc9g0XkNDCrJ4MUZvHQ8nVazJE-Gjgc","index?wid=bar",reqdata) func Wx_templatepost(openid string, templateid string, parameter string, reqdata string) string { logs.Println("发送微信模板:", " openid:", openid, " templateid:", templateid, " parameter:", parameter, " reqdata:", reqdata) breakHere: accessToken := GetAccessToken(false) url := "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + accessToken reqbody := "{\"touser\":\"" + openid + "\" , \"template_id\":\"" + templateid + "\", \"data\": " + reqdata + "}" //reqbody := "{\"touser\":\"" + openid + "\", \"miniprogram\":{\"appid\":\"wx1f12337d314725e0\",\"path\":\"" + parameter + "\"} , \"template_id\":\"" + templateid + "\", \"data\": " + reqdata + "}" logs.Println("reqbody:", reqbody) resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(string(reqbody))) if err != nil { logs.PrintlnE("Wx_templatepost:", err) return " -> 请求失败" + err.Error() } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { logs.PrintlnE("Wx_templatepost:", err) return " -> 请求失败" + err.Error() } logs.Println(openid, " body:", string(body)) if strings.Contains(string(body), "access_token expired") { accessToken = GetAccessToken(true) logs.PrintlnE("强制重新获取 微信TOKEY!!") goto breakHere // 跳转到标签 } if strings.Contains(string(body), "\"errcode\":0,\"errmsg\":\"ok\"") { return "发送成功!" } return string(body) } // 生成参数二维码 func GenerateQRCode(QR_str string) string { breakHere: accessToken := GetAccessToken(false) logs.Println("accessToken:", accessToken) url := fmt.Sprintf("https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s", accessToken) println("GenerateQRCode QR_str:", QR_str) reqbody := "{\"expire_seconds\": 604800, \"action_name\": \"QR_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + QR_str + "\"}}}" resp, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(string(reqbody))) if err != nil { logs.Println("GenerateQRCode:", err) return "" } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { logs.PrintlnE("GenerateQRCode:", err) return "" } logs.Println(string(body)) if strings.Contains(string(body), "access_token") { logs.PrintlnE(" AccessToken 失效!!!") accessToken = GetAccessToken(true) goto breakHere // 跳转到标签 return "" } var Ms_project map[string]interface{} err = json.Unmarshal(body, &Ms_project) if err != nil { logs.PrintlnE("JSON反序列化失败[GenerateQRCode],err=", err, string(body)) return "" } smg, ok := Ms_project["ticket"] if !ok { logs.PrintlnE("JSON反序列化失败[GenerateQRCode] ticket 没有!body:", string(body)) return "" } logs.Println("二维码 ticket :", smg.(string)) return smg.(string) }