package NatsServer import ( "Cold_mqtt/lib" "Cold_mqtt/logs" "Cold_mqtt/models/Account" "Cold_mqtt/models/Warning" "github.com/vmihailenco/msgpack/v5" "strconv" "strings" "time" ) func Wx_MessageTemplate(Company_Charging_r Account.Company, Warning_r *Warning.Warning, openid string, templateid string, reqdata string) { logs.Println(" => Nats", lib.FuncName(), openid, templateid, reqdata) type Wx_MessageTemplate struct { Openid string Templateid string Parameter string Reqdata string Ttime int64 // 发布时间 } b, err := msgpack.Marshal(&Wx_MessageTemplate{ Openid: openid, Templateid: templateid, Parameter: "index?wid=" + strconv.Itoa(Warning_r.T_id), Reqdata: reqdata, Ttime: time.Now().Unix(), }) if err != nil { logs.Println("Wx_MessageTemplate", err) return } cun_x := 0 Data := "" breakHere: cun_x++ // 控制发送 if cun_x > 3 { // 添加 Warning_r_e := Warning.Warning{} Warning_r_e.T_pid = 0 Warning_r_e.T_tp = 1014 Warning_r_e.T_D_name = "微信服务通讯" Warning_r_e.T_Remark = "微信服务通讯 异常!" Warning_r_e.T_Log = Data Warning_r_e.T_State = 3 Warning_r_e.T_Ut = time.Now() //WarningNotice.WarningToAdminId(&Warning_r_e, []int{1}) // 发送给 绑定公司管理员 // 添加报警 Warning.Add_Warning(Warning_r_e) logs.PrintlnError("微信通知", "微信服务通讯 异常!", reqdata, Data) // 达到数量上线跳出 return } // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息 msg, err := lib.Nats.Request("Wx_MessageTemplate", b, 30*time.Second) // 30 秒, 订阅端 超时 30秒 将丢弃消息 if err != nil { logs.PrintlnError("Wx_MessageTemplate %s", err.Error(), reqdata) Warning.Add_Warning_Log(Warning_r, "---->微信服务发送超时 异常!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n") Data = "微信服务通讯 异常!Nats Err!" time.Sleep(time.Second * time.Duration(cun_x)) goto breakHere // 跳转到标签 } Data = string(msg.Data) logs.Println("Wx_MessageTemplate => : %s\n", Data) if !strings.Contains(Data, "成功") { if strings.Contains(Data, ":43004") { //未关注微 Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户未关注公众") } else if strings.Contains(Data, ":43101") { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户拒绝接受消息,取消了订阅注公众") } else if strings.Contains(Data, ":40001") { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!token过期") } else if strings.Contains(Data, ":40013") { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户APPID错误,请重新绑定!") } else if strings.Contains(Data, ":40037") { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!模板id不正确") } else if strings.Contains(Data, ":40073") { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!openid不存在") } else { Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!["+Data+"]!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n") time.Sleep(time.Second * time.Duration(cun_x)) goto breakHere // 跳转到标签 } } Warning.Add_Warning_Log(Warning_r, "->"+Data+" "+time.Now().Format("15:04:05")+"\n") // ============== 计费 ============== Warning.Add_WarningSand(Warning.WarningSand{ T_pid: Company_Charging_r.Id, T_Spid: Warning_r.T_pid, T_tp: Warning_r.T_tp, T_Notice: openid, T_Ntype: 0, T_Remark: "WID:" + strconv.FormatInt(Warning_r.Id, 10) + "\n" + "类型:" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\n" + "SN:" + Warning_r.T_sn + "\n" + "主机:" + Warning_r.T_D_name + "\n" + "设备:" + Warning_r.T_DS_name + "\n" + "备注:" + Warning_r.T_Remark + "\n" + "通知方式:微信通知\n" + "通知对象:" + openid, }) }