公司开发收银系统,小票打印是必不可少,系统是通过uniapp进行开发,可以说uniapp还是非常强大,里面有不少插件可以使用,其中就有连接usb打印机打印小票功能
插件地址:Android USB打印机插件(含钱箱控制)
注意:钱箱必须是连接在打印机上的钱箱才可以
直接上代码
<script>
// 首先第一步引入插件
var usbscan = uni.requireNativePlugin('usbscan-UsbScanModule');
export default {
...
onLoad() {
this.init();
},
methods: {
init(){
// 初始化打印机
printModule.initPrintDevice({ vendorId: 1046, productId: 20497 }, (res) => {});
},
// 打开钱箱
handleClickCollectMoney() {
printModule.openDrawerCmd();
},
// 打印订单
// 打印订单
printorder(res) {
let that = this
this.payresultmsg = '正在打印小票...';
let params = {
merchname: res.merchname,
opuser: res.opuser,
time: res.time,
ordersn: res.ordersn,
total_money: res.fee,
cash_money: res.cash_fee,
credit8_money: res.deductcredit8,
credit7_money: res.deductcredit7,
wx_money: res.wx_money,
goodslist: res.goodsdata.map((item) => {
return {
name: item.goodstitle,
num: item.total,
price: item.price,
total: item.total_price
};
})
};
// 打印订单小票
printModule.printOrder(params, (result) => {
printcount = printcount + 1
console.log("打印:",printcount,res.print_num)
if(printcount - res.print_num == 0){
console.log('-----打印完成------')
setTimeout(() => {
that.payresultmsg = "打印完成"
}, 1000 * res.print_num / 2);
setTimeout(() => {
that.handleClickCancelOne();
}, 1000 * res.print_num);
}
});
},
}
}
</script>
插件功能
* 打印文字 public void printText(String msg)
* 换行打印文字 printTextNewLine(String msg)
* 换行 printLine()
* 打开钱箱 openDrawerCmd()
* 打印空行 printLine(int size)
* 设置字体大小 setTextSize(int size)
* 字体加粗 bold(boolean isBold)
* 打印一维条形码 printBarCode(String data)
* 打印二维码 printQrCode(String data)
* 设置对齐方式(0居左 1居中 2居右) setAlign(int position)
* 获取字符串的宽度 getStringWidth(String str)
* 切纸 cutPager()
* 获取所有打印设备 getAllUsbDevice(UniJSCallback callback)
* 获取设备号 getDevicesId(UniJSCallback callback)
* 打印订单 printOrder(JSONObject options,UniJSCallback callback)
完整示例
<template>
<div>
<button type="primary" @click="getDevicesId()">获取设备号</button>
<button type="primary" @click="test2()">test2</button>
<button type="primary" @click="test3()">test3</button>
<button type="primary" @click="getusb()">获取打印机列表</button>
<button type="primary" @click="initPrintDevice()">初始化打印</button>
<button type="primary" @click="printText()">printText</button>
<button type="primary" @click="printText2()">printText</button>
<button type="primary" @click="openDrawerCmd()">打开钱箱</button>
<button type="primary" @click="printOrder()">打印订单</button>
</div>
</template>
<script>
// 获取 module
var printModule = uni.requireNativePlugin("zantprint-PrintModule")
export default {
onLoad() {
},
methods: {
getDevicesId() {
uni.showToast({
title:'getDevicesId'
})
// 获取设备号
printModule.getDevicesId((res)=>{
if(res.status==1){
uni.showModal({
content:res.data.deviceid
})
}else{
uni.showToast({
title:res.message
})
}
})
},
getusb(){
printModule.getAllUsbDevice((res)=>{
if(res.status==1){
uni.showModal({
content:JSON.stringify(res.data)
})
}else{
uni.showToast({
title:res.message
})
}
})
},
initPrintDevice(){
// 初始化打印机
printModule.initPrintDevice({
"vendorId": 1046,
"productId": 20497
},(res)=>{
if(res.status==1){
uni.showModal({
content:res.message
})
}else{
uni.showModal({
content:res.message
})
}
})
},
printText(){
printModule.printText('测试打印文字')
uni.showModal({
content: 'ok'
})
},
printText2(){
printModule.printText2('测试打印文字',(res)=>{
uni.showModal({
content: res.message
})
})
},
openDrawerCmd(){
printModule.openDrawerCmd((res)=>{
uni.showModal({
content: JSON.stringify(res)
})
})
},
test2(){
let result = printModule.test2({"key":'测试打印文字'})
uni.showModal({
content: JSON.stringify(result)
})
},
test3(){
let result = printModule.test3("测试打印文字")
uni.showModal({
content: result
})
},
printOrder(){
// 打印订单
printModule.printOrder({
"merchname": "综合超市",
"opuser": "臭猪",
"time": "2023-12-08 20:20:20",
"ordersn": "XX0000000222223333224556677",
"total_money": "600.00",
"cash_money": "10.00",
"credit8_money": "500.00",
"credit7_money": "40.00",
"wx_money": "60.00",
"goodslist": [
{
"name": "收银系统",
"num": "20",
"price": "10.00",
"total": "200.00",
},
{
"name": "收银系统",
"num": "20",
"price": "10.00",
"total": "200.00",
},
{
"name": "收银系统",
"num": "20",
"price": "10.00",
"total": "200.00",
}
]
},(res)=>{
uni.showModal({
content: '打印成功'
})
})
}
}
}
</script>
打印效果
<img src="https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/dccc9bc5908b4cffb762c94d8acc1560~tplv-k3u1fbpfcp-jj-mark:0:0:0:0:q75.image#?w=720&h=1280&s=196830&e=jpg&b=c3c2bd" width="20%" height="auto">
<img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/a0b841bc1edd4e579691318afdf39212~tplv-k3u1fbpfcp-jj-mark:0:0:0:0:q75.image#?w=490&h=758&s=528588&e=png&b=c6c5be" width="20%" height="auto">
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。