diff --git a/platformapi/yilianyunapi/yilianyunapi.go b/platformapi/yilianyunapi/yilianyunapi.go
index ea67be88..17236f09 100644
--- a/platformapi/yilianyunapi/yilianyunapi.go
+++ b/platformapi/yilianyunapi/yilianyunapi.go
@@ -14,10 +14,11 @@ import (
)
const (
- prodURL = "https://open-api.10ss.net"
- signKey = "sign"
- timestampKey = "timestamp"
- oathApiPath = "oauth/oauth"
+ prodURL = "https://open-api.10ss.net"
+ signKey = "sign"
+ timestampKey = "timestamp"
+ oathApiPath = "oauth/oauth"
+ scanCodemodelApiPath = "oauth/scancodemodel"
)
const (
@@ -50,7 +51,9 @@ type TokenInfo struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int64 `json:"expires_in"`
- Scope string `json:"scope"`
+
+ Scope string `json:"scope"`
+ MachineCode string `json:"machine_code"`
}
func New(clientID, clientSecret string, config ...*platformapi.APIConfig) *API {
@@ -70,7 +73,10 @@ func (a *API) signParams(timestamp int64) string {
return fmt.Sprintf("%x", md5.Sum([]byte(a.clientID+utils.Int64ToStr(timestamp)+a.clientSecret)))
}
-func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVal map[string]interface{}, err error) {
+func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}, token string) (retVal map[string]interface{}, err error) {
+ if len(token) < len("d65578a6fa414d738e0c44f85ac4b950") {
+ token = ""
+ }
err = platformapi.AccessPlatformAPIWithRetry(a.client,
func() *http.Request {
timestamp := time.Now().Unix()
@@ -80,8 +86,11 @@ func (a *API) AccessAPI(apiName string, apiParams map[string]interface{}) (retVa
"client_id": a.clientID,
"id": utils.GetUpperUUID(),
}, apiParams)
- if oathApiPath != apiName {
- params["access_token"] = a.GetToken()
+ if oathApiPath != apiName && scanCodemodelApiPath != apiName {
+ if token == "" {
+ token = a.GetToken()
+ }
+ params["access_token"] = token
}
request, _ := http.NewRequest(http.MethodPost, utils.GenerateGetURL(prodURL, apiName, nil), strings.NewReader(utils.Map2URLValues(params).Encode()))
request.Header.Set("charset", "UTF-8")
@@ -131,13 +140,35 @@ func (a *API) RetrieveToken() (tokenInfo *TokenInfo, err error) {
result, err := a.AccessAPI(oathApiPath, map[string]interface{}{
"grant_type": "client_credentials",
"scope": "all",
- })
- if err != nil {
- return nil, err
+ }, "")
+ if err == nil {
+ if err = utils.Map2StructByJson(result, &tokenInfo, false); err == nil {
+ a.SetToken(tokenInfo.AccessToken)
+ }
}
- tokenInfo = &TokenInfo{}
- if err = utils.Map2StructByJson(result, tokenInfo, false); err == nil {
- a.SetToken(tokenInfo.AccessToken)
+ return tokenInfo, err
+}
+
+func (a *API) Code2Token(code string) (tokenInfo *TokenInfo, err error) {
+ result, err := a.AccessAPI(oathApiPath, map[string]interface{}{
+ "grant_type": "authorization_code",
+ "scope": "all",
+ "code": code,
+ }, "")
+ if err == nil {
+ err = utils.Map2StructByJson(result, &tokenInfo, false)
+ }
+ return tokenInfo, err
+}
+
+func (a *API) RefreshToken(refreshToken string) (tokenInfo *TokenInfo, err error) {
+ result, err := a.AccessAPI(oathApiPath, map[string]interface{}{
+ "grant_type": "refresh_token",
+ "scope": "all",
+ "refresh_token": refreshToken,
+ }, "")
+ if err == nil {
+ err = utils.Map2StructByJson(result, &tokenInfo, false)
}
return tokenInfo, err
}
@@ -147,18 +178,18 @@ func (a *API) AddPrinter(machineCode, msign, printerName string) (err error) {
"machine_code": machineCode,
"msign": msign,
"print_name": printerName,
- })
+ }, "")
return err
}
func (a *API) DeletePrinter(machineCode string) (err error) {
_, err = a.AccessAPI("printer/deleteprinter", map[string]interface{}{
"machine_code": machineCode,
- })
+ }, "")
return err
}
-func (a *API) PrintMsg(machineCode, orderID, content string) (err error) {
+func (a *API) printMsg(machineCode, orderID, content, token string) (err error) {
if orderID == "" {
orderID = utils.GetUUID()
}
@@ -166,16 +197,45 @@ func (a *API) PrintMsg(machineCode, orderID, content string) (err error) {
"machine_code": machineCode,
"origin_id": orderID,
"content": content,
- })
+ }, token)
return err
}
-func (a *API) GetPrintStatus(machineCode string) (state int, err error) {
+func (a *API) PrintMsg(machineCode, orderID, content string) (err error) {
+ return a.printMsg(machineCode, orderID, content, "")
+}
+
+func (a *API) PrintMsgWithToken(machineCode, orderID, content, token string) (err error) {
+ return a.printMsg(machineCode, orderID, content, token)
+}
+
+func (a *API) getPrintStatus(machineCode, token string) (state int, err error) {
result, err := a.AccessAPI("printer/getprintstatus", map[string]interface{}{
"machine_code": machineCode,
- })
+ }, token)
if err != nil {
return 0, err
}
return int(utils.Str2Int64(utils.Interface2String(result["state"]))), nil
}
+
+func (a *API) GetPrintStatus(machineCode string) (state int, err error) {
+ return a.getPrintStatus(machineCode, "")
+}
+
+func (a *API) GetPrintStatusWithToken(machineCode, token string) (state int, err error) {
+ return a.getPrintStatus(machineCode, token)
+}
+
+func (a *API) GetPrinterToken(machineCode, qrKey string) (tokenInfo *TokenInfo, err error) {
+ result, err := a.AccessAPI(scanCodemodelApiPath, map[string]interface{}{
+ "machine_code": machineCode,
+ "qr_key": qrKey,
+ "scope": "all",
+ }, "")
+ if err == nil {
+ err = utils.Map2StructByJson(result, &tokenInfo, false)
+ a.SetToken(tokenInfo.AccessToken)
+ }
+ return tokenInfo, err
+}
diff --git a/platformapi/yilianyunapi/yilianyunapi_test.go b/platformapi/yilianyunapi/yilianyunapi_test.go
index ba545d97..b9dabfbb 100644
--- a/platformapi/yilianyunapi/yilianyunapi_test.go
+++ b/platformapi/yilianyunapi/yilianyunapi_test.go
@@ -19,8 +19,13 @@ func init() {
sugarLogger = logger.Sugar()
baseapi.Init(sugarLogger)
+ // 自有应用
api = New("1039586024", "4885d07c2997b661102e4b6099c0bf3b")
- api.SetToken("a25e039ea5d14a7aa61653eb58118d9c")
+ api.SetToken("968b52db8a0a40d184d67578aa4888f8")
+
+ // 开放应用
+ // api = New("1098307169", "d5eedb40c99e6691b1ca2ba82a363d6a")
+ // api.SetToken("d65578a6fa414d738e0c44f85ac4b950")
}
func handleError(t *testing.T, err error) {
@@ -47,7 +52,14 @@ func TestDeletePrinter(t *testing.T) {
}
func TestPrintMsg(t *testing.T) {
- err := api.PrintMsg("4004600675", utils.GetUUID(), "hello world")
+ // 4004606481
+ err := api.PrintMsg("4004600675", utils.GetUUID(), "饿百取货码")
+ handleError(t, err)
+}
+
+func TestPrintMsgWithToken(t *testing.T) {
+ // 4004606481
+ err := api.PrintMsgWithToken("4004600675", utils.GetUUID(), "饿百取货码", "faketoken")
handleError(t, err)
}
@@ -56,3 +68,15 @@ func TestGetPrintStatus(t *testing.T) {
handleError(t, err)
baseapi.SugarLogger.Debug(state)
}
+
+func TestGetPrintStatusWithToken(t *testing.T) {
+ state, err := api.GetPrintStatusWithToken("4004600675", "faketoken")
+ handleError(t, err)
+ baseapi.SugarLogger.Debug(state)
+}
+
+func TestGetPrinterToken(t *testing.T) {
+ TokenInfo, err := api.GetPrinterToken("4004600675", "cb3UTvMq")
+ handleError(t, err)
+ baseapi.SugarLogger.Debug(TokenInfo)
+}