Skip to content

Commit

Permalink
上传商品封面图及图片集后台接口完成
Browse files Browse the repository at this point in the history
  • Loading branch information
shen100 committed May 23, 2017
1 parent 35da5cf commit 24b89ab
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 19 deletions.
3 changes: 2 additions & 1 deletion configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"MaxNameLen" : 100, /*最大的名称长度*/
"MaxRemarkLen" : 500, /*最大的备注长度*/
"MaxContentLen" : 10000, /*最大的内容长度*/
"MaxProductCateCount" : 6 /*产品最多的分类个数*/
"MaxProductCateCount" : 6, /*商品最多的分类个数*/
"MaxProductImgCount" : 6 /*商品最多的图片集个数*/
},
"software": {
"name" : "wemall微商城", /*软件名称*/
Expand Down
3 changes: 2 additions & 1 deletion configuration.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"MaxNameLen" : 100, /*最大的名称长度*/
"MaxRemarkLen" : 500, /*最大的备注长度*/
"MaxContentLen" : 10000, /*最大的内容长度*/
"MaxProductCateCount" : 6 /*产品最多的分类个数*/
"MaxProductCateCount" : 6, /*商品最多的分类个数*/
"MaxProductImgCount" : 6 /*商品最多的图片集个数*/
},
"software": {
"name" : "wemall微商城", /*软件名称*/
Expand Down
1 change: 1 addition & 0 deletions go/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type serverConfig struct {
MaxRemarkLen int
MaxContentLen int
MaxProductCateCount int
MaxProductImgCount int
}

// ServerConfig 服务器相关配置
Expand Down
18 changes: 17 additions & 1 deletion go/controller/product/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
"time"
"unicode/utf8"
"encoding/json"
"github.com/jinzhu/gorm"
"gopkg.in/kataras/iris.v6"
"wemall/go/config"
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions go/model/product.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
30 changes: 22 additions & 8 deletions nodejs/static/javascripts/admin/containers/product/EditProduct.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Icon,
Input,
InputNumber,
message,
Modal,
Select,
TreeSelect,
Expand Down Expand Up @@ -50,6 +51,7 @@ class EditProduct extends Component {
price : 0,
remark : '',
status : '3', //等待上架
imageID : '',
imageData : '',
imageURL : '',
previewVisible : false,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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() {
Expand All @@ -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,
Expand Down Expand Up @@ -277,7 +290,7 @@ class EditProduct extends Component {
<div>
<Row gutter={24}>
<Col span={24}>
<div id="productBox" className="product-box">
<div id="productBox">
<div className="product-title">{editLabel}商品</div>
{
isLoading ? null :
Expand Down Expand Up @@ -314,6 +327,7 @@ class EditProduct extends Component {
listType="picture-card"
fileList={imageList}
onPreview={this.onPreview}
beforeUpload={this.onBeforeUpload}
onChange={this.onImageListChange}>
{ imageList.length >= 3 ? null : uploadButton }
</Upload>
Expand Down
4 changes: 0 additions & 4 deletions nodejs/static/styles/admin/product/editProduct.css
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
.product-box {

}

.product-title {
font-size: 20px;
margin-bottom: 6px;
Expand Down

0 comments on commit 24b89ab

Please sign in to comment.