1
This commit is contained in:
@@ -1,14 +1,29 @@
|
|||||||
package uuptapi
|
package uuptapi
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
"fmt"
|
||||||
// "fmt"
|
"git.rosy.net.cn/baseapi"
|
||||||
// "testing"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
//)
|
"go.uber.org/zap"
|
||||||
//
|
"testing"
|
||||||
//var (
|
"time"
|
||||||
// api = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e")
|
)
|
||||||
//)
|
|
||||||
|
var (
|
||||||
|
api *API = New("55c4542ae60e4d348edcfc93b06dd302", "76b362c06b1b4baa9e47bab6387a5356", "8d8464e7c9354c1e88a3f5afa2a7922e")
|
||||||
|
sugarLogger *zap.SugaredLogger
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
logger, _ := zap.NewDevelopment()
|
||||||
|
sugarLogger = logger.Sugar()
|
||||||
|
baseapi.Init(sugarLogger)
|
||||||
|
|
||||||
|
//api = New("6705486294797503379", "c1e6c280-e618-4103-9d0a-673bc54fb22e", "5375691", "cabrXQf9eFMVWVYg4hNlwu")
|
||||||
|
//token, _ := api.GetAccessToken()
|
||||||
|
//api.accessToken = token.BusinessDataObj.AccessToken
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
//func TestSignParam(t *testing.T) {
|
//func TestSignParam(t *testing.T) {
|
||||||
// testStr := map[string]interface{}{
|
// testStr := map[string]interface{}{
|
||||||
@@ -25,39 +40,24 @@ package uuptapi
|
|||||||
// fmt.Print(randStr())
|
// fmt.Print(randStr())
|
||||||
//}
|
//}
|
||||||
//
|
//
|
||||||
////计算运单价格
|
//计算运单价格
|
||||||
//func TestGetWaybillPrice(t *testing.T) {
|
func TestGetWaybillPrice(t *testing.T) {
|
||||||
// needPayMoney, priceToken, err := api.GetOrderPrice(&GetOrderPriceReq{
|
|
||||||
// //BaseReqInfo: &BaseReqInfo{
|
needPayMoney, priceToken, err := api.GetOrderPrice(&GetOrderPriceReq{
|
||||||
// //Sign: "A9D4D4380B6012F2E9A00E37243306D4",
|
OriginID: "T00001",
|
||||||
// // NonceStr: "01c3ce7ce7604ab49d12ec69dfbdcfe8",
|
FromAddress: "郑州国际会展中心",
|
||||||
// //Timestamp: "1672998838",
|
ToAddress: "商务外环路17号",
|
||||||
// // OpenID: "8d8464e7c9354c1e88a3f5afa2a7922e",
|
CityName: "郑州市",
|
||||||
// // AppID: "55c4542ae60e4d348edcfc93b06dd302",
|
SendType: "0",
|
||||||
// //},
|
ToLat: "34.784275",
|
||||||
// OriginID: "T00001",
|
ToLng: "113.736445",
|
||||||
// FromAddress: "郑州国际会展中心",
|
FromLat: "34.778494",
|
||||||
// ToAddress: "商务外环路17号",
|
FromLng: "113.736858",
|
||||||
// CityName: "郑州市",
|
})
|
||||||
// SendType: "0",
|
|
||||||
// ToLat: "34.784275",
|
fmt.Println(needPayMoney, priceToken, err)
|
||||||
// ToLng: "113.736445",
|
}
|
||||||
// FromLat: "34.778494",
|
|
||||||
// FromLng: "113.736858",
|
func Test(t *testing.T) {
|
||||||
// })
|
fmt.Println(utils.Int64ToStr(time.Now().Unix() * 1000))
|
||||||
//
|
}
|
||||||
// fmt.Println(needPayMoney, priceToken, err)
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//type testStruct struct {
|
|
||||||
// name string
|
|
||||||
//}
|
|
||||||
//
|
|
||||||
//func Test(t *testing.T) {
|
|
||||||
// var temp testStruct
|
|
||||||
// //var test bool
|
|
||||||
// //test = true
|
|
||||||
// //temp.name = "hdueu"
|
|
||||||
// fmt.Println(temp == testStruct{})
|
|
||||||
// //fmt.Print(utils.IsNil(temp))
|
|
||||||
//}
|
|
||||||
|
|||||||
@@ -1,107 +1,115 @@
|
|||||||
package uuptapi
|
package uuptapi
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
"crypto/md5"
|
||||||
// "crypto/md5"
|
"encoding/json"
|
||||||
// "encoding/json"
|
"fmt"
|
||||||
// "fmt"
|
"git.rosy.net.cn/baseapi/platformapi"
|
||||||
// "git.rosy.net.cn/baseapi/platformapi"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
// "git.rosy.net.cn/baseapi/utils"
|
"math/rand"
|
||||||
// "math/rand"
|
"net/http"
|
||||||
// "net/http"
|
"sort"
|
||||||
// "sort"
|
"strings"
|
||||||
// "strings"
|
"time"
|
||||||
// "time"
|
)
|
||||||
//)
|
|
||||||
//
|
const (
|
||||||
//const (
|
BaseURL = "https://openapi.uupt.com/v2_0"
|
||||||
// BaseURL = "https://openapi.uupt.com/v2_0"
|
ReturnSuccess = "ok"
|
||||||
// ReturnSuccess = "ok"
|
ReturnFail = "fail"
|
||||||
// ReturnFail = "fail"
|
signKey = "sign"
|
||||||
// signKey = "sign"
|
secretKey = "secret"
|
||||||
// secretKey = "secret"
|
UUOpenID = "8d8464e7c9354c1e88a3f5afa2a7922e"
|
||||||
// UUOpenID = "8d8464e7c9354c1e88a3f5afa2a7922e"
|
UUAppID = "55c4542ae60e4d348edcfc93b06dd302"
|
||||||
// UUAppID = "55c4542ae60e4d348edcfc93b06dd302"
|
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
||||||
// letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
)
|
||||||
//)
|
|
||||||
//
|
func (a *API) MakeUURequestHead() map[string]interface{} {
|
||||||
//func (a *API) MakeUURequestHead() map[string]interface{} {
|
requestParam := make(map[string]interface{}, 5)
|
||||||
// requestParam := make(map[string]interface{}, 5)
|
requestParam["sign"] = a.sign
|
||||||
// requestParam["sign"] = a.sign
|
requestParam["timestamp"] = a.timestamp
|
||||||
// requestParam["timestamp"] = a.timestamp
|
requestParam["nonce_str"] = a.noncestr
|
||||||
// requestParam["nonce_str"] = a.noncestr
|
requestParam["openid"] = a.openid
|
||||||
// requestParam["openid"] = a.openid
|
requestParam["appid"] = a.appid
|
||||||
// requestParam["appid"] = a.appid
|
return requestParam
|
||||||
// return requestParam
|
}
|
||||||
//}
|
func New(appID, appKey, openID string, config ...*platformapi.APIConfig) *API {
|
||||||
//func New(appID, appKey, openID string) *API {
|
curConfig := platformapi.DefAPIConfig
|
||||||
// if appID == "" {
|
if len(config) > 0 {
|
||||||
// return nil
|
curConfig = *config[0]
|
||||||
// }
|
}
|
||||||
// return &API{
|
if appID == "" {
|
||||||
// appid: appID,
|
return nil
|
||||||
// appKey: appKey,
|
}
|
||||||
// openid: openID,
|
return &API{
|
||||||
// }
|
appid: appID,
|
||||||
//}
|
appKey: appKey,
|
||||||
//
|
openid: openID,
|
||||||
//func (a *API) signParam(params map[string]interface{}) (sign string) {
|
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||||
// keyValues := make([]string, 0)
|
config: &curConfig,
|
||||||
// for k, v := range params {
|
}
|
||||||
// if k != signKey {
|
}
|
||||||
// if temp := fmt.Sprint(v); temp != "" {
|
|
||||||
// keyValues = append(keyValues, k+"="+temp)
|
func (a *API) signParam(params map[string]interface{}) (sign string) {
|
||||||
// }
|
keyValues := make([]string, 0)
|
||||||
// }
|
for k, v := range params {
|
||||||
// }
|
if k != signKey {
|
||||||
// sort.Sort(sort.StringSlice(keyValues)) //字典升序
|
if temp := fmt.Sprint(v); temp != "" {
|
||||||
// sign = strings.Join(keyValues, "&")
|
keyValues = append(keyValues, k+"="+temp)
|
||||||
// sign += "&key=" + a.appKey //末尾拼接密钥
|
}
|
||||||
// sign = strings.ToUpper(sign) //大写
|
}
|
||||||
// return fmt.Sprintf("%X", md5.Sum([]byte(sign)))
|
}
|
||||||
//}
|
sort.Sort(sort.StringSlice(keyValues)) //字典升序
|
||||||
//
|
sign = strings.Join(keyValues, "&")
|
||||||
//func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
sign += "&key=" + a.appKey //末尾拼接密钥
|
||||||
// bizParams["timestamp"] = utils.Int64ToStr(time.Now().Unix() * 1000)
|
sign = strings.ToUpper(sign) //大写
|
||||||
// bizParams["sign"] = a.signParam(bizParams)
|
return fmt.Sprintf("%X", md5.Sum([]byte(sign)))
|
||||||
// //序列化
|
}
|
||||||
// data, err := json.Marshal(bizParams)
|
|
||||||
// if err != nil {
|
func (a *API) AccessAPI(baseUrl, actionApi, method string, bizParams map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||||
// return nil, err
|
bizParams["timestamp"] = utils.Int64ToStr(time.Now().Unix() * 1000)
|
||||||
// }
|
bizParams["nonce_str"] = randStr()
|
||||||
// //完整请求url
|
bizParams["sign"] = a.signParam(bizParams)
|
||||||
// fullPath := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
//序列化
|
||||||
// //发送请求
|
data, err := json.Marshal(bizParams)
|
||||||
// sendUrl := func() *http.Request {
|
if err != nil {
|
||||||
// var request *http.Request
|
return nil, err
|
||||||
// if method == RequestPost {
|
}
|
||||||
// request, _ = http.NewRequest(http.MethodPost, fullPath, strings.NewReader(string(data)))
|
//完整请求url
|
||||||
// } else {
|
fullPath := utils.GenerateGetURL(baseUrl, actionApi, nil)
|
||||||
// request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
//发送请求
|
||||||
// }
|
sendUrl := func() *http.Request {
|
||||||
// return request
|
var request *http.Request
|
||||||
// }
|
if method == RequestPost {
|
||||||
// //数据解析
|
request, _ = http.NewRequest(http.MethodPost, fullPath, strings.NewReader(string(data)))
|
||||||
// dataMarshal := func(response *http.Response, bodyStr string, jsonResp map[string]interface{}) (errMsg string, err error) {
|
} else {
|
||||||
// if jsonResp == nil {
|
request, _ = http.NewRequest(http.MethodGet, utils.GenerateGetURL(baseUrl, actionApi, bizParams), nil)
|
||||||
// return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
}
|
||||||
// }
|
//request.Header.Set("Content-Type", "form-data")
|
||||||
// if jsonResp["return_code"] != ReturnSuccess {
|
request.Header.Set("Content-Type", "application/json")
|
||||||
// errMsg = platformapi.ErrLevelGeneralFail
|
return request
|
||||||
// err = utils.NewErrorCode(jsonResp["return_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResp["return_code"])))
|
}
|
||||||
// }
|
//数据解析
|
||||||
// retVal = jsonResp
|
dataMarshal := func(response *http.Response, bodyStr string, jsonResp map[string]interface{}) (errMsg string, err error) {
|
||||||
// return errMsg, err
|
if jsonResp == nil {
|
||||||
// }
|
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||||
// err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
}
|
||||||
// return retVal, err
|
if jsonResp["return_code"] != ReturnSuccess {
|
||||||
//}
|
errMsg = platformapi.ErrLevelGeneralFail
|
||||||
//
|
err = utils.NewErrorCode(jsonResp["return_msg"].(string), utils.Int64ToStr(utils.MustInterface2Int64(jsonResp["return_code"])))
|
||||||
////以下为辅助函数
|
}
|
||||||
//func randStr() string {
|
retVal = jsonResp
|
||||||
// b := make([]byte, 16)
|
return errMsg, err
|
||||||
// for i := range b {
|
}
|
||||||
// b[i] = letters[rand.Intn(len(letters))]
|
err = platformapi.AccessPlatformAPIWithRetry(a.client, sendUrl, a.config, dataMarshal)
|
||||||
// }
|
return retVal, err
|
||||||
// return string(b)
|
}
|
||||||
//}
|
|
||||||
|
//以下为辅助函数
|
||||||
|
func randStr() string {
|
||||||
|
b := make([]byte, 16)
|
||||||
|
for i := range b {
|
||||||
|
b[i] = letters[rand.Intn(len(letters))]
|
||||||
|
}
|
||||||
|
return string(b)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +1,30 @@
|
|||||||
package uuptapi
|
package uuptapi
|
||||||
|
|
||||||
//
|
import (
|
||||||
//import (
|
"encoding/json"
|
||||||
// "encoding/json"
|
"git.rosy.net.cn/baseapi/utils"
|
||||||
// "git.rosy.net.cn/baseapi/utils"
|
)
|
||||||
//)
|
|
||||||
//
|
//计算订单价格
|
||||||
////计算订单价格
|
func (a *API) GetOrderPrice(req *GetOrderPriceReq) (needPayMoney, priceToken string, err error) {
|
||||||
//func (a *API) GetOrderPrice(req *GetOrderPriceReq) (needPayMoney, priceToken string, err error) {
|
preOrder := a.MakeUURequestHead()
|
||||||
// preOrder := a.MakeUURequestHead()
|
bytes, err := json.Marshal(req)
|
||||||
// bytes, err := json.Marshal(req)
|
if err != nil {
|
||||||
// if err != nil {
|
return "", "", err
|
||||||
// return "", "", err
|
}
|
||||||
// }
|
preOrder["param"] = string(bytes)
|
||||||
// preOrder["param"] = string(bytes)
|
resp, err := a.AccessAPI(BaseURL, "getorderprice.ashx", RequestPost, preOrder)
|
||||||
// resp, err := a.AccessAPI(BaseURL, "getorderprice.ashx", RequestPost, preOrder)
|
if err != nil {
|
||||||
// if err != nil {
|
return "", "", err
|
||||||
// return "", "", err
|
}
|
||||||
// }
|
retVal := GetOrderPriceResp{}
|
||||||
// retVal := GetOrderPriceResp{}
|
if err := json.Unmarshal([]byte(resp["param"].(string)), &retVal); err != nil {
|
||||||
// if err := json.Unmarshal([]byte(resp["param"].(string)), &retVal); err != nil {
|
return "", "", err
|
||||||
// return "", "", err
|
}
|
||||||
// }
|
con := retVal == GetOrderPriceResp{}
|
||||||
// con := retVal == GetOrderPriceResp{}
|
if !utils.IsNil(retVal) && !con {
|
||||||
// if !utils.IsNil(retVal) && !con {
|
needPayMoney = retVal.NeedPayMoney
|
||||||
// needPayMoney = retVal.NeedPayMoney
|
priceToken = retVal.PriceToken
|
||||||
// priceToken = retVal.PriceToken
|
}
|
||||||
// }
|
return needPayMoney, priceToken, nil
|
||||||
// return needPayMoney, priceToken, nil
|
}
|
||||||
//}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user