banner.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package http
  2. import (
  3. "net/http"
  4. "time"
  5. "go-common/library/log"
  6. bm "go-common/library/net/http/blademaster"
  7. "go-common/library/net/http/blademaster/render"
  8. )
  9. func banners(c *bm.Context) {
  10. v := new(struct {
  11. From int64 `form:"from" validate:"min=0" default:"0"`
  12. Limit int64 `form:"limit" validate:"min=1" default:"20"`
  13. })
  14. if err := c.Bind(v); err != nil {
  15. return
  16. }
  17. total, bs, err := svr.Banners(c, v.From, v.Limit)
  18. if err != nil {
  19. log.Error("growup svr.Banners error(%v)", err)
  20. c.JSON(nil, err)
  21. return
  22. }
  23. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  24. "code": 0,
  25. "message": "0",
  26. "data": bs,
  27. "paging": map[string]int64{
  28. "page_size": v.Limit,
  29. "total": total,
  30. },
  31. }))
  32. }
  33. func addBanner(c *bm.Context) {
  34. v := new(struct {
  35. Image string `form:"image" validate:"required"`
  36. Link string `form:"link" validate:"required"`
  37. StartAt int64 `form:"start_at" validate:"required"`
  38. EndAt int64 `form:"end_at" validate:"required"`
  39. })
  40. if err := c.Bind(v); err != nil {
  41. return
  42. }
  43. dup, err := svr.AddBanner(c, v.Image, v.Link, v.StartAt, v.EndAt)
  44. if err != nil {
  45. log.Error("growup svr.AddBanner error(%v)", err)
  46. c.JSON(nil, err)
  47. return
  48. }
  49. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  50. "code": 0,
  51. "message": "0",
  52. "data": dup,
  53. }))
  54. }
  55. func editBanner(c *bm.Context) {
  56. v := new(struct {
  57. ID int64 `form:"id" validate:"required"`
  58. Image string `form:"image" validate:"required"`
  59. Link string `form:"link" validate:"required"`
  60. StartAt int64 `form:"start_at" validate:"required"`
  61. EndAt int64 `form:"end_at" validate:"required"`
  62. })
  63. if err := c.Bind(v); err != nil {
  64. return
  65. }
  66. dup, err := svr.EditBanner(c, v.ID, v.StartAt, v.EndAt, v.Image, v.Link)
  67. if err != nil {
  68. log.Error("growup svr.AddBanner error(%v)", err)
  69. c.JSON(nil, err)
  70. return
  71. }
  72. c.Render(http.StatusOK, render.MapJSON(map[string]interface{}{
  73. "code": 0,
  74. "message": "0",
  75. "data": dup,
  76. }))
  77. }
  78. func off(c *bm.Context) {
  79. v := new(struct {
  80. ID int64 `form:"id" validate:"required"`
  81. })
  82. if err := c.Bind(v); err != nil {
  83. return
  84. }
  85. err := svr.Off(c, time.Now().Unix(), v.ID)
  86. if err != nil {
  87. log.Error("growup svr.AddBanner error(%v)", err)
  88. }
  89. c.JSON(nil, err)
  90. }