NatsWx.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package NatsServer
  2. import (
  3. "Cold_mqtt/lib"
  4. "Cold_mqtt/logs"
  5. "Cold_mqtt/models/Account"
  6. "Cold_mqtt/models/Warning"
  7. "github.com/vmihailenco/msgpack/v5"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. func Wx_MessageTemplate(Company_Charging_r Account.Company, Warning_r *Warning.Warning, openid string, templateid string, reqdata string) {
  13. logs.Println(" => Nats", lib.FuncName(), openid, templateid, reqdata)
  14. type Wx_MessageTemplate struct {
  15. Openid string
  16. Templateid string
  17. Parameter string
  18. Reqdata string
  19. Ttime int64 // 发布时间
  20. }
  21. b, err := msgpack.Marshal(&Wx_MessageTemplate{
  22. Openid: openid,
  23. Templateid: templateid,
  24. Parameter: "index?wid=" + strconv.Itoa(Warning_r.T_id),
  25. Reqdata: reqdata,
  26. Ttime: time.Now().Unix(),
  27. })
  28. if err != nil {
  29. logs.Println("Wx_MessageTemplate", err)
  30. return
  31. }
  32. cun_x := 0
  33. Data := ""
  34. breakHere:
  35. cun_x++
  36. // 控制发送
  37. if cun_x > 3 {
  38. // 添加
  39. Warning_r_e := Warning.Warning{}
  40. Warning_r_e.T_pid = 0
  41. Warning_r_e.T_tp = 1014
  42. Warning_r_e.T_D_name = "微信服务通讯"
  43. Warning_r_e.T_Remark = "微信服务通讯 异常!"
  44. Warning_r_e.T_Log = Data
  45. Warning_r_e.T_State = 3
  46. Warning_r_e.T_Ut = time.Now()
  47. //WarningNotice.WarningToAdminId(&Warning_r_e, []int{1}) // 发送给 绑定公司管理员
  48. // 添加报警
  49. Warning.Add_Warning(Warning_r_e)
  50. logs.PrintlnError("微信通知", "微信服务通讯 异常!", reqdata, Data)
  51. // 达到数量上线跳出
  52. return
  53. }
  54. // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  55. msg, err := lib.Nats.Request("Wx_MessageTemplate", b, 30*time.Second) // 30 秒, 订阅端 超时 30秒 将丢弃消息
  56. if err != nil {
  57. logs.PrintlnError("Wx_MessageTemplate %s", err.Error(), reqdata)
  58. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送超时 异常!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  59. Data = "微信服务通讯 异常!Nats Err!"
  60. time.Sleep(time.Second * time.Duration(cun_x))
  61. goto breakHere // 跳转到标签
  62. }
  63. Data = string(msg.Data)
  64. logs.Println("Wx_MessageTemplate => : %s\n", Data)
  65. if !strings.Contains(Data, "成功") {
  66. if strings.Contains(Data, ":43004") { //未关注微
  67. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户未关注公众")
  68. } else if strings.Contains(Data, ":43101") {
  69. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户拒绝接受消息,取消了订阅注公众")
  70. } else if strings.Contains(Data, ":40001") {
  71. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!token过期")
  72. } else if strings.Contains(Data, ":40013") {
  73. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!用户APPID错误,请重新绑定!")
  74. } else if strings.Contains(Data, ":40037") {
  75. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!模板id不正确")
  76. } else if strings.Contains(Data, ":40073") {
  77. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!openid不存在")
  78. } else {
  79. Warning.Add_Warning_Log(Warning_r, "---->微信服务发送失败!["+Data+"]!尝试发送 "+strconv.Itoa(cun_x)+" 次 ["+time.Now().Format("15:04:05")+"]\n")
  80. time.Sleep(time.Second * time.Duration(cun_x))
  81. goto breakHere // 跳转到标签
  82. }
  83. }
  84. Warning.Add_Warning_Log(Warning_r, "->"+Data+" "+time.Now().Format("15:04:05")+"\n")
  85. // ============== 计费 ==============
  86. Warning.Add_WarningSand(Warning.WarningSand{
  87. T_pid: Company_Charging_r.Id,
  88. T_Spid: Warning_r.T_pid,
  89. T_tp: Warning_r.T_tp,
  90. T_Notice: openid,
  91. T_Ntype: 0,
  92. T_Remark: "WID:" + strconv.FormatInt(Warning_r.Id, 10) + "\n" +
  93. "类型:" + Warning.Read_WarningType_Get(Warning_r.T_tp) + "\n" +
  94. "SN:" + Warning_r.T_sn + "\n" +
  95. "主机:" + Warning_r.T_D_name + "\n" +
  96. "设备:" + Warning_r.T_DS_name + "\n" +
  97. "备注:" + Warning_r.T_Remark + "\n" +
  98. "通知方式:微信通知\n" +
  99. "通知对象:" + openid,
  100. })
  101. }