-
Notifications
You must be signed in to change notification settings - Fork 0
/
clientApplication.go
83 lines (74 loc) · 2.56 KB
/
clientApplication.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
openApi "github.com/Qzm6826/yly-go-sdk"
"encoding/json"
"fmt"
"github.com/gomodule/redigo/redis"
)
var conf openApi.Config
func CheckWhetherAuthorizationIsGranted (redisCli redis.Conn) bool {
tokenJson, _ := redis.Bytes(redisCli.Do("GET", "tokenJson"))
var token openApi.Token
_ = json.Unmarshal(tokenJson, &token)
if token.Error_description != "success" {
return false
}
conf.SetToken(token)
return true
}
func ConnectRedis() redis.Conn {
address := "127.0.0.1:6379" //host + port
password := ""
db := 1
conn, _ := redis.Dial("tcp", address, redis.DialPassword(password), redis.DialDatabase(db))
return conn
}
/**
openApi.NewAuthClient下集成了5个API:
GetAuthUrl、GetAccessToken、GetAccessTokenByAuthCode、GetAccessTokenByRefreshToken、GetAccessTokenBySecret
*/
func GetAccessToken(redisCli redis.Conn) {
oauth := openApi.NewAuthClient(conf)
tokenData := oauth.GetAccessToken()
tokenJson, _ := json.Marshal(tokenData)
_, err := redis.String(redisCli.Do("SET", "tokenJson", tokenJson))
if err != nil {
fmt.Println("Failed to cache token data to redis", err)
}
conf.SetToken(tokenData)
}
/**
ApiClient.PrintService下集成了4个API:
TextPrint、PicturePrint、ExpressOrderPrint、ExpressOrderCancel
*/
func TextPrint(client openApi.ApiClient, machineCode string, content string, originId string, idempotence int) {
res, _ := client.PrintService.TextPrint(machineCode, content, originId, idempotence)
fmt.Println(res)
}
/**
ApiClient.SetPrinter下集成了24个API:
AddPrinter、SetVoice、DelVoice、DelPrinter、SetPrinterMenu、
ShutdownOrRestart、SetSound、GetPrinterInfo、GetPrinterVersion、CancelAllPrintOrders、
CancelAPrintOrder、SetIcon、DelIcon、SetPrintMode、SetOrderConfirmation、
SetPushUrlByClient、SetPushUrlByAuth、GetOrderStatus、GetOrderList、Reprint、GetPrintStatus、
PushSwitch、SetKeywords、Setting
*/
func AddPrinter(client openApi.ApiClient, machineCode string, mSign string, printName string) {
res, _ := client.SetPrinter.AddPrinter(machineCode, mSign, printName)
fmt.Println(res)
}
func main() {
cid := "" //你的应用id
secret := "" //你的应用密钥
conf = openApi.NewConfig(cid, secret)
conf.SetRequestUrl("https://open-api.10ss.net/v2") //设置v2版本接口
var logger openApi.SimpleLogger
conf.SetLogger(&logger)
redisCli := ConnectRedis()
if !CheckWhetherAuthorizationIsGranted(redisCli) {
GetAccessToken(redisCli)
}
client := openApi.NewClient(conf)
AddPrinter(client, "", "", "") //未绑定打印机,需先调用此方法
TextPrint(client, "", "", "", 0)
}