This commit is contained in:
suyl
2021-07-16 16:52:46 +08:00
parent ddea5f5228
commit 4f68cf371c
2 changed files with 32 additions and 18 deletions

View File

@@ -12,7 +12,7 @@ import (
)
const (
prodURL = "https://api.tibiot.cn/api"
prodURL = "https://api.tibiot.cn/api/v1"
signKey = "password"
)
@@ -30,41 +30,39 @@ var (
type API struct {
platformapi.APICookie
appID string
appKey string
client *http.Client
config *platformapi.APIConfig
username string
password string
client *http.Client
config *platformapi.APIConfig
}
func New(appID, appKey string, config ...*platformapi.APIConfig) *API {
func New(username, password string, config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig
if len(config) > 0 {
curConfig = *config[0]
}
return &API{
appID: appID,
appKey: appKey,
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
username: username,
password: password,
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
}
}
func (a *API) signParams(apiParams map[string]interface{}) string {
return fmt.Sprintf("%x", md5.Sum([]byte(a.appID+a.appKey+apiParams["timestamp"].(string))))
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%x", md5.Sum([]byte(a.username+apiParams["tkey"].(string)))))))
}
func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVal interface{}, err error) {
func (a *API) AccessAPI(url string, apiParams map[string]interface{}) (retVal interface{}, err error) {
params := utils.MergeMaps(map[string]interface{}{
"app_id": a.appID,
"method": apiName,
"username": a.username,
}, apiParams)
fullURL := utils.GenerateGetURL(prodURL, url, nil)
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
params["timestamp"] = utils.Int64ToStr(time.Now().Unix())
params["tkey"] = time.Now().Format("20060102150405")
sign := a.signParams(params)
params[signKey] = sign
fullURL := prodURL
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
return request
@@ -91,3 +89,10 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVa
})
return retVal, err
}
func (a *API) GetCardInfo(iccid string) (err error) {
a.AccessAPI("card/getCardInfo", map[string]interface{}{
"iccid": iccid,
})
return err
}

View File

@@ -3,6 +3,7 @@ package tibiotapi
import (
"git.rosy.net.cn/baseapi"
"go.uber.org/zap"
"testing"
)
var (
@@ -15,5 +16,13 @@ func init() {
sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger)
api = New("1000", "rfBd56ti2SMtYvSg")
api = New("ruoxikeji", "Ruoxi@369")
}
func TestGetCardInfo(t *testing.T) {
err := api.GetCardInfo("")
if err != nil {
t.Fatalf("PrintMsg return error:%v", err)
}
//baseapi.SugarLogger.Debug(result)
}