This commit is contained in:
苏尹岚
2020-10-20 17:24:30 +08:00
parent e09ffd1c92
commit c162a20312
3 changed files with 96 additions and 12 deletions

View File

@@ -0,0 +1,65 @@
package mtmemberapi
import (
"fmt"
"net/http"
"strings"
"git.rosy.net.cn/baseapi"
"git.rosy.net.cn/baseapi/platformapi"
"git.rosy.net.cn/baseapi/utils"
)
const (
prodURL = "https://wechat.chengquan.cn"
)
type API struct {
client *http.Client
config *platformapi.APIConfig
}
func New(config ...*platformapi.APIConfig) *API {
curConfig := platformapi.DefAPIConfig
if len(config) > 0 {
curConfig = *config[0]
}
return &API{
client: &http.Client{Timeout: curConfig.ClientTimeout},
config: &curConfig,
}
}
func (a *API) AccessAPI(action string, url string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
fullURL := utils.GenerateGetURL(url, action, nil)
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(bizParams).Encode()))
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
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 utils.MustInterface2Int64(jsonResult1["errorCode"]) != http.StatusOK {
errLevel = platformapi.ErrLevelGeneralFail
err = utils.NewErrorCode(utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["errorMsg"])), utils.Int64ToStr(utils.MustInterface2Int64(jsonResult1["errorCode"])))
baseapi.SugarLogger.Debugf("mtmember AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
}
retVal = jsonResult1
}
return errLevel, err
})
return retVal, err
}
func (a *API) RechargeExchange(account int, shortLink string) (err error) {
_, err = a.AccessAPI("uniteExchange/rechargeExchange", prodURL, map[string]interface{}{
"account": account,
"shortLink": shortLink,
})
return err
}

View File

@@ -0,0 +1,28 @@
package mtmemberapi
import (
"testing"
"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() //总店
}
func TestRechargeExchange(t *testing.T) {
err := api.RechargeExchange(15, "a")
if err != nil {
t.Fatal(err)
}
// t.Log(utils.Format4Output(result, false))
}