From 3c34997623d76f9a57a1630b4ff1fa1858bbb5b8 Mon Sep 17 00:00:00 2001 From: wzj Date: Thu, 21 Nov 2024 21:52:35 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=94=AF=E6=8C=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=A4=9A=E5=BC=A0=E5=95=86=E5=93=81=E5=9B=BE=E7=89=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/item_controller.go | 4 ++-- ecode/auth_ecode.go | 4 ++++ ecode/file_ecode.go | 8 +++++--- ecode/item_ecode.go | 2 ++ model/item_model.go | 17 ++++++++++++++++- service/captcha_service.go | 3 +++ service/file_service.go | 13 +++++++++++++ service/item_service.go | 7 +++++-- service/user_service.go | 4 ++++ validate/item_validate.go | 6 +++--- 10 files changed, 57 insertions(+), 11 deletions(-) diff --git a/controller/item_controller.go b/controller/item_controller.go index 771cf7a..1218c68 100644 --- a/controller/item_controller.go +++ b/controller/item_controller.go @@ -135,13 +135,13 @@ func (c *ItemController) Create() mvc.Result { } // 验证图片文件是否存在 - _, e = c.Service.File.GetFileId(create_item_param.Img) + e = c.Service.File.CheckFiles(create_item_param.Imgs) if e.Error() { return e.Response() } uid := GetUidFromCtx(c.Ctx) - item, e := c.Service.Item.CreateItem(uid, create_item_param.Itemname, create_item_param.Desc, create_item_param.Types, create_item_param.Img, create_item_param.Price) + item, e := c.Service.Item.CreateItem(uid, create_item_param.Itemname, create_item_param.Desc, create_item_param.Types, create_item_param.Imgs, create_item_param.Price) if e.Error() { return e.Response() } diff --git a/ecode/auth_ecode.go b/ecode/auth_ecode.go index 4d0a05e..230582c 100644 --- a/ecode/auth_ecode.go +++ b/ecode/auth_ecode.go @@ -7,6 +7,8 @@ const ( USERNAME_EXIST EMAIL_EXIST + USER_SIGNUP_ERR + OAUTH_ERROR_KEY OAUTH_ERROR_TOKEN ) @@ -18,6 +20,8 @@ var AUTH_MSG = map[int]string{ USERNAME_EXIST: "用户名有点火啊", EMAIL_EXIST: "邮箱已经被注册", + USER_SIGNUP_ERR: "注册失败", + OAUTH_ERROR_KEY: "错误的 OAuth Key", OAUTH_ERROR_TOKEN: "错误的 OAuth Token", } diff --git a/ecode/file_ecode.go b/ecode/file_ecode.go index c57aa77..b1493d0 100644 --- a/ecode/file_ecode.go +++ b/ecode/file_ecode.go @@ -4,12 +4,14 @@ const ( FILE_OK = iota FILE_TYPE_ERR FILE_NOT_FOUND + FILE_CREATE_ERR ) var FILE_MSG = map[int]string{ - FILE_OK: "文件操作成功", - FILE_TYPE_ERR: "文件类型错误!", - FILE_NOT_FOUND: "文件不存在", + FILE_OK: "文件操作成功", + FILE_TYPE_ERR: "文件类型错误!", + FILE_NOT_FOUND: "文件不存在", + FILE_CREATE_ERR: "文件创建失败", } func File(id int) *Ecode { diff --git a/ecode/item_ecode.go b/ecode/item_ecode.go index aa1a632..9e07c3d 100644 --- a/ecode/item_ecode.go +++ b/ecode/item_ecode.go @@ -5,6 +5,7 @@ const ( ITEM_NOT_FOUND ITEM_PERMISSION_DENIED ITEM_TYPE_EXIST + ITEM_CREATE_ERR ) var ITEM_MSG = map[int]string{ @@ -12,6 +13,7 @@ var ITEM_MSG = map[int]string{ ITEM_NOT_FOUND: "商品不存在", ITEM_PERMISSION_DENIED: "操作权限不足", ITEM_TYPE_EXIST: "商品类型已存在", + ITEM_CREATE_ERR: "商品创建失败", } func Item(id int) *Ecode { diff --git a/model/item_model.go b/model/item_model.go index ba0b54e..f86dee8 100644 --- a/model/item_model.go +++ b/model/item_model.go @@ -20,7 +20,8 @@ type ItemModel struct { TypeString string Price int Desc string - Img string + Imgs []string `gorm:"-"` + ImgString string State string Uid uint } @@ -32,11 +33,25 @@ func (i *ItemModel) BeforeCreate(tx *gorm.DB) (err error) { types_string, _ := json.Marshal(i.Types) types := string(types_string) i.TypeString = strings.Replace(types, ",", "][", -1) + + imgs_string, _ := json.Marshal(i.Imgs) + imgs := string(imgs_string) + i.ImgString = strings.Replace(imgs, ",", "][", -1) return } func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) { type_string := strings.Replace(i.TypeString, "][", ",", -1) err = json.Unmarshal([]byte(type_string), &i.Types) + if err != nil { + return + } + + img_string := strings.Replace(i.ImgString, "][", ",", -1) + err = json.Unmarshal([]byte(img_string), &i.Imgs) + if err != nil { + return + } + return } diff --git a/service/captcha_service.go b/service/captcha_service.go index 0069e81..ed928e2 100644 --- a/service/captcha_service.go +++ b/service/captcha_service.go @@ -38,6 +38,9 @@ func (s *CaptchaService) SaveToDB(key, code string) { captcha.Code = code captcha.Key = key model.DB.Create(captcha) + if captcha.ID == 0 { + return + } } func (s *CaptchaService) Validate(key, code string) *ecode.Ecode { diff --git a/service/file_service.go b/service/file_service.go index 65d14b1..de9e577 100644 --- a/service/file_service.go +++ b/service/file_service.go @@ -20,6 +20,9 @@ func (s *FileService) UploadItemImg(header *multipart.FileHeader) (*model.FileMo file.Url = "/upload/item/img/" + file.Key + ".jpg" file.SystemPath = "./public" + file.Url model.DB.Create(file) + if file.ID == 0 { + return nil, ecode.File(ecode.FILE_CREATE_ERR) + } return file, ecode.OK() } @@ -45,3 +48,13 @@ func (s *FileService) GetFileId(key string) (uint, *ecode.Ecode) { } return file.ID, ecode.OK() } + +// 批量验证文件是否存在 +func (s *FileService) CheckFiles(keys []string) *ecode.Ecode { + files := make([]model.FileModel, 0) + model.DB.Where("`key` in (?)", keys).Find(&files) + if len(files) != len(keys) { + return ecode.File(ecode.FILE_NOT_FOUND) + } + return ecode.OK() +} diff --git a/service/item_service.go b/service/item_service.go index a6151a7..325609c 100644 --- a/service/item_service.go +++ b/service/item_service.go @@ -8,17 +8,20 @@ import ( type ItemService struct{} -func (s *ItemService) CreateItem(uid uint, itemname string, desc string, types []int, img string, price int) (item *model.ItemModel, e *ecode.Ecode) { +func (s *ItemService) CreateItem(uid uint, itemname string, desc string, types []int, imgs []string, price int) (item *model.ItemModel, e *ecode.Ecode) { item = new(model.ItemModel) e = ecode.OK() item.Itemname = itemname item.Desc = desc item.Types = types - item.Img = img + item.Imgs = imgs item.Price = price item.State = model.ItemStateOnSale item.Uid = uid model.DB.Create(item) + if item.ID == 0 { + return nil, ecode.Item(ecode.ITEM_CREATE_ERR) + } return } diff --git a/service/user_service.go b/service/user_service.go index 7ee3f30..89cd7f9 100644 --- a/service/user_service.go +++ b/service/user_service.go @@ -37,6 +37,10 @@ func (u *UserService) AuthSignup(username string, password string) (user *model. user.Passhash = util.MD5(user.Secret + password + user.Secret) user.State = model.UserStateComfirmed model.DB.Create(user) + if user.ID == 0 { + // 注册失败 + return user, ecode.Auth(ecode.USER_SIGNUP_ERR) + } return } diff --git a/validate/item_validate.go b/validate/item_validate.go index 8147f6b..7e0a0ae 100644 --- a/validate/item_validate.go +++ b/validate/item_validate.go @@ -6,9 +6,9 @@ type CreateItemParam struct { Itemname string `json:"itemname" validate:"required,max=12" example:"女大自用iPhone18"` // @description 商品价格,单位为分 Price int `json:"price" validate:"required" example:"1000"` - //@description 图片ID,请用 /file/upload/item-img 接口上传图片后获取的图片唯一标识 - Img string `json:"img" validate:"required,len=12" example:"123456789012" description:""` - Desc string `json:"desc" validate:"required" example:"这是一个女大自用的iPhone18"` + //@description 图片ID,请用 /file/upload/item-img 接口上传图片后获取的图片唯一标识,多张图片请数组上传 + Imgs []string `json:"img" validate:"required" example:"123456789012,abcdefghijkl" description:""` + Desc string `json:"desc" validate:"required" example:"这是一个女大自用的iPhone18"` // @description 商品类型,可以有多个类型,请用数组传过来,类型id请使用 /item/types 接口获取 Types []int `json:"types" validate:"required" example:"1,2"` }