task.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package http
  2. import (
  3. "net/url"
  4. "strconv"
  5. "strings"
  6. "time"
  7. pushmdl "go-common/app/service/main/push/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. xtime "go-common/library/time"
  12. )
  13. func addTask(c *bm.Context) {
  14. var (
  15. id int64
  16. err error
  17. req = c.Request
  18. auth = req.Header.Get("Authorization")
  19. )
  20. req.ParseMultipartForm(500 * 1024 * 1024) // 500M
  21. appID, _ := strconv.ParseInt(req.FormValue("app_id"), 10, 64)
  22. if appID < 1 {
  23. log.Error("app_id is wrong: %s", req.FormValue("app_id"))
  24. c.JSON(nil, ecode.RequestErr)
  25. return
  26. }
  27. businessID, _ := strconv.ParseInt(req.FormValue("business_id"), 10, 64)
  28. if businessID < 1 {
  29. log.Error("business_id is wrong: %s", req.FormValue("business_id"))
  30. c.JSON(nil, ecode.RequestErr)
  31. return
  32. }
  33. platform := req.FormValue("platform")
  34. am, err := url.ParseQuery(auth)
  35. if err != nil {
  36. log.Error("parse Authorization(%s) error(%v)", auth, err)
  37. c.JSON(nil, ecode.RequestErr)
  38. return
  39. }
  40. token := am.Get("token")
  41. if token == "" {
  42. log.Error("token is empty")
  43. c.JSON(nil, ecode.RequestErr)
  44. return
  45. }
  46. alertTitle := req.FormValue("alert_title")
  47. alertBody := req.FormValue("alert_body")
  48. if alertBody == "" {
  49. log.Error("alert_body is empty")
  50. c.JSON(nil, ecode.RequestErr)
  51. return
  52. }
  53. mids := req.FormValue("mids")
  54. if mids == "" {
  55. log.Error("mids is empty")
  56. c.JSON(nil, ecode.RequestErr)
  57. return
  58. }
  59. linkType, _ := strconv.Atoi(req.FormValue("link_type"))
  60. if linkType < 1 {
  61. log.Error("link_type is wrong: %s", req.FormValue("link_type"))
  62. c.JSON(nil, ecode.RequestErr)
  63. return
  64. }
  65. linkValue := req.FormValue("link_value")
  66. expireTime, _ := strconv.ParseInt(req.FormValue("expire_time"), 10, 64)
  67. if expireTime == 0 {
  68. expireTime = time.Now().Add(3 * 24 * time.Hour).Unix()
  69. }
  70. pushTime, _ := strconv.ParseInt(req.FormValue("push_time"), 10, 64)
  71. if pushTime == 0 {
  72. pushTime = time.Now().Unix()
  73. }
  74. passThrough, _ := strconv.Atoi(req.FormValue("pass_throught"))
  75. builds := req.FormValue("builds")
  76. group := req.FormValue("group")
  77. uuid := req.FormValue("uuid")
  78. imageURL := req.FormValue("image_url")
  79. task := &pushmdl.Task{
  80. Job: pushmdl.JobName(time.Now().UnixNano(), alertBody, linkValue, group),
  81. Type: pushmdl.TaskTypeStrategyMid,
  82. APPID: appID,
  83. BusinessID: businessID,
  84. Platform: pushmdl.SplitInts(platform),
  85. Title: alertTitle,
  86. Summary: alertBody,
  87. LinkType: int8(linkType),
  88. LinkValue: linkValue,
  89. Build: pushmdl.ParseBuild(builds),
  90. PassThrough: passThrough,
  91. PushTime: xtime.Time(pushTime),
  92. ExpireTime: xtime.Time(expireTime),
  93. Status: pushmdl.TaskStatusPretreatmentPrepared,
  94. Group: group,
  95. ImageURL: imageURL,
  96. Extra: &pushmdl.TaskExtra{Group: group},
  97. }
  98. log.Info("http add task(%d) uuid(%s) business(%d) link_value(%s) mids(%d)", task.Job, uuid, task.BusinessID, task.LinkValue, len(strings.Split(mids, ",")))
  99. if id, err = srv.AddTask(c, uuid, token, task, mids); err != nil {
  100. c.JSON(nil, err)
  101. return
  102. }
  103. c.JSON(id, nil)
  104. }