其他分享
首页 > 其他分享> > 【Android】Android 集成芯烨云打印机实现打印票据

【Android】Android 集成芯烨云打印机实现打印票据

作者:互联网

文章目录

【Android】Android 集成芯烨云打印机实现打印票据

1、集成芯烨云依赖

打开官网,下载对应的依赖模块,芯烨云官网,安卓和Java都下载Java版本的,下载完成之后,全部复制到项目里面,懒得打包官网提供的源码了

在这里插入图片描述

2、配置账号秘钥

找到util下面的Config,修改成自己的账号,key
在这里插入图片描述

3、编写打印接口

import android.content.Intent;
import android.os.Build;
import android.webkit.JavascriptInterface;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhhy.qneatrice.app.cashier.MainActivity;
import com.zhhy.qneatrice.app.cashier.entity.BizOrderInfoVo;

import net.xpyun.platform.opensdk.service.PrintService;
import net.xpyun.platform.opensdk.util.Config;
import net.xpyun.platform.opensdk.util.NoteFormatter;
import net.xpyun.platform.opensdk.vo.PrintRequest;

import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

public class BusinessService {

    private static final PrintService printService = new PrintService();

    /**
     * 配送单
     *
     * @param param
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    @JavascriptInterface
    public void deliverOrderPrinter(String param) throws Exception {
        System.out.println("配送单参数:" + param);
        if (StringUtils.isEmpty(param)) {
            Toast.makeText(MainActivity.CONTEXT, "配送单不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        JSONObject map = JSONObject.parseObject(param);
        String printerNumber = String.valueOf(map.get("printerNumber"));
        System.out.println("打印机编号:" + printerNumber);
        if (StringUtils.isEmpty(printerNumber)) {
            Toast.makeText(MainActivity.CONTEXT, "打印机编号不能为空", Toast.LENGTH_SHORT).show();
            return;
        }
        String[] printer = printerNumber.split(",");
        PrintRequest request = new PrintRequest();
        Config.createRequestHeader(request);
        request.setMode(0);
        for (int i = 0; i < printer.length; i++) {
            List<BizOrderInfoVo> totalList = JSON.parseArray((String) map.get("orderInfoVoList"), BizOrderInfoVo.class);
            StringBuilder content = new StringBuilder();
            // 打印配送单
            content.append("<CB>配送单<BR></CB>");
            content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
            content.append("部门")
                    .append(org.apache.commons.lang3.StringUtils.repeat(" ", 12))
                    .append("位置").append(org.apache.commons.lang3.StringUtils.repeat(" ", 6))
                    .append("份数").append(org.apache.commons.lang3.StringUtils.repeat(" ", 2))
                    .append("<BR>");
            content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
            for (BizOrderInfoVo total : totalList) {
                if (StringUtils.isEmpty(total.getDeptName())) {
                    total.setDeptName("未设置");
                }
                if (StringUtils.isEmpty(total.getDeptAddress())) {
                    total.setDeptAddress("未设置");
                }
                content.append(NoteFormatter.formatPrintOrderItemMy(total.getDeptName(), total.getDeptAddress(), total.getCountNumber()));
            }
            content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
            int sum = totalList.stream().collect(Collectors.summingInt(BizOrderInfoVo::getCountNumber));
            content.append(NoteFormatter.formatPrintOrderItemMy("总计", "", sum));
            content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
            content.append("<L>打印时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "<BR></L>");
            content.append("<L>部门签字:<BR></L>");
            content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");

            request.setCopies(2);
            request.setSn(printer[i]);
            request.setContent(content.toString());
            printService.print(request);
            content.setLength(0);
            // 打印配送分单
            for (BizOrderInfoVo total : totalList) {
                content.append("<CB>配送分单<BR></CB>");
                content.append("<CB>" + total.getDeptName() + "<BR></CB>");
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
                content.append("<L>部门名称:" + total.getDeptName() + "<BR></L>");
                content.append("<L>部门位置:" + total.getDeptAddress() + "<BR></L>");
                content.append("<L>总份数:" + total.getCountNumber() + "<BR></L>");
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
                content.append("电话")
                        .append(org.apache.commons.lang3.StringUtils.repeat(" ", 12))
                        .append("姓名").append(org.apache.commons.lang3.StringUtils.repeat(" ", 6))
                        .append("数量").append(org.apache.commons.lang3.StringUtils.repeat(" ", 2))
                        .append("<BR>");
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
                List<BizOrderInfoVo> orderList = total.getOrderInfoVoList();
                for (BizOrderInfoVo order : orderList) {
                    if (StringUtils.isEmpty(total.getUserName())) {
                        total.setUserName("未设置");
                    }
                    if (StringUtils.isEmpty(total.getNickName())) {
                        total.setNickName("未设置");
                    }
                    content.append(NoteFormatter.formatPrintOrderItemMy(order.getUserName(), order.getNickName(), order.getCountNumber()));
                }
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
                int orderSum = orderList.stream().collect(Collectors.summingInt(BizOrderInfoVo::getCountNumber));
                content.append(NoteFormatter.formatPrintOrderItemMy("总计", "", orderSum));
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
                content.append("<L>打印时间:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + "<BR></L>");
                content.append("<L>部门签字:<BR></L>");
                content.append(org.apache.commons.lang3.StringUtils.repeat("-", 32)).append("<BR>");
            }
            request.setCopies(1);
            request.setSn(printer[i]);
            request.setContent(content.toString());
            printService.print(request);
        }
    }
}

3、MainActivity初始化接口

// 初始化业务接口
BusinessService businessService = new BusinessService();
webView.addJavascriptInterface(businessService, "businessService");

4、Uniapp调用方法

在接口方法已经使用了@JavascriptInterface注解,可以使用JavaScript直接调用

/**
 * 打印配送单
 */
function deliverOrderPrinter(content) {
    window.businessService.deliverOrderPrinter(content);
}

微信公众号

每天Get一个小技巧

标签:打印机,commons,StringUtils,content,lang3,import,Android,芯烨云,append
来源: https://blog.csdn.net/qq_38762237/article/details/120991365