123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- package http
- import (
- "io/ioutil"
- "mime/multipart"
- "net/http"
- "go-common/app/admin/main/card/model"
- "go-common/library/ecode"
- bm "go-common/library/net/http/blademaster"
- "go-common/library/net/http/blademaster/binding"
- )
- func groups(c *bm.Context) {
- var err error
- arg := new(model.ArgQueryGroup)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(srv.GroupList(c, arg))
- }
- func cards(c *bm.Context) {
- var err error
- arg := new(model.ArgQueryCards)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(srv.CardsByGid(c, arg.GroupID))
- }
- func addGroup(c *bm.Context) {
- var err error
- username, ok := c.Get("username")
- if !ok {
- c.JSON(nil, ecode.AccessDenied)
- return
- }
- arg := new(model.AddGroup)
- arg.Operator = username.(string)
- if err = c.Bind(arg); err != nil {
- return
- }
- if len(arg.Name) > _maxnamelen {
- c.JSON(nil, ecode.CardNameTooLongErr)
- return
- }
- c.JSON(nil, srv.AddGroup(c, arg))
- }
- func updateGroup(c *bm.Context) {
- var err error
- username, ok := c.Get("username")
- if !ok {
- c.JSON(nil, ecode.AccessDenied)
- return
- }
- arg := new(model.UpdateGroup)
- arg.Operator = username.(string)
- if err = c.Bind(arg); err != nil {
- return
- }
- if len(arg.Name) > _maxnamelen {
- c.JSON(nil, ecode.CardNameTooLongErr)
- return
- }
- c.JSON(nil, srv.UpdateGroup(c, arg))
- }
- func addCard(c *bm.Context) {
- var err error
- username, ok := c.Get("username")
- if !ok {
- c.JSON(nil, ecode.AccessDenied)
- return
- }
- arg := new(model.AddCard)
- arg.Operator = username.(string)
- if err = c.BindWith(arg, binding.FormMultipart); err != nil {
- return
- }
- if len(arg.Name) > _maxnamelen {
- c.JSON(nil, ecode.CardNameTooLongErr)
- return
- }
- if arg.CardBody, arg.CardFileType, err = file(c, "card_url"); err != nil {
- c.JSON(nil, err)
- return
- }
- if arg.CardFileType == "" {
- c.JSON(nil, ecode.CardImageEmptyErr)
- return
- }
- if arg.BigCardBody, arg.BigCardFileType, err = file(c, "big_crad_url"); err != nil {
- c.JSON(nil, err)
- return
- }
- if arg.BigCardFileType == "" {
- c.JSON(nil, ecode.CardImageEmptyErr)
- return
- }
- c.JSON(nil, srv.AddCard(c, arg))
- }
- func updateCard(c *bm.Context) {
- var err error
- username, ok := c.Get("username")
- if !ok {
- c.JSON(nil, ecode.AccessDenied)
- return
- }
- arg := new(model.UpdateCard)
- arg.Operator = username.(string)
- if err = c.BindWith(arg, binding.FormMultipart); err != nil {
- return
- }
- if len(arg.Name) > _maxnamelen {
- c.JSON(nil, ecode.CardNameTooLongErr)
- return
- }
- if arg.CardBody, arg.CardFileType, err = file(c, "card_url"); err != nil {
- c.JSON(nil, err)
- return
- }
- if arg.BigCardBody, arg.BigCardFileType, err = file(c, "big_crad_url"); err != nil {
- c.JSON(nil, err)
- return
- }
- c.JSON(nil, srv.UpdateCard(c, arg))
- }
- func cardStateChange(c *bm.Context) {
- var err error
- arg := new(model.ArgState)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.UpdateCardState(c, arg))
- }
- func deleteCard(c *bm.Context) {
- var err error
- arg := new(model.ArgID)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.DeleteCard(c, arg.ID))
- }
- func groupStateChange(c *bm.Context) {
- var err error
- arg := new(model.ArgState)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.UpdateGroupState(c, arg))
- }
- func deleteGroup(c *bm.Context) {
- var err error
- arg := new(model.ArgID)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.DeleteGroup(c, arg.ID))
- }
- func cardOrderChange(c *bm.Context) {
- var err error
- arg := new(model.ArgIds)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.CardOrderChange(c, arg))
- }
- func groupOrderChange(c *bm.Context) {
- var err error
- arg := new(model.ArgIds)
- if err = c.Bind(arg); err != nil {
- return
- }
- c.JSON(nil, srv.GroupOrderChange(c, arg))
- }
- func file(c *bm.Context, name string) (body []byte, filetype string, err error) {
- var file multipart.File
- if file, _, err = c.Request.FormFile(name); err != nil {
- if err == http.ErrMissingFile {
- err = nil
- return
- }
- err = ecode.RequestErr
- return
- }
- if file == nil {
- return
- }
- defer file.Close()
- if body, err = ioutil.ReadAll(file); err != nil {
- err = ecode.RequestErr
- return
- }
- filetype = http.DetectContentType(body)
- if err = checkImgFileType(filetype); err != nil {
- return
- }
- err = checkFileBody(body)
- return
- }
- func checkImgFileType(filetype string) error {
- switch filetype {
- case "image/jpeg", "image/jpg":
- case "image/png":
- default:
- return ecode.VipFileTypeErr
- }
- return nil
- }
- func checkFileBody(body []byte) error {
- if len(body) == 0 {
- return ecode.FileNotExists
- }
- if len(body) > cf.Bfs.MaxFileSize {
- return ecode.FileTooLarge
- }
- return nil
- }
|