wechat.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package alarm
  2. import (
  3. "context"
  4. "crypto/md5"
  5. "encoding/hex"
  6. "encoding/json"
  7. "fmt"
  8. "net/http"
  9. "net/url"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "go-common/app/service/main/resource/model"
  14. "go-common/library/log"
  15. )
  16. var (
  17. _states = map[int8]string{
  18. int8(0): "开放浏览",
  19. int8(-1): "待审",
  20. int8(-2): "打回稿件回收站",
  21. int8(-3): "网警锁定删除",
  22. int8(-4): "锁定稿件",
  23. // -5: "锁定稿件开放浏览",
  24. int8(-6): "修复待审",
  25. int8(-7): "暂缓审核",
  26. // -8: "补档待审",
  27. int8(-9): "等待转码",
  28. int8(-10): "延迟发布",
  29. int8(-11): "视频源待修",
  30. // -12: "上传失败",
  31. int8(-13): "允许评论待审",
  32. // -14: "临时回收站",
  33. int8(-15): "分发中",
  34. int8(-16): "转码失败",
  35. int8(-30): "创建已提交",
  36. int8(-40): "UP主定时发布",
  37. int8(-100): "UP主删除",
  38. }
  39. _codes = map[int]string{
  40. 404: "页面未找到",
  41. 502: "服务端异常",
  42. 504: "SLB超时",
  43. }
  44. )
  45. func stateDescribe(state int8) string {
  46. des, ok := _states[state]
  47. if ok {
  48. return des
  49. }
  50. return strconv.Itoa(int(state))
  51. }
  52. const (
  53. _warnTitle = `
  54. 【线上投放告警】您有一个投放发生了异常,请尽快确认是否影响投放。
  55. %v
  56. 以上内容确认后,如有异常请联系相关人员手动处理。`
  57. _offLineTitle = `
  58. 【线上投放下线告警】您有一个投放内容变不可见状态,已自动下线。请尽快补充投放。
  59. %v
  60. 以上投放所涉及的排期申请已经置为 <申请未投放>。`
  61. _archiveContent = `
  62. 投放ID:%d
  63. 稿件ID:%d
  64. 投放标题:%s
  65. 投放位置:%s (位置ID:%d)
  66. 投放时间段:%s - %s
  67. 当前稿件状态:%s
  68. `
  69. _urlContent = `
  70. 投放ID:%d
  71. URL:%v
  72. 投放标题:%s
  73. 投放位置:%s (位置ID:%d)
  74. 投放时间段:%s - %s
  75. 错误信息:%v(错误码: %v)
  76. `
  77. )
  78. type wxParams struct {
  79. Username string `json:"username"`
  80. Content string `json:"content"`
  81. Token string `json:"token"`
  82. Timestamp int64 `json:"timestamp"`
  83. Sign string `json:"signature"`
  84. }
  85. type resp struct {
  86. Status int64 `json:"status"`
  87. Msg string `json:"msg"`
  88. }
  89. // SendWeChart send message to QYWX
  90. func (d *Dao) SendWeChart(c context.Context, ns int8, userName string, res []*model.ResWarnInfo, titleType string) (err error) {
  91. var (
  92. users = append(d.c.WeChantUsers, userName)
  93. newStateStr = stateDescribe(ns)
  94. contents []string
  95. )
  96. for _, re := range res {
  97. stime := re.STime.Time().Format("2006-01-02 15:04:05")
  98. etime := re.ETime.Time().Format("2006-01-02 15:04:05")
  99. contents = append(contents, fmt.Sprintf(_archiveContent, re.AssignmentID, re.AID, re.AssignmentName, re.ResourceName, re.ResourceID, stime, etime, newStateStr))
  100. }
  101. params := url.Values{}
  102. params.Set("username", strings.Join(users, ","))
  103. if titleType == "warn" {
  104. params.Set("content", fmt.Sprintf(_warnTitle, strings.Join(contents, "")))
  105. } else {
  106. params.Set("content", fmt.Sprintf(_offLineTitle, strings.Join(contents, "")))
  107. }
  108. params.Set("token", d.c.WeChatToken)
  109. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  110. mh := md5.Sum([]byte(params.Encode() + d.c.WeChatSecret))
  111. params.Set("signature", hex.EncodeToString(mh[:]))
  112. p := &wxParams{
  113. Username: params.Get("username"),
  114. Content: params.Get("content"),
  115. Token: params.Get("token"),
  116. Sign: params.Get("signature"),
  117. }
  118. p.Timestamp, _ = strconv.ParseInt(params.Get("timestamp"), 10, 64)
  119. bs, _ := json.Marshal(p)
  120. payload := strings.NewReader(string(bs))
  121. req, _ := http.NewRequest("POST", "http://bap.bilibili.co/api/v1/message/add", payload)
  122. req.Header.Add("content-type", "application/json; charset=utf-8")
  123. v := &resp{}
  124. if err = d.httpClient.Do(context.TODO(), req, v); err != nil {
  125. log.Error("s.httpClient.Do error(%v)", err)
  126. }
  127. return
  128. }
  129. // sendWeChartURL send message to QYWX
  130. func (d *Dao) sendWeChartURL(c context.Context, code int, userName string, res []*model.ResWarnInfo) (err error) {
  131. var (
  132. users = append(d.c.WeChantUsers, userName)
  133. codeInfo string
  134. contents []string
  135. )
  136. if codeInfo = _codes[code]; codeInfo == "" {
  137. codeInfo = "未知错误,请手动确认"
  138. }
  139. for _, re := range res {
  140. stime := re.STime.Time().Format("2006-01-02 15:04:05")
  141. etime := re.ETime.Time().Format("2006-01-02 15:04:05")
  142. contents = append(contents, fmt.Sprintf(_urlContent, re.AssignmentID, re.URL, re.AssignmentName, re.ResourceName, re.ResourceID, stime, etime, codeInfo, code))
  143. }
  144. params := url.Values{}
  145. params.Set("username", strings.Join(users, ","))
  146. params.Set("content", fmt.Sprintf(_warnTitle, strings.Join(contents, "")))
  147. params.Set("token", d.c.WeChatToken)
  148. params.Set("timestamp", strconv.FormatInt(time.Now().Unix(), 10))
  149. mh := md5.Sum([]byte(params.Encode() + d.c.WeChatSecret))
  150. params.Set("signature", hex.EncodeToString(mh[:]))
  151. p := &wxParams{
  152. Username: params.Get("username"),
  153. Content: params.Get("content"),
  154. Token: params.Get("token"),
  155. Sign: params.Get("signature"),
  156. }
  157. p.Timestamp, _ = strconv.ParseInt(params.Get("timestamp"), 10, 64)
  158. bs, _ := json.Marshal(p)
  159. payload := strings.NewReader(string(bs))
  160. req, _ := http.NewRequest("POST", "http://bap.bilibili.co/api/v1/message/add", payload)
  161. req.Header.Add("content-type", "application/json; charset=utf-8")
  162. v := &resp{}
  163. if err = d.httpClient.Do(context.TODO(), req, v); err != nil {
  164. log.Error("s.httpClient.Do error(%v)", err)
  165. }
  166. return
  167. }