ERP-admin/src/utils/print.ts

492 lines
14 KiB
TypeScript
Raw Normal View History

2024-05-22 17:26:44 +08:00
import { hiprint, defaultElementTypeProvider } from "vue-plugin-hiprint";
2024-05-27 18:10:04 +08:00
import { cashierclassSetPrintAdd } from "@/api/opurchaseclass";
2024-05-22 17:26:44 +08:00
// 引入后使用示例
hiprint.init();
hiprint.hiwebSocket.setHost("http://localhost:17521");
2024-05-23 18:45:48 +08:00
// window.open("hiprint://");
2024-05-22 17:26:44 +08:00
// 配置
const WIDTH = 58; //纸张宽度 mm
2024-05-27 18:10:04 +08:00
const HEIGHT = 155; //纸张高度 mm 使用时需另外计算商品高度
2024-05-22 17:26:44 +08:00
const T_WIDTH = 150; //文本宽度 pt
const T_LEFT = 4; //文本左边距 pt
2024-05-27 18:10:04 +08:00
const textHeight = 10; //文本高度
2024-05-22 17:26:44 +08:00
export const print = (data: any) => {
// hiprint对象获取
const list = hiprint.hiwebSocket.getPrinterList();
console.log(list);
2024-05-25 15:15:08 +08:00
let nowHeight = 0;
2024-05-22 17:26:44 +08:00
// 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
// 想要实现拖拽设计页面,请往下看 '自定义设计'
var hiprintTemplate = new hiprint.PrintTemplate();
2024-05-25 15:15:08 +08:00
// 纸张高度 固定为 130 pt + 商品数量高度
2024-05-27 18:10:04 +08:00
let oneHeight = HEIGHT + Math.ceil((data.info.length * 2 * 10) / 2.84);
2024-05-25 15:15:08 +08:00
2024-05-22 17:26:44 +08:00
// 模板宽度单位是mm ( 1mm ~= 2.84pt ) 其他的宽高单位是pt
var panel = hiprintTemplate.addPrintPanel({
2024-05-27 18:10:04 +08:00
width: WIDTH, // 58mm = 164pt
2024-05-25 15:15:08 +08:00
height: oneHeight,
2024-05-22 17:26:44 +08:00
paperNumberDisabled: true,
});
2024-05-25 15:15:08 +08:00
let options = (e: any) => {
nowHeight += textHeight;
let opt = {
2024-05-22 17:26:44 +08:00
width: T_WIDTH,
2024-05-25 15:15:08 +08:00
height: textHeight,
top: nowHeight,
2024-05-22 17:26:44 +08:00
left: T_LEFT,
2024-05-25 15:15:08 +08:00
title: "",
};
if (typeof e === "string") opt.title = e;
else opt = Object.assign(opt, e);
return {
options: opt,
};
};
let lineTitle = () => {
nowHeight += textHeight;
let left = Math.floor(T_WIDTH / 3);
panel.addPrintText({
options: {
width: left,
height: 10,
top: nowHeight,
left: 0,
title: "单价",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left,
title: "数量",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left * 2,
title: "小计",
textAlign: "center",
},
});
};
let lineOptions = (text1: any, text2: any, text3: any) => {
nowHeight += textHeight;
let left = Math.floor(T_WIDTH / 3);
panel.addPrintText({
options: {
width: left,
height: 10,
top: nowHeight,
left: 0,
title: text1 + "",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left,
title: text2 + "",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left * 2,
title: text3 + "",
textAlign: "center",
},
});
};
2024-05-27 18:10:04 +08:00
//文本 *3
panel.addPrintText(options("======================"));
panel.addPrintText(options(""));
2024-05-25 15:15:08 +08:00
panel.addPrintText(
options({
title: "泸优采-采购单",
2024-05-22 17:26:44 +08:00
textAlign: "center",
2024-05-25 15:15:08 +08:00
})
);
//文本 *6
panel.addPrintText(options("单号: " + data.number));
panel.addPrintText(options("配送时间: " + data.create_time));
panel.addPrintText(options("配送员: " + data.mer_nickname));
panel.addPrintText(options("配送电话: " + data.mer_phone));
panel.addPrintText(options("======================"));
panel.addPrintText(options("商品信息: "));
//格式化 *1 + x * y
lineTitle();
data.info.forEach((item: any) => {
panel.addPrintText(options(item.goods_name));
2024-05-27 18:10:04 +08:00
if (item.nums == Math.floor(+item.nums))
item.nums = Number(item.nums).toFixed(0);
2024-05-25 15:15:08 +08:00
lineOptions(
`${item.price}`,
`${item.nums}${item.unit_name}`,
`${item.total}`
);
2024-05-22 17:26:44 +08:00
});
2024-05-25 15:15:08 +08:00
//文本 *5
panel.addPrintText(options("======================"));
panel.addPrintText(options(`合计: ${data.total}`));
panel.addPrintText(options("提货点: " + data.mer_name));
panel.addPrintText(options("提货点电话: " + data.mer_phone));
panel.addPrintText(options("提货点负责人签字:"));
// https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png //无字
// https://lihai001.oss-cn-chengdu.aliyuncs.com/def/54705202405221504133485.png //有字
// panel.addPrintRect({ options: { width: T_WIDTH, height:30,top: nowHeight + 15, left: T_LEFT,borderColor:'',borderWidth:0.75 } });
// nowHeight+=40;
// 文本 *6
panel.addPrintImage({
2024-05-22 17:26:44 +08:00
options: {
width: T_WIDTH,
2024-05-25 15:15:08 +08:00
height: 50,
top: nowHeight + 15,
2024-05-22 17:26:44 +08:00
left: T_LEFT,
2024-05-25 15:15:08 +08:00
title: "",
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
2024-05-22 17:26:44 +08:00
},
});
2024-05-25 15:15:08 +08:00
nowHeight += 60;
2024-05-27 18:10:04 +08:00
2024-05-25 15:15:08 +08:00
// 文本 *4
panel.addPrintText(options("收货人: " + data.real_name));
panel.addPrintText(options("收货地址: " + data.user_address));
panel.addPrintText(options("联系电话: " + data.user_phone));
panel.addPrintText(options("收货人签字:"));
// panel.addPrintRect({ options: { width: T_WIDTH, height:30,top: nowHeight+15, left: T_LEFT,borderColor:'',borderWidth:0.75 } });
// 文本 *6
panel.addPrintImage({
2024-05-22 17:26:44 +08:00
options: {
width: T_WIDTH,
2024-05-25 15:15:08 +08:00
height: 50,
top: nowHeight + 15,
2024-05-22 17:26:44 +08:00
left: T_LEFT,
2024-05-25 15:15:08 +08:00
title: "",
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
2024-05-22 17:26:44 +08:00
},
});
2024-05-25 15:15:08 +08:00
nowHeight += 60;
2024-05-27 18:10:04 +08:00
// 文本 *3
panel.addPrintText(options("出库码: "));
panel.addPrintText(options(""));
panel.addPrintText(options(""));
// 文本 *4
panel.addPrintText({ options: { width: T_WIDTH, height: 35, top: nowHeight, left: T_LEFT, title: data.number, textType: 'barcode',textAlign: "center" } });
nowHeight += 40
2024-05-25 15:15:08 +08:00
// 文本 *4
panel.addPrintText(options(""));
panel.addPrintText(options(""));
panel.addPrintText(options(""));
panel.addPrintText(options("======================"));
// 合计高度 23 + length * 2
2024-05-22 17:26:44 +08:00
//打印
2024-05-27 18:10:04 +08:00
hiprintTemplate.print({});
2024-05-22 17:26:44 +08:00
//直接打印,需要安装客户端
2024-05-27 18:10:04 +08:00
// hiprintTemplate.print2({});
2024-05-22 17:26:44 +08:00
// 直接打印回调
// 发送任务到打印机成功
hiprintTemplate.on("printSuccess", (e: any) => {
console.log("printSuccess", e);
2024-05-27 18:10:04 +08:00
ElMessage.success("订单已加入打印队列");
cashierclassSetPrintAdd({
id: data.id,
});
2024-05-22 17:26:44 +08:00
});
// 发送任务到打印机失败
hiprintTemplate.on("printError", (e: any) => {
console.log("printError", e);
2024-05-27 18:10:04 +08:00
ElMessage.error("打印失败,请检查是否正确连接打印机!");
2024-05-22 17:26:44 +08:00
});
};
export const testPrint = () => {
// hiprint对象获取
const list = hiprint.hiwebSocket.getPrinterList();
console.log(list);
let nowHeight = 0;
// 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
// 想要实现拖拽设计页面,请往下看 '自定义设计'
var hiprintTemplate = new hiprint.PrintTemplate();
2024-05-27 18:10:04 +08:00
// 纸张高度 固定为 130 pt + 商品数量高度
let oneHeight = HEIGHT + Math.ceil((9 * 2 * 10) / 2.84);
2024-05-22 17:26:44 +08:00
// 模板宽度单位是mm ( 1mm ~= 2.84pt ) 其他的宽高单位是pt
var panel = hiprintTemplate.addPrintPanel({
2024-05-27 18:10:04 +08:00
width: WIDTH, // 58mm = 164pt
height: oneHeight,
2024-05-22 17:26:44 +08:00
paperNumberDisabled: true,
});
2024-05-25 15:15:08 +08:00
let options = (e: any) => {
nowHeight += textHeight;
2024-05-22 17:26:44 +08:00
let opt = {
width: T_WIDTH,
height: textHeight,
top: nowHeight,
left: T_LEFT,
2024-05-25 15:15:08 +08:00
title: "",
};
if (typeof e === "string") opt.title = e;
2024-05-22 17:26:44 +08:00
else opt = Object.assign(opt, e);
return {
2024-05-25 15:15:08 +08:00
options: opt,
};
};
2024-05-22 17:26:44 +08:00
2024-05-25 15:15:08 +08:00
let lineTitle = () => {
nowHeight += textHeight;
let left = Math.floor(T_WIDTH / 3);
2024-05-22 17:26:44 +08:00
panel.addPrintText({
options: {
width: left,
height: 10,
top: nowHeight,
left: 0,
title: "单价",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left,
title: "数量",
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left * 2,
title: "小计",
textAlign: "center",
},
});
2024-05-25 15:15:08 +08:00
};
2024-05-22 17:26:44 +08:00
2024-05-25 15:15:08 +08:00
let lineOptions = (text1: any, text2: any, text3: any) => {
nowHeight += textHeight;
let left = Math.floor(T_WIDTH / 3);
2024-05-22 17:26:44 +08:00
panel.addPrintText({
options: {
width: left,
height: 10,
top: nowHeight,
left: 0,
2024-05-25 15:15:08 +08:00
title: text1 + "",
2024-05-22 17:26:44 +08:00
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left,
2024-05-25 15:15:08 +08:00
title: text2 + "",
2024-05-22 17:26:44 +08:00
textAlign: "center",
},
});
panel.addPrintText({
options: {
width: left,
height: textHeight,
top: nowHeight,
left: left * 2,
2024-05-25 15:15:08 +08:00
title: text3 + "",
2024-05-22 17:26:44 +08:00
textAlign: "center",
},
});
2024-05-25 15:15:08 +08:00
};
2024-05-22 17:26:44 +08:00
2024-05-27 18:10:04 +08:00
//文本 *3
panel.addPrintText(options("======================"));
panel.addPrintText(options(""));
2024-05-25 15:15:08 +08:00
panel.addPrintText(
options({
2024-05-27 18:10:04 +08:00
title: "泸优采-小票测试",
2024-05-25 15:15:08 +08:00
textAlign: "center",
})
);
2024-05-22 17:26:44 +08:00
2024-05-27 18:10:04 +08:00
2024-05-22 17:26:44 +08:00
//文本 *6
panel.addPrintText(options("单号: PF171617436315965155"));
panel.addPrintText(options("配送时间: 2024-05-20 11:06:03"));
panel.addPrintText(options("配送员: 二狗"));
panel.addPrintText(options("配送电话: 19330904744"));
panel.addPrintText(options("======================"));
panel.addPrintText(options("商品信息: "));
//格式化 *1 + 2*3
lineTitle();
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
2024-05-27 18:10:04 +08:00
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
panel.addPrintText(options("朝天椒, 辣椒"));
lineOptions("0.01元", "25个", "0.25元");
2024-05-22 17:26:44 +08:00
2024-05-27 18:10:04 +08:00
//文本 *5
2024-05-22 17:26:44 +08:00
panel.addPrintText(options("======================"));
2024-05-25 15:15:08 +08:00
panel.addPrintText(options("合计: 0.75元"));
panel.addPrintText(options("提货点: 莲花农贸市场"));
panel.addPrintText(options("提货点电话: 19330904744"));
panel.addPrintText(options("提货点负责人签字:"));
2024-05-22 17:26:44 +08:00
// https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png //无字
// https://lihai001.oss-cn-chengdu.aliyuncs.com/def/54705202405221504133485.png //有字
// panel.addPrintRect({ options: { width: T_WIDTH, height:30,top: nowHeight + 15, left: T_LEFT,borderColor:'',borderWidth:0.75 } });
// nowHeight+=40;
2024-05-27 18:10:04 +08:00
// 文本 *6
2024-05-22 17:26:44 +08:00
panel.addPrintImage({
options: {
width: T_WIDTH,
2024-05-25 15:15:08 +08:00
height: 50,
top: nowHeight + 15,
left: T_LEFT,
title: "",
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
},
});
nowHeight += 60;
2024-05-27 18:10:04 +08:00
// 文本 *4
2024-05-25 15:15:08 +08:00
panel.addPrintText(options("收货人: 阿哈"));
panel.addPrintText(options("收货地址: 里海科技"));
panel.addPrintText(options("联系电话: 17685151643"));
panel.addPrintText(options("收货人签字:"));
2024-05-22 17:26:44 +08:00
// panel.addPrintRect({ options: { width: T_WIDTH, height:30,top: nowHeight+15, left: T_LEFT,borderColor:'',borderWidth:0.75 } });
2024-05-27 18:10:04 +08:00
// 文本 *6
2024-05-22 17:26:44 +08:00
panel.addPrintImage({
options: {
width: T_WIDTH,
height: 50,
2024-05-25 15:15:08 +08:00
top: nowHeight + 15,
left: T_LEFT,
title: "",
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
},
});
nowHeight += 60;
2024-05-22 17:26:44 +08:00
2024-05-27 18:10:04 +08:00
panel.addPrintText(options("出库码: "));
// 文本 *4
panel.addPrintText({ options: { width: T_WIDTH, height: 35, top: nowHeight, left: T_LEFT, title: '65155', textType: 'barcode',textAlign: "center" } });
nowHeight += 40
// 文本 *1
2024-05-22 17:26:44 +08:00
panel.addPrintText(options(""));
2024-05-27 18:10:04 +08:00
// 文本 *3
2024-05-22 17:26:44 +08:00
panel.addPrintText(options(""));
panel.addPrintText(options(""));
panel.addPrintText(options("======================"));
2024-05-25 15:15:08 +08:00
// 合计高度 23 + length * 2
2024-05-22 17:26:44 +08:00
//打印
2024-05-27 18:10:04 +08:00
hiprintTemplate.print({});
2024-05-22 17:26:44 +08:00
//直接打印,需要安装客户端
2024-05-27 18:10:04 +08:00
// hiprintTemplate.print2({});
2024-05-22 17:26:44 +08:00
// 直接打印回调
// 发送任务到打印机成功
hiprintTemplate.on("printSuccess", (e: any) => {
console.log("printSuccess", e);
2024-05-27 18:10:04 +08:00
ElMessage.success("订单已加入打印队列");
2024-05-22 17:26:44 +08:00
});
// 发送任务到打印机失败
hiprintTemplate.on("printError", (e: any) => {
console.log("printError", e);
2024-05-27 18:10:04 +08:00
ElMessage.error("打印失败,请检查是否正确连接打印机!");
2024-05-22 17:26:44 +08:00
});
};
2024-05-25 15:15:08 +08:00
export const printerList = () => {
try {
2024-05-23 18:45:48 +08:00
const list = hiprint.hiwebSocket.getPrinterList();
return list;
2024-05-25 15:15:08 +08:00
} catch {
ElMessage.error("请先安装打印机客户端");
2024-05-23 18:45:48 +08:00
return [];
}
2024-05-25 15:15:08 +08:00
};
2024-05-23 18:45:48 +08:00
2024-05-22 17:26:44 +08:00
// 计算字符串长度
function calculateStringLength(str: string) {
let count = 0;
for (let i = 0; i < str.length; i++) {
const charCode = str.charCodeAt(i);
// 检查字符是否在汉字的Unicode范围内注意这也会包括日韩文字等更精确的检查可能需要更复杂的逻辑
if (
(charCode >= 0x4e00 && charCode <= 0x9fff) ||
(charCode >= 0x3400 && charCode <= 0x4dff) ||
(charCode >= 0x20000 && charCode <= 0x2ffff)
) {
count += 2; // 汉字算2个字符
} else {
count += 1; // 其他字符算1个字符
}
}
return count;
}