diff --git a/platformapi/fnpsapi/fnpsapi.go b/platformapi/fnpsapi/fnpsapi.go index eeb874c3..bea0a005 100644 --- a/platformapi/fnpsapi/fnpsapi.go +++ b/platformapi/fnpsapi/fnpsapi.go @@ -4,9 +4,13 @@ import ( "crypto/md5" "encoding/json" "fmt" + "math/rand" "net/http" + "net/url" "sort" "strings" + "sync" + "time" "git.rosy.net.cn/baseapi" "git.rosy.net.cn/baseapi/platformapi" @@ -16,18 +20,26 @@ import ( const ( sigKey = "signature" - TestURL = "https://exam-anubis.ele.me/anubis-webapi/v2" - URL = "https://open-anubis.ele.me/anubis-webapi/v2" + TestURL = "https://exam-anubis.ele.me/anubis-webapi" + URL = "https://open-anubis.ele.me/anubis-webapi" + tokenAction = "get_access_token" ) type API struct { accessToken string appID string appSecret string + locker sync.RWMutex client *http.Client config *platformapi.APIConfig } +func (a *API) SetToken(token string) { + a.locker.Lock() + defer a.locker.Unlock() + a.accessToken = token +} + func New(accessToken, appID, appSecret string, config ...*platformapi.APIConfig) *API { curConfig := platformapi.DefAPIConfig if len(config) > 0 { @@ -47,34 +59,42 @@ func (a *API) signParam(params map[string]interface{}) (sig string) { for k, v := range params { if k != sigKey { if str := fmt.Sprint(v); str != "" { - valueList = append(valueList, fmt.Sprintf("%s%s", k, 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) + valueList = append(valueList, fmt.Sprintf("secret_key=%s", a.appSecret)) + sig = strings.Join(valueList, "&") + binSig := md5.Sum([]byte(url.QueryEscape(sig))) + sig = fmt.Sprintf("%x", binSig) return sig } func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}, isPost bool) (retVal map[string]interface{}, err error) { - data, _ := json.Marshal(bizParams) + params := make(map[string]interface{}) + params["salt"] = GetSalt() + params["app_id"] = a.appID + if action != tokenAction { + if a.accessToken == "" { + params["access_token"] = a.accessToken + } + data, _ := json.Marshal(bizParams) + params["data"] = string(data) + } + signStr := a.signParam(params) + params[sigKey] = signStr + data, _ := json.Marshal(params) fullURL := utils.GenerateGetURL(url, action, nil) 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") } else { - request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, bizParams), nil) + request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil) } + request.Header.Set("Content-Type", "application/json") return request }, a.config, @@ -86,7 +106,7 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac if utils.MustInterface2Int64(jsonResult1["code"]) != 200 { errLevel = platformapi.ErrLevelGeneralFail err = utils.NewErrorCode(jsonResult1["msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["code"]))) - baseapi.SugarLogger.Debugf("ejiay AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) + baseapi.SugarLogger.Debugf("fnps AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true)) } retVal = jsonResult1 } @@ -94,3 +114,22 @@ func (a *API) AccessAPI(action string, url string, bizParams map[string]interfac }) return retVal, err } + +func GetSalt() (salt int) { + rand.Seed(time.Now().UnixNano()) + return rand.Intn(8999) + 1000 +} + +type TokenInfo struct { + AccessToken string `json:"access_token"` + AppID string `json:"app_id"` + ExpireTime int64 `json:"expire_time"` +} + +func (a *API) GetAccessToken() (tokenInfo *TokenInfo, err error) { + result, err := a.AccessAPI(tokenAction, TestURL, nil, false) + if err == nil { + utils.Map2StructByJson(result["data"], &tokenInfo, false) + } + return tokenInfo, err +} diff --git a/platformapi/fnpsapi/fnpsapi_test.go b/platformapi/fnpsapi/fnpsapi_test.go index 02157d42..073bd8f0 100644 --- a/platformapi/fnpsapi/fnpsapi_test.go +++ b/platformapi/fnpsapi/fnpsapi_test.go @@ -1,7 +1,10 @@ package fnpsapi import ( + "testing" + "git.rosy.net.cn/baseapi" + "git.rosy.net.cn/baseapi/utils" "go.uber.org/zap" ) @@ -14,5 +17,13 @@ func init() { logger, _ := zap.NewDevelopment() sugarLogger = logger.Sugar() baseapi.Init(sugarLogger) - // api = New("9ad9cf5fc8b140b19a1dbbb700b47676kown", "D5E8352BE0786ED16F77B4548F62F09A", "71e1061ac2f246f6ac27efb900edba12") + api = New("", "6a3e2073-1850-413b-9eb7-6c342ec36e1c", "a8248088-a742-4c33-a0db-03aeae00ca7d") +} + +func TestGetAccessToken(t *testing.T) { + result, err := api.GetAccessToken() + if err != nil { + t.Fatal(err) + } + t.Log(utils.Format4Output(result, false)) }