This commit is contained in:
richboo111
2023-04-25 10:31:54 +08:00
parent 352bb15636
commit 0901cfc573
12 changed files with 784 additions and 90 deletions

View File

@@ -14,35 +14,38 @@ func TestLen(t *testing.T) {
//打印
func TestAPI_Print(t *testing.T) {
content := `<C><font# bolder=1 height=2 width=2>美团外卖</font#></C>
<font# bolder=1 height=1 width=1>--------------------------------</font#>
<LEFT>下单时间2023-03-27 13:22:05</LEFT>
<LEFT>期望送达2023-03-27 14:22:05</LEFT>
<LEFT>客户姓名:梅朵(女士)</LEFT>
<LEFT>客户电话163473526172</LEFT>
<LEFT><font# bolder=0 height=2 width=1>订单编号: E22092832084572779</font#></LEFT><BR>
<C><font# bolder=1 height=2 width=2>美团外卖#20</font#></C>
<C><BAR>E22092832084572779</BAR></C>
<C><font# bolder=1 height=1 width=1>--------------------------------</font#></C>
<LEFT><font# bolder=0 height=2 width=1>客户地址:四川省成都市武侯区双流县金华镇芳草街道小区5栋1单元104号</font#></LEFT>
<C><font# bolder=0 height=1 width=1>--------------------------------</font#></C>
<LEFT><font# bolder=0 height=2 width=1>客户备注:缺货时电话与我沟通 收货人隐私号17882904902——5355手机号 181****6752</font#></LEFT>
<font# bolder=1 height=1 width=1>--------------------------------</font#>
name1 := "[优]猪肉馅约250g/份"
name2 := "鲜鸡蛋约250g/份"
name3 := "[精选优品][精选优品][精选优品][精选优品][精选优品]豌豆米-手工剥豆约100g/份"
name4 := "娃娃菜200g/个"
len1 := len(name1)
len2 := len(name2)
len3 := len(name3)
len4 := len(name4)
len5 := len("--------------------------------")
fmt.Println(len1, len2, len3, len4, len5)
content := `
<LEFT>商品列表</LEFT><BR>`
content += "商品名" + StrRepeat(" ", 3) + "数量" + StrRepeat(" ", 4) + "单价" + StrRepeat(" ", 6) + "小计" + "<BR>"
content += "商品名" + StrRepeat(" ", 13) + "数量" + "<BR>"
content += `--------------------------------`
content += FormatPrintOrderItem("[优]猪肉馅约250g/份", 1, 999)
content += FormatPrintOrderItem("鲜鸡蛋约250g/份", 1, 17.8)
content += FormatPrintOrderItem("豌豆米-手工剥豆约100g/份", 1, 40)
content += FormatPrintOrderItem("娃娃菜200g/个", 5, 2)
content += `<LEFT>共4种9件商品
实付金额: 327.83元</LEFT><BR>
<font# bolder=0 height=1 width=1>--------------#20完-------------</font#>`
content += FormatPrintOrderItemV2(name1, 1, 1)
content += FormatPrintOrderItemV2(name2, 1, 2)
content += FormatPrintOrderItemV2(name3, 1, 3)
content += FormatPrintOrderItemV2(name4, 5, 4)
content += `<BR><LEFT><font# bolder=0 height=2 width=1>共4种9件商品</font#></LEFT><BR>`
content += `<font# bolder=0 height=2 width=1>--------------#20完-------------</font#>`
msg, err := api.Print(TestSn, content, VoiceNewShort)
fmt.Println(msg)
fmt.Println(err)
}
func TestCal(t *testing.T) {
str := "豌豆米-手工剥豆约100g/份"
//ans := "--------------------------------"
cnt := CalWidth(str)
fmt.Println(cnt)
}
//打印取消/退货模板
func TestAPI_Print2(t *testing.T) {
content := `<C><font# bolder=1 height=2 width=2>京西菜市</font#></C><BR>`
@@ -81,7 +84,7 @@ func TestAPI_DelPrinter(t *testing.T) {
//设置打印浓度
func TestAPI_SetDensity(t *testing.T) {
err := api.SetDensity(TestSn, DensityStronger)
err := api.SetDensity(TestSn, DensityStrong)
fmt.Println(err)
}
@@ -93,7 +96,7 @@ func TestAPI_SetVolume(t *testing.T) {
//查询打印机状态
func TestGetDevicesStatus(t *testing.T) {
onlineStatus, workStatus, err := api.GetDeviceStatus(TestSn)
onlineStatus, workStatus, err := api.GetDeviceStatus("570010021789")
fmt.Println(onlineStatus)
fmt.Println(workStatus)
fmt.Println(err)
@@ -104,3 +107,9 @@ func TestAPI_CleanWaitingQueue(t *testing.T) {
err := api.CleanWaitingQueue(TestSn)
fmt.Println(err)
}
func TestInt(t *testing.T) {
temp := 1000
fmt.Println(int(temp / 1000))
}

View File

@@ -9,10 +9,12 @@ import (
"io/ioutil"
r "math/rand"
"net/http"
"regexp"
"strconv"
"strings"
"sync"
"time"
"unicode"
)
type API struct {
@@ -155,3 +157,74 @@ func FormatPrintOrderItem(foodName string, quantity int, price float64) string {
result += "<BR>"
return result
}
const (
ROW_MAX_CHAR_LEN = 34
LAST_ROW_MAX_NAME_CHAR_LEN = 16
MAX_NAME_CHAR_LEN = 16
MaxLineLength = 30
LineLength = 32
NewLineLength = 28
)
//不带单价版本
func FormatPrintOrderItemV2(foodName string, quantity, cnt int) string {
var (
result = ""
restLen int
)
quantityStr := strconv.Itoa(quantity)
foodNameLen := CalWidth(foodName) + 3
if foodNameLen >= MaxLineLength {
if n := foodNameLen / LineLength; n > 0 {
restLen = foodNameLen % LineLength
} else {
restLen = foodNameLen - LineLength
}
result += `<font# bolder=0 height=2 width=1>` + utils.Int2Str(cnt) + `.` + foodName
result += StrRepeat(" ", MaxLineLength-restLen) + `x` + quantityStr + `</font#>`
} else {
result += `<font# bolder=0 height=2 width=1>` + utils.Int2Str(cnt) + `.` + foodName + StrRepeat(" ", MaxLineLength-foodNameLen) + `x` + quantityStr + `</font#>`
}
result += "<BR>"
fmt.Println(result)
return result
}
func FormatPrintOrderItemBigV2(foodName string, quantity, cnt int) string {
var (
result = ""
restLen int
)
quantityStr := strconv.Itoa(quantity)
foodNameLen := CalWidth(foodName) + 3
if foodNameLen >= MaxLineLength {
if n := foodNameLen / LineLength; n > 0 {
restLen = foodNameLen % LineLength
} else {
restLen = foodNameLen - LineLength
}
result += `<font# bolder=1 height=2 width=1>` + utils.Int2Str(cnt) + `.` + foodName
result += StrRepeat(" ", MaxLineLength-restLen) + `x` + quantityStr + `</font#>`
} else {
result += `<font# bolder=1 height=2 width=1>` + utils.Int2Str(cnt) + `.` + foodName + StrRepeat(" ", MaxLineLength-foodNameLen) + `x` + quantityStr + `</font#>`
}
result += "<BR>"
fmt.Println(result)
return result
}
//正则计算宽度
func CalWidth(str string) int {
hzc := 0
for _, v := range str {
if unicode.Is(unicode.Han, v) {
hzc++
}
}
updateStr := regexp.MustCompile("[\u4e00-\u9fa5]{1,}").ReplaceAllString(str, "")
l := len(updateStr)
fmt.Println(hzc)
ans := 2*hzc + l
return ans
}