task.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package http
  2. import (
  3. "regexp"
  4. "strconv"
  5. "go-common/app/admin/main/laser/model"
  6. "go-common/library/ecode"
  7. "go-common/library/log"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. emailRegex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"
  12. )
  13. func addTask(c *bm.Context) {
  14. req := c.Request
  15. v := new(struct {
  16. MID int64 `form:"mid" validate:"required"`
  17. LogDate int64 `form:"log_date" validate:"required"`
  18. ContactEmail string `form:"contact_email" validate:"required"`
  19. Platform int `form:"platform" validate:"required"`
  20. SourceType int `form:"source_type" validate:"required"`
  21. })
  22. err := c.Bind(v)
  23. if err != nil {
  24. return
  25. }
  26. if v.LogDate <= 0 || v.MID <= 0 || !checkEmail(v.ContactEmail) || v.Platform <= 0 || v.SourceType <= 0 {
  27. c.JSON(nil, ecode.RequestErr)
  28. return
  29. }
  30. userCookie, err := req.Cookie("username")
  31. if err != nil {
  32. c.JSON(nil, err)
  33. return
  34. }
  35. username := userCookie.Value
  36. uidCookie, err := req.Cookie("uid")
  37. var adminID int64
  38. if err != nil {
  39. adminID = 0
  40. } else {
  41. adminID, err = strconv.ParseInt(uidCookie.Value, 10, 64)
  42. if err != nil {
  43. c.JSON(nil, err)
  44. return
  45. }
  46. }
  47. err = svc.AddTask(c, v.MID, username, adminID, v.LogDate, v.ContactEmail, v.Platform, v.SourceType)
  48. if err != nil {
  49. log.Error("svc.AddTask() error(%v)", err)
  50. }
  51. c.JSON(nil, err)
  52. }
  53. func checkEmail(emailAddr string) (match bool) {
  54. if emailAddr == "" {
  55. return false
  56. }
  57. match, err := regexp.MatchString(emailRegex, emailAddr)
  58. if err != nil {
  59. return false
  60. }
  61. return match
  62. }
  63. func deleteTask(c *bm.Context) {
  64. req := c.Request
  65. v := new(struct {
  66. TaskID int64 `form:"task_id"`
  67. })
  68. err := c.Bind(v)
  69. if err != nil {
  70. return
  71. }
  72. uidCookie, err := req.Cookie("uid")
  73. var adminID int64
  74. if err != nil {
  75. adminID = 0
  76. } else {
  77. adminID, err = strconv.ParseInt(uidCookie.Value, 10, 64)
  78. if err != nil {
  79. c.JSON(nil, ecode.RequestErr)
  80. return
  81. }
  82. }
  83. userCookie, err := req.Cookie("username")
  84. if err != nil {
  85. c.JSON(nil, ecode.Unauthorized)
  86. return
  87. }
  88. username := userCookie.Value
  89. if err = svc.DeleteTask(c, v.TaskID, username, adminID); err != nil {
  90. log.Error("svc.DeleteTask() error(%v)", err)
  91. c.JSON(nil, err)
  92. return
  93. }
  94. c.JSON(nil, nil)
  95. }
  96. func queryTask(c *bm.Context) {
  97. v := new(struct {
  98. Mid int64 `form:"mid"`
  99. LogDateStart int64 `form:"log_date_start"`
  100. LogDateEnd int64 `form:"log_date_end"`
  101. SourceType int `form:"source_type"`
  102. Platform int `form:"platform"`
  103. State int `form:"state"`
  104. SortBy string `form:"sort"`
  105. PageNo int `form:"page_no"`
  106. PageSize int `form:"page_size"`
  107. })
  108. err := c.Bind(v)
  109. if err != nil {
  110. return
  111. }
  112. tasks, count, err := svc.QueryTask(c, v.Mid, v.LogDateStart, v.LogDateEnd, v.SourceType, v.Platform, v.State, v.SortBy, v.PageNo, v.PageSize)
  113. if err != nil {
  114. log.Error("svc.QueryTask() error(%v)", err)
  115. c.JSON(nil, err)
  116. return
  117. }
  118. pager := &model.TaskPager{
  119. PageSize: v.PageSize,
  120. PageNo: v.PageNo,
  121. Total: count,
  122. Items: tasks,
  123. }
  124. c.JSON(pager, nil)
  125. }
  126. func updateTask(c *bm.Context) {
  127. req := c.Request
  128. v := new(struct {
  129. TaskID int64 `form:"task_id" validate:"required"`
  130. MID int64 `form:"mid" validate:"required"`
  131. LogDate int64 `form:"log_date" validate:"required"`
  132. ContactEmail string `form:"contact_email" validate:"required"`
  133. SourceType int `form:"source_type" validate:"required"`
  134. Platform int `form:"platform" validate:"required"`
  135. })
  136. err := c.Bind(v)
  137. if err != nil {
  138. return
  139. }
  140. if v.ContactEmail != "" && !checkEmail(v.ContactEmail) {
  141. c.JSON(nil, ecode.RequestErr)
  142. return
  143. }
  144. uidCookie, err := req.Cookie("uid")
  145. var adminID int64
  146. if err != nil {
  147. adminID = 0
  148. } else {
  149. adminID, err = strconv.ParseInt(uidCookie.Value, 10, 64)
  150. if err != nil {
  151. c.JSON(nil, err)
  152. return
  153. }
  154. }
  155. userCookie, err := req.Cookie("username")
  156. if err != nil {
  157. c.JSON(nil, err)
  158. return
  159. }
  160. username := userCookie.Value
  161. err = svc.UpdateTask(c, username, adminID, v.TaskID, v.MID, v.LogDate, v.ContactEmail, v.SourceType, v.Platform)
  162. if err != nil {
  163. log.Error("svc.UpdateTask() error(%v)", err)
  164. }
  165. c.JSON(nil, err)
  166. }