This commit is contained in:
richboo111
2022-11-14 09:36:25 +08:00
13 changed files with 221 additions and 171 deletions

View File

@@ -219,8 +219,8 @@ func (p *PurchaseHandler) getOrder(vendorOrgCode, vendorOrderID, vendorStoreID s
// GetOrderRider 商家自配送同步配送信息
func (p *PurchaseHandler) GetOrderRider(vendorOrgCode, vendorStoreID string, param map[string]interface{}) (err error) {
appKey := `7153997323561879075` // 暂时定死
appSecret := `3517d3ea-b96b-4379-a7a4-2d9389e8ffeb` //暂时定死
appKey := `7152420904331429407` // 暂时定死
appSecret := `cc7ba367-2394-4cbb-81c6-26f0e929d1c6` //暂时定死
return tiktokShop.NewExpress(appKey, appSecret, "").OrderStatusAndPsInfo(param)
}

View File

@@ -281,17 +281,17 @@ func (p *PurchaseHandler) UpdateStoreSkusStatus(ctx *jxcontext.Context, vendorOr
func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
api := getAPI(vendorOrgCode, storeID, vendorStoreID)
for _, v := range storeSkuList {
// 更新主品
err = api.EditPrice(&sku_editPrice_request.SkuEditPriceParam{
Price: v.VendorPrice,
SkuId: utils.Str2Int64(v.VendorSkuAttrId),
ProductId: utils.Str2Int64(v.VendorMainId),
})
if err != nil {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "更新主品价格异常")...)
} else {
failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "更新主品价格正常")...)
}
// 更新主品 (暂不支持渠道主商品)
//err = api.EditPrice(&sku_editPrice_request.SkuEditPriceParam{
// Price: v.VendorPrice,
// SkuId: utils.Str2Int64(v.VendorSkuAttrId),
// ProductId: utils.Str2Int64(v.VendorMainId),
//})
//if err != nil {
// failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "更新主品价格异常")...)
//} else {
// failedList = append(failedList, putils.GetErrMsg2FailedSingleList(v, err, storeID, model.VendorChineseNames[model.VendorIDDD], "更新主品价格正常")...)
//}
// 更新子品
err = api.EditPrice(&sku_editPrice_request.SkuEditPriceParam{

View File

@@ -125,15 +125,18 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
if isCreate {
syncType = "创建商品"
for _, storeSku := range storeSkuList { // 创建商品
// 如果当前商品,主商品id,子商品id都已经存在了,就不在创建了,主要防止,后期更新时,某些商品为创建成功,批量修改数据库再去批量创建报错(商品已创建)
if storeSku.VendorMainId != "" && storeSku.VendorSkuID != "" && len(storeSku.VendorSkuID) == 19 {
continue
}
param := &product_addV2_request.ProductAddV2Param{
CategoryLeafId: utils.Str2Int64(storeSku.SkuVendorMapCatID),
Name: utils.LimitUTF8StringLen(storeSku.SkuName, mtwmapi.MaxSkuNameCharCount),
PayType: tiktokShop.TiktokPayType1,
ReduceType: tiktokShop.SkuReduceTypePayMakeOrder,
Weight: utils.Int2Float64(storeSku.Weight),
DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay,
PresellType: tiktokShop.SendGoodsTypeNow,
Supply7dayReturn: 2, // 是否支持7天无理由0不支持1支持2支持拆封后不支持
Supply7dayReturn: 0, // 是否支持7天无理由0不支持1支持2支持拆封后不支持
Mobile: storeDetail.Tel1,
Commit: true,
Specs: "重量|" + utils.Float64ToStr(float64(storeSku.SpecQuality)) + storeSku.SpecUnit,
@@ -143,64 +146,49 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
PickupMethod: "0",
OuterProductId: utils.Int2Str(storeSku.SkuID), // 本地skuId为外部商品id
}
// param.AccountTemplateId = ""
// 部分商品没有所属的分类,直接跳过!
if storeSku.SkuVendorMapCatID != "" {
param.CategoryLeafId = utils.Str2Int64(storeSku.SkuVendorMapCatID)
} else if storeSku.VendorVendorCatID != 0 {
param.CategoryLeafId = storeSku.VendorVendorCatID
} else {
continue
}
// 是否支持七天无理由
if api.GetProductUpdateRule(param.CategoryLeafId) {
param.Supply7dayReturn = 1
} else {
param.Supply7dayReturn = 0
}
// 获取上传图,商品轮播图
img, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5)
img, detailImg, err := GetTiktokImgList(api, utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.DescImg, storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5)
if err != nil {
return nil, err
}
param.Pic = img
// 商品详情图
if storeSku.DescImg == "" {
param.Description = strings.Split(img, ",")[0]
} else {
img2, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.DescImg)
if err != nil {
return nil, err
}
param.Description = img2
}
param.Description = detailImg
// weight_unit 目前抖音只支持g和kg两种
switch storeSku.Unit {
case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
case "kg", "l", "L", "KG":
param.WeightUnit = tiktokShop.WeightUint_G
}
//switch storeSku.Unit {
//case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
//case "kg", "l", "L", "KG":
// param.WeightUnit = tiktokShop.WeightUint_G
//}
// spec_prices
param.SpecPrices = GetSpecPrices(param.Specs, vendorStoreID, 0, storeSku)
// 获取商品的属性
if storeSku.TiktokAttribute == "" || storeSku.TiktokAttribute == "{}" {
categoryList, err := api.GetCatePropertyV2(param.CategoryLeafId)
if storeSku.TiktokAttribute != "" && storeSku.TiktokAttribute != "{}" {
param.ProductFormatNew = storeSku.TiktokAttribute
} else if storeSku.VendorSkuAttrId != "" && storeSku.VendorSkuAttrId != "{}" {
param.ProductFormatNew = storeSku.TiktokAttribute
} else {
param.ProductFormatNew, err = MakeProductFormatNew(api, int64(storeSku.NameID), param.CategoryLeafId)
if err != nil {
return nil, err
}
categoryMap := make(map[string][]map[string]interface{})
for _, v := range categoryList.Data.Data {
if v.Required == model.YES {
options := make([]map[string]interface{}, 0)
if v.PropertyName == "品牌" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 789194134, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if v.PropertyName == "产地" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 13850, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if len(options) == 0 {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 0, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": v.Options[0].Value, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
}
}
continue
}
param.ProductFormatNew = utils.Format4Output(categoryMap, false)
dao.UpdateSkuNameTiktokAttr(dao.GetDB(), int64(storeSku.NameID), param.ProductFormatNew)
} else {
param.ProductFormatNew = storeSku.TiktokAttribute
}
// 获取品牌
@@ -272,7 +260,9 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
tiktokResultChildren, err := api.CreateStoreCommodity(param)
if err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType)
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
if storeSku.VendorSkuID == "" {
storeSku.VendorSkuID = utils.Int2Str(storeSku.SkuID)
}
continue
}
var attrId2 []string
@@ -286,79 +276,55 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
} else {
syncType = "更新商品"
for _, storeSku := range storeSkuList {
if storeSku.StoreSkuStatus != 1 { // 未可售的商品不参与修改
continue
}
// 更新商品(目前只更新子商品,主商品暂不支持)
param := &product_editV2_request.ProductEditV2Param{
CategoryLeafId: utils.Str2Int64(storeSku.SkuVendorMapCatID),
Name: storeSku.Name,
PayType: tiktokShop.TiktokPayType1,
ReduceType: tiktokShop.SkuReduceTypePayMakeOrder,
Weight: utils.Int2Float64(storeSku.Weight),
DeliveryDelayDay: tiktokShop.DeliveryDelayDayToDay,
PresellType: tiktokShop.SendGoodsTypeNow,
Supply7dayReturn: 2,
Supply7dayReturn: 0,
Mobile: storeDetail.Tel1,
Commit: true,
Specs: "重量|" + fmt.Sprintf("%f", storeSku.SpecQuality) + storeSku.SpecUnit,
Specs: "重量|" + utils.Float64ToStr(float64(storeSku.SpecQuality)) + storeSku.SpecUnit,
NeedRechargeMode: false,
SellChannel: []int64{0},
StartSaleType: 0,
PickupMethod: "0",
}
// param.AccountTemplateId = ""
if storeSku.SkuVendorMapCatID != "" {
param.CategoryLeafId = utils.Str2Int64(storeSku.SkuVendorMapCatID)
} else {
param.CategoryLeafId = storeSku.VendorVendorCatID
}
// 获取上传图,商品轮播图
img, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5)
img, detailImg, err := GetTiktokImgList(api, utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.DescImg, storeSku.ImgOrigin, storeSku.Img2, storeSku.Img3, storeSku.Img4, storeSku.Img5, storeSku.DescImg)
if err != nil {
return nil, err
}
param.Pic = img
// 商品详情图
if storeSku.DescImg == "" {
param.Description = strings.Split(img, ",")[0]
} else {
img2, err := GetTiktokImgList(utils.Int2Str(storeSku.StoreID), storeDetail.VendorOrgCode, storeSku.DescImg)
if err != nil {
return nil, err
}
param.Description = img2
}
param.Description = detailImg
// weight_unit 目前抖音只支持g和kg两种
switch storeSku.Unit {
case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
case "kg", "l", "L", "KG":
param.WeightUnit = tiktokShop.WeightUint_G
}
//switch storeSku.Unit {
//case "g", "ml", "G", "ML":
param.WeightUnit = tiktokShop.WeightUint_G
//case "kg", "l", "L", "KG":
// param.WeightUnit = tiktokShop.WeightUint_G
//}
param.SpecPrices = GetSpecPrices(param.Specs, vendorStoreID, 0, storeSku)
//param.SpecPrices = GetSpecPrices(param.Specs, vendorStoreID, 0, storeSku)
// 获取商品的属性
if storeSku.TiktokAttribute == "" || storeSku.TiktokAttribute == "{}" {
categoryList, err := api.GetCatePropertyV2(param.CategoryLeafId)
param.ProductFormatNew, err = MakeProductFormatNew(api, int64(storeSku.NameID), param.CategoryLeafId)
if err != nil {
return nil, err
}
categoryMap := make(map[string][]map[string]interface{})
for _, v := range categoryList.Data.Data {
if v.Required == model.YES {
options := make([]map[string]interface{}, 0)
if v.PropertyName == "品牌" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 789194134, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if v.PropertyName == "产地" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 13850, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if len(options) == 0 {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 0, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": v.Options[0].Value, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
}
}
continue
}
param.ProductFormatNew = utils.Format4Output(categoryMap, false)
dao.UpdateSkuNameTiktokAttr(dao.GetDB(), int64(storeSku.NameID), param.ProductFormatNew)
} else {
param.ProductFormatNew = storeSku.TiktokAttribute
}
@@ -376,6 +342,8 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
// 创建子商品
//param.FreightId = freightId
param.ProductId = utils.Str2Int64(storeSku.VendorSkuID)
param.MainProductId = utils.Str2Int64(storeSku.VendorMainId)
param.SpecPrices = GetSpecPrices(param.Specs, vendorStoreID, param.MainProductId, storeSku)
//param.SpecPrices = GetSpecPrices(param.Specs, vendorStoreID, tiktokResult.ProductId, storeSku)
// 获取门店限售模板
//saleLimitId, err := CreateSaleTemp(utils.Str2Int64(vendorStoreID), api)
@@ -383,7 +351,7 @@ func (p *PurchaseHandler) createOrUpdateStoreSkus(ctx *jxcontext.Context, storeI
// return nil, err
//}
//param.SaleLimitId = saleLimitId
//param.StoreId = utils.Str2Int64(vendorStoreID)
param.StoreId = utils.Str2Int64(vendorStoreID)
// 抖店创建商品
if err := api.EditStoreCommodity(param); err != nil {
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDDD], syncType)
@@ -543,9 +511,8 @@ func GetProductFormatNew(categoryLeftId int64, vendorOrgCode string) (*product_g
}
// GetTiktokImgList 获取抖音图片链接
func GetTiktokImgList(storeId, appOrgCode string, img ...string) (string, error) {
func GetTiktokImgList(api *tiktokShop.API, storeId, appOrgCode string, detailImg string, img ...string) (string, string, error) {
imgs := make([]tiktokShop.Imgs, 0, 0)
var tiktokImg []string
for _, v := range img {
if v != "" {
imgs = append(imgs, tiktokShop.Imgs{
@@ -554,21 +521,61 @@ func GetTiktokImgList(storeId, appOrgCode string, img ...string) (string, error)
})
}
}
tiktokImgList, err := getAPI(appOrgCode, 0, "").BatchUploadImages(imgs)
if detailImg != "" {
imgs = append(imgs, tiktokShop.Imgs{
Name: "detail_" + storeId + "_" + detailImg[21:54],
Url: detailImg,
})
}
tiktokImgList, err := api.BatchUploadImages(imgs)
if err != nil {
return "", "", err
}
detailTiktok := ""
var tiktokImg []string
for _, v := range tiktokImgList {
if strings.Contains(v.Name, "detail_") {
detailTiktok = v.ByteUrl
continue
}
tiktokImg = append(tiktokImg, v.ByteUrl)
}
if detailTiktok == "" {
detailTiktok = tiktokImg[0]
}
return strings.Join(tiktokImg, "|"), detailTiktok, nil
}
func MakeProductFormatNew(api *tiktokShop.API, skuNameId int64, categoryLeafId int64) (string, error) {
categoryList, err := api.GetCatePropertyV2(categoryLeafId)
if err != nil {
return "", err
}
for _, v := range tiktokImgList.SuccessMap {
tiktokImg = append(tiktokImg, v.ByteUrl)
}
if len(tiktokImg) <= model.NO {
for _, v := range tiktokImgList.FailedMap {
return "", errors.New(v.ErrMsg)
categoryMap := make(map[string][]map[string]interface{})
for _, v := range categoryList.Data.Data {
if v.Required != model.YES {
continue
}
options := make([]map[string]interface{}, 0)
if v.PropertyName == "品牌" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 789194134, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if v.PropertyName == "产地" {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 13850, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else if len(options) == 0 {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": 0, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
} else {
options = append(options, map[string]interface{}{"name": v.PropertyName, "value": v.Options[0].Value, "diy_type": v.DiyType})
categoryMap[utils.Int64ToStr(v.PropertyId)] = options
}
}
return strings.Join(tiktokImg, "|"), nil
productFormatNew := utils.Format4Output(categoryMap, false)
dao.UpdateSkuNameTiktokAttr(dao.GetDB(), skuNameId, productFormatNew)
return utils.Format4Output(categoryMap, false), nil
}
// GetSpecPrices 解析属性和规格参数
@@ -578,6 +585,20 @@ func GetSpecPrices(specs, storeId string, mainSkuId int64, localSku *dao.StoreSk
if len(detail1) > 3 {
detail1 = detail1[0:3]
}
specsUnit := "g"
var infoValue float64 = 1 // 同意计量单位,目前抖店只支持g
var specQuality = float64(localSku.SpecQuality) // 可能SpecQuality为0,给默认值
switch localSku.SpecUnit {
case "g", "G", "ml", "ML":
specsUnit = "g"
infoValue = 1
case "l", "L", "KG", "kg":
specsUnit = "g"
infoValue = 1000
}
if specQuality == 0 {
specQuality = 1
}
switch len(detail1) {
case 1:
name1 := strings.Split(strings.Split(detail1[0], "|")[1], ",")
@@ -589,11 +610,12 @@ func GetSpecPrices(specs, storeId string, mainSkuId int64, localSku *dao.StoreSk
StepStockNum: 0,
SupplierID: "",
OuterSkuID: utils.Int2Str(localSku.SkuID),
DeliveryInfos: []*tiktokShop.DeliveryInfos{
{InfoType: "weight", InfoUnit: localSku.SpecUnit, InfoValue: fmt.Sprintf("%f", localSku.SpecQuality)},
},
}
stock := 0
sku.DeliveryInfos = []*tiktokShop.DeliveryInfos{
{InfoType: "weight", InfoUnit: specsUnit, InfoValue: utils.Float64ToStr(specQuality * infoValue)},
}
stock := localSku.Stock
if localSku.Stock == 0 {
stock = 9999
}
@@ -616,10 +638,10 @@ func GetSpecPrices(specs, storeId string, mainSkuId int64, localSku *dao.StoreSk
SupplierID: "",
OuterSkuID: utils.Int2Str(localSku.NameID),
DeliveryInfos: []*tiktokShop.DeliveryInfos{
{InfoType: "weight", InfoUnit: localSku.SpecUnit, InfoValue: fmt.Sprintf("%f", localSku.SpecQuality)},
{InfoType: "weight", InfoUnit: specsUnit, InfoValue: utils.Float64ToStr(specQuality * infoValue)},
},
}
stock := 0
stock := localSku.Stock
if localSku.Stock == 0 {
stock = 9999
}
@@ -646,15 +668,14 @@ func GetSpecPrices(specs, storeId string, mainSkuId int64, localSku *dao.StoreSk
SupplierID: "",
OuterSkuID: utils.Int2Str(localSku.NameID),
DeliveryInfos: []*tiktokShop.DeliveryInfos{
{InfoType: "weight", InfoUnit: localSku.SpecUnit, InfoValue: fmt.Sprintf("%f", localSku.SpecQuality)},
{InfoType: "weight", InfoUnit: specsUnit, InfoValue: utils.Float64ToStr(specQuality * infoValue)},
},
}
stock := 0
stock := localSku.Stock
if localSku.Stock == 0 {
stock = 9999
}
sku.StockNum = stock
sku.StockNum = 0
sku.SkuType = 1
sku.StockNumMap = map[string]int64{storeId: int64(stock)}
skuSize = append(skuSize, sku)