This commit is contained in:
邹宗楠
2024-12-13 17:04:36 +08:00
parent 5fa49d0b98
commit ba9ded985a
2 changed files with 38 additions and 18 deletions

View File

@@ -12,7 +12,10 @@ func (a *API) AddPrinter(sn, key, name string) error {
Key: key,
Name: name,
}}
resp := a.HttpPostJson("addPrinter", params)
resp, err := a.HttpPostJson("addPrinter", params)
if err != nil {
return err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return errors.New("HTTP请求错误请检查重试")
}
@@ -33,11 +36,14 @@ func (a *API) AddPrinter(sn, key, name string) error {
//修改打印机信息
func (a *API) EditPrinter(sn, name string) (string, error) {
resp := a.HttpPostJson("editPrinter", []EditPrinterReq{{
resp, err := a.HttpPostJson("editPrinter", []EditPrinterReq{{
Sn: sn,
Name: name,
},
})
if err != nil {
return "", err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return "", errors.New("HTTP请求错误请检查重试")
}
@@ -58,7 +64,10 @@ func (a *API) EditPrinter(sn, name string) (string, error) {
//删除打印机
func (a *API) DelPrinter(snList []string) error {
resp := a.HttpPostJson("delPrinter", snList)
resp, err := a.HttpPostJson("delPrinter", snList)
if err != nil {
return err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return errors.New("HTTP请求错误请检查重试")
}
@@ -79,10 +88,13 @@ func (a *API) DelPrinter(snList []string) error {
//设置打印机浓度
func (a *API) SetDensity(sn string, density int) error {
resp := a.HttpPostJson("setDensity", SetDensityReq{
resp, err := a.HttpPostJson("setDensity", SetDensityReq{
Sn: sn,
Density: density,
})
if err != nil {
return err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return errors.New("HTTP请求错误请检查重试")
}
@@ -94,10 +106,13 @@ func (a *API) SetDensity(sn string, density int) error {
//设置音量
func (a *API) SetVolume(sn string, volume int) error {
resp := a.HttpPostJson("setVolume", SetVolumeReq{
resp, err := a.HttpPostJson("setVolume", SetVolumeReq{
Sn: sn,
Volume: volume,
})
if err != nil {
return err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return errors.New("HTTP请求错误请检查重试")
}
@@ -109,7 +124,10 @@ func (a *API) SetVolume(sn string, volume int) error {
//查询打印机状态
func (a *API) GetDeviceStatus(sn string) (float64, float64, error) {
resp := a.HttpPostJson("getDeviceStatus", GetDeviceStatusReq{Sn: sn})
resp, err := a.HttpPostJson("getDeviceStatus", GetDeviceStatusReq{Sn: sn})
if err != nil {
return 0, 0, err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return 0, 0, errors.New("HTTP请求错误请检查重试")
}
@@ -122,7 +140,10 @@ func (a *API) GetDeviceStatus(sn string) (float64, float64, error) {
//清空设备待打印队列
func (a *API) CleanWaitingQueue(sn string) error {
resp := a.HttpPostJson("cleanWaitingQueue", sn)
resp, err := a.HttpPostJson("cleanWaitingQueue", sn)
if err != nil {
return err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return errors.New("HTTP请求错误请检查重试")
}
@@ -134,7 +155,7 @@ func (a *API) CleanWaitingQueue(sn string) error {
//打印小票
func (a *API) Print(sn, content, voice string) (string, error) {
resp := a.HttpPostJson("print", PrintReq{
resp, err := a.HttpPostJson("print", PrintReq{
Sn: sn,
Content: content,
Voice: voice,
@@ -142,7 +163,9 @@ func (a *API) Print(sn, content, voice string) (string, error) {
VoicePlayInterval: 3,
Copies: 1,
})
if err != nil {
return "", err
}
if resp.HttpStatusCode != HttpStatusSuccessCode {
return "", errors.New("HTTP请求错误请检查重试")
}

View File

@@ -68,7 +68,7 @@ func (a *API) sign(uid, timestamp string, param string) (sign string) {
return fmt.Sprintf("%x", md5.Sum([]byte(aa)))
}
func (a *API) HttpPostJson(url string, data interface{}) *TIResponse {
func (a *API) HttpPostJson(url string, data interface{}) (*TIResponse, error) {
//序列化参数
b, err := json.Marshal(&data)
if err != nil {
@@ -77,7 +77,7 @@ func (a *API) HttpPostJson(url string, data interface{}) *TIResponse {
result := TIResponse{
HttpStatusCode: 500,
}
return &result
return &result, nil
}
fullUrl := utils.GenerateGetURL(BaseUrl, url, nil)
@@ -97,14 +97,12 @@ func (a *API) HttpPostJson(url string, data interface{}) *TIResponse {
resp, err := client.Do(request)
//resp, err := http.Post(utils.GenerateGetURL(BaseUrl, url, nil), "application/json;charset=UTF-8", bytes.NewBuffer(b))
if err != nil {
return nil
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
glo
var msg = fmt.Sprintf("post json error:%+v", err)
fmt.Println(msg)
return nil, err
}
result := TIResponse{
@@ -116,11 +114,10 @@ func (a *API) HttpPostJson(url string, data interface{}) *TIResponse {
if err == nil {
result.BaseRes = &content
} else {
var msg = fmt.Sprintf("unmarshal body failed, error:%+v", err)
fmt.Println(msg)
return nil, err
}
return &result
return &result, nil
}
func StrRepeat(str string, repeatTimes int) string {