invite.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package http
  2. import (
  3. "strconv"
  4. "time"
  5. "go-common/library/ecode"
  6. "go-common/library/log"
  7. bm "go-common/library/net/http/blademaster"
  8. )
  9. const (
  10. _format = "2006-01-02"
  11. )
  12. var (
  13. _loc = time.Now().Location()
  14. )
  15. // generate generate invite code in batches.
  16. func generate(c *bm.Context) {
  17. var (
  18. err error
  19. params = c.Request.Form
  20. )
  21. numStr := params.Get("num")
  22. if numStr == "" {
  23. httpCode(c, ecode.RequestErr)
  24. return
  25. }
  26. num, err := strconv.ParseInt(numStr, 10, 64)
  27. if err != nil {
  28. httpCode(c, ecode.RequestErr)
  29. return
  30. }
  31. midStr := params.Get("mid")
  32. if midStr == "" {
  33. httpCode(c, ecode.RequestErr)
  34. return
  35. }
  36. mid, err := strconv.ParseInt(midStr, 10, 64)
  37. if err != nil {
  38. httpCode(c, ecode.RequestErr)
  39. return
  40. }
  41. expireDayStr := params.Get("expire")
  42. if expireDayStr == "" {
  43. httpCode(c, ecode.RequestErr)
  44. return
  45. }
  46. expireDay, err := strconv.ParseInt(expireDayStr, 10, 64)
  47. if err != nil {
  48. httpCode(c, ecode.RequestErr)
  49. return
  50. }
  51. invs, err := svc.Generate(c, mid, num, expireDay)
  52. if err != nil {
  53. log.Error("service.Generate(%d, %d, %d) error(%v)", mid, num, expireDay, err)
  54. httpCode(c, err)
  55. return
  56. }
  57. httpData(c, invs, nil)
  58. }
  59. // list get invite codes range from and to.
  60. func list(c *bm.Context) {
  61. params := c.Request.Form
  62. midStr := params.Get("mid")
  63. if midStr == "" {
  64. httpCode(c, ecode.RequestErr)
  65. return
  66. }
  67. mid, err := strconv.ParseInt(midStr, 10, 64)
  68. if err != nil {
  69. httpCode(c, ecode.RequestErr)
  70. return
  71. }
  72. fromStr := params.Get("from")
  73. toStr := params.Get("to")
  74. if fromStr == "" || toStr == "" {
  75. httpCode(c, ecode.RequestErr)
  76. return
  77. }
  78. from, to, ok := rangeDate(fromStr, toStr)
  79. if !ok {
  80. httpCode(c, ecode.RequestErr)
  81. return
  82. }
  83. fromTs := from.Unix()
  84. toTs := to.Unix()
  85. if fromTs < 0 || toTs < 0 || fromTs > toTs {
  86. httpCode(c, ecode.RequestErr)
  87. return
  88. }
  89. invs, err := svc.List(c, mid, fromTs, toTs)
  90. if err != nil {
  91. log.Error("service.List(%d, %d, %d) error(%v)", mid, from, to, err)
  92. httpCode(c, err)
  93. return
  94. }
  95. httpData(c, invs, nil)
  96. }
  97. func rangeDate(fromStr, toStr string) (from, to time.Time, ok bool) {
  98. ok = true
  99. if fromStr == "" || toStr == "" {
  100. ok = false
  101. return
  102. }
  103. fromDate, err := time.ParseInLocation(_format, fromStr, _loc)
  104. if err != nil {
  105. ok = false
  106. return
  107. }
  108. toDate, err := time.ParseInLocation(_format, toStr, _loc)
  109. if err != nil {
  110. ok = false
  111. return
  112. }
  113. from = time.Date(fromDate.Year(), fromDate.Month(), fromDate.Day(), 0, 0, 0, 0, fromDate.Location())
  114. to = time.Date(toDate.Year(), toDate.Month(), toDate.Day(), 23, 59, 59, 0, toDate.Location())
  115. return
  116. }