修改支持添加多张商品图片
This commit is contained in:
parent
c16de4bbaf
commit
3c34997623
@ -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() {
|
if e.Error() {
|
||||||
return e.Response()
|
return e.Response()
|
||||||
}
|
}
|
||||||
|
|
||||||
uid := GetUidFromCtx(c.Ctx)
|
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() {
|
if e.Error() {
|
||||||
return e.Response()
|
return e.Response()
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,8 @@ const (
|
|||||||
USERNAME_EXIST
|
USERNAME_EXIST
|
||||||
EMAIL_EXIST
|
EMAIL_EXIST
|
||||||
|
|
||||||
|
USER_SIGNUP_ERR
|
||||||
|
|
||||||
OAUTH_ERROR_KEY
|
OAUTH_ERROR_KEY
|
||||||
OAUTH_ERROR_TOKEN
|
OAUTH_ERROR_TOKEN
|
||||||
)
|
)
|
||||||
@ -18,6 +20,8 @@ var AUTH_MSG = map[int]string{
|
|||||||
USERNAME_EXIST: "用户名有点火啊",
|
USERNAME_EXIST: "用户名有点火啊",
|
||||||
EMAIL_EXIST: "邮箱已经被注册",
|
EMAIL_EXIST: "邮箱已经被注册",
|
||||||
|
|
||||||
|
USER_SIGNUP_ERR: "注册失败",
|
||||||
|
|
||||||
OAUTH_ERROR_KEY: "错误的 OAuth Key",
|
OAUTH_ERROR_KEY: "错误的 OAuth Key",
|
||||||
OAUTH_ERROR_TOKEN: "错误的 OAuth Token",
|
OAUTH_ERROR_TOKEN: "错误的 OAuth Token",
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,14 @@ const (
|
|||||||
FILE_OK = iota
|
FILE_OK = iota
|
||||||
FILE_TYPE_ERR
|
FILE_TYPE_ERR
|
||||||
FILE_NOT_FOUND
|
FILE_NOT_FOUND
|
||||||
|
FILE_CREATE_ERR
|
||||||
)
|
)
|
||||||
|
|
||||||
var FILE_MSG = map[int]string{
|
var FILE_MSG = map[int]string{
|
||||||
FILE_OK: "文件操作成功",
|
FILE_OK: "文件操作成功",
|
||||||
FILE_TYPE_ERR: "文件类型错误!",
|
FILE_TYPE_ERR: "文件类型错误!",
|
||||||
FILE_NOT_FOUND: "文件不存在",
|
FILE_NOT_FOUND: "文件不存在",
|
||||||
|
FILE_CREATE_ERR: "文件创建失败",
|
||||||
}
|
}
|
||||||
|
|
||||||
func File(id int) *Ecode {
|
func File(id int) *Ecode {
|
||||||
|
@ -5,6 +5,7 @@ const (
|
|||||||
ITEM_NOT_FOUND
|
ITEM_NOT_FOUND
|
||||||
ITEM_PERMISSION_DENIED
|
ITEM_PERMISSION_DENIED
|
||||||
ITEM_TYPE_EXIST
|
ITEM_TYPE_EXIST
|
||||||
|
ITEM_CREATE_ERR
|
||||||
)
|
)
|
||||||
|
|
||||||
var ITEM_MSG = map[int]string{
|
var ITEM_MSG = map[int]string{
|
||||||
@ -12,6 +13,7 @@ var ITEM_MSG = map[int]string{
|
|||||||
ITEM_NOT_FOUND: "商品不存在",
|
ITEM_NOT_FOUND: "商品不存在",
|
||||||
ITEM_PERMISSION_DENIED: "操作权限不足",
|
ITEM_PERMISSION_DENIED: "操作权限不足",
|
||||||
ITEM_TYPE_EXIST: "商品类型已存在",
|
ITEM_TYPE_EXIST: "商品类型已存在",
|
||||||
|
ITEM_CREATE_ERR: "商品创建失败",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Item(id int) *Ecode {
|
func Item(id int) *Ecode {
|
||||||
|
@ -20,7 +20,8 @@ type ItemModel struct {
|
|||||||
TypeString string
|
TypeString string
|
||||||
Price int
|
Price int
|
||||||
Desc string
|
Desc string
|
||||||
Img string
|
Imgs []string `gorm:"-"`
|
||||||
|
ImgString string
|
||||||
State string
|
State string
|
||||||
Uid uint
|
Uid uint
|
||||||
}
|
}
|
||||||
@ -32,11 +33,25 @@ func (i *ItemModel) BeforeCreate(tx *gorm.DB) (err error) {
|
|||||||
types_string, _ := json.Marshal(i.Types)
|
types_string, _ := json.Marshal(i.Types)
|
||||||
types := string(types_string)
|
types := string(types_string)
|
||||||
i.TypeString = strings.Replace(types, ",", "][", -1)
|
i.TypeString = strings.Replace(types, ",", "][", -1)
|
||||||
|
|
||||||
|
imgs_string, _ := json.Marshal(i.Imgs)
|
||||||
|
imgs := string(imgs_string)
|
||||||
|
i.ImgString = strings.Replace(imgs, ",", "][", -1)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) {
|
func (i *ItemModel) AfterFind(tx *gorm.DB) (err error) {
|
||||||
type_string := strings.Replace(i.TypeString, "][", ",", -1)
|
type_string := strings.Replace(i.TypeString, "][", ",", -1)
|
||||||
err = json.Unmarshal([]byte(type_string), &i.Types)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,9 @@ func (s *CaptchaService) SaveToDB(key, code string) {
|
|||||||
captcha.Code = code
|
captcha.Code = code
|
||||||
captcha.Key = key
|
captcha.Key = key
|
||||||
model.DB.Create(captcha)
|
model.DB.Create(captcha)
|
||||||
|
if captcha.ID == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *CaptchaService) Validate(key, code string) *ecode.Ecode {
|
func (s *CaptchaService) Validate(key, code string) *ecode.Ecode {
|
||||||
|
@ -20,6 +20,9 @@ func (s *FileService) UploadItemImg(header *multipart.FileHeader) (*model.FileMo
|
|||||||
file.Url = "/upload/item/img/" + file.Key + ".jpg"
|
file.Url = "/upload/item/img/" + file.Key + ".jpg"
|
||||||
file.SystemPath = "./public" + file.Url
|
file.SystemPath = "./public" + file.Url
|
||||||
model.DB.Create(file)
|
model.DB.Create(file)
|
||||||
|
if file.ID == 0 {
|
||||||
|
return nil, ecode.File(ecode.FILE_CREATE_ERR)
|
||||||
|
}
|
||||||
|
|
||||||
return file, ecode.OK()
|
return file, ecode.OK()
|
||||||
}
|
}
|
||||||
@ -45,3 +48,13 @@ func (s *FileService) GetFileId(key string) (uint, *ecode.Ecode) {
|
|||||||
}
|
}
|
||||||
return file.ID, ecode.OK()
|
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()
|
||||||
|
}
|
||||||
|
@ -8,17 +8,20 @@ import (
|
|||||||
|
|
||||||
type ItemService struct{}
|
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)
|
item = new(model.ItemModel)
|
||||||
e = ecode.OK()
|
e = ecode.OK()
|
||||||
item.Itemname = itemname
|
item.Itemname = itemname
|
||||||
item.Desc = desc
|
item.Desc = desc
|
||||||
item.Types = types
|
item.Types = types
|
||||||
item.Img = img
|
item.Imgs = imgs
|
||||||
item.Price = price
|
item.Price = price
|
||||||
item.State = model.ItemStateOnSale
|
item.State = model.ItemStateOnSale
|
||||||
item.Uid = uid
|
item.Uid = uid
|
||||||
model.DB.Create(item)
|
model.DB.Create(item)
|
||||||
|
if item.ID == 0 {
|
||||||
|
return nil, ecode.Item(ecode.ITEM_CREATE_ERR)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +37,10 @@ func (u *UserService) AuthSignup(username string, password string) (user *model.
|
|||||||
user.Passhash = util.MD5(user.Secret + password + user.Secret)
|
user.Passhash = util.MD5(user.Secret + password + user.Secret)
|
||||||
user.State = model.UserStateComfirmed
|
user.State = model.UserStateComfirmed
|
||||||
model.DB.Create(user)
|
model.DB.Create(user)
|
||||||
|
if user.ID == 0 {
|
||||||
|
// 注册失败
|
||||||
|
return user, ecode.Auth(ecode.USER_SIGNUP_ERR)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,8 +6,8 @@ type CreateItemParam struct {
|
|||||||
Itemname string `json:"itemname" validate:"required,max=12" example:"女大自用iPhone18"`
|
Itemname string `json:"itemname" validate:"required,max=12" example:"女大自用iPhone18"`
|
||||||
// @description 商品价格,单位为分
|
// @description 商品价格,单位为分
|
||||||
Price int `json:"price" validate:"required" example:"1000"`
|
Price int `json:"price" validate:"required" example:"1000"`
|
||||||
//@description 图片ID,请用 /file/upload/item-img 接口上传图片后获取的图片唯一标识
|
//@description 图片ID,请用 /file/upload/item-img 接口上传图片后获取的图片唯一标识,多张图片请数组上传
|
||||||
Img string `json:"img" validate:"required,len=12" example:"123456789012" description:""`
|
Imgs []string `json:"img" validate:"required" example:"123456789012,abcdefghijkl" description:""`
|
||||||
Desc string `json:"desc" validate:"required" example:"这是一个女大自用的iPhone18"`
|
Desc string `json:"desc" validate:"required" example:"这是一个女大自用的iPhone18"`
|
||||||
// @description 商品类型,可以有多个类型,请用数组传过来,类型id请使用 /item/types 接口获取
|
// @description 商品类型,可以有多个类型,请用数组传过来,类型id请使用 /item/types 接口获取
|
||||||
Types []int `json:"types" validate:"required" example:"1,2"`
|
Types []int `json:"types" validate:"required" example:"1,2"`
|
||||||
|
Loading…
Reference in New Issue
Block a user