card.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package http
  2. import (
  3. "io/ioutil"
  4. "mime/multipart"
  5. "net/http"
  6. "go-common/app/admin/main/card/model"
  7. "go-common/library/ecode"
  8. bm "go-common/library/net/http/blademaster"
  9. "go-common/library/net/http/blademaster/binding"
  10. )
  11. func groups(c *bm.Context) {
  12. var err error
  13. arg := new(model.ArgQueryGroup)
  14. if err = c.Bind(arg); err != nil {
  15. return
  16. }
  17. c.JSON(srv.GroupList(c, arg))
  18. }
  19. func cards(c *bm.Context) {
  20. var err error
  21. arg := new(model.ArgQueryCards)
  22. if err = c.Bind(arg); err != nil {
  23. return
  24. }
  25. c.JSON(srv.CardsByGid(c, arg.GroupID))
  26. }
  27. func addGroup(c *bm.Context) {
  28. var err error
  29. username, ok := c.Get("username")
  30. if !ok {
  31. c.JSON(nil, ecode.AccessDenied)
  32. return
  33. }
  34. arg := new(model.AddGroup)
  35. arg.Operator = username.(string)
  36. if err = c.Bind(arg); err != nil {
  37. return
  38. }
  39. if len(arg.Name) > _maxnamelen {
  40. c.JSON(nil, ecode.CardNameTooLongErr)
  41. return
  42. }
  43. c.JSON(nil, srv.AddGroup(c, arg))
  44. }
  45. func updateGroup(c *bm.Context) {
  46. var err error
  47. username, ok := c.Get("username")
  48. if !ok {
  49. c.JSON(nil, ecode.AccessDenied)
  50. return
  51. }
  52. arg := new(model.UpdateGroup)
  53. arg.Operator = username.(string)
  54. if err = c.Bind(arg); err != nil {
  55. return
  56. }
  57. if len(arg.Name) > _maxnamelen {
  58. c.JSON(nil, ecode.CardNameTooLongErr)
  59. return
  60. }
  61. c.JSON(nil, srv.UpdateGroup(c, arg))
  62. }
  63. func addCard(c *bm.Context) {
  64. var err error
  65. username, ok := c.Get("username")
  66. if !ok {
  67. c.JSON(nil, ecode.AccessDenied)
  68. return
  69. }
  70. arg := new(model.AddCard)
  71. arg.Operator = username.(string)
  72. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  73. return
  74. }
  75. if len(arg.Name) > _maxnamelen {
  76. c.JSON(nil, ecode.CardNameTooLongErr)
  77. return
  78. }
  79. if arg.CardBody, arg.CardFileType, err = file(c, "card_url"); err != nil {
  80. c.JSON(nil, err)
  81. return
  82. }
  83. if arg.CardFileType == "" {
  84. c.JSON(nil, ecode.CardImageEmptyErr)
  85. return
  86. }
  87. if arg.BigCardBody, arg.BigCardFileType, err = file(c, "big_crad_url"); err != nil {
  88. c.JSON(nil, err)
  89. return
  90. }
  91. if arg.BigCardFileType == "" {
  92. c.JSON(nil, ecode.CardImageEmptyErr)
  93. return
  94. }
  95. c.JSON(nil, srv.AddCard(c, arg))
  96. }
  97. func updateCard(c *bm.Context) {
  98. var err error
  99. username, ok := c.Get("username")
  100. if !ok {
  101. c.JSON(nil, ecode.AccessDenied)
  102. return
  103. }
  104. arg := new(model.UpdateCard)
  105. arg.Operator = username.(string)
  106. if err = c.BindWith(arg, binding.FormMultipart); err != nil {
  107. return
  108. }
  109. if len(arg.Name) > _maxnamelen {
  110. c.JSON(nil, ecode.CardNameTooLongErr)
  111. return
  112. }
  113. if arg.CardBody, arg.CardFileType, err = file(c, "card_url"); err != nil {
  114. c.JSON(nil, err)
  115. return
  116. }
  117. if arg.BigCardBody, arg.BigCardFileType, err = file(c, "big_crad_url"); err != nil {
  118. c.JSON(nil, err)
  119. return
  120. }
  121. c.JSON(nil, srv.UpdateCard(c, arg))
  122. }
  123. func cardStateChange(c *bm.Context) {
  124. var err error
  125. arg := new(model.ArgState)
  126. if err = c.Bind(arg); err != nil {
  127. return
  128. }
  129. c.JSON(nil, srv.UpdateCardState(c, arg))
  130. }
  131. func deleteCard(c *bm.Context) {
  132. var err error
  133. arg := new(model.ArgID)
  134. if err = c.Bind(arg); err != nil {
  135. return
  136. }
  137. c.JSON(nil, srv.DeleteCard(c, arg.ID))
  138. }
  139. func groupStateChange(c *bm.Context) {
  140. var err error
  141. arg := new(model.ArgState)
  142. if err = c.Bind(arg); err != nil {
  143. return
  144. }
  145. c.JSON(nil, srv.UpdateGroupState(c, arg))
  146. }
  147. func deleteGroup(c *bm.Context) {
  148. var err error
  149. arg := new(model.ArgID)
  150. if err = c.Bind(arg); err != nil {
  151. return
  152. }
  153. c.JSON(nil, srv.DeleteGroup(c, arg.ID))
  154. }
  155. func cardOrderChange(c *bm.Context) {
  156. var err error
  157. arg := new(model.ArgIds)
  158. if err = c.Bind(arg); err != nil {
  159. return
  160. }
  161. c.JSON(nil, srv.CardOrderChange(c, arg))
  162. }
  163. func groupOrderChange(c *bm.Context) {
  164. var err error
  165. arg := new(model.ArgIds)
  166. if err = c.Bind(arg); err != nil {
  167. return
  168. }
  169. c.JSON(nil, srv.GroupOrderChange(c, arg))
  170. }
  171. func file(c *bm.Context, name string) (body []byte, filetype string, err error) {
  172. var file multipart.File
  173. if file, _, err = c.Request.FormFile(name); err != nil {
  174. if err == http.ErrMissingFile {
  175. err = nil
  176. return
  177. }
  178. err = ecode.RequestErr
  179. return
  180. }
  181. if file == nil {
  182. return
  183. }
  184. defer file.Close()
  185. if body, err = ioutil.ReadAll(file); err != nil {
  186. err = ecode.RequestErr
  187. return
  188. }
  189. filetype = http.DetectContentType(body)
  190. if err = checkImgFileType(filetype); err != nil {
  191. return
  192. }
  193. err = checkFileBody(body)
  194. return
  195. }
  196. func checkImgFileType(filetype string) error {
  197. switch filetype {
  198. case "image/jpeg", "image/jpg":
  199. case "image/png":
  200. default:
  201. return ecode.VipFileTypeErr
  202. }
  203. return nil
  204. }
  205. func checkFileBody(body []byte) error {
  206. if len(body) == 0 {
  207. return ecode.FileNotExists
  208. }
  209. if len(body) > cf.Bfs.MaxFileSize {
  210. return ecode.FileTooLarge
  211. }
  212. return nil
  213. }