uniapp 打印小票

uniapp 链接热敏打印机打印下票,一下是页面代码,引入js 放在下载里面https://download.csdn.net/download/weixin_43073383/25156646
注:此处因业务需要走的缓存,可自行修改为传参获取参数

<template>
    <aPage :headerStyle="4" :footerHeight="0" title="蓝牙打印">
        <view slot="gBody" class="grace-body gracedy">
            <view>
                <button @click="searchBle">{{ searching ? '停止搜索' : '搜索蓝牙' }}</button>
                <view style="margin-top: 30upx;" :key="index" v-for="(item, index) in info.devices">
                    <view class="onBluetoothList" @click="connect(item, index)" style="width: 100%; ">
                        <view class="bluetoothName" style="{color:item.deviceId == info.deviceId ? '#0081FF' : ''}">
                            {{ item.name }}
                            <view v-if="item.deviceId == info.deviceId">
                                {{ info.readySucc ? '连接成功' : info.readyFail ? '连接失败' : '连接中' }}
                            </view>
                        </view>
                        <view class="">
                            <image :src="item.deviceId == info.deviceId ? '../../static/img/pay/selectGift.png' : '../../static/img/pay/noGift.png'"
                                mode=""></image>
                        </view>
                    </view>
                </view>

                <view v-if="print.els">
                    <button style="margin-top: 100upx;" @click="printClick()">打印</button>
                    <view>
                        {{print.show}}
                    </view>
                </view>
                <view v-else>
                    <button style="margin-top: 100upx;" @click="receiptTest()">标签测试</button>
                    <button style="margin-top: 100upx;" @click="receiptTest2()">标签测试2</button>
                </view>
            </view>
        </view>
    </aPage>
</template>

<script>
    var bluetooth = require('util/bluetooth/bluetooth')
    bluetooth = bluetooth.default || bluetooth
    var tsc = require("../../util/ble/tsc.js");
    var esc = require("../../util/ble/esc.js");
    var encode = require("../../util/ble/encoding.js");
    export default {
        data() {
            return {
                searching: false,
                info: {},
                print: {},
            };
        },
        onLoad: function() {
            var self = this
            try {
                var print = uni.getStorageSync('@print')
                // print = '{"show": "ddd", "els":[{"text":"ddd"}]}'
                if (print) {
                    self.print = JSON.parse(print)
                }

            } catch (e) {
                console.error(e)
            }

            self._delegate = {
                fail(e, tag) {
                    self.$store.$common.loading(false)
                    this.change()
                    var code = e && e.code
                    if (code == 0) {
                        //正常
                    } else if (code == 10000) {
                        uni.showToast({
                            title: '未初始化蓝牙适配器',
                            icon: 'none'
                        })
                    } else if (code == 10001) {
                        uni.showToast({
                            title: '当前蓝牙适配器不可用',
                            icon: 'none'
                        })
                    } else if (code == 10002) {
                        uni.showToast({
                            title: '没有找到指定设备',
                            icon: 'none'
                        })
                    } else if (code == 10003) {
                        uni.showToast({
                            title: '连接失败',
                            icon: 'none'
                        })
                    } else if (code == 10004) {
                        uni.showToast({
                            title: '没有找到指定服务',
                            icon: 'none'
                        })
                    } else if (code == 10005) {
                        uni.showToast({
                            title: '没有找到指定特征值',
                            icon: 'none'
                        })
                    } else if (code == 10006) {
                        uni.showToast({
                            title: '当前连接已断开',
                            icon: 'none'
                        })
                    } else if (code == 10007) {
                        uni.showToast({
                            title: '当前特征值不支持此操作',
                            icon: 'none'
                        })
                    } else if (code == 10008) {
                        uni.showToast({
                            title: '其余所有系统上报的异常',
                            icon: 'none'
                        })
                    } else if (code == 10009) {
                        uni.showToast({
                            title: 'Android 系统特有,系统版本低于 4.3 不支持 BLE',
                            icon: 'none'
                        })
                    }
                },
                discovering() {
                    uni.showToast({
                        title: '正在搜索附近打印机设备',
                        icon: "none"
                    })
                },
                stateDisable() {
                    uni.showModal({
                        title: '提示',
                        content: '本机蓝牙不可用',
                    })
                },
                search(ing) {
                    self.searching = ing
                },
                change() {
                    self.info = Object.assign({}, self._bluetooth.formation())
                    self.$store.$common.loading(false)
                },
                connection() {
                    self.$store.$common.loading(true)
                    this.change()
                },
                ready() {
                    this.change()
                },
            }

            self._bluetooth = bluetooth.load("print", self._delegate, true)
            self._delegate.change()
        },

        beforeDestroy() {
            this._bluetooth._clear(this._delegate)
        },

        methods: {
            searchBle() {
                if (this.searching) {
                    this._bluetooth.stopSearch()

                } else {
                    this._bluetooth.startSearch()
                }
            },
            connect(item) {
                this._bluetooth.connection(item.deviceId)
            },
            printEsc(els) {
                var command = esc.jpPrinter.createNew()
                command.init()
                var len = els.length
                for (var i = 0; i < len; i++) {
                    var el = els[i]
                    // 加粗
                    el.bold !== undefined && command.bold(el.bold)
                    // 字体大小
                    el.fontSize !== undefined && command.setFontSize(el.fontSize)
                    // 对齐方式
                    el.align !== undefined && command.setSelectJustification(el.align)
                    // 行空白
                    el.rowSpace && command.rowSpace(el.rowSpace)
                    // 绝对定位
                    el.offsetX && command.setAbsolutePrintPosition(el.offsetX)
                    // 打印文字
                    el.text && command.setText(el.text)
                    // 打印换行
                    el.print !== undefined && command.setPrint()
                    // 打印走纸
                    el.printAndFeed !== undefined && command.setPrintAndFeed(el.printAndFeed)
                }

                command.setPrint()
                command.setPrintAndFeedRow(1);
                this._bluetooth.printer().prepareSend(command.getData());
            },

            printClick() {
                this.printEsc(this.print.els)
            },

            receiptTest2() {
                this.printEsc([{
                    bold: 1,
                    fontSize: 16,
                    align: 1,
                    rowSpace: 100,
                    text: '杭州总店',
                    print: 1,
                }, {
                    rowSpace: 60,
                    bold: 0,
                    fontSize: 0,
                    align: 0,
                    text: '时间:2019-11-11 12:10:30',
                    print: 1,
                }, {
                    bold: 0,
                    fontSize: 0,
                    align: 0,
                    text: '编号:SD10000000000000000',
                    printAndFeed: 80,
                }, {
                    bold: 5,
                    rowSpace: 80,
                    text: '货号',
                    printAndFeed: 80,
                }, {
                    offsetX: 80,
                    text: '颜色',
                }, {
                    offsetX: 180,
                    text: '尺码',
                }, {
                    offsetX: 270,
                    text: '单价',
                }, {
                    offsetX: 380,
                    text: '数量',
                }, {
                    offsetX: 480,
                    text: '金额',
                }])
            },

            //票据测试
            receiptTest() {
                var that = this;
                var canvasWidth = that.canvasWidth
                var canvasHeight = that.canvasHeight
                var command = esc.jpPrinter.createNew()
                command.init()
                // 标题
                command.bold(1); //加粗
                command.setFontSize(16); //字体大小
                command.setSelectJustification(1) //居中
                command.rowSpace(100);
                command.setText("杭州总店");
                command.setPrint();
                command.rowSpace(60);

                command.bold(0); //取消加粗
                command.setFontSize(0); //正常字体
                //时间
                command.setSelectJustification(0); //居左
                command.setText("时间:2019-11-11 12:10:30");
                command.setPrint();
                //编号
                command.setSelectJustification(0); //居左
                command.setText("编号:SD10000000000000000");
                command.setPrintAndFeed(80); //打印并走纸feed个单位
                //列表
                command.rowSpace(80); //间距
                command.bold(5); //加粗
                command.setText("货号");
                command.setAbsolutePrintPosition(100);
                command.setText("颜色");
                command.setAbsolutePrintPosition(180);
                command.setText("尺码");
                command.setAbsolutePrintPosition(270);
                command.setText("单价");
                command.setAbsolutePrintPosition(380);
                command.setText("数量");
                command.setAbsolutePrintPosition(480);
                command.setText("金额");
                command.setPrint()
                command.bold(0); //加粗
                // -------1
                command.setText("86001W");
                command.setAbsolutePrintPosition(100);
                command.setText("黄色");
                command.setAbsolutePrintPosition(180);
                command.setText("均码");
                command.setAbsolutePrintPosition(270);
                command.setText("16947.92");
                command.setAbsolutePrintPosition(390);
                command.setText("1");
                command.setAbsolutePrintPosition(480);
                command.setText("19647.92");
                command.setPrint()
                // ------2
                command.setText("86001W");
                command.setAbsolutePrintPosition(100);
                command.setText("黄色");
                command.setAbsolutePrintPosition(180);
                command.setText("均码");
                command.setAbsolutePrintPosition(270);
                command.setText("16947.92");
                command.setAbsolutePrintPosition(390);
                command.setText("1");
                command.setAbsolutePrintPosition(480);
                command.setText("19647.92");
                command.setPrint()
                // -------3
                command.setText("86001W");
                command.setAbsolutePrintPosition(100);
                command.setText("黄色");
                command.setAbsolutePrintPosition(180);
                command.setText("均码");
                command.setAbsolutePrintPosition(270);
                command.setText("16947.92");
                command.setAbsolutePrintPosition(390);
                command.setText("1");
                command.setAbsolutePrintPosition(480);
                command.setText("19647.92");
                command.setPrint();


                //合计
                command.bold(5); //加粗
                command.setAbsolutePrintPosition(120);
                command.setText("总数:10");
                command.setAbsolutePrintPosition(320);
                command.setText("合计:10000");
                command.setPrint();
                command.setAbsolutePrintPosition(120);
                command.setText("实收:10000");
                command.setAbsolutePrintPosition(320);
                command.setText("找零:0");
                command.setPrint();
                // 收银员
                command.rowSpace(120); //间距
                command.setAbsolutePrintPosition(120);
                command.setText("店员:何丹");
                command.setAbsolutePrintPosition(320);
                command.setText("会员:1000000000");
                command.setPrint()

                //提示
                command.rowSpace(80); //间距
                command.bold(2); //加粗
                command.setSelectJustification(1); //居中
                command.setText("售出商品购买后7天内,可凭小票退换");
                command.setPrint();
                command.setText("(注:吊牌未拆剪,商品未洗涤)");
                command.setPrint();

                //电话
                command.setSelectJustification(0); //居左
                command.setText("客服电话:(0571)86011123");
                command.setPrint();
                command.setText("联系地址:浙江省诸暨市暨阳街道健康路1-1号");
                command.setPrint();
                command.setPrintAndFeedRow(1);
                this._bluetooth.printer().prepareSend(command.getData());
            },
        }
    };
</script>

<style>
    .onBluetoothList {
        width: 100%;
        display: flex;
        justify-content: space-around;
        align-items: center;
        height: 60rpx;
    }

    .bluetoothName {
        width: 80%;
    }

    .onBluetoothList image {
        width: 60rpx;
        height: 60rpx;
    }
</style>

附:项目调用参数

{
	"show": "订单编号:wdy2s210429403850",
	"els": [{
		"bold": 1,
		"fontSize": 16,
		"align": 1,
		"rowSpace": 100,
		"text": "测试",
		"print": 1
	}, {
		"bold": 1,
		"fontSize": 16,
		"align": 1,
		"rowSpace": 100,
		"text": "喝牛奶坎坎坷坷",
		"print": 1
	}, {
		"rowSpace": 60,
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "姓名:以后还会",
		"print": 1
	}, {
		"rowSpace": 60,
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "联系方式:15168828260",
		"print": 1
	}, {
		"rowSpace": 60,
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "下单时间:2021-04-29 11:13:05",
		"print": 1
	}, {
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "订单编号:wdys210429403850",
		"print": 1
	}, {
		"bold": 5,
		"rowSpace": 80,
		"text": "商品",
		"printAndFeed": 80
	}, {
		"offsetX": 80,
		"text": "单价"
	}, {
		"offsetX": 180,
		"text": "数量"
	}, {
		"offsetX": 270,
		"text": "总价",
		"print": 1
	}, {
		"bold": 0,
		"rowSpace": 80,
		"text": "农家生咸鸭蛋生新鲜黄",
		"printAndFeed": 80
	}, {
		"offsetX": 80,
		"text": "20"
	}, {
		"offsetX": 180,
		"text": 1
	}, {
		"offsetX": 270,
		"text": "20",
		"print": 1
	}, {
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "运费:0"
	}, {
		"offsetX": 200,
		"text": "优惠金额:0",
		"print": 1
	}, {
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "总计金额:20"
	}, {
		"offsetX": 200,
		"text": "支付金额:20",
		"print": 1
	}, {
		"rowSpace": 60,
		"bold": 0,
		"fontSize": 0,
		"align": 0,
		"text": "地址:江苏省南京市雨花台区",
		"print": 1
	}]
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给钱,谢谢!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值