NatsAliM.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package NatsServer
  2. import (
  3. "Cold_DeductionNotice/lib"
  4. "Cold_DeductionNotice/logs"
  5. "Cold_DeductionNotice/models/Warning"
  6. "github.com/vmihailenco/msgpack/v5"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. func Ali_Message(Warning_r *Warning.Warning, PhoneNumbers string, TemplateCode string, TemplateParam string) {
  12. logs.Println("Nats =>", lib.FuncName(), PhoneNumbers, TemplateCode, TemplateParam)
  13. type Ali_Message struct {
  14. PhoneNumbers string
  15. TemplateCode string
  16. TemplateParam string
  17. }
  18. b, err := msgpack.Marshal(&Ali_Message{
  19. PhoneNumbers: PhoneNumbers,
  20. TemplateCode: TemplateCode,
  21. TemplateParam: TemplateParam,
  22. })
  23. if err != nil {
  24. logs.Println("Ali_Message", err)
  25. return
  26. }
  27. cun_x := 0
  28. Data := ""
  29. breakHere:
  30. cun_x++
  31. // 控制发送
  32. if cun_x > 3 {
  33. // 添加
  34. Warning_r_e := Warning.Warning{}
  35. Warning_r_e.T_pid = 0
  36. Warning_r_e.T_tp = 1012
  37. Warning_r_e.T_D_name = "短信服务通讯"
  38. Warning_r_e.T_Remark = "短信服务通讯 异常!"
  39. Warning_r_e.T_Log = Data
  40. Warning_r_e.T_State = 3
  41. Warning_r_e.T_Ut = time.Now()
  42. //WarningNotice.WarningToAdminId(&Warning_r_e, []int{1}) // 发送给 绑定公司管理员
  43. // 添加报警
  44. Warning.Add_Warning(Warning_r_e)
  45. logs.PrintlnError("短信通知","短信服务通讯 异常!")
  46. // 达到数量上线跳出
  47. return
  48. }
  49. // 请求-响应, 向 test3 发布一个 `help me` 请求数据,设置超时间3秒,如果有多个响应,只接收第一个收到的消息
  50. msg, err := lib.Nats.Request("Ali_Message", b, 3*time.Second)
  51. if err != nil {
  52. logs.PrintlnError("Ali_Message %s", err.Error())
  53. Warning.Add_Warning_Log(Warning_r, "---->短信服务通讯 异常!Nats Err!尝试发送 "+strconv.Itoa(cun_x)+" 次\n")
  54. Data = "短信服务通讯 异常!Nats Err!"
  55. time.Sleep(time.Second * time.Duration(cun_x))
  56. goto breakHere // 跳转到标签
  57. }
  58. Data = string(msg.Data)
  59. logs.Println("Ali_Message => : %s\n", Data)
  60. if !strings.Contains(Data, "成功"){
  61. Warning.Add_Warning_Log(Warning_r, "---->短信服务发送失败!["+Data+"]!尝试发送 "+strconv.Itoa(cun_x)+" 次\n")
  62. time.Sleep(time.Second * time.Duration(cun_x))
  63. goto breakHere // 跳转到标签
  64. }
  65. Warning.Add_Warning_Log(Warning_r, "->"+Data+"\n")
  66. // ============== 计费 ==============
  67. Warning.Add_WarningSand(Warning.WarningSand{
  68. T_pid: Warning_r.T_pid,
  69. T_tp: Warning_r.T_tp,
  70. T_Notice: PhoneNumbers,
  71. T_Ntype: 1,
  72. T_Remark: "WID:"+strconv.FormatInt(Warning_r.Id,10)+"\n" +
  73. "类型:"+Warning.Read_WarningType_Get(Warning_r.T_tp)+"\n"+
  74. "SN:"+Warning_r.T_sn+"\n"+
  75. "主机:"+Warning_r.T_D_name+"\n"+
  76. "设备:"+Warning_r.T_DS_name+"\n"+
  77. "备注:"+Warning_r.T_Remark+"\n"+
  78. "通知方式:短信通知\n"+
  79. "通知对象:"+PhoneNumbers,
  80. })
  81. }