Parcourir la source

kuaishouPhotoStruct

Xingyu Xian il y a 4 mois
Parent
commit
11256544af

+ 1 - 0
handler/find_kuaishou_product.go

@@ -47,6 +47,7 @@ func (h *FindKuaishouProductHandler) run() {
 	if err != nil {
 		logrus.WithContext(h.ctx).Errorf("[FindKuaishouProductHandler] error FindKuaishouProductHandler, err:%+v", err)
 		util.HandlerPackErrorResp(h.resp, consts.ErrorInternal, consts.DefaultToast)
+		h.resp.Message = err.Error()
 		return
 	}
 	h.resp.Status = 20000

+ 15 - 14
model/http_model/find_kuaishou_product.go

@@ -16,20 +16,21 @@ type KuaishouDetailPhoto struct {
 }
 
 type FindKuaishouProductData struct {
-	ProductName                 string   `json:"product_name"`                    // 商品名称
-	ProductPrice                float64  `json:"product_price"`                   // 商品价值
-	ProductDetail               string   `json:"product_detail"`                  // 商品描述
-	KuaishouMainPhoto           string   `json:"kuaishou_main_photo"`             // 商品主图
-	KuaishouCarousePhotoList    []string `json:"kuaishou_carouse_photo_list"`     // 商品轮播图列表
-	KuaishouDetailPhotoList     []string `json:"kuaishou_detail_photo_list"`      // 商品详情图列表
-	ProductUrl                  string   `json:"product_url"`                     // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
-	EnterpriseID                string   `json:"enterprise_id"`                   // 所属企业id
-	BrandName                   string   `json:"brand_name"`                      // 品牌名称
-	PublicCommission            float64  `json:"public_commission"`               // 公开佣金
-	ExclusiveCommission         float64  `json:"exclusive_commission"`            // 专属佣金
-	CommissionPrice             float64  `json:"commission_price"`                // 佣金金额
-	KuaishouProductId           int64    `json:"kuaishou_product_id"`             // 快手商品ID
-	MerchantSoldCountThirtyDays int64    `json:"merchant_sold_count_thirty_days"` // 商品30天销量
+	ProductName                 string          `json:"product_name"`                    // 商品名称
+	ProductPrice                float64         `json:"product_price"`                   // 商品价值
+	ProductDetail               string          `json:"product_detail"`                  // 商品描述
+	KuaishouMainPhoto           string          `json:"kuaishou_main_photo"`             // 商品主图
+	KuaishouCarousePhotoList    []string        `json:"kuaishou_carouse_photo_list"`     // 商品轮播图列表
+	KuaishouDetailPhotoList     []string        `json:"kuaishou_detail_photo_list"`      // 商品详情图列表
+	ProductUrl                  string          `json:"product_url"`                     // 商品链接,可为电商网址、公司官网、大众点评的店铺地址等可以说明商品信息或者品牌信息的线上地址;
+	EnterpriseID                string          `json:"enterprise_id"`                   // 所属企业id
+	BrandName                   string          `json:"brand_name"`                      // 品牌名称
+	PublicCommission            float64         `json:"public_commission"`               // 公开佣金
+	ExclusiveCommission         float64         `json:"exclusive_commission"`            // 专属佣金
+	CommissionPrice             float64         `json:"commission_price"`                // 佣金金额
+	KuaishouProductId           int64           `json:"kuaishou_product_id"`             // 快手商品ID
+	MerchantSoldCountThirtyDays int64           `json:"merchant_sold_count_thirty_days"` // 商品30天销量
+	ProductPhotos               []*ProductPhoto `json:"product_photos"`                  // 结构化的图片信息
 }
 
 func NewFindKuaishouProductRequest() *FindKuaishouProductRequest {

+ 1 - 1
model/http_model/product_find.go

@@ -7,7 +7,7 @@ type FindProductRequest struct {
 type ProductPhoto struct {
 	PhotoUrl string `json:"photo_url"` // 图片或视频url
 	PhotoUid string `json:"photo_uid"`
-	Symbol   int64  `json:"symbol"` // 图片为主图或详情图标志位,1为主图,2为详情图,3为视频
+	Symbol   int64  `json:"symbol"` // 图片为主图或详情图标志位
 }
 
 type FindProductData struct {

+ 50 - 1
service/product.go

@@ -4,7 +4,9 @@ import (
 	"context"
 	"fmt"
 	"github.com/lin-jim-leon/kuaishou"
+	"math/rand"
 	"strconv"
+	"time"
 	"youngee_b_api/consts"
 	"youngee_b_api/db"
 	"youngee_b_api/model/gorm_model"
@@ -191,7 +193,7 @@ func (*product) QueryKuaishouProduct(ctx context.Context, newKuaishouProduct htt
 	//fmt.Println(newAccessToken.AccessToken)
 
 	// 3. 查找对应的快手商品
-	kuaishouProduct, err := kuaishou.Queryselectiondetail(appKey, signSecret, "ChFvYXV0aC5hY2Nlc3NUb2tlbhJgl2DZPM2ZSVHLUoU-t4425Bznh2b2QjIJkTjxF5tY6XYfjJ9X0bG4sB9W5if90iC22glyHe7ZbOQ22ndVHCKgCgYEc7QXN0w-lHlTCnKLsKmh1u46gZ0ivhHqf0QEQcLrGhLiICkHPKJFr7JGeErh7m5jzagiIKZOZcTU8A6KIgJoqCLbADf7fRlv7aYWcwEdfjGl6T8YKAUwAQ", newKuaishouProduct.ItemList)
+	kuaishouProduct, err := kuaishou.Queryselectiondetail(appKey, signSecret, "ChFvYXV0aC5hY2Nlc3NUb2tlbhJgVUcKpMMPhw6IsRe6j49Z_u5HNB4X4mEcFrPKQ6S4adSCGiJ_fnoNzQOfv5hv2iCI3jEU380e44aG0fH5cqVrghZWQxs-LlatHGWltM2einjuMdOLU-hw_-7gAgg46DHaGhJwAv7lQ4RI-YbDWturzqUxgFYiIH7EzXN1tK4s3OnxyrD227gtYmLoDs0jsnXzYvhocbBeKAUwAQ", newKuaishouProduct.ItemList)
 	if err != nil {
 		fmt.Println("快手错误信息: ", err)
 		return nil, err
@@ -209,9 +211,56 @@ func (*product) QueryKuaishouProduct(ctx context.Context, newKuaishouProduct htt
 		MerchantSoldCountThirtyDays: kuaishouProduct.ItemList[0].MerchantSoldCountThirtyDays,
 		KuaishouProductId:           kuaishouProduct.ItemList[0].ItemID,
 	}
+
+	// 5. 图片数据结构化
+	// 5.1. 主图
+	var currProductMainPhoto *http_model.ProductPhoto
+	rand.Seed(time.Now().UnixNano())
+	currProductMainPhoto = &http_model.ProductPhoto{
+		PhotoUrl: findKuaishouProductData.KuaishouMainPhoto,
+		PhotoUid: generateRandomString(10),
+		Symbol:   1,
+	}
+	findKuaishouProductData.ProductPhotos = append(findKuaishouProductData.ProductPhotos, currProductMainPhoto)
+
+	// 5.2. 轮播图
+	for _, url := range findKuaishouProductData.KuaishouCarousePhotoList {
+		var currProductPhoto *http_model.ProductPhoto
+		rand.Seed(time.Now().UnixNano())
+		currProductPhoto = &http_model.ProductPhoto{
+			PhotoUrl: url,
+			PhotoUid: generateRandomString(10),
+			Symbol:   2,
+		}
+		findKuaishouProductData.ProductPhotos = append(findKuaishouProductData.ProductPhotos, currProductPhoto)
+	}
+
+	// 5.3. 详情图
+	for _, url := range findKuaishouProductData.KuaishouDetailPhotoList {
+		var currProductPhoto *http_model.ProductPhoto
+		rand.Seed(time.Now().UnixNano())
+		currProductPhoto = &http_model.ProductPhoto{
+			PhotoUrl: url,
+			PhotoUid: generateRandomString(10),
+			Symbol:   4,
+		}
+		findKuaishouProductData.ProductPhotos = append(findKuaishouProductData.ProductPhotos, currProductPhoto)
+	}
 	return &findKuaishouProductData, err
 }
 
+const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
+var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
+
+func generateRandomString(length int) string {
+	b := make([]byte, length)
+	for i := range b {
+		b[i] = charset[seededRand.Intn(len(charset))]
+	}
+	return string(b)
+}
+
 /*
 type T struct {
 	CreatedAt           time.Time `json:"CreatedAt"`