【Android】Android 集成商米内置打印机打印票据
1、集成商米打印依赖
也可以去Maven看一下有没得最新的版本
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'
implementation group: 'com.sunmi', name: 'printerlibrary', version: '1.0.14'
2、规范接口接口
个人习惯,先写接口,再写功能实现类
import android.content.Context;
public interface SunMiPrintingService {
void bindServicePrinting();
void beforeOrderPrinting(String param);
}
实现接口
public class SunMiPrintingServiceImpl implements SunMiPrintingService {
private SunmiPrinterService sunmiPrinterService;
@Override
@JavascriptInterface
public void bindServicePrinting() {
try {
boolean result = InnerPrinterManager.getInstance().bindService(MainActivity.CONTEXT, new InnerPrinterCallback() {
@Override
protected void onConnected(SunmiPrinterService service) {
if (service != null) {
sunmiPrinterService = service;
}
}
@Override
protected void onDisconnected() {
}
});
if (!result) {
Toast.makeText(MainActivity.CONTEXT, "没有绑定到服务,请检查机型是否支持", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.CONTEXT, "打印机连接成功", Toast.LENGTH_LONG).show();
}
} catch (InnerPrinterException e) {
e.printStackTrace();
}
}
@Override
@JavascriptInterface
public void beforeOrderPrinting(String param) {
System.out.println("预结单参数:" + param);
if (param.isEmpty()) {
Toast.makeText(MainActivity.CONTEXT, "打印参数不能为空", Toast.LENGTH_LONG).show();
}
OrderPrinting entity = new Gson().fromJson(param, OrderPrinting.class);
try {
if (sunmiPrinterService == null) {
Toast.makeText(MainActivity.CONTEXT, "商米打印机:打印对象为空", Toast.LENGTH_LONG).show();
return;
}
if (entity.getOrderDetailList().isEmpty()) {
Toast.makeText(MainActivity.CONTEXT, "商米打印机:商品为空", Toast.LENGTH_LONG).show();
return;
}
sunmiPrinterService.printerInit(null);
int paper = sunmiPrinterService.getPrinterPaper();
sunmiPrinterService.setAlignment(1, null);
sunmiPrinterService.printTextWithFont("预结账单 " + entity.getTakeOrderNumber() + "\n", null, 48F, null);
sunmiPrinterService.printTextWithFont(entity.getStoreName() + "\n", null, 40F, null);
sunmiPrinterService.printText(" \n", null);
setInterval(paper);
sunmiPrinterService.setAlignment(0, null);
sunmiPrinterService.setPrinterStyle(WoyouConsts.SET_LINE_SPACING, 0);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printTextWithFont("桌台号:" + entity.getDeskName() + "\n", null, 28F, null);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printTextWithFont("配送类型:" + entity.getDeskType() + "\n", null, 28F, null);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printTextWithFont("订单号:" + entity.getOrderNumber() + "\n", null, 28F, null);
sunmiPrinterService.printText(" \n", null);
setInterval(paper);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.setPrinterStyle(WoyouConsts.ENABLE_BOLD, WoyouConsts.ENABLE);
String[] txts = new String[]{"商品", "数量", "单价"};
int[] width = new int[]{2, 2, 2};
int[] align = new int[]{0, 1, 2};
sunmiPrinterService.printColumnsString(txts, width, align, null);
sunmiPrinterService.setPrinterStyle(WoyouConsts.ENABLE_BOLD, WoyouConsts.DISABLE);
for (OrderPrintingDetail item : entity.getOrderDetailList()) {
sunmiPrinterService.printText(" \n", null);
if (IntegerUtils.eq(item.getSpuAttrType(), 2)) {
txts[0] = isEmptyValue(item.getSpuTitle());
txts[1] = "";
txts[2] = "";
sunmiPrinterService.printColumnsString(txts, width, align, null);
txts[0] = isEmptyValue(item.getSpuDescription());
} else {
txts[0] = isEmptyValue(item.getSpuTitle());
}
txts[1] = isEmptyValue(item.getNumber());
txts[2] = isEmptyValue(item.getSpuSellPrice());
sunmiPrinterService.printColumnsString(txts, width, align, null);
if (item.getSpuRemark() != null) {
txts[0] = isEmptyValue(item.getSpuRemark());
txts[1] = "";
txts[2] = "";
sunmiPrinterService.printColumnsString(txts, width, align, null);
}
}
sunmiPrinterService.printText(" \n", null);
setInterval(paper);
if (entity.getOrderRemark() != null) {
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printTextWithFont("订单备注:" + entity.getOrderRemark() + "\n", null, 28F, null);
}
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printTextWithFont("预付金额:" + entity.getOriginPrice() + "\n", null, 40F, null);
setInterval(paper);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.printText(" \n", null);
sunmiPrinterService.cutPaper(null);
} catch (RemoteException e) {
Toast.makeText(MainActivity.CONTEXT, "打印异常:" + e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
public String isEmptyValue(Object value) {
if (value == null) {
return "";
}
return String.valueOf(value);
}
public void setInterval(int paper) throws RemoteException {
if (paper == 1) {
sunmiPrinterService.printText("--------------------------------\n", null);
} else {
sunmiPrinterService.printText("------------------------------------------------\n", null);
}
}
}
3、使用到的相关对象以及工具类
订单对象
@Data
@Accessors(chain = true)
public class OrderPrinting {
private Long id;
private String storeName;
private String takeOrderNumber;
private String deskName;
private String deskType;
private String orderNumber;
private String serialNumber;
private List<OrderPrintingDetail> orderDetailList;
private BigDecimal originPrice;
private BigDecimal reducePrice;
private BigDecimal integerPrice;
private String payWayName;
private BigDecimal payMoney;
private String placeOrderTime;
private String payTime;
private String orderRemark;
}
订单商品对象
@Data
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class OrderPrintingDetail {
private Long spuAttrType;
private String spuTitle;
private String spuDescription;
private BigDecimal number;
private BigDecimal spuSellPrice;
private String spuRemark;
}
使用到的比较工具类
public class IntegerUtils {
public static boolean eq(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() == val2.intValue();
}
return false;
}
public static boolean eq(Long val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() == val2.intValue();
}
return false;
}
public static boolean gt(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() > val2.intValue();
}
return false;
}
public static boolean gt(Long val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() > val2.intValue();
}
return false;
}
public static boolean lt(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() < val2.intValue();
}
return false;
}
public static boolean ge(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return (val1.intValue() > val2.intValue()) || (val1.intValue() == val2.intValue());
}
return false;
}
public static boolean le(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return (val1.intValue() < val2.intValue()) || (val1.intValue() == val2.intValue());
}
return false;
}
public static boolean ne(Integer val1, Integer val2) {
if (val1 != null && val2 != null) {
return val1.intValue() != val2.intValue();
}
return false;
}
}
4、MainActivity初始化接口
SunMiPrintingService sunmiPrintingService = new SunMiPrintingServiceImpl();
sunmiPrintingService.bindServicePrinting();
5、Uniapp调用方法
在接口方法已经使用了@JavascriptInterface注解,可以使用JavaScript直接调用
function makeOrderPrinting(param) {
window.sunMiPrinting.makeOrderPrinting(param);
}
微信公众号
|