diff --git a/configuration.json b/configuration.json index cbf7eb87..955a09ff 100644 --- a/configuration.json +++ b/configuration.json @@ -39,7 +39,8 @@ "MaxNameLen" : 100, /*最大的名称长度*/ "MaxRemarkLen" : 500, /*最大的备注长度*/ "MaxContentLen" : 10000, /*最大的内容长度*/ - "MaxProductCateCount" : 6 /*产品最多的分类个数*/ + "MaxProductCateCount" : 6, /*商品最多的分类个数*/ + "MaxProductImgCount" : 6 /*商品最多的图片集个数*/ }, "software": { "name" : "wemall微商城", /*软件名称*/ diff --git a/configuration.prod.json b/configuration.prod.json index 0979cff8..f2db0403 100644 --- a/configuration.prod.json +++ b/configuration.prod.json @@ -39,7 +39,8 @@ "MaxNameLen" : 100, /*最大的名称长度*/ "MaxRemarkLen" : 500, /*最大的备注长度*/ "MaxContentLen" : 10000, /*最大的内容长度*/ - "MaxProductCateCount" : 6 /*产品最多的分类个数*/ + "MaxProductCateCount" : 6, /*商品最多的分类个数*/ + "MaxProductImgCount" : 6 /*商品最多的图片集个数*/ }, "software": { "name" : "wemall微商城", /*软件名称*/ diff --git a/go/config/config.go b/go/config/config.go index 0e1cb354..0530de47 100644 --- a/go/config/config.go +++ b/go/config/config.go @@ -66,6 +66,7 @@ type serverConfig struct { MaxRemarkLen int MaxContentLen int MaxProductCateCount int + MaxProductImgCount int } // ServerConfig 服务器相关配置 diff --git a/go/controller/product/product.go b/go/controller/product/product.go index 32b14ce6..7c69c6bd 100644 --- a/go/controller/product/product.go +++ b/go/controller/product/product.go @@ -6,6 +6,7 @@ import ( "strings" "time" "unicode/utf8" + "encoding/json" "github.com/jinzhu/gorm" "gopkg.in/kataras/iris.v6" "wemall/go/config" @@ -143,13 +144,16 @@ func save(ctx *iris.Context, isEdit bool) { } else if isEdit && product.Status != model.ProductUpShelf && product.Status != model.ProductDownShelf && product.Status != model.ProductPending { isError = true msg = "status无效" + } else if product.ImageID <= 0 { + isError = true + msg = "封面图片不能为空" } else if product.Remark != "" && utf8.RuneCountInString(product.Remark) > config.ServerConfig.MaxRemarkLen { isError = true msg = "备注不能超过" + strconv.Itoa(config.ServerConfig.MaxRemarkLen) + "个字符" } else if product.Detail == "" || utf8.RuneCountInString(product.Detail) <= 0 { isError = true msg = "商品详情不能为空" - } else if utf8.RuneCountInString(product.Detail) > config.ServerConfig.MaxContentLen { + } else if utf8.RuneCountInString(product.Detail) > config.ServerConfig.MaxContentLen { isError = true msg = "商品详情不能超过" + strconv.Itoa(config.ServerConfig.MaxContentLen) + "个字符" } else if product.Categories == nil || len(product.Categories) <= 0 { @@ -164,6 +168,18 @@ func save(ctx *iris.Context, isEdit bool) { } else if product.OriginalPrice < 0 { isError = true msg = "无效的商品原价" + } else { + var images []uint + if err := json.Unmarshal([]byte(product.ImageIDs), &images); err != nil { + isError = true + msg = "商品图片集无效" + } else if images == nil || len(images) <= 0 { + isError = true + msg = "商品图片集不能为空" + } else if len(images) > config.ServerConfig.MaxProductImgCount { + isError = true + msg = "商品图片集个数不能超过" + strconv.Itoa(config.ServerConfig.MaxProductImgCount) + "个" + } } if isError { diff --git a/go/model/product.go b/go/model/product.go index ad32ec61..ccf6abe5 100644 --- a/go/model/product.go +++ b/go/model/product.go @@ -15,8 +15,10 @@ type Product struct { Price float64 `json:"price"` OriginalPrice float64 `json:"originalPrice"` Status int `json:"status"` - Image Image `gorm:"ForeignKey:ImageID"` - ImageID string `json:"imageID"` + Image Image `json:"image"` + ImageID uint `json:"imageID"` + ImageIDs string `json:"imageIDs"` + Images []Image `json:"images"` Remark string `json:"remark"` Detail string `json:"detail"` Categories []Category `gorm:"many2many:product_category;ForeignKey:ID;AssociationForeignKey:ID" json:"categories"` diff --git a/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js b/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js index 8a77ac5c..9cbdd715 100644 --- a/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js +++ b/nodejs/static/javascripts/admin/actions/product/requestSaveProduct.js @@ -20,12 +20,13 @@ export default function(product) { id: parseInt(idArr[idArr.length - 1]) }); } - var reqData ={ + var reqData = { id : product.id, name : product.name, categories : categories, status : parseInt(product.status), - imageURL : product.imageURL, + imageID : product.imageID, + imageIDs : JSON.stringify(product.imageIDs), originalPrice : product.originalPrice, price : product.price, remark : product.remark, diff --git a/nodejs/static/javascripts/admin/containers/product/EditProduct.js b/nodejs/static/javascripts/admin/containers/product/EditProduct.js index 890a1cd3..fcccd01b 100644 --- a/nodejs/static/javascripts/admin/containers/product/EditProduct.js +++ b/nodejs/static/javascripts/admin/containers/product/EditProduct.js @@ -9,6 +9,7 @@ import { Icon, Input, InputNumber, + message, Modal, Select, TreeSelect, @@ -50,6 +51,7 @@ class EditProduct extends Component { price : 0, remark : '', status : '3', //等待上架 + imageID : '', imageData : '', imageURL : '', previewVisible : false, @@ -124,6 +126,7 @@ class EditProduct extends Component { price : product.price, remark : product.remark, status : product.status + '', + imageID : product.imageID, imageData : product.imageURL, imageURL : product.imageURL, isLoading : false @@ -154,21 +157,22 @@ class EditProduct extends Component { this.setState({ status: status }); } onBeforeUpload(file) { - var isJPG = file.type === 'image/jpeg'; - if (!isJPG) { + var isImage = file.type === 'image/jpeg' || file.type === 'image/png'; + if (!isImage) { message.error('只支持jpg或png格式的图片'); } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { message.error('图片大小要小于2M'); } - return isJPG && isLt2M; + return isImage && isLt2M; } onImageChange(info) { var self = this; if (info.file.status === 'done') { self.setState({ - imageURL: info.file.response.data.url + imageURL : info.file.response.data.url, + imageID : info.file.response.data.id }); (function(originFileObj, callback) { var reader = new FileReader(); @@ -191,8 +195,16 @@ class EditProduct extends Component { }); } onImageListChange(data) { - this.setState({ - imageList: data.fileList + var fileList = data.fileList || []; + var imageIDs = []; + for (var i = 0; i < fileList.length; i++) { + if (fileList[i].response) { + imageIDs.push(fileList[i].response.data.id); + } + } + this.setState({ + imageIDs : imageIDs, + imageList : data.fileList }); } onSubmit() { @@ -205,7 +217,8 @@ class EditProduct extends Component { name : this.state.name, categories : this.state.categories, status : this.state.status, - imageURL : this.state.imageURL, + imageID : this.state.imageID, + imageIDs : this.state.imageIDs, originalPrice : this.state.originalPrice, price : this.state.price, remark : this.state.remark, @@ -277,7 +290,7 @@ class EditProduct extends Component {