diff --git a/platformapi/jdshopapi/store_page_test.go b/platformapi/jdshopapi/store_page_test.go index fdd08a06..068eb90c 100644 --- a/platformapi/jdshopapi/store_page_test.go +++ b/platformapi/jdshopapi/store_page_test.go @@ -3,16 +3,13 @@ package jdshopapi import ( "crypto/aes" "crypto/cipher" - "crypto/md5" - "encoding/binary" + "encoding/base64" "fmt" + "git.rosy.net.cn/baseapi/utils" "math" "sort" "strings" "testing" - "time" - - "git.rosy.net.cn/baseapi/utils" ) func TestCreateShopCategory(t *testing.T) { @@ -105,12 +102,11 @@ func TestTryGetCookie(t *testing.T) { } func TestAAADS(t *testing.T) { - now := time.Now().Unix() - fmt.Println(utils.Int64ToStr(now)) - result := md5.Sum([]byte("2108849168000062" + utils.Int64ToStr(now))) - chanIndex := int(int64(binary.LittleEndian.Uint32(result[8:])) % int64(2000)) - - fmt.Println(chanIndex) + key, _ := base64.StdEncoding.DecodeString("DW+rL8hlSFt65gUnDXW13A==") + data, _ := base64.StdEncoding.DecodeString("Zh1/Gnb3GWkt7slPocCh5EjgfCrivUMrEjQZT+ND2fsDZYGXIny8NazZJ7UA8DgMpuJf9vBPlf3C8l8oT7aYebJMhAEx8kRFdQip8yEFqbzTfHfnNnMsI4Wkh1Ij06iLiZITnPUdWsuHjwI7BKKmZFDloiOkn3p+gtACFThm7B03SfPmvOdokdIQvDa1nEzPL/2DkfhAwDPxzgGHI0HaAQ==") + iv, _ := base64.StdEncoding.DecodeString("BBYl0UZhgnV59eP6MkxeeQ==") + result, _ := Decrypt(data, key, iv) + fmt.Println(string(result)) } func Decrypt(decryptBytes, key, iv []byte) ([]byte, error) { @@ -205,7 +201,7 @@ func TestUpdateBasic(t *testing.T) { func TestUpdateExpand(t *testing.T) { str := "https://wx.jxc4.com/jxcsRegist?mobile=15520595380" - fmt.Println(str[:strings.LastIndex(str,"?")]) + fmt.Println(str[:strings.LastIndex(str, "?")]) } func TestCreateGisFence(t *testing.T) { diff --git a/platformapi/mtunionapi/mtunionapi_test.go b/platformapi/mtunionapi/mtunionapi_test.go index 4214fb92..31b31ce3 100644 --- a/platformapi/mtunionapi/mtunionapi_test.go +++ b/platformapi/mtunionapi/mtunionapi_test.go @@ -36,7 +36,7 @@ func TestMiniCode(t *testing.T) { } func TestGenerateLink(t *testing.T) { - result, err := api.GenerateLink(2, 1, strings.ToLower("12521")) + result, err := api.GenerateLink(2, 2, strings.ToLower("9999999999999")) if err != nil { t.Fatal(err) } diff --git a/platformapi/mtunionapi/order_test.go b/platformapi/mtunionapi/order_test.go index 39a143bb..4e6da67b 100644 --- a/platformapi/mtunionapi/order_test.go +++ b/platformapi/mtunionapi/order_test.go @@ -14,7 +14,7 @@ func TestOrderList(t *testing.T) { } func TestCouponList(t *testing.T) { - result, err := api.CouponList(4, utils.Str2Time("2021-03-19").Unix(), utils.Str2Time("2021-03-25").Unix(), 1, "000000001") + result, err := api.CouponList(4, utils.Str2Time("2021-04-18").Unix(), utils.Str2Time("2021-04-23").Unix(), 1, "9999999999999") if err != nil { t.Fatal(err) } diff --git a/platformapi/tbunionapi/sku.go b/platformapi/tbunionapi/sku.go new file mode 100644 index 00000000..cb4976c3 --- /dev/null +++ b/platformapi/tbunionapi/sku.go @@ -0,0 +1,17 @@ +package tbunionapi + +import "git.rosy.net.cn/baseapi/utils" + +//物料精选 +func (a *API) OptimusMaterial(materialID, adzoneID, pageNo, pageSize int) (activityInfoGetResult *ActivityInfoGetResult, err error) { + result, err := a.AccessAPI("taobao.tbk.dg.optimus.material", false, map[string]interface{}{ + "material_id": materialID, + "adzone_id": adzoneID, + "page_no": pageNo, + "page_size": pageSize, + }) + if err == nil { + utils.Map2StructByJson(result, &activityInfoGetResult, false) + } + return activityInfoGetResult, err +} diff --git a/platformapi/tbunionapi/sku_test.go b/platformapi/tbunionapi/sku_test.go new file mode 100644 index 00000000..916fe433 --- /dev/null +++ b/platformapi/tbunionapi/sku_test.go @@ -0,0 +1,14 @@ +package tbunionapi + +import ( + "git.rosy.net.cn/baseapi/utils" + "testing" +) + +func TestOptimusMaterial(t *testing.T) { + result, err := api.OptimusMaterial(13366, 111339100149, 1, 20) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} diff --git a/platformapi/tbunionapi/tbunionapi.go b/platformapi/tbunionapi/tbunionapi.go new file mode 100644 index 00000000..0b3fbd3a --- /dev/null +++ b/platformapi/tbunionapi/tbunionapi.go @@ -0,0 +1,141 @@ +package tbunionapi + +import ( + "crypto/md5" + "encoding/json" + "fmt" + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/platformapi" + "git.rosy.net.cn/baseapi/utils" + "net/http" + "sort" + "strings" + "time" +) + +const ( + prodURL = "http://gw.api.taobao.com/router/rest" + testURL = "http://gw.api.tbsandbox.com/router/rest" + sigKey = "sign" +) + +type API struct { + platformapi.APICookie + + appKey string + appSecret string + client *http.Client + config *platformapi.APIConfig +} + +func New(appKey, appSecret string, config ...*platformapi.APIConfig) *API { + curConfig := platformapi.DefAPIConfig + if len(config) > 0 { + curConfig = *config[0] + } + return &API{ + appKey: appKey, + appSecret: appSecret, + client: &http.Client{Timeout: curConfig.ClientTimeout}, + config: &curConfig, + } +} + +func (a *API) signParam(params map[string]interface{}) (sig string) { + var valueList []string + for k, v := range params { + if k != sigKey { + if str := fmt.Sprint(v); str != "" { + valueList = append(valueList, fmt.Sprintf("%s%s", k, str)) + } + } + } + sort.Sort(sort.StringSlice(valueList)) + valueList = append(valueList, fmt.Sprintf("%s", a.appSecret)) + var valueList2 = make([]string, len(valueList)+1) + at := copy(valueList2, valueList[:0]) + at += copy(valueList2[at:], []string{a.appSecret}) + copy(valueList2[at:], valueList[0:]) + sig = strings.Join(valueList2, "") + binSig := md5.Sum([]byte(sig)) + sig = fmt.Sprintf("%X", binSig) + return sig +} + +func (a *API) AccessAPI(action string, isPost bool, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) { + params := make(map[string]interface{}) + params["app_key"] = a.appKey + params["method"] = action + params["sign_method"] = "md5" + params["timestamp"] = utils.Time2Str(time.Now()) + params["v"] = "2.0" + params["format"] = "json" + params = utils.MergeMaps(params, bizParams) + signStr := a.signParam(params) + params[sigKey] = signStr + //fullURL := utils.GenerateGetURL(prodURL, action, nil) + err = platformapi.AccessPlatformAPIWithRetry(a.client, + func() *http.Request { + var request *http.Request + if !isPost { + request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(prodURL, "", params), nil) + } else { + request, _ = http.NewRequest(http.MethodPost, prodURL, strings.NewReader(utils.Map2URLValues(params).Encode())) + } + return request + }, + a.config, + func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) { + if jsonResult1 == nil { + return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil") + } + if err == nil { + if jsonResult1["error_response"] != nil { + errLevel = platformapi.ErrLevelGeneralFail + err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["error_response"].(map[string]interface{})["code"]))) + baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + } + retVal = jsonResult1 + } + return errLevel, err + }) + return retVal, err +} + +func (a *API) AccessStorePage(fullURL string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) { + if a.GetCookieCount() == 0 { + return nil, fmt.Errorf("需要设置Store Cookie才能使用此方法") + } + data, _ := json.Marshal(bizParams) + err = platformapi.AccessPlatformAPIWithRetry(a.client, + func() *http.Request { + var request *http.Request + if isPost { + request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data))) + request.Header.Set("Content-Type", "application/json;charset=utf-8") + } else { + request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil) + } + a.FillRequestCookies(request) + return request + }, + a.config, + func(response *http.Response, bodyStr string, jsonResult1 map[string]interface{}) (errLevel string, err error) { + if jsonResult1 == nil { + return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil") + } + if strings.Contains(bodyStr, "登录") || strings.Contains(bodyStr, "访问的内容") { + return platformapi.ErrLevelRecoverableErr, fmt.Errorf("cookie可能过期了!") + } + if err == nil { + if jsonResult1["error_response"] != nil { + errLevel = platformapi.ErrLevelGeneralFail + err = utils.NewErrorCode(jsonResult1["error_response"].(map[string]interface{})["zh_desc"].(string), jsonResult1["error_response"].(map[string]interface{})["code"].(string)) + baseapi.SugarLogger.Debugf("jdeclp AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + } + retVal = jsonResult1 + } + return errLevel, err + }) + return retVal, err +} diff --git a/platformapi/tbunionapi/tbunionapi_test.go b/platformapi/tbunionapi/tbunionapi_test.go new file mode 100644 index 00000000..79d66ef2 --- /dev/null +++ b/platformapi/tbunionapi/tbunionapi_test.go @@ -0,0 +1,22 @@ +package tbunionapi + +import ( + "git.rosy.net.cn/baseapi" + "go.uber.org/zap" +) + +var ( + api *API + sugarLogger *zap.SugaredLogger +) + +func init() { + logger, _ := zap.NewDevelopment() + sugarLogger = logger.Sugar() + baseapi.Init(sugarLogger) + + api = New("32724809", "2c2bce02eab860d486f68aa59a0127d9") + api.SetCookieWithStr(` + t=948ba2dc83b4b34e02a9d87212e574a8; cna=A9cHGZw1jjgCAd3tly+uuYIG; account-path-guide-s1=true; 1172080007-payment-time=true; xlly_s=1; cookie2=1a1a1b2e0fdeaea7c73f238a708b2bd5; v=0; _tb_token_=7e385ebeee37e; alimamapwag=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgNi4xOyBXaW42NDsgeDY0KSBBcHBsZVdlYktpdC81MzcuMzYgKEtIVE1MLCBsaWtlIEdlY2tvKSBDaHJvbWUvOTAuMC40NDMwLjg1IFNhZmFyaS81MzcuMzY%3D; cookie32=5887051fc70ab30be7a2655652e82dc6; alimamapw=RkAFUwICU1JQXAA%2BBw0GVlcDAAYGBwIEXVYBAgZUBA9RUQNTAABQUQZXAQY%3D; cookie31=MTE3MjA4MDAwNyxxcTM2NTE3NjI4NixmZW5nLnNoaUByb3N5Lm5ldC5jbixUQg%3D%3D; login=Vq8l%2BKCLz3%2F65A%3D%3D; isg=BCEhHCMG5XUeJUmGx-aU4ApDMO07zpXAI2znAoP2HSiH6kG8yx6lkE8oSB7sOS34; l=eB_opK6mjKn7XdlbBOfanurza77OSIRYYuPzaNbMiOCPOw1B5qD5W615yR86C3GVh6DMR3-XtET6BeYBqQAonxvTabLUOWMmn; tfstk=cqS5BpaGtbcW084E4TwqzMaD6QtdwgM6dzOlFNn8Yx8HAC1cwcR6wP_n9mL9h +`) +} diff --git a/platformapi/tbunionapi/union.go b/platformapi/tbunionapi/union.go new file mode 100644 index 00000000..9041fa7c --- /dev/null +++ b/platformapi/tbunionapi/union.go @@ -0,0 +1,122 @@ +package tbunionapi + +import ( + "git.rosy.net.cn/baseapi/utils" + "time" +) + +const ( + JxAdzoneID = 111339100149 +) + +//创建推广位 +func (a *API) AdzoneCreate(sid int64, adzoneName string) (err error) { + _, err = a.AccessAPI("taobao.tbk.adzone.create", false, map[string]interface{}{ + "site_id": sid, + "adzone_name": adzoneName, + }) + return err +} + +type ActivityInfoGetResult struct { + TbkActivityInfoGetResponse struct { + Data struct { + ClickURL string `json:"click_url"` + ShortClickURL string `json:"short_click_url"` + WxMiniprogramPath string `json:"wx_miniprogram_path"` + WxQrcodeURL string `json:"wx_qrcode_url"` + } `json:"data"` + RequestID string `json:"request_id"` + } `json:"tbk_activity_info_get_response"` +} + +//官方活动转链 +func (a *API) ActivityInfoGet(uid, actMaterialID string, adzoneID int64) (activityInfoGetResult *ActivityInfoGetResult, err error) { + result, err := a.AccessAPI("taobao.tbk.activity.info.get", false, map[string]interface{}{ + "union_id": uid, + "activity_material_id": actMaterialID, + "adzone_id": adzoneID, + }) + if err == nil { + utils.Map2StructByJson(result, &activityInfoGetResult, false) + } + return activityInfoGetResult, err +} + +//订单查询 +func (a *API) AllOrders(beginTime, endTime string) (activityInfoGetResult *ActivityInfoGetResult, err error) { + result, err := a.AccessAPI("taobao.tbk.order.details.get", false, map[string]interface{}{ + "start_time": beginTime, + "end_time": endTime, + }) + if err == nil { + utils.Map2StructByJson(result, &activityInfoGetResult, false) + } + return activityInfoGetResult, err +} + +//新用户订单查询 +func (a *API) NewUserAllOrders(actID string) (activityInfoGetResult *ActivityInfoGetResult, err error) { + result, err := a.AccessAPI("taobao.tbk.dg.newuser.order.get", false, map[string]interface{}{ + "activity_id": actID, + "start_time": "2021-04-20 00:00:00", + "end_time": "2021-04-25 00:00:00", + }) + if err == nil { + utils.Map2StructByJson(result, &activityInfoGetResult, false) + } + return activityInfoGetResult, err +} + +//返利订单查询 +func (a *API) RebateOrderGet(beginTime string) (activityInfoGetResult *ActivityInfoGetResult, err error) { + result, err := a.AccessAPI("taobao.tbk.rebate.order.get", false, map[string]interface{}{ + "fields": "tb_trade_parent_id,tb_trade_id,num_iid,item_title,item_num,price,pay_price,seller_nick,seller_shop_title,commission,commission_rate,unid,create_time,earning_time", + "start_time": beginTime, + "span": 600, + }) + if err == nil { + utils.Map2StructByJson(result, &activityInfoGetResult, false) + } + return activityInfoGetResult, err +} + +type GatewayUnionpubResult struct { + Eventbusourceplatform string `json:"eventBuSourcePlatform"` + Pagesourcebu string `json:"pageSourceBu"` + Eventenname string `json:"eventEnName"` + Pagename string `json:"pageName"` + Pageurl string `json:"pageUrl"` + Pagestarttime string `json:"pageStartTime"` + Pageid string `json:"pageId"` + Pageendtime string `json:"pageEndTime"` + Pagepicturl string `json:"pagePictUrl"` + UdfTempStore struct { + } `json:"udf_temp_store"` + Terminaltype string `json:"terminalType"` + Pageurlmd5 string `json:"pageUrlMd5"` + Busourceplatform string `json:"buSourcePlatform"` + Eventperiodtype int `json:"eventPeriodType"` + Eventid string `json:"eventId"` + Lensid string `json:"lensId"` + Tobizshowforcetop int `json:"toBizShowForceTop"` + Pagetracetype string `json:"pageTraceType"` + Eventname string `json:"eventName"` +} + +//查询饿了么活动(扒的) +func (a *API) GatewayUnionpub() (gatewayUnionpubResult []*GatewayUnionpubResult, err error) { + result, err := a.AccessStorePage("https://pub.alimama.com/openapi/param2/1/gateway.unionpub/xt.entry.json", map[string]interface{}{ + "floorId": 38766, + "t": time.Now().UnixNano(), + "_tb_token_": "7e385ebeee37e", + "pageSize": 20, + "refpid": "mm_1172080007_0_0", + "pageNum": 0, + "variableMap": "{\"traceType\":\"4\",\"terminalType\":\"\",\"filterQuery\":\"\"}", + }, false) + if err == nil { + utils.Map2StructByJson(result["data"].(map[string]interface{})["resultList"], &gatewayUnionpubResult, false) + } + return gatewayUnionpubResult, err +} diff --git a/platformapi/tbunionapi/union_test.go b/platformapi/tbunionapi/union_test.go new file mode 100644 index 00000000..ccc61b45 --- /dev/null +++ b/platformapi/tbunionapi/union_test.go @@ -0,0 +1,54 @@ +package tbunionapi + +import ( + "git.rosy.net.cn/baseapi/utils" + "testing" +) + +func TestAdzoneCreate(t *testing.T) { + err := api.AdzoneCreate(2293250207, "999999") + if err != nil { + t.Fatal(err) + } + //t.Log(utils.Format4Output(result, false)) +} + +func TestActivityInfoGet(t *testing.T) { + result, err := api.ActivityInfoGet("999999", "20150318020002597", 999999) + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestAllOrders(t *testing.T) { + result, err := api.AllOrders("2021-04-23 00:00:00", "2021-04-23 23:59:59") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestNewUserAllOrders(t *testing.T) { + result, err := api.NewUserAllOrders("20150318020002597") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestRebateOrderGet(t *testing.T) { + result, err := api.RebateOrderGet("2021-04-05 09:10:00") + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +} + +func TestGatewayUnionpub(t *testing.T) { + result, err := api.GatewayUnionpub() + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) +}