银豹api自动换cookie
This commit is contained in:
@@ -8,11 +8,13 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.rosy.net.cn/baseapi/platformapi/yinbaoapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/partner/putils"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"git.rosy.net.cn/jx-callback/globals/api"
|
||||
"git.rosy.net.cn/jx-callback/globals/refutil"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
|
||||
@@ -533,6 +535,11 @@ func syncStoreSkuNew(ctx *jxcontext.Context, parentTask tasksch.ITask, causeFlag
|
||||
v.JxPrice = price
|
||||
}
|
||||
}
|
||||
api.YinBaoAPI = yinbaoapi.New(storeDetail.YbAppKey, storeDetail.YbAppID)
|
||||
if configs, err := dao.QueryConfigs(dao.GetDB(), "yinbaoCookie", model.ConfigTypeCookie, ""); err == nil {
|
||||
yinbaoCookie := configs[0].Value
|
||||
api.YinBaoAPI.SetCookie(".POSPALAUTH30220", yinbaoCookie)
|
||||
}
|
||||
}
|
||||
task := tasksch.NewParallelTask("syncStoreSkuNew", tasksch.NewParallelConfig().SetParallelCount(1).SetIsContinueWhenError(isContinueWhenError2), ctx,
|
||||
func(task *tasksch.ParallelTask, batchItemList []interface{}, params ...interface{}) (retVal interface{}, err error) {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package yb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"git.rosy.net.cn/jx-callback/business/jxstore/cms"
|
||||
@@ -26,8 +25,6 @@ var (
|
||||
func (p *PurchaseHandler) CreateStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*dao.StoreSkuSyncInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
storeSku := storeSkuList[0]
|
||||
buildYbConfigs(storeID)
|
||||
buildYbApiCookie(ctx)
|
||||
result, err := api.YinBaoAPI.AddProductInfo(buildProductInfoParam(storeSku))
|
||||
if err != nil {
|
||||
failedList = putils.GetErrMsg2FailedSingleList(storeSkuList, err, storeID, model.VendorChineseNames[model.VendorIDYB], "创建商品")
|
||||
@@ -45,7 +42,6 @@ func (p *PurchaseHandler) UpdateStoreSkus(ctx *jxcontext.Context, storeID int, v
|
||||
|
||||
func (p *PurchaseHandler) DeleteStoreSkus(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbConfigs(storeID)
|
||||
for _, v := range storeSkuList {
|
||||
var productInfo = &yinbaoapi.ProductInfo{
|
||||
UID: utils.Str2Int64(v.VendorSkuID),
|
||||
@@ -61,7 +57,6 @@ func (p *PurchaseHandler) DeleteStoreSkus(ctx *jxcontext.Context, storeID int, v
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) GetStoreSkusFullInfo(ctx *jxcontext.Context, parentTask tasksch.ITask, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (skuNameList []*partner.SkuNameInfo, err error) {
|
||||
buildYbConfigs(storeID)
|
||||
if storeSkuList != nil {
|
||||
if len(storeSkuList) == 1 {
|
||||
storeSku := storeSkuList[0]
|
||||
@@ -118,7 +113,6 @@ func (p *PurchaseHandler) GetStoreSkusFullInfo(ctx *jxcontext.Context, parentTas
|
||||
|
||||
func (p *PurchaseHandler) UpdateStoreSkusStatus(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo, status int) (failedList []*partner.StoreSkuInfoWithErr, err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbConfigs(storeID)
|
||||
for _, v := range storeSkuList {
|
||||
var productInfo = &yinbaoapi.ProductInfo{
|
||||
UID: utils.Str2Int64(v.VendorSkuID),
|
||||
@@ -135,7 +129,6 @@ 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) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbConfigs(storeID)
|
||||
for _, v := range storeSkuList {
|
||||
var productInfo = &yinbaoapi.ProductInfo{
|
||||
UID: utils.Str2Int64(v.VendorSkuID),
|
||||
@@ -153,7 +146,6 @@ func (p *PurchaseHandler) UpdateStoreSkusPrice(ctx *jxcontext.Context, vendorOrg
|
||||
|
||||
func (p *PurchaseHandler) UpdateStoreSkusStock(ctx *jxcontext.Context, vendorOrgCode string, storeID int, vendorStoreID string, storeSkuList []*partner.StoreSkuInfo) (failedList []*partner.StoreSkuInfoWithErr, err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbConfigs(storeID)
|
||||
for _, v := range storeSkuList {
|
||||
var productInfo = &yinbaoapi.ProductInfo{
|
||||
UID: utils.Str2Int64(v.VendorSkuID),
|
||||
@@ -169,21 +161,38 @@ func (p *PurchaseHandler) UpdateStoreSkusStock(ctx *jxcontext.Context, vendorOrg
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) GetStoreAllCategories(ctx *jxcontext.Context, storeID int, vendorStoreID string) (cats []*partner.BareCategoryInfo, err error) {
|
||||
remoteCats, err := api.YinBaoAPI.LoadCategorysWithOption(vendorStoreID)
|
||||
if err == nil {
|
||||
cats = convertVendorCatList(remoteCats)
|
||||
}
|
||||
remoteCats, err := loadCategorysWithOption(vendorStoreID)
|
||||
cats = convertVendorCatList(remoteCats)
|
||||
return cats, err
|
||||
}
|
||||
|
||||
func loadCategorysWithOption(vendorStoreID string) (remoteCats []*yinbaoapi.LoadCategorysWithOptionResult, err error) {
|
||||
for {
|
||||
remoteCats, err = api.YinBaoAPI.LoadCategorysWithOption(vendorStoreID)
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
if yinbaoapi.IsErrCookie(err) {
|
||||
err = changeYbCookie()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
remoteCats, err = loadCategorysWithOption(vendorStoreID)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return remoteCats, err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) CreateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbApiCookie(ctx)
|
||||
var (
|
||||
catName = utils.FilterEmoji(storeCat.Name)
|
||||
parentCatName = utils.FilterEmoji(storeCat.ParentCatName)
|
||||
)
|
||||
vendorCatID, err := api.YinBaoAPI.AddNewCategory(vendorStoreID, catName, parentCatName)
|
||||
vendorCatID, err := addNewCategory(vendorStoreID, catName, parentCatName)
|
||||
if err == nil {
|
||||
storeCat.VendorCatID = vendorCatID
|
||||
}
|
||||
@@ -192,35 +201,80 @@ func (p *PurchaseHandler) CreateStoreCategory(ctx *jxcontext.Context, storeID in
|
||||
return err
|
||||
}
|
||||
|
||||
func buildYbApiCookie(ctx *jxcontext.Context) {
|
||||
yinbaoCookiePageCount := ""
|
||||
if configs, err := dao.QueryConfigs(dao.GetDB(), "yinbaoCookie", model.ConfigTypeCookie, ""); err == nil {
|
||||
yinbaoCookie := configs[0].Value
|
||||
api.YinBaoAPI.SetCookie(".POSPALAUTH30220", yinbaoCookie)
|
||||
if configs2, err := dao.QueryConfigs(dao.GetDB(), "yinbaoCookiePageCount", model.ConfigTypeCookie, ""); err == nil {
|
||||
yinbaoCookiePageCount = configs2[0].Value
|
||||
api.YinBaoAPI.SetCookie("pageOpenCount", yinbaoCookiePageCount)
|
||||
func addNewCategory(vendorStoreID, catName, parentCatName string) (vendorCatID string, err error) {
|
||||
for {
|
||||
vendorCatID, err = api.YinBaoAPI.AddNewCategory(vendorStoreID, catName, parentCatName)
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
if yinbaoapi.IsErrCookie(err) {
|
||||
err = changeYbCookie()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
vendorCatID, err = addNewCategory(vendorStoreID, catName, parentCatName)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
cms.UpdateConfig(ctx, "yinbaoCookiePageCount", "Cookie", utils.Int64ToStr(utils.Str2Int64(yinbaoCookiePageCount)+1))
|
||||
return vendorCatID, err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) UpdateStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID string, storeCat *dao.SkuStoreCatInfo) (err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbApiCookie(ctx)
|
||||
var (
|
||||
catName = utils.FilterEmoji(storeCat.Name)
|
||||
parentCatName = utils.FilterEmoji(storeCat.ParentCatName)
|
||||
)
|
||||
err = api.YinBaoAPI.UpdateCategory(vendorStoreID, storeCat.VendorCatID, catName, parentCatName)
|
||||
err = updateCategory(vendorStoreID, storeCat.VendorCatID, catName, parentCatName)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func updateCategory(vendorStoreID, vendorCatID, catName, parentCatName string) (err error) {
|
||||
for {
|
||||
err = api.YinBaoAPI.UpdateCategory(vendorStoreID, vendorCatID, catName, parentCatName)
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
if yinbaoapi.IsErrCookie(err) {
|
||||
err = changeYbCookie()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = updateCategory(vendorStoreID, vendorCatID, catName, parentCatName)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (p *PurchaseHandler) DeleteStoreCategory(ctx *jxcontext.Context, storeID int, vendorStoreID, vendorCatID string, level int) (err error) {
|
||||
if globals.EnableYbStoreWrite {
|
||||
buildYbApiCookie(ctx)
|
||||
err = api.YinBaoAPI.DeleteCategory(vendorStoreID, []string{vendorCatID})
|
||||
err = deleteCategory(vendorStoreID, []string{vendorCatID})
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func deleteCategory(vendorStoreID string, vendorCatIDs []string) (err error) {
|
||||
for {
|
||||
err = api.YinBaoAPI.DeleteCategory(vendorStoreID, vendorCatIDs)
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
if yinbaoapi.IsErrCookie(err) {
|
||||
err = changeYbCookie()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = api.YinBaoAPI.DeleteCategory(vendorStoreID, vendorCatIDs)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -343,18 +397,6 @@ func buildProductInfoParam(storeSku *dao.StoreSkuSyncInfo) (productInfoParam *yi
|
||||
return productInfoParam
|
||||
}
|
||||
|
||||
func buildYbConfigs(storeID int) (err error) {
|
||||
if storeID == 0 {
|
||||
return fmt.Errorf("门店ID不能为空!平台:[%v]", model.VendorIDYB)
|
||||
}
|
||||
store, err := dao.GetStoreDetail(dao.GetDB(), storeID, model.VendorIDYB)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
api.YinBaoAPI = yinbaoapi.New(store.YbAppKey, store.YbAppID)
|
||||
return err
|
||||
}
|
||||
|
||||
func convertVendorCatList(remoteCats []*yinbaoapi.LoadCategorysWithOptionResult) (cats []*partner.BareCategoryInfo) {
|
||||
for _, rCat := range remoteCats {
|
||||
cat := &partner.BareCategoryInfo{
|
||||
@@ -372,6 +414,31 @@ func convertVendorCatList(remoteCats []*yinbaoapi.LoadCategorysWithOptionResult)
|
||||
}
|
||||
|
||||
func updateYbSkuPluCode(vendorStoreID, ybBarCode string) (err error) {
|
||||
err = api.YinBaoAPI.SaveProduct(vendorStoreID, ybBarCode)
|
||||
for {
|
||||
err = api.YinBaoAPI.SaveProduct(vendorStoreID, ybBarCode)
|
||||
if err == nil {
|
||||
break
|
||||
} else {
|
||||
if yinbaoapi.IsErrCookie(err) {
|
||||
err = changeYbCookie()
|
||||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = updateYbSkuPluCode(vendorStoreID, ybBarCode)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func changeYbCookie() (err error) {
|
||||
cookie, err := api.YinBaoAPI.TryGetCookie()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
api.YinBaoAPI.SetCookie(".POSPALAUTH30220", cookie)
|
||||
cms.UpdateConfig(jxcontext.AdminCtx, "yinbaoCookie", model.ConfigTypeCookie, cookie)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user