task.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package http
  2. import (
  3. "fmt"
  4. "net"
  5. "net/http"
  6. "strings"
  7. "go-common/app/admin/main/dm/model"
  8. "go-common/library/ecode"
  9. bm "go-common/library/net/http/blademaster"
  10. "go-common/library/xstr"
  11. )
  12. func taskList(c *bm.Context) {
  13. v := new(model.TaskListArg)
  14. if err := c.Bind(v); err != nil {
  15. return
  16. }
  17. c.JSON(dmSvc.TaskList(c, v))
  18. }
  19. func addTask(c *bm.Context) {
  20. var (
  21. uname, _ = c.Get("username")
  22. v = new(model.AddTaskArg)
  23. err error
  24. )
  25. if err = c.Bind(v); err != nil {
  26. return
  27. }
  28. if v.Regex == "" && v.Mids == "" && v.IPs == "" && v.Cids == "" && v.KeyWords == "" {
  29. c.JSON(nil, ecode.RequestErr)
  30. return
  31. }
  32. if _, err = xstr.SplitInts(v.Mids); err != nil {
  33. c.JSON(nil, ecode.RequestErr)
  34. return
  35. }
  36. if _, err = xstr.SplitInts(v.Cids); err != nil {
  37. c.JSON(nil, ecode.RequestErr)
  38. return
  39. }
  40. if v.IPs != "" {
  41. ips := strings.Split(v.IPs, ",")
  42. for _, ip := range ips {
  43. tmp := net.ParseIP(ip)
  44. if tmp == nil {
  45. c.JSON(nil, ecode.RequestErr)
  46. return
  47. }
  48. }
  49. }
  50. v.Creator = uname.(string)
  51. c.JSON(nil, dmSvc.AddTask(c, v))
  52. }
  53. func editTaskState(c *bm.Context) {
  54. v := new(model.EditTasksStateArg)
  55. if err := c.Bind(v); err != nil {
  56. return
  57. }
  58. c.JSON(nil, dmSvc.EditTaskState(c, v))
  59. }
  60. func reviewTask(c *bm.Context) {
  61. var (
  62. reviewer, _ = c.Get("username")
  63. v = new(model.ReviewTaskArg)
  64. )
  65. if err := c.Bind(v); err != nil {
  66. return
  67. }
  68. v.Reviewer = reviewer.(string)
  69. c.JSON(nil, dmSvc.ReviewTask(c, v))
  70. }
  71. func taskView(c *bm.Context) {
  72. v := new(model.TaskViewArg)
  73. if err := c.Bind(v); err != nil {
  74. return
  75. }
  76. c.JSON(dmSvc.TaskView(c, v))
  77. }
  78. func taskCsv(c *bm.Context) {
  79. var (
  80. bs []byte
  81. err error
  82. contentType = "text/csv"
  83. )
  84. v := new(model.TaskCsvArg)
  85. if err = c.Bind(v); err != nil {
  86. return
  87. }
  88. if bs, err = dmSvc.TaskCsv(c, v.ID); err != nil {
  89. c.JSON(nil, err)
  90. return
  91. }
  92. c.Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment;filename=%v.csv", v.ID))
  93. c.Bytes(http.StatusOK, contentType, bs)
  94. }