sms.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package handler
  2. import (
  3. "crypto/rand"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io"
  8. "math/big"
  9. "net/http"
  10. "strings"
  11. "time"
  12. "github.com/go-admin-team/go-admin-core/logger"
  13. // "github.com/tjfoc/gmsm/sm3"
  14. sm3 "go-admin/utils/sm"
  15. )
  16. type SingleMsg struct {
  17. ID string `json:"id"`
  18. MsgContent string `json:"msgcontent"`
  19. MsgType string `json:"msgtype"`
  20. PhoneNum string `json:"phonenum"`
  21. }
  22. type SendMsg struct {
  23. SubmitTime string `json:"submittime"`
  24. MqReceipt bool `json:"mqreceipt"`
  25. ExtCode string `json:"extcode"`
  26. MsgList []SingleMsg `json:"msgList"`
  27. }
  28. func SM3(data string) string {
  29. // 这里应该实现SM3算法,由于Go标准库中没有直接支持,这里只是返回一个模拟的字符串
  30. h := sm3.New()
  31. h.Write([]byte(data))
  32. sum := h.Sum(nil)
  33. logger.Infof("digest value is: %x\n", sum)
  34. logger.Infof("digest value string: %s\n", hex.EncodeToString(sum))
  35. return hex.EncodeToString(sum)
  36. }
  37. func (e *EventHandler) SendMsg() *EventHandler {
  38. logger.Debugf("In sms Send func, %s", e.RP.AppAlias)
  39. //判断该应用是否添加了短信通知
  40. _, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
  41. if !ok {
  42. return e
  43. }
  44. logger.Debug("has key......", e.JR.AlertStatus)
  45. //判断是否需要告警
  46. if !(e.JR.AlertStatus == FIRING) {
  47. return e
  48. }
  49. logger.Info("start to send sms...")
  50. appkey := e.SmsInfo.Appkey
  51. appsecret := e.SmsInfo.Appsecret
  52. headers := make(map[string]string)
  53. nonce, err := rand.Int(rand.Reader, big.NewInt(1<<62))
  54. if err != nil {
  55. e.Errs["rander error"] = err
  56. return e
  57. }
  58. timestamp := time.Now().UnixNano() / int64(time.Millisecond)
  59. headers["x-system-key"] = appkey
  60. headers["x-system-nonce"] = nonce.String()
  61. headers["x-system-timestamp"] = fmt.Sprintf("%d", timestamp)
  62. headers["x-system-token"] = SM3(appkey + fmt.Sprintf("%d", timestamp) + appsecret + nonce.String())
  63. // set smsContent
  64. // 所属系统:{{app_name}}
  65. // 事件名称:{{events_name}}
  66. // 事件创建时间:{{date_time}}
  67. // 事件描述:{{events}}`
  68. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{app_name}}", e.RP.AppName, -1)
  69. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events_name}}", e.RP.RuleName, -1)
  70. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{date_time}}", e.RP.CreateTime.Format("2006-01-02 15:04:05"), -1)
  71. e.SmsInfo.SmsTpl = strings.Replace(e.SmsInfo.SmsTpl, "{{events}}", fmt.Sprintf("%s, %d分钟内发生%s%v次, 当前值为: %v",
  72. e.RP.RuleName, e.AC.Interval, e.AC.Condition, e.JR.CompareV, e.JR.CompareV), -1)
  73. logger.Info("sms build: ", e.SmsInfo.SmsTpl)
  74. MsgList := make([]SingleMsg, 0)
  75. phoneList, ok := e.SmsInfo.AppsGroup[e.RP.AppAlias]
  76. if ok {
  77. for id, phone := range phoneList {
  78. ssm := SingleMsg{
  79. ID: fmt.Sprintf("%d", id),
  80. MsgContent: e.SmsInfo.SmsTpl,
  81. MsgType: "",
  82. PhoneNum: phone,
  83. }
  84. MsgList = append(MsgList, ssm)
  85. }
  86. }
  87. sendMsg := SendMsg{
  88. SubmitTime: time.Now().Format("2006-01-02 15:04:05"),
  89. MqReceipt: false,
  90. ExtCode: "",
  91. MsgList: MsgList,
  92. }
  93. jsonData, err := json.Marshal(sendMsg)
  94. if err != nil {
  95. e.Errs["sms json marshal error"] = err
  96. return e
  97. }
  98. logger.Debug("sms json content: ", string(jsonData))
  99. client := &http.Client{}
  100. req, err := http.NewRequest("POST", e.SmsInfo.Url, strings.NewReader(string(jsonData)))
  101. if err != nil {
  102. e.Errs["sms post build error"] = err
  103. return e
  104. }
  105. for key, value := range headers {
  106. req.Header.Set(key, value)
  107. }
  108. // 设定json格式
  109. req.Header.Set("Content-Type", "application/json")
  110. logger.Debug("sms json header: ", req.Header)
  111. resp, err := client.Do(req)
  112. if err != nil {
  113. e.Errs["sms post do error"] = err
  114. return e
  115. }
  116. defer resp.Body.Close()
  117. body, err := io.ReadAll(resp.Body)
  118. if err != nil {
  119. e.Errs["sms body read error"] = err
  120. return e
  121. }
  122. logger.Infof("sms response body: %v", string(body))
  123. e.JR.AlertStatus = SENDSUCCESS
  124. return e
  125. }
  126. // smsTpl =`
  127. // 【江苏省电力公司】
  128. // 所属系统:{{app_alias}}
  129. // 事件名称:{{events_name}}
  130. // 事件创建时间:{{date_time}}
  131. // 事件描述:{{events}}`