This commit is contained in:
苏尹岚
2021-04-14 15:37:49 +08:00
parent a472b60ae0
commit 8997436415
2 changed files with 80 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ package mtunionapi
import (
"crypto/md5"
"encoding/json"
"fmt"
"net/http"
"sort"
@@ -15,9 +16,13 @@ import (
const (
prodURL = "https://runion.meituan.com"
sigKey = "sign"
ActTypeQB = 1 //券包推广
)
type API struct {
platformapi.APICookie
appKey string
appSecret string
client *http.Client
@@ -93,6 +98,44 @@ func (a *API) AccessAPI(action string, isPost bool, bizParams map[string]interfa
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
}
//https://union.meituan.com/v2/apiDetail?id=8
//https://runion.meituan.com/generateLink
func (a *API) GenerateLink(actID int, userID string) (url string, err error) {
@@ -102,3 +145,27 @@ func (a *API) GenerateLink(actID int, userID string) (url string, err error) {
})
return url, err
}
type ActivityListResult struct {
ID int `json:"id"`
ActName string `json:"actName"`
ActDes string `json:"actDes"`
URL string `json:"url"`
ActRule string `json:"actRule"`
Ratio string `json:"ratio"`
DateBound string `json:"dateBound"`
ActSrc string `json:"actSrc"`
}
//https://union.meituan.com/api/activity/list
func (a *API) ActivityList(actType, limit, offset int) (activityListResult []*ActivityListResult, err error) {
result, err := a.AccessStorePage("https://union.meituan.com/api/activity/list", map[string]interface{}{
"actType": actType,
"limit": limit,
"offset": offset,
}, true)
if err == nil {
utils.Map2StructByJson(result["data"].(map[string]interface{})["dataList"], &activityListResult, false)
}
return activityListResult, err
}

View File

@@ -5,6 +5,7 @@ import (
"testing"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/utils"
"go.uber.org/zap"
)
@@ -19,6 +20,10 @@ func init() {
baseapi.Init(sugarLogger)
api = New("b6481f92b47918cd6e42e7ea4fae6084", "84d390777ddf691ff092e744ba26bfdd")
api.SetCookieWithStr(`
uuid=e8034a4d222c4b51b81c.1574126611.1.0.0; _ga=GA1.2.827950563.1574128001; _lxsdk_cuid=16eb02a8a02c8-0a92cb9af9798c-3d375b01-15f900-16eb02a8a02c8; _lxsdk=16eb02a8a02c8-0a92cb9af9798c-3d375b01-15f900-16eb02a8a02c8; t_lxid=1719bfe9d5e30-0cf08957b60ff-3d375b01-15f900-1719bfe9d5fc8-tid; e_u_id_3299326472=e5ae16afe444349d24af7d33b66620a1; mtcdn=K; u=74293087; n=%E6%9A%96%E6%B4%8B%E6%B4%8B780; lt=-MCY1pDT4PeX0lQfwwtk0fopfp4AAAAASg0AAGrpsRgMgRyfrItJI72VozmV508m0TpAAxLhJPZqKBxw3xeClF25NKbWolIKKQIB-g; mt_c_token=-MCY1pDT4PeX0lQfwwtk0fopfp4AAAAASg0AAGrpsRgMgRyfrItJI72VozmV508m0TpAAxLhJPZqKBxw3xeClF25NKbWolIKKQIB-g; token=-MCY1pDT4PeX0lQfwwtk0fopfp4AAAAASg0AAGrpsRgMgRyfrItJI72VozmV508m0TpAAxLhJPZqKBxw3xeClF25NKbWolIKKQIB-g; lsu=
`)
}
func TestGenerateLink(t *testing.T) {
@@ -28,3 +33,11 @@ func TestGenerateLink(t *testing.T) {
}
// t.Log(utils.Format4Output(result, false))
}
func TestActivityList(t *testing.T) {
result, err := api.ActivityList(1, 10, 0)
if err != nil {
t.Fatal(err)
}
t.Log(utils.Format4Output(result, false))
}