阿里云查询商品条码
This commit is contained in:
83
platformapi/aliupcapi/aliupcapi.go
Normal file
83
platformapi/aliupcapi/aliupcapi.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package aliupcapi
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
)
|
||||
|
||||
const (
|
||||
url = "https://codequery.market.alicloudapi.com"
|
||||
successCode = "200"
|
||||
)
|
||||
|
||||
type API struct {
|
||||
appCode string
|
||||
client *http.Client
|
||||
config *platformapi.APIConfig
|
||||
}
|
||||
|
||||
func New(appCode string, config ...*platformapi.APIConfig) *API {
|
||||
curConfig := platformapi.DefAPIConfig
|
||||
if len(config) > 0 {
|
||||
curConfig = *config[0]
|
||||
}
|
||||
return &API{
|
||||
appCode: appCode,
|
||||
client: &http.Client{Timeout: curConfig.ClientTimeout},
|
||||
config: &curConfig,
|
||||
}
|
||||
}
|
||||
|
||||
func (a *API) AccessAPI(action string, params map[string]interface{}) (retVal map[string]interface{}, err error) {
|
||||
err = platformapi.AccessPlatformAPIWithRetry(a.client,
|
||||
func() *http.Request {
|
||||
request, _ := http.NewRequest(http.MethodGet, utils.GenerateGetURL(url, action, params), nil)
|
||||
request.Header.Set("Authorization", "APPCODE "+a.appCode)
|
||||
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 jsonResult1["status"] != successCode {
|
||||
errLevel = platformapi.ErrLevelGeneralFail
|
||||
errMsg := jsonResult1["msg"].(string)
|
||||
err = utils.NewErrorCode(errMsg, utils.Int64ToStr(utils.Interface2Int64WithDefault(jsonResult1["status"], 0)))
|
||||
baseapi.SugarLogger.Debugf("aliupc AccessAPI failed, jsonResult1:%s", utils.Format4Output(jsonResult1, true))
|
||||
}
|
||||
retVal = jsonResult1
|
||||
}
|
||||
return errLevel, err
|
||||
})
|
||||
return retVal, err
|
||||
}
|
||||
|
||||
type GetAliUpcInfoResult struct {
|
||||
Code string `json:"code"`
|
||||
GoodsName string `json:"goodsName"`
|
||||
ManuName string `json:"manuName"`
|
||||
ManuAddress string `json:"manuAddress"`
|
||||
Spec string `json:"spec"`
|
||||
Price string `json:"price"`
|
||||
Img string `json:"img"`
|
||||
GoodsType string `json:"goodsType"`
|
||||
Ycg string `json:"ycg"`
|
||||
Trademark string `json:"trademark"`
|
||||
Remark string `json:"remark"`
|
||||
}
|
||||
|
||||
func (a *API) GetAliUpcInfo(code string) (getAliUpcInfoResult *GetAliUpcInfoResult, err error) {
|
||||
result, err := a.AccessAPI("querybarcode", map[string]interface{}{
|
||||
"code": code,
|
||||
})
|
||||
if err == nil {
|
||||
utils.Map2StructByJson(result["result"], &getAliUpcInfoResult, false)
|
||||
}
|
||||
return getAliUpcInfoResult, err
|
||||
}
|
||||
30
platformapi/aliupcapi/aliupcapi_test.go
Normal file
30
platformapi/aliupcapi/aliupcapi_test.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package aliupcapi
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.rosy.net.cn/baseapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
api *API
|
||||
sugarLogger *zap.SugaredLogger
|
||||
)
|
||||
|
||||
func init() {
|
||||
logger, _ := zap.NewDevelopment()
|
||||
sugarLogger = logger.Sugar()
|
||||
baseapi.Init(sugarLogger)
|
||||
api = New("52a6ee59970844eab9bc4de2f397f6f8")
|
||||
}
|
||||
|
||||
func TestGetAliUpcInfo(t *testing.T) {
|
||||
result, err := api.GetAliUpcInfo("6901028227278")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else {
|
||||
t.Log(utils.Format4Output(result, false))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user