push.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package dao
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "mime/multipart"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "go-common/app/interface/main/push-archive/model"
  12. "go-common/library/log"
  13. "go-common/library/xstr"
  14. )
  15. type _response struct {
  16. Code int `json:"code"`
  17. Data int `json:"data"`
  18. }
  19. // NoticeFans pushs the notification to fans.
  20. func (d *Dao) NoticeFans(fans *[]int64, params map[string]interface{}) (err error) {
  21. arc := params["archive"].(*model.Archive)
  22. group := strings.TrimSpace(params["group"].(string))
  23. msgTemplate := params["msgTemplate"].(string)
  24. uuid := params["uuid"].(string)
  25. relationType := params["relationType"].(int)
  26. author := "UP主"
  27. if arc.Author != "" {
  28. author = fmt.Sprintf(`“%s”`, arc.Author)
  29. }
  30. // 普通关注和特殊关注用不同的业务组推
  31. businessID := d.c.Push.BusinessID
  32. businessToken := d.c.Push.BusinessToken
  33. if relationType == model.RelationSpecial {
  34. businessID = d.c.Push.BusinessSpecialID
  35. businessToken = d.c.Push.BusinessSpecialToken
  36. }
  37. msg := fmt.Sprintf(msgTemplate, author, arc.Title)
  38. sp := strings.SplitN(msg, "\r\n", 2)
  39. buf := new(bytes.Buffer)
  40. w := multipart.NewWriter(buf)
  41. w.WriteField("group", group) // 实验组名,值为实验组数据表名
  42. w.WriteField("app_id", "1") // 1表示哔哩哔哩动画
  43. w.WriteField("business_id", strconv.Itoa(businessID))
  44. w.WriteField("alert_title", sp[0])
  45. w.WriteField("alert_body", sp[1])
  46. w.WriteField("mids", xstr.JoinInts(*fans))
  47. w.WriteField("link_type", "2") // 2代表视频稿件播放页
  48. w.WriteField("link_value", strconv.FormatInt(arc.ID, 10))
  49. w.WriteField("uuid", uuid)
  50. // 1、v5.20.0 后客户端才接特殊关注 2、 iPad版本没更新,不推
  51. w.WriteField("builds", `{"2":{"Build":6500,"Condition":"gte"}, "3":{"Build":0,"Condition":"lt"}, "1":{"Build":519010,"Condition":"gte"}}`)
  52. w.Close()
  53. query := map[string]string{
  54. "ts": strconv.FormatInt(time.Now().Unix(), 10),
  55. "appkey": d.c.HTTPClient.Key,
  56. }
  57. query["sign"] = d.signature(query, d.c.HTTPClient.Secret)
  58. url := fmt.Sprintf("%s?ts=%s&appkey=%s&sign=%s", d.c.Push.AddAPI, query["ts"], query["appkey"], query["sign"])
  59. req, err := http.NewRequest(http.MethodPost, url, buf)
  60. if err != nil {
  61. log.Error("http.NewRequest(%s) error(%v) uuid(%s)", url, err, uuid)
  62. PromError("http:NewRequest")
  63. return
  64. }
  65. req.Header.Set("Content-Type", w.FormDataContentType())
  66. req.Header.Set("Authorization", fmt.Sprintf("token=%s", businessToken))
  67. res := &_response{}
  68. if err = d.httpClient.Do(context.TODO(), req, &res); err != nil {
  69. log.Error("httpClient.Do() error(%v)", err)
  70. PromError("http:Do")
  71. return
  72. }
  73. if res.Code != 0 || res.Data == 0 {
  74. log.Error("push failed archive(%d) upper(%d) fans_total(%d) group(%s) response(%+v)", arc.ID, arc.Mid, len(*fans), group, res)
  75. } else {
  76. log.Info("push success archive(%d) upper(%d) fans_total(%d) group(%s) response(%+v)", arc.ID, arc.Mid, len(*fans), group, res)
  77. }
  78. return
  79. }