物联卡
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/sha1"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
@@ -768,14 +769,8 @@ func TestUpdateStatus(t *testing.T) {
|
||||
//fmt.Println(int2h8l8(int64(len("fd003401005b76325dc8d5d5d5cfe3c2afc9fad7cfd1cca3acd2a3bfb4c6d9b2bcb9d2c7b0b4a8a3acb7c9c1f7d6b1cfc2c8fdc7a7b3dfa3acd2c9cac7d2f8bad3c2e4bec5ccec") / 2)))
|
||||
//fmt.Println(hex.EncodeToString(data))
|
||||
//fmt.Println(fmt.Sprintf("%x", len("fd003401005b76325dc8d5d5d5cfe3c2afc9fad7cfd1cca3acd2a3bfb4c6d9b2bcb9d2c7b0b4a8a3acb7c9c1f7d6b1cfc2c8fdc7a7b3dfa3acd2c9cac7d2f8bad3c2e4bec5ccec")/2))
|
||||
str := "1e00180200015054353139444130303031375a31510001"
|
||||
var sum int64
|
||||
for i := 0; i < len(str); i = i + 2 {
|
||||
b := string(str[i]) + string(str[i+1])
|
||||
bt, _ := strconv.ParseInt(b, 16, 32)
|
||||
sum += bt
|
||||
}
|
||||
fmt.Println(int2h8l8(sum))
|
||||
str := "AA0D5ABE801F11E98489525400AE46A6e10adc3949ba59abbe56e057f20f883e"
|
||||
fmt.Println(fmt.Sprintf("%x", sha1.Sum([]byte(str))))
|
||||
}
|
||||
|
||||
func sss() (data2 []byte) {
|
||||
|
||||
@@ -2,6 +2,7 @@ package tibiotapi
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
@@ -21,12 +22,6 @@ const (
|
||||
ResponseCodeSuccess = 0
|
||||
)
|
||||
|
||||
var (
|
||||
exceedLimitCodes = map[int]int{}
|
||||
|
||||
canRetryCodes = map[int]int{}
|
||||
)
|
||||
|
||||
type API struct {
|
||||
platformapi.APICookie
|
||||
|
||||
@@ -50,21 +45,35 @@ func New(username, password string, config ...*platformapi.APIConfig) *API {
|
||||
}
|
||||
|
||||
func (a *API) signParams(apiParams map[string]interface{}) string {
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%x", md5.Sum([]byte(a.username+apiParams["tkey"].(string)))))))
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(fmt.Sprintf("%x", md5.Sum([]byte(a.password)))+apiParams["tkey"].(string))))
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(url string, apiParams map[string]interface{}) (retVal interface{}, err error) {
|
||||
func (a *API) AccessAPI(url string, apiParams map[string]interface{}, isJson bool) (retVal map[string]interface{}, err error) {
|
||||
params := utils.MergeMaps(map[string]interface{}{
|
||||
"username": a.username,
|
||||
}, apiParams)
|
||||
fullURL := utils.GenerateGetURL(prodURL, url, nil)
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
params["tkey"] = time.Now().Format("20060102150405")
|
||||
tkey := time.Now().Format("20060102150405")
|
||||
params["tkey"] = tkey
|
||||
sign := a.signParams(params)
|
||||
params[signKey] = sign
|
||||
request, _ := http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
var request *http.Request
|
||||
if isJson {
|
||||
delete(params, "username")
|
||||
delete(params, "password")
|
||||
delete(params, "tkey")
|
||||
params["userName"] = a.username
|
||||
params["passWord"] = sign
|
||||
params["tKey"] = tkey
|
||||
data, _ := json.Marshal(params)
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(string(data)))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
} else {
|
||||
request, _ = http.NewRequest(http.MethodPost, fullURL, strings.NewReader(utils.Map2URLValues(params).Encode()))
|
||||
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
}
|
||||
return request
|
||||
},
|
||||
a.config,
|
||||
@@ -72,27 +81,102 @@ func (a *API) AccessAPI(url string, apiParams map[string]interface{}) (retVal in
|
||||
if jsonResult1 == nil {
|
||||
return platformapi.ErrLevelRecoverableErr, fmt.Errorf("mapData is nil")
|
||||
}
|
||||
code := int(utils.Interface2Int64WithDefault(jsonResult1["ret"], ResponseCodeSuccess))
|
||||
code := int(utils.Interface2Int64WithDefault(jsonResult1["status"], ResponseCodeSuccess))
|
||||
if code == ResponseCodeSuccess {
|
||||
retVal = jsonResult1["data"]
|
||||
if jsonResult1["data"] != nil {
|
||||
retVal = jsonResult1["data"].(map[string]interface{})
|
||||
} else {
|
||||
retVal = nil
|
||||
}
|
||||
return platformapi.ErrLevelSuccess, nil
|
||||
}
|
||||
newErr := utils.NewErrorIntCode(jsonResult1["msg"].(string), code)
|
||||
if _, ok := exceedLimitCodes[code]; ok {
|
||||
return platformapi.ErrLevelExceedLimit, newErr
|
||||
} else if _, ok := canRetryCodes[code]; ok {
|
||||
return platformapi.ErrLevelRecoverableErr, newErr
|
||||
} else {
|
||||
baseapi.SugarLogger.Debugf("feie AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
}
|
||||
newErr := utils.NewErrorIntCode(jsonResult1["message"].(string), code)
|
||||
baseapi.SugarLogger.Debugf("tibiot AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
return platformapi.ErrLevelCodeIsNotOK, newErr
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
func (a *API) GetCardInfo(iccid string) (err error) {
|
||||
a.AccessAPI("card/getCardInfo", map[string]interface{}{
|
||||
"iccid": iccid,
|
||||
})
|
||||
return err
|
||||
type GetCardInfoResult struct {
|
||||
CardStatus string `json:"card_status"` //卡状态 1 - 正常 2 - 停机 3 - 待激活
|
||||
Iccid string `json:"iccid"`
|
||||
Msisdn string `json:"msisdn"`
|
||||
CardType string `json:"card_type"` //卡类型 1 - 单卡 2 - 池卡
|
||||
Imsi string `json:"imsi"`
|
||||
}
|
||||
|
||||
func (a *API) GetCardInfo(iccid string) (getCardInfoResult *GetCardInfoResult, err error) {
|
||||
result, err := a.AccessAPI("card/getCardInfo", map[string]interface{}{
|
||||
"iccid": iccid,
|
||||
}, false)
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result, &getCardInfoResult, false)
|
||||
}
|
||||
return getCardInfoResult, err
|
||||
}
|
||||
|
||||
type BatchQueryCardInfoResult struct {
|
||||
PageNum int `json:"pageNum"`
|
||||
PageSize int `json:"pageSize"`
|
||||
TotalCount int `json:"totalCount"`
|
||||
PageCount int `json:"pageCount"`
|
||||
OrderBy interface{} `json:"orderBy"`
|
||||
Records []struct {
|
||||
Iccid string `json:"iccid"`
|
||||
Msisdn string `json:"msisdn"`
|
||||
Cardstatus int `json:"cardStatus"`
|
||||
Operator int `json:"operator"`
|
||||
Packagename string `json:"packageName"`
|
||||
Totalflow string `json:"totalFlow"`
|
||||
Cardflow string `json:"cardFlow"`
|
||||
Leftflow string `json:"leftFlow"`
|
||||
Opencardtime string `json:"openCardTime"`
|
||||
Activatetime string `json:"activateTime"`
|
||||
Packagetime string `json:"packageTime"`
|
||||
} `json:"records"`
|
||||
}
|
||||
|
||||
func (a *API) BatchQueryCardInfo(pageNum int) (batchQueryCardInfoResult *BatchQueryCardInfoResult, err error) {
|
||||
result, err := a.AccessAPI("card/batchQueryCardInfo", map[string]interface{}{
|
||||
"pageNum": 1,
|
||||
"cardType": 1,
|
||||
}, true)
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result, &batchQueryCardInfoResult, false)
|
||||
}
|
||||
return batchQueryCardInfoResult, err
|
||||
}
|
||||
|
||||
func (a *API) IotData(iccid, queryTime string) (getCardInfoResult *GetCardInfoResult, err error) {
|
||||
result, err := a.AccessAPI("card/iotData", map[string]interface{}{
|
||||
"iccid": iccid,
|
||||
"query_time": queryTime,
|
||||
}, false)
|
||||
if err == nil && result != nil {
|
||||
utils.Map2StructByJson(result, &getCardInfoResult, false)
|
||||
}
|
||||
return getCardInfoResult, err
|
||||
}
|
||||
|
||||
func (a *API) CardActivation(iccid string) (getCardInfoResult *GetCardInfoResult, err error) {
|
||||
result, err := a.AccessAPI("card/cardActivation", map[string]interface{}{
|
||||
"iccid": iccid,
|
||||
}, true)
|
||||
if err == nil && result != nil {
|
||||
utils.Map2StructByJson(result, &getCardInfoResult, false)
|
||||
}
|
||||
return getCardInfoResult, err
|
||||
}
|
||||
|
||||
//卡续费
|
||||
//duration 续费时长,非必填,月默认12月 其余周期默认1
|
||||
func (a *API) Submit(iccid string, duration int) (getCardInfoResult *GetCardInfoResult, err error) {
|
||||
result, err := a.AccessAPI("renew/submit", map[string]interface{}{
|
||||
"iccid": iccid,
|
||||
"duration": duration,
|
||||
}, true)
|
||||
if err == nil && result != nil {
|
||||
utils.Map2StructByJson(result, &getCardInfoResult, false)
|
||||
}
|
||||
return getCardInfoResult, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package tibiotapi
|
||||
|
||||
import (
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
"testing"
|
||||
)
|
||||
@@ -20,9 +21,41 @@ func init() {
|
||||
}
|
||||
|
||||
func TestGetCardInfo(t *testing.T) {
|
||||
err := api.GetCardInfo("")
|
||||
result, err := api.GetCardInfo("89860426102180138547")
|
||||
if err != nil {
|
||||
t.Fatalf("PrintMsg return error:%v", err)
|
||||
}
|
||||
//baseapi.SugarLogger.Debug(result)
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestBatchQueryCardInfo(t *testing.T) {
|
||||
result, err := api.BatchQueryCardInfo(1)
|
||||
if err != nil {
|
||||
t.Fatalf("PrintMsg return error:%v", err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestIotData(t *testing.T) {
|
||||
result, err := api.IotData("89860490102070456598", "2021-07-19")
|
||||
if err != nil {
|
||||
t.Fatalf("PrintMsg return error:%v", err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestCardActivation(t *testing.T) {
|
||||
result, err := api.CardActivation("1")
|
||||
if err != nil {
|
||||
t.Fatalf("PrintMsg return error:%v", err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
func TestSubmit(t *testing.T) {
|
||||
result, err := api.Submit("1", 0)
|
||||
if err != nil {
|
||||
t.Fatalf("PrintMsg return error:%v", err)
|
||||
}
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user