aa
This commit is contained in:
88
platformapi/jdunionapi/jdunionapi.go
Normal file
88
platformapi/jdunionapi/jdunionapi.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package jdunionapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
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) 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)))
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(fullURL, "", bizParams), nil)
|
||||
request.Header.Set("accept", "application/json, text/plain, */*")
|
||||
//request.Header.Set("accept-encoding", "gzip, deflate, br")
|
||||
request.Header.Set("accept-language", "zh-CN,zh;q=0.9")
|
||||
request.Header.Set("origin", "https://union.jd.com")
|
||||
request.Header.Set("referer", "https://union.jd.com")
|
||||
request.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36")
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
func (a *API) ListActivitys() (result map[string]interface{}, err error) {
|
||||
result, err = a.AccessStorePage("https://api.m.jd.com/api", map[string]interface{}{
|
||||
"functionId": "listActivitys",
|
||||
"appid": "unionpc",
|
||||
"_": time.Now().UnixNano(),
|
||||
"loginType": 3,
|
||||
"body": "{\"funName\":\"listActivitys\",\"param\":{\"activityStatus\":\"3\"},\"page\":{\"pageNo\":1,\"pageSize\":20}}",
|
||||
}, false)
|
||||
return result, err
|
||||
}
|
||||
32
platformapi/jdunionapi/jdunionapi_test.go
Normal file
32
platformapi/jdunionapi/jdunionapi_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package jdunionapi
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
|
||||
api = New("b6481f92b47918cd6e42e7ea4fae6084", "84d390777ddf691ff092e744ba26bfdd")
|
||||
api.SetCookieWithStr(`
|
||||
__jdu=16193999982702078345144; shshshfp=dee09226776379a93b45b81da4715c98; shshshfpa=972f47df-9252-dc1a-1d6d-40594ce0501b-1619400562; shshshfpb=msX4%2B2n384blCOGusOxGDvQ%3D%3D; __jdv=56585130|baidu|-|organic|not set|1619430075643; language=zh_CN; _base_=YKH2KDFHMOZBLCUV7NSRBWQUJPBI7JIMU5R3EFJ5UDHJ5LCU7R2NILKK5UJ6GLA2RGYT464UKXAI4Z6HPCTN4UQM3WHVQ4ENFP57OC2NCIWTST4KDNCWZES6EV2TD5XDTCNE6YVKRXISVWNQHZDBO6QEKYXMXGPYGVEOCQCSG4SOQWCP5WPWO6EFS7HEHMRWVKBRVHB33TFD4AUHV3TOJD7MGBX53BNNP2E6MSSOORYGL5H2GYFRURR5ACFJ54GH7PTHFL22YL3C6ODFYFTWGV5UAWCX5C6J372TDCL6LDNEERERTGC3KTG7LVQKL6T7DHP7K6V7RIDKHVQAQIWW3S2YBFMIMTF6SWAFAQ52UF5TATP7KZIQ; 3AB9D23F7A4B3C9B=CKENC5MAUU744ZQDKVRY2L7UIJ23PJRDYJ6HF3ZMQHC5FFD46FMUQXRINFCOC4SKHL5MU3PPNPP4C4AX5ZYBXJ7LZ4; TrackID=1-4fAVea4KplSma_hIJGsw5phupUEFkQdSsJg9ZFa-npEreLy9OChx9os40_wvxLSWCobRme6E77RFqq1TCAH58Y_FyHMCvepCTD76DkrWoU; pinId=j0UlOyGus3AVS-ADVdcgUg; pin=jingxicaishi; unick=jingxicaishi; ceshi3.com=000; _tp=kapINs%2BaezZAzjGD9fk%2FCQ%3D%3D; _pst=jingxicaishi; __jda=209449046.16193999982702078345144.1619399998.1620289515.1620375213.10; __jdc=209449046; __jdb=209449046.40.16193999982702078345144|10.1620375213; RT="z=1&dm=jd.com&si=a7jrd7a068o&ss=koe1kc1r&sl=11&tt=6ei&ld=7fz9&nu=9181cc8dff125f3f43c49c7b30f258ce&cl=7f1f"; thor=3E6120B5171447C2B8036184D5BAA67EC585CCFCAC8414C7AD68EC9ABD97AFAA229AAE4F2DAC096B6CE4C2630A264BEA68EAFB7EB88642B7733632ADCD9A4D80D73FC2A5E83E4DD05173EEA64EA5E3C34F10C695334CED27944FAB2E31EED2D9093DB30601C620F363A3F467018B4FDCB82B5DD76A21EBD7FD1F5C38582A58B3E11CD422682E1347EA3A24C32CB1902C
|
||||
`)
|
||||
}
|
||||
|
||||
func TestListActivitys(t *testing.T) {
|
||||
result, err := api.ListActivitys()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
@@ -15,9 +15,8 @@ func TestGoodsCatsGet(t *testing.T) {
|
||||
|
||||
func TestGoodsSearch(t *testing.T) {
|
||||
result, err := api.GoodsSearch(&GoodsSearchParam{
|
||||
ActivityTags: []int{10564},
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
Page: 1,
|
||||
PageSize: 10,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestResourceURLGen(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGoodsPromotionURLGen(t *testing.T) {
|
||||
result, err := api.GoodsPromotionURLGen("14501219_204919171", "Y9j2gIZ_GQ1KWDjRwfbZd9rMFl_x9NJ4_JQmOjnZyb5", false)
|
||||
result, err := api.GoodsPromotionURLGen("14501219_204919171", "Y9n2oxrce6hKWDjRwfDZDbjMGeyz1JsC_J7fydjteU", false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ func TestAdzoneCreate(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestActivityInfoGet(t *testing.T) {
|
||||
result, err := api.ActivityInfoGet("999999", "20150318020002597", 999999)
|
||||
result, err := api.ActivityInfoGet("1439B3E07D3911EA881A525400E86DC0", "20150318020002597", 111339100149)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user