492 lines
14 KiB
TypeScript
492 lines
14 KiB
TypeScript
import { hiprint, defaultElementTypeProvider } from "vue-plugin-hiprint";
|
||
import { cashierclassSetPrintAdd } from "@/api/opurchaseclass";
|
||
|
||
// 引入后使用示例
|
||
hiprint.init();
|
||
hiprint.hiwebSocket.setHost("http://localhost:17521");
|
||
|
||
// window.open("hiprint://");
|
||
|
||
// 配置
|
||
const WIDTH = 58; //纸张宽度 mm
|
||
const HEIGHT = 155; //纸张高度 mm 使用时需另外计算商品高度
|
||
const T_WIDTH = 150; //文本宽度 pt
|
||
const T_LEFT = 4; //文本左边距 pt
|
||
const textHeight = 10; //文本高度
|
||
|
||
export const print = (data: any) => {
|
||
// hiprint对象获取
|
||
const list = hiprint.hiwebSocket.getPrinterList();
|
||
console.log(list);
|
||
|
||
let nowHeight = 0;
|
||
|
||
// 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
|
||
// 想要实现拖拽设计页面,请往下看 '自定义设计'
|
||
var hiprintTemplate = new hiprint.PrintTemplate();
|
||
|
||
// 纸张高度 固定为 130 pt + 商品数量高度
|
||
let oneHeight = HEIGHT + Math.ceil((data.info.length * 2 * 10) / 2.84);
|
||
|
||
// 模板宽度单位是mm ( 1mm ~= 2.84pt ) 其他的宽高单位是pt
|
||
var panel = hiprintTemplate.addPrintPanel({
|
||
width: WIDTH, // 58mm = 164pt
|
||
height: oneHeight,
|
||
paperNumberDisabled: true,
|
||
});
|
||
|
||
let options = (e: any) => {
|
||
nowHeight += textHeight;
|
||
let opt = {
|
||
width: T_WIDTH,
|
||
height: textHeight,
|
||
top: nowHeight,
|
||
left: T_LEFT,
|
||
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",
|
||
},
|
||
});
|
||
};
|
||
|
||
//文本 *3
|
||
panel.addPrintText(options("======================"));
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(
|
||
options({
|
||
title: "泸优采-采购单",
|
||
textAlign: "center",
|
||
})
|
||
);
|
||
|
||
//文本 *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));
|
||
if (item.nums == Math.floor(+item.nums))
|
||
item.nums = Number(item.nums).toFixed(0);
|
||
lineOptions(
|
||
`${item.price}元`,
|
||
`${item.nums}${item.unit_name}`,
|
||
`${item.total}元`
|
||
);
|
||
});
|
||
|
||
//文本 *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({
|
||
options: {
|
||
width: T_WIDTH,
|
||
height: 50,
|
||
top: nowHeight + 15,
|
||
left: T_LEFT,
|
||
title: "",
|
||
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
|
||
},
|
||
});
|
||
nowHeight += 60;
|
||
|
||
// 文本 *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({
|
||
options: {
|
||
width: T_WIDTH,
|
||
height: 50,
|
||
top: nowHeight + 15,
|
||
left: T_LEFT,
|
||
title: "",
|
||
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
|
||
},
|
||
});
|
||
nowHeight += 60;
|
||
|
||
// 文本 *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
|
||
|
||
// 文本 *4
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(options("======================"));
|
||
|
||
// 合计高度 23 + length * 2
|
||
|
||
//打印
|
||
hiprintTemplate.print({});
|
||
//直接打印,需要安装客户端
|
||
// hiprintTemplate.print2({});
|
||
// 直接打印回调
|
||
// 发送任务到打印机成功
|
||
hiprintTemplate.on("printSuccess", (e: any) => {
|
||
console.log("printSuccess", e);
|
||
ElMessage.success("订单已加入打印队列");
|
||
cashierclassSetPrintAdd({
|
||
id: data.id,
|
||
});
|
||
});
|
||
// 发送任务到打印机失败
|
||
hiprintTemplate.on("printError", (e: any) => {
|
||
console.log("printError", e);
|
||
ElMessage.error("打印失败,请检查是否正确连接打印机!");
|
||
});
|
||
};
|
||
|
||
export const testPrint = () => {
|
||
// hiprint对象获取
|
||
const list = hiprint.hiwebSocket.getPrinterList();
|
||
console.log(list);
|
||
|
||
let nowHeight = 0;
|
||
|
||
// 下列方法都是没有拖拽设计页面的, 相当于代码模式, 使用代码设计页面
|
||
// 想要实现拖拽设计页面,请往下看 '自定义设计'
|
||
var hiprintTemplate = new hiprint.PrintTemplate();
|
||
|
||
// 纸张高度 固定为 130 pt + 商品数量高度
|
||
let oneHeight = HEIGHT + Math.ceil((9 * 2 * 10) / 2.84);
|
||
|
||
// 模板宽度单位是mm ( 1mm ~= 2.84pt ) 其他的宽高单位是pt
|
||
var panel = hiprintTemplate.addPrintPanel({
|
||
width: WIDTH, // 58mm = 164pt
|
||
height: oneHeight,
|
||
paperNumberDisabled: true,
|
||
});
|
||
|
||
let options = (e: any) => {
|
||
nowHeight += textHeight;
|
||
let opt = {
|
||
width: T_WIDTH,
|
||
height: textHeight,
|
||
top: nowHeight,
|
||
left: T_LEFT,
|
||
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",
|
||
},
|
||
});
|
||
};
|
||
|
||
|
||
//文本 *3
|
||
panel.addPrintText(options("======================"));
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(
|
||
options({
|
||
title: "泸优采-小票测试",
|
||
textAlign: "center",
|
||
})
|
||
);
|
||
|
||
|
||
|
||
//文本 *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元");
|
||
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元");
|
||
|
||
//文本 *5
|
||
panel.addPrintText(options("======================"));
|
||
panel.addPrintText(options("合计: 0.75元"));
|
||
|
||
panel.addPrintText(options("提货点: 莲花农贸市场"));
|
||
panel.addPrintText(options("提货点电话: 19330904744"));
|
||
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({
|
||
options: {
|
||
width: T_WIDTH,
|
||
height: 50,
|
||
top: nowHeight + 15,
|
||
left: T_LEFT,
|
||
title: "",
|
||
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
|
||
},
|
||
});
|
||
nowHeight += 60;
|
||
|
||
// 文本 *4
|
||
panel.addPrintText(options("收货人: 阿哈"));
|
||
panel.addPrintText(options("收货地址: 里海科技"));
|
||
panel.addPrintText(options("联系电话: 17685151643"));
|
||
panel.addPrintText(options("收货人签字:"));
|
||
// panel.addPrintRect({ options: { width: T_WIDTH, height:30,top: nowHeight+15, left: T_LEFT,borderColor:'',borderWidth:0.75 } });
|
||
|
||
// 文本 *6
|
||
panel.addPrintImage({
|
||
options: {
|
||
width: T_WIDTH,
|
||
height: 50,
|
||
top: nowHeight + 15,
|
||
left: T_LEFT,
|
||
title: "",
|
||
src: "https://lihai001.oss-cn-chengdu.aliyuncs.com/def/db264202405221455038529.png",
|
||
},
|
||
});
|
||
nowHeight += 60;
|
||
|
||
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
|
||
panel.addPrintText(options(""));
|
||
|
||
|
||
// 文本 *3
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(options(""));
|
||
panel.addPrintText(options("======================"));
|
||
|
||
// 合计高度 23 + length * 2
|
||
|
||
//打印
|
||
hiprintTemplate.print({});
|
||
//直接打印,需要安装客户端
|
||
// hiprintTemplate.print2({});
|
||
// 直接打印回调
|
||
// 发送任务到打印机成功
|
||
hiprintTemplate.on("printSuccess", (e: any) => {
|
||
console.log("printSuccess", e);
|
||
ElMessage.success("订单已加入打印队列");
|
||
});
|
||
// 发送任务到打印机失败
|
||
hiprintTemplate.on("printError", (e: any) => {
|
||
console.log("printError", e);
|
||
ElMessage.error("打印失败,请检查是否正确连接打印机!");
|
||
});
|
||
};
|
||
|
||
export const printerList = () => {
|
||
try {
|
||
const list = hiprint.hiwebSocket.getPrinterList();
|
||
return list;
|
||
} catch {
|
||
ElMessage.error("请先安装打印机客户端");
|
||
return [];
|
||
}
|
||
};
|
||
|
||
// 计算字符串长度
|
||
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;
|
||
}
|