浏览代码

修改封面上传逻辑

kobe6258 4 月之前
父节点
当前提交
167028cab5
共有 2 个文件被更改,包括 15 次插入6 次删除
  1. 13 5
      controllers/product.go
  2. 2 1
      models/merchant_product.go

+ 13 - 5
controllers/product.go

@@ -187,10 +187,10 @@ func (this *ProductController) AddProduct() {
 			} else {
 				var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
 				index := rnd.Intn(len(imageList))
-				product.CoverSrc = imageList[index].SrcUrl
+				product.CoverSrc = imageList[index].Id
 			}
 		} else {
-			product.CoverSrc = req.CoverSrc
+			product.CoverUrl = req.CoverSrc
 		}
 
 	}
@@ -495,13 +495,21 @@ func (this *ProductController) ProductList() {
 			PublishedTime: product.CreatedTime.Format(time.DateTime),
 			Price:         fmt.Sprintf("¥%s", product.Price),
 			SaleStatus:    CNSaleStatusMap[product.SaleStatus],
-			CoverSrc:      product.CoverSrc,
+			CoverSrc:      product.CoverUrl,
 			ValidDays:     product.ValidDays,
 			Creator:       product.Creator,
 			IsPermanent:   product.IsPermanent,
 			Description:   product.Description,
 			SourceId:      product.SourceId,
 		}
+		if product.CoverUrl == "" && product.CoverSrc > 0 {
+			image, imageErr := models.GetImageById(product.CoverSrc)
+			if err != nil {
+				utils.FileLog.Warn("获取图片资源失败,err:%s,imageId:%d", imageErr, product.CoverSrc)
+			} else {
+				view.CoverSrc = image.SrcUrl
+			}
+		}
 		if !product.UpdatedTime.IsZero() {
 			view.UpdatedTime = product.UpdatedTime.Format(time.DateTime)
 		}
@@ -602,10 +610,10 @@ func (this *ProductController) EditProduct() {
 			} else {
 				var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))
 				index := rnd.Intn(len(imageList))
-				product.CoverSrc = imageList[index].SrcUrl
+				product.CoverSrc = imageList[index].Id
 			}
 		} else {
-			product.CoverSrc = req.CoverSrc
+			product.CoverUrl = req.CoverSrc
 		}
 
 	}

+ 2 - 1
models/merchant_product.go

@@ -22,7 +22,8 @@ type MerchantProduct struct {
 	Id          int                 `description:"column:id;primary_key;autoIncrement;comment:主键"`
 	SourceId    int                 `description:"column:source_id;type:int(11);comment:单品或者套餐对应的主键"`
 	Title       string              `description:"column:title;type:varchar(255);comment:标题"`
-	CoverSrc    string              `description:"column:cover_src;type:varchar(255);comment:封面图片"`
+	CoverSrc    int                 `description:"column:cover_src;type:int(11);comment:封面图片资源库id"`
+	CoverUrl    string              `description:"column:cover_url;type:varchar(255);comment:封面图片url"`
 	Description string              `description:"column:description;type:varchar(255);comment:描述"`
 	Price       string              `description:"column:price;type:decimal(10,2);comment:价格"`
 	Type        MerchantProductType `description:"column:type;type:enum('report','video','audio','package');not null;comment:类型"`