package taobao import ( "applet/app/utils" "applet/app/utils/cachesecond" "applet/app/utils/logx" "encoding/json" "errors" "fmt" "github.com/tidwall/gjson" "time" ) type DgOptimusMaterialStruct struct { TbkDgOptimusMaterialResponse struct { ResultList struct { MapData []TBFeatureStruct `json:"map_data"` } `json:"result_list"` } `json:"tbk_dg_optimus_material_response"` } type dgTbkDgMaterialRecommendResponse struct { TbkDgMaterialRecommendResponse struct { ResultList struct { MapData []NewMarterial `json:"result_list"` } IsDefault string `json:"is_default"` TotalCount int `json:"total_count"` UvidMsg string `json:"uvid_msg"` } `json:"tbk_dg_material_recommend_response"` } type ScOptimusMaterialStruct struct { TbkScOptimusMaterialResponse struct { ResultList struct { MapData []TBFeatureStruct `json:"map_data"` } `json:"result_list"` } `json:"tbk_sc_optimus_material_response"` } type scTbkScMaterialRecommendResponse struct { TbkScMaterialRecommendResponse struct { ResultList struct { MapData []NewMarterial `json:"map_data"` } `json:"result_list"` } `json:"tbk_sc_material_recommend_response"` } type TBFeatureStruct struct { CategoryID int `json:"category_id"` CategoryName string `json:"category_name"` ClickURL string `json:"click_url"` CommissionRate string `json:"commission_rate"` CouponAmount float64 `json:"coupon_amount"` CouponClickURL string `json:"coupon_click_url"` CouponEndTime string `json:"coupon_end_time"` GoodsCoupon string `json:"goods_coupon"` CouponRemainCount int `json:"coupon_remain_count"` CouponShareURL string `json:"coupon_share_url"` CouponStartFee string `json:"coupon_start_fee"` CouponStartTime string `json:"coupon_start_time"` CouponTotalCount int `json:"coupon_total_count"` ItemDescription string `json:"item_description"` ItemID interface{} `json:"item_id"` JhsPriceUspList string `json:"jhs_price_usp_list"` LevelOneCategoryID int `json:"level_one_category_id"` LevelOneCategoryName string `json:"level_one_category_name"` Nick string `json:"nick"` PictURL string `json:"pict_url"` ReservePrice string `json:"reserve_price"` SellerID int64 `json:"seller_id"` ShopTitle string `json:"shop_title"` ShortTitle string `json:"short_title"` SmallImages struct { String []string `json:"string"` } `json:"small_images"` SubTitle string `json:"sub_title"` Title string `json:"title"` UserType int `json:"user_type"` Volume int `json:"volume"` WhiteImage string `json:"white_image"` ZkFinalPrice string `json:"zk_final_price"` } type TBSearchFeatureStruct struct { CategoryID int `json:"category_id"` CategoryName string `json:"category_name"` ClickURL string `json:"click_url"` CommissionRate string `json:"commission_rate"` CouponAmount string `json:"coupon_amount"` CouponClickURL string `json:"coupon_click_url"` CouponEndTime string `json:"coupon_end_time"` CouponRemainCount int `json:"coupon_remain_count"` CouponShareURL string `json:"coupon_share_url"` CouponStartFee string `json:"coupon_start_fee"` CouponStartTime string `json:"coupon_start_time"` CouponTotalCount int `json:"coupon_total_count"` ItemDescription string `json:"item_description"` ItemID interface{} `json:"item_id"` JhsPriceUspList string `json:"jhs_price_usp_list"` LevelOneCategoryID int `json:"level_one_category_id"` LevelOneCategoryName string `json:"level_one_category_name"` Nick string `json:"nick"` PictURL string `json:"pict_url"` ReservePrice string `json:"reserve_price"` SellerID int64 `json:"seller_id"` ShopTitle string `json:"shop_title"` ShortTitle string `json:"short_title"` SmallImages struct { String []string `json:"string"` } `json:"small_images"` SubTitle string `json:"sub_title"` Title string `json:"title"` UserType int `json:"user_type"` Volume int `json:"volume"` WhiteImage string `json:"white_image"` ZkFinalPrice string `json:"zk_final_price"` } type TBKShopData struct { UserId string `json:"user_id"` PictUrl string `json:"pict_url"` } type Autogenerated struct { TbkShopGetResponse struct { Results struct { NTbkShop []TBKShopData `json:"n_tbk_shop"` } `json:"results"` TotalResults int `json:"total_results"` RequestID string `json:"request_id"` } `json:"tbk_shop_get_response"` } // 淘宝客-推广者-店铺搜索 // https://open.taobao.com/api.htm?spm=a219a.7386797.0.0.6180669aZ2HyFi&source=search&docId=24521&docType=2 // taobao.tbk.shop.get func (t *TB) TBKShopGet(SellerId, Nick string) (*TBKShopData, error) { var res *TBKShopData var Autogenerated struct { TbkShopGetResponse struct { Results struct { NTbkShop []struct { PictURL string `json:"pict_url"` UserID interface{} `json:"user_id"` } `json:"n_tbk_shop"` } `json:"results"` TotalResults int `json:"total_results"` RequestID string `json:"request_id"` } `json:"tbk_shop_get_response"` } cacheKey := fmt.Sprintf("tb_shop_get:%s", Nick) err := cachesecond.GetJson(cacheKey, &res) if err != nil || res == nil { method := "taobao.tbk.shop.get" Sid := t.SID if t.AuthType == 1 { Sid = t.UserSID } args := map[string]string{ "fields": "user_id,pict_url", "q": Nick, "page_no": "1", "page_size": "20", "session": Sid, } resp, err := send(args, method, t.User_Web_AK, t.User_Web_SK) if err != nil { return nil, err } if gjson.GetBytes(resp, "error_response.sub_msg").String() != "" { return nil, logx.Warn(errors.New("Not Result")) //return nil, logx.Warn(errors.New(gjson.GetBytes(resp, "error_response.sub_msg").String())) } var data TBKShopData utils.Unserialize(resp, &Autogenerated) for _, v := range Autogenerated.TbkShopGetResponse.Results.NTbkShop { if utils.AnyToString(utils.AnyToInt64(v.UserID)) == SellerId { data.UserId = utils.AnyToString(utils.AnyToInt64(v.UserID)) data.PictUrl = v.PictURL } } res = &data if data.UserId != "" { cachesecond.SetJson(cacheKey, res, 60*3) } } return res, nil } // 精选列表(淘宝客-服务商-物料精选) // https://open.taobao.com/api.htm?spm=a219a.7386797.0.0.1f6f669aNUCptn&source=search&docId=37884&docType=2 // TODO 物料id查找 https://market.m.taobao.com/app/qn/toutiao-new/index-pc.html#/detail/10628875?_k=gpov9a func (t *TB) Featured(pageIndex, pageSize, materialID, deviceValue, gid string) (*[]TBFeatureStruct, error) { var resp []byte method := "taobao.tbk.dg.material.recommend" //判断是否跟随官方 如果是官方的要用服务商接口 Sid := t.SID if t.AuthType == 1 { method = "taobao.tbk.sc.material.recommend" Sid = t.UserSID } args := map[string]string{ "material_id": materialID, "page_no": pageIndex, "page_size": pageSize, "session": Sid, } if deviceValue != "" { args["device_value"] = deviceValue args["device_type"] = "UTDID" } if gid != "" { args["item_id"] = gid } _, args["site_id"], args["adzone_id"] = SplitPid(t.SelfPromoIdWeb) fmt.Println(args) resp, _ = send(args, method, t.Web_AK, t.Web_SK) fmt.Println("======淘宝=====", string(resp)) if gjson.GetBytes(resp, "error_response.sub_msg").String() != "" { return nil, logx.Warn(errors.New("Not Result")) //return nil, logx.Warn(errors.New(gjson.GetBytes(resp, "error_response.sub_msg").String())) } if t.AuthType == 1 { var tmp = scTbkScMaterialRecommendResponse{} err := json.Unmarshal(resp, &tmp) fmt.Println(err) if len(tmp.TbkScMaterialRecommendResponse.ResultList.MapData) == 0 { return nil, logx.Warn(resp) } data := tmp.TbkScMaterialRecommendResponse.ResultList.MapData list := make([]TBFeatureStruct, 0) for _, v := range data { tmp1 := TBFeatureStruct{ CategoryID: v.ItemBasicInfo.CategoryId, CategoryName: v.ItemBasicInfo.CategoryName, CommissionRate: v.PublishInfo.IncomeRate, ItemID: v.ItemId, Nick: v.ItemBasicInfo.ShopTitle, PictURL: v.ItemBasicInfo.PictUrl, SellerID: int64(v.ItemBasicInfo.SellerId), ShopTitle: v.ItemBasicInfo.ShopTitle, ShortTitle: v.ItemBasicInfo.ShortTitle, Title: v.ItemBasicInfo.Title, ZkFinalPrice: v.PricePromotionInfo.ZkFinalPrice, ReservePrice: v.PricePromotionInfo.ReservePrice, Volume: v.ItemBasicInfo.Volume, } tmp1.SmallImages.String = []string{tmp1.PictURL} if utils.StrToFloat64(v.PricePromotionInfo.FinalPromotionPrice) > 0 { for _, v1 := range v.PricePromotionInfo.FinalPromotionPathList.FinalPromotionPathMapData { tmp1.CouponAmount = utils.StrToFloat64(v1.PromotionFee) + tmp1.CouponAmount tmp1.CouponStartTime = utils.IntToStr(utils.StrToInt(v1.PromotionStartTime) / 1000) tmp1.CouponEndTime = utils.IntToStr(utils.StrToInt(v1.PromotionEndTime) / 1000) if v1.PromotionTitle == "商品券" || v1.PromotionTitle == "店铺券" { if utils.StrToInt64(v1.PromotionEndTime)/1000 > time.Now().Unix() { tmp1.GoodsCoupon = "1" } } } } list = append(list, tmp1) } return &list, nil } else { var tmp = dgTbkDgMaterialRecommendResponse{} utils.Unserialize(resp, &tmp) if len(tmp.TbkDgMaterialRecommendResponse.ResultList.MapData) == 0 { return nil, logx.Warn(resp) } data := tmp.TbkDgMaterialRecommendResponse.ResultList.MapData list := make([]TBFeatureStruct, 0) for _, v := range data { tmp1 := TBFeatureStruct{ CategoryID: v.ItemBasicInfo.CategoryId, CategoryName: v.ItemBasicInfo.CategoryName, CommissionRate: v.PublishInfo.IncomeRate, ItemID: v.ItemId, Nick: v.ItemBasicInfo.ShopTitle, PictURL: v.ItemBasicInfo.PictUrl, SellerID: int64(v.ItemBasicInfo.SellerId), ShopTitle: v.ItemBasicInfo.ShopTitle, ShortTitle: v.ItemBasicInfo.ShortTitle, Title: v.ItemBasicInfo.Title, ZkFinalPrice: v.PricePromotionInfo.ZkFinalPrice, ReservePrice: v.PricePromotionInfo.ReservePrice, Volume: v.ItemBasicInfo.Volume, } tmp1.SmallImages.String = []string{tmp1.PictURL} if utils.StrToFloat64(v.PricePromotionInfo.FinalPromotionPrice) > 0 { for _, v1 := range v.PricePromotionInfo.FinalPromotionPathList.FinalPromotionPathMapData { tmp1.CouponAmount = utils.StrToFloat64(v1.PromotionFee) + tmp1.CouponAmount tmp1.CouponStartTime = utils.IntToStr(utils.StrToInt(v1.PromotionStartTime) / 1000) tmp1.CouponEndTime = utils.IntToStr(utils.StrToInt(v1.PromotionEndTime) / 1000) if v1.PromotionTitle == "商品券" || v1.PromotionTitle == "店铺券" { if utils.StrToInt64(v1.PromotionEndTime)/1000 > time.Now().Unix() { tmp1.GoodsCoupon = "1" } } } } list = append(list, tmp1) } return &list, nil } }