diff --git a/constants.go b/constants.go index 99f9801..c21939c 100644 --- a/constants.go +++ b/constants.go @@ -1,15 +1,18 @@ package tbksdk type constants struct { - AlimamaKey string - AlimamaSecret string - AlimamaApiUrl string - AlimamaTpwdConvertUrl string - AlimamaItemInfoGetUrl string - AlimamaPrivilegeGet string - AlimamaTklCreate string - AlimamaScMaterialOptional string - AlimamaTbkCouponGet string + AlimamaKey string + AlimamaSecret string + AlimamaApiUrl string + AlimamaTpwdConvertUrl string + AlimamaItemInfoGetUrl string + AlimamaPrivilegeGet string + AlimamaTklCreate string + AlimamaScMaterialOptional string + AlimamaTbkCouponGet string + AlimamaTbkScOrderDetailsGet string + AlimamaTbkScActivityLinkToolGet string + AlimamaTbkSpreadGet string } var Constants = constants{} @@ -18,7 +21,7 @@ func init() { // alimama related Constants.AlimamaKey = "" Constants.AlimamaSecret = "" - //api + // api Constants.AlimamaApiUrl = "http://gw.api.taobao.com/router/rest?" Constants.AlimamaTpwdConvertUrl = "taobao.tbk.sc.tpwd.convert" Constants.AlimamaItemInfoGetUrl = "taobao.tbk.item.info.get" @@ -26,4 +29,7 @@ func init() { Constants.AlimamaTklCreate = "taobao.tbk.tpwd.create" Constants.AlimamaScMaterialOptional = "taobao.tbk.sc.material.optional" Constants.AlimamaTbkCouponGet = "taobao.tbk.coupon.get" + Constants.AlimamaTbkScOrderDetailsGet = "taobao.tbk.sc.order.details.get" + Constants.AlimamaTbkScActivityLinkToolGet = "taobao.tbk.sc.activitylink.toolget" // 官方活动转链 + Constants.AlimamaTbkSpreadGet = "taobao.tbk.spread.get" // 官方活动长链转短链 } diff --git a/tkl_convert.go b/tkl_convert.go index b4d55ff..6531960 100644 --- a/tkl_convert.go +++ b/tkl_convert.go @@ -170,7 +170,7 @@ func PrivilegeGet(session string, itemId int64, adzoneId string, siteId string) if err != nil { return nil, err } - //得到了返回,但不是item信息,尝试解析为ErrorResponse + // 得到了返回,但不是item信息,尝试解析为ErrorResponse if respData.TbkPrivilegeGetResponse.Result.Data.ItemID == 0 { errorResponse := ErrorResponse{} _ = json.Unmarshal(*bodyByte, &errorResponse) @@ -343,6 +343,181 @@ func TbkCouponGet(itemId int64, couponId string) (*TbkCouponGetData, error) { return &respData.TbkCouponGetResponse.Data, nil } +type tbkScOrderDetailsGetResponseData struct { + TbkScOrderDetailsGetResponse TbkScOrderDetailsGetResponse `json:"tbk_sc_order_details_get_response"` +} +type TbkScOrderDetailsGetResponse struct { + Data struct { + Results struct { + PublisherOrderDto []PublisherOrderDto `json:"publisher_order_dto"` + } `json:"results"` + HasPre bool `json:"has_pre"` + PositionIndex string `json:"position_index"` + HasNext bool `json:"has_next"` + PageNo int64 `json:"page_no"` + PageSize int64 `json:"page_size"` + } `json:"data"` +} +type PublisherOrderDto struct { + TbPaidTime string `json:"tb_paid_time"` + TkPaidTime string `json:"tk_paid_time"` + PayPrice string `json:"pay_price"` + PubShareFee string `json:"pub_share_fee"` + TradeId string `json:"trade_id"` + TkOrderRole int64 `json:"tk_order_role"` + TkEarningTime string `json:"tk_earning_time"` + AdzoneId int64 `json:"adzone_id"` + PubShareRate string `json:"pub_share_rate"` + RefundTag int64 `json:"refund_tag"` + SubsidyRate string `json:"subsidy_rate"` + TkTotalRate string `json:"tk_total_rate"` + ItemCategoryName string `json:"item_category_name"` + SellerNick string `json:"seller_nick"` + PubId int64 `json:"pub_id"` + AlimamaRate string `json:"alimama_rate"` + SubsidyType string `json:"subsidy_type"` + ItemImg string `json:"item_img"` + PubSharePreFee string `json:"pub_share_pre_fee"` + AlipayTotalPrice string `json:"alipay_total_price"` + ItemTitle string `json:"item_title"` + SiteName string `json:"site_name"` + ItemNum int64 `json:"item_num"` + SubsidyFee string `json:"subsidy_fee"` + AlimamaShareFee string `json:"alimama_share_fee"` + TradeParentId string `json:"trade_parent_id"` + OrderType string `json:"order_type"` + TkCreateTime string `json:"tk_create_time"` + FlowSource string `json:"flow_source"` + TerminalType string `json:"terminal_type"` + ClickTime string `json:"click_time"` + TkStatus int8 `json:"tk_status"` + ItemPrice string `json:"item_price"` + ItemId int64 `json:"item_id"` + AdzoneName string `json:"adzone_name"` + TotalCommissionRate string `json:"total_commission_rate"` + ItemLink string `json:"item_link"` + SiteId int64 `json:"site_id"` + SellerShopTitle string `json:"seller_shop_title"` + IncomeRate string `json:"income_rate"` + TotalCommissionFee string `json:"total_commission_fee"` + TkCommissionPreFeeForMediaPlatform string `json:"tk_commission_pre_fee_for_media_platform"` + TkCommissionFeeForMediaPlatform string `json:"tk_commission_fee_for_media_platform"` + TkCommissionRateForMediaPlatform string `json:"tk_commission_rate_for_media_platform"` + SpecialId int64 `json:"special_id"` + RelationId int64 `json:"relation_id"` +} + +type TbkScActivityResponse struct { + TbkScActivitylinkToolgetResponse struct { + ResultMsg string `json:"result_msg"` + Data string `json:"data"` + ResultCode int64 `json:"result_code"` + BizErrorDesc string `json:"biz_error_desc"` + BizErrorCode int64 `json:"biz_error_code"` + } `json:"tbk_sc_activitylink_toolget_response"` +} + +/** +淘宝客-服务商-所有订单查询 +https://open.taobao.com/api.htm?spm=a219a.7386653.0.0.2b43669aJY6jMz&source=search&docId=43755&docType=2 +*/ +func TbkScOrderDetailsGet(startTime string, endTime string, session string, pageNo int64, pageSize int64, positionIndex string, queryType, orderScene int64) (*TbkScOrderDetailsGetResponse, error) { + orderSceneStr := "1" + if orderScene != 0 { + orderSceneStr = strconv.FormatInt(orderScene, 10) + } + var paramsMap = map[string]string{ + "start_time": startTime, + "end_time": endTime, + "session": session, + "page_no": strconv.FormatInt(pageNo, 10), + "page_size": strconv.FormatInt(pageSize, 10), + "position_index": positionIndex, + "query_type": strconv.FormatInt(queryType, 10), + "jump_type": "1", + "order_scene": orderSceneStr, + } + var bodyByte, err = apply(Constants.AlimamaTbkScOrderDetailsGet, paramsMap) + if err != nil { + return nil, err + } + var respData = tbkScOrderDetailsGetResponseData{} + err = json.Unmarshal(*bodyByte, &respData) + if err != nil { + return nil, err + } + if respData.TbkScOrderDetailsGetResponse.Data.PageNo == 0 { + errorResponse := ErrorResponse{} + _ = json.Unmarshal(*bodyByte, &errorResponse) + if errorResponse.ErrorResponse.Code != 0 { + return nil, errors.New(string(*bodyByte)) + } + return nil, errors.New("data is empty") + } + return &respData.TbkScOrderDetailsGetResponse, nil +} + +// 官方活动转链 https://open.taobao.com/api.htm?docId=41921&docType=2&source=search +func TbkScActivityLinkToolGet(activityId int64, adzoneId, siteId, session string) (string, error) { + paramMap := map[string]string{ + "adzone_id": adzoneId, + "site_id": siteId, + "promotion_scene_id": strconv.FormatInt(activityId, 10), + "platform": "2", + "session": session, + } + var bodyByte, err = apply(Constants.AlimamaTbkScActivityLinkToolGet, paramMap) + if err != nil { + return "", err + } + var respData = TbkScActivityResponse{} + err = json.Unmarshal(*bodyByte, &respData) + if err != nil { + return "", err + } + return respData.TbkScActivitylinkToolgetResponse.Data, nil +} + +type TbkSpreadGetResponse struct { + TbkSpreadGetResponse struct { + Results struct { + TbkSpread []struct { + Content string `json:"content"` + ErrMsg string `json:"err_msg"` + } `json:"tbk_spread"` + } `json:"results"` + TotalResults int `json:"total_results"` + RequestID string `json:"request_id"` + } `json:"tbk_spread_get_response"` +} + +// 长链转短链 https://open.taobao.com/api.htm?spm=a219a.7386797.0.0.723f669aQgtoLl&source=search&docId=27832&docType=2 +func TbkSpreadGet(url, session string) (string, error) { + paramMap := map[string]string{ + "app_key": Constants.AlimamaKey, + "method": Constants.AlimamaTbkSpreadGet, + "session": session, + "format": "json", + "v": "2.0", + "sign_method": "md5", + "timestamp": time.Now().Format("2006-01-02 15:04:05"), + "requests": `[{"url":"` + url + `"}]`, + } + var bodyByte, err = apply(Constants.AlimamaTbkSpreadGet, paramMap) + if err != nil { + return "", err + } + var respData = TbkSpreadGetResponse{} + err = json.Unmarshal(*bodyByte, &respData) + if err != nil { + return "", err + } + if len(respData.TbkSpreadGetResponse.Results.TbkSpread) > 0 { + return respData.TbkSpreadGetResponse.Results.TbkSpread[0].Content, nil + } + return "", nil +} + func apply(api string, params map[string]string) (*[]byte, error) { paramMap := map[string]string{ "app_key": Constants.AlimamaKey, @@ -387,9 +562,9 @@ func createSign(paramMap map[string]string) string { sign := Constants.AlimamaSecret var paramKeySlice []string - for k, v := range paramMap { - //&& k != "session" - if k != "" && v != "" { + for k, _ := range paramMap { + // && k != "session" + if k != "" { paramKeySlice = append(paramKeySlice, k) } }