welfare.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "net/http"
  6. "go-common/app/admin/main/vip/model"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. )
  10. const (
  11. _maxFileSize = 1048576
  12. )
  13. func welfareTypeSave(c *bm.Context) {
  14. arg := new(struct {
  15. ID int `form:"id"`
  16. Name string `form:"name" validate:"required"`
  17. })
  18. if err := c.Bind(arg); err != nil {
  19. return
  20. }
  21. username, _ := c.Get("username")
  22. c.JSON(nil, vipSvc.WelfareTypeSave(arg.ID, arg.Name, username.(string)))
  23. }
  24. func welfareTypeState(c *bm.Context) {
  25. arg := new(struct {
  26. ID int `form:"id" validate:"required"`
  27. })
  28. if err := c.Bind(arg); err != nil {
  29. return
  30. }
  31. username, _ := c.Get("username")
  32. c.JSON(nil, vipSvc.WelfareTypeState(c, arg.ID, username.(string)))
  33. }
  34. func welfareTypeList(c *bm.Context) {
  35. c.JSON(vipSvc.WelfareTypeList())
  36. }
  37. func welfareSave(c *bm.Context) {
  38. const _redirectThridPage = 3
  39. wfReq := new(model.WelfareReq)
  40. if err := c.Bind(wfReq); err != nil {
  41. return
  42. }
  43. if wfReq.UsageForm == _redirectThridPage && wfReq.ReceiveUri == "" {
  44. c.JSON(nil, ecode.VipWelfareRequestErr)
  45. return
  46. }
  47. username, _ := c.Get("username")
  48. c.JSON(nil, vipSvc.WelfareSave(username.(string), wfReq))
  49. }
  50. func welfareState(c *bm.Context) {
  51. arg := new(struct {
  52. ID int `form:"id" validate:"required"`
  53. })
  54. if err := c.Bind(arg); err != nil {
  55. return
  56. }
  57. username, _ := c.Get("username")
  58. c.JSON(nil, vipSvc.WelfareState(arg.ID, username.(string)))
  59. }
  60. func welfareList(c *bm.Context) {
  61. arg := new(struct {
  62. TID int `form:"tid"`
  63. })
  64. if err := c.Bind(arg); err != nil {
  65. return
  66. }
  67. c.JSON(vipSvc.WelfareList(arg.TID))
  68. }
  69. func welfareBatchUpload(c *bm.Context) {
  70. arg := new(struct {
  71. WID int `form:"wid"`
  72. Filename string `form:"filename"`
  73. Vtime int `form:"vtime"`
  74. })
  75. if err := c.Bind(arg); err != nil {
  76. return
  77. }
  78. fileBody, _, err := getFileBody(c, "file")
  79. if err != nil {
  80. c.JSON(nil, err)
  81. return
  82. }
  83. username, _ := c.Get("username")
  84. c.JSON(nil, vipSvc.WelfareBatchUpload(fileBody, arg.Filename, username.(string), arg.WID, arg.Vtime))
  85. }
  86. func welfareBatchList(c *bm.Context) {
  87. arg := new(struct {
  88. WID int `form:"wid" validate:"required"`
  89. })
  90. if err := c.Bind(arg); err != nil {
  91. return
  92. }
  93. c.JSON(vipSvc.WelfareBatchList(arg.WID))
  94. }
  95. func welfareBatchState(c *bm.Context) {
  96. arg := new(struct {
  97. ID int `form:"id" validate:"required"`
  98. })
  99. if err := c.Bind(arg); err != nil {
  100. return
  101. }
  102. username, _ := c.Get("username")
  103. c.JSON(nil, vipSvc.WelfareBatchState(c, arg.ID, username.(string)))
  104. }
  105. func getFileBody(c *bm.Context, name string) (body []byte, filetype string, err error) {
  106. var file multipart.File
  107. if file, _, err = c.Request.FormFile(name); err != nil {
  108. if err == http.ErrMissingFile {
  109. err = nil
  110. return
  111. }
  112. err = ecode.RequestErr
  113. return
  114. }
  115. if file == nil {
  116. return
  117. }
  118. defer file.Close()
  119. if body, err = ioutil.ReadAll(file); err != nil {
  120. err = ecode.RequestErr
  121. return
  122. }
  123. filetype = http.DetectContentType(body)
  124. if filetype != "text/plain; charset=utf-8" {
  125. err = ecode.VipFileTypeErr
  126. return
  127. }
  128. if len(body) > _maxFileSize {
  129. err = ecode.FileTooLarge
  130. return
  131. }
  132. return
  133. }