emis-frontend/src/views/emis/EmisBaseTools/TanexPrinterPanel.vue
2024-10-15 07:43:50 +08:00

856 lines
27 KiB
Vue

<template>
<div>
<!-- 打印机状态 -->
<!-- style="padding:5px 0px;text-align: right;position: fixed;top: 150px;right: 10px; z-index: 9999;" -->
<div >
<div style="display: inline-flex;">
<div v-if="printerStatus==0" style="display: inline-flex;">
<el-tag type="danger" >{{ printerStatusMessage }}</el-tag>
</div>
<div v-if="printerStatus==1" style="display: inline-flex;">
<div v-if="isShowTpl==1" style="font-size: 12px; line-height: 28px;font-weight: 600;">模板:</div>
<div v-if="isShowTpl==1" style="width: 150px;">
<el-select v-model="selTplCode" size="mini" placeholder="选择打印模板" clearable filterable>
<template slot="prefix">
<span style="padding: 5px;line-height: 28px;font-size: 16px;color: rgb(111 111 111);cursor: pointer;" @click="refreshPrintTpl()">
<i class="el-icon-refresh"></i>
</span>
</template>
<el-option
v-for="item in printTplList"
:key="item.tplCode"
:label="item.tplName"
:value="item.tplCode"
/>
</el-select>
</div>
<div v-if="(selTplCode=='TAN-GC-130' || selTplCode=='TAN-ENGC-130') && isShowSubBillRange" class="subBillRange" style="display: inline-flex;font-size: 12px; line-height: 28px; font-weight: 600;">
<span >子单范围:
<el-input size="mini" style="width:40px" v-model="subBillStart" type="text" maxlength="4" ></el-input>
-
<el-input size="mini" style="width:40px" v-model="subBillEnd" type="text" maxlength="4" ></el-input>
</span>
</div>
<div v-if="isShowPinters==1" style="font-size: 12px;line-height: 28px;font-weight: 600;">选择打印机:</div>
<div v-if="isShowPinters==1" style="width: 150px;">
<el-select v-model="selPrinterName" size="mini" placeholder="打印机" clearable filterable>
<el-option
v-for="item in printerList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</div>
</div>
<div v-if="printerStatus==1" style="display: inline-flex;">
<!-- <span style="color: green;font-weight: 600;font-size:13px;">【Printer Online】</span> -->
</div>
<div v-if="printerStatus==0" style="display: inline-flex;">
<a :href="printerDownUrl" style="color: blue;font-weight: 600;font-size:13px;text-decoration: underline">下载打印组件</a>
</div>
<div>
<span style="color: #b12e29;font-size:23px;cursor: pointer" class="icon iconfont icon-printer" title="" @click="configPrinter"></span>
<!-- <el-button type="text" size="mini" style="padding: 7px 5px;" icon="el-icon-setting" @click="configPrinter"></el-button> -->
</div>
</div>
</div>
<el-dialog v-if="openPdfView&&!isInitA4" :key="resetRefreshId+'.dlg'" class="printPreDialog" title="预览" :resizable="true" :visible.sync="openPdfView" width="50%" :close-on-click-modal="false" :destroy-on-close="true" v-dialogDrag append-to-body @close="handleClose">
<div style="height:100%;width:100%;overflow: auto;">
<PdfViewer :key="resetRefreshId+'.pdf'" ref="pdfViewer" :pdfList="pdfList" :isLoadComplete="isLoadComplete" @onPrint="onPdfViewPrint" @close="handleClose"></PdfViewer>
</div>
</el-dialog>
<el-dialog v-if="openPdfView&&isInitA4" :key="resetRefreshId+'.dlg'" class="printPreDialog" title="预览" :resizable="true" :visible.sync="openPdfView" width="70%" :close-on-click-modal="false" :destroy-on-close="true" v-dialogDrag append-to-body @close="handleClose">
<div style="height:100%;width:100%;overflow: auto;">
<A4Viewer :key="resetRefreshId+'.pdf'" ref="pdfViewer" :pdfList="pdfList" :isLoadComplete="isLoadComplete" @close="handleClose"></A4Viewer>
</div>
</el-dialog>
<el-dialog title="设置打印机" :visible.sync="openShowConfig" width="30%">
<el-form ref="form" :model="form" size="mini" :rules="rules" @submit.native.prevent>
<el-row :gutter="0" style="display: flex;flex-wrap: wrap;">
<el-col :span="24">
<el-alert
title="注意"
type="info"
description="配置打印机信息,不要轻易更改!"
show-icon
:closable="false"
style="margin-bottom: 10px;"
>
</el-alert>
<el-form-item label="打印机地址" prop="printerHost">
<el-input v-model="form.printerHost" type="text" placeholder="" />
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="打印模板" prop="selTplCode">
<el-select v-model="selTplCode" size="mini" style="width:100%" placeholder="选择打印模板" clearable filterable>
<template slot="prefix">
<span style="padding: 5px;line-height: 28px;font-size: 16px;color: rgb(111 111 111);cursor: pointer;" @click="refreshPrintTpl()">
<i class="el-icon-refresh"></i>
</span>
</template>
<el-option
v-for="item in printTplList"
:key="item.tplCode"
:label="item.tplName"
:value="item.tplCode"
/>
</el-select>
</el-form-item>
</el-col>
<el-col :span="24">
<el-form-item label="打印机" prop="selPrinterName">
<el-select v-model="selPrinterName" style="width:100%" size="mini" placeholder="打印机" clearable filterable>
<el-option
v-for="item in printerList"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
</el-col>
</el-row>
</el-form>
<div slot="footer" class="dialog-footer" style="text-align: center;margin-bottom:20px">
<el-button type="primary" size="mini" @click="setupPrinter">设置</el-button>
<el-button size="mini" @click="openShowConfig = false">关 闭</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import emitter from 'element-ui/src/mixins/emitter';
import PdfViewer from '@/components/PdfViewer/viewer.vue';
import A4Viewer from '@/components/PdfViewer/A4Viewer.vue';
import PdfUtils from '@/utils/pdfUtils'
import { deepClone } from '@/utils/index'
export default {
name: "TanexPrinterPanel",
components: {
PdfViewer,A4Viewer
},
mixins: [emitter],
props: {
value: {
type: [Number],
required: false
},
placeholder:{
type: [String],
required: false
},
isShowPinters:{
type: [Boolean],
required: false,
default: false
},
isShowTpl:{
type: [Boolean],
required: false,
default: false
},
isShowSubBillRange:{
type: [Boolean],
required: false,
default: false
},
},
data() {
return {
resetRefreshId:null,
svalue: this.value,
subBillStart:1,
subBillEnd:null,
openPdfView:false,
orderDetailMap:new Map(),
isInitA4:false,
printManCode:this.$store.getters.empCode,
// 计数器
showCounter:0,
pdfList:[],
pdfBase64List:[],
isLoadComplete:0,
tanexPrinter:null,
printerDownUrl:null,
printerStatus:0,
printerStatusMessage:'打印组件未连接',
orderList:[],
printOrderList:[],
printerList:[],
printTplList:[],
selTplCode:null,
selPrinterName:null,
currentSelection:[],
openShowConfig:false,
pdfUtils:new PdfUtils({}),
form:{
printerHost:null,
},
rules:{
},
};
},
watch: {
value(newVal) {
this.svalue = newVal;
if(this.svalue!=null) {
}
},
},
created() {
this.initTanexPrinterSdk();
},
beforeDestroy(){
// this.tanexPrinter.close();
},
methods: {
configPrinter(){
this.form.printerHost=this.$tanexPrinter.getPrinterHost();
console.log("===>===>",this.form.printerHost);
this.openShowConfig=true;
},
setupPrinter(){
console.log("===>===>",this.form.printerHost);
if(this.form.printerHost){
this.$tanexPrinter.restPrinterHost(this.form.printerHost);
}
this.openShowConfig=false;
},
getUUID() {
return Math.random().toString(36).substring(3,10);
},
refreshKey(){
this.resetRefreshId="print-"+this.getUUID();
},
initTanexPrinterSdk(){
this.refreshKey();
this.tanexPrinter = this.$tanexPrinter;
this.getConfigKey("sys.printer.downurl").then(response => {
this.printerDownUrl = response.msg;
});
this.tanexPrinter.init();
this.tanexPrinter.setOnOpen(this.onPrinterOpen);
this.tanexPrinter.setOnClose(this.onPrinterClose);
this.tanexPrinter.setOnError(this.onPrinterError);
this.tanexPrinter.setOnGetPrinter(this.onGetPrinter);
this.tanexPrinter.setOnGetPrintTpl(this.onGetPrintTpl);
},
onPrinterOpen(evt){
// console.log("onPrinterOpen")
this.printerStatus = 1;
this.$emit("input",this.printerStatus);
this.printerStatusMessage="打印组件已连接,可以打印.";
},
onPrinterClose(evt){
// console.log("onPrinterClose")
this.printerStatus = 0;
this.$emit("input",this.printerStatus);
this.printerStatusMessage="打印组件已断开!";
},
onPrinterError(evt){
// console.log("onPrinterError")
this.printerStatus = 0;
this.$emit("input",this.printerStatus);
this.printerStatusMessage="打印组件已断开!";
},
onGetPrinter(msg){
// console.log("onGetPrinter")
this.printerList=this.tanexPrinter.getPrinterTplList();
this.selPrinterName=this.tanexPrinter.getDefaultPrinter();
},
onGetPrintTpl(msg){
console.log("onGetPrintTpl")
this.printTplList=[];
this.printTplList=this.tanexPrinter.getPrintTplList();
if(this.isInitA4){
const newList=this.printTplList.filter(item=>item.tplCode == 'A4-EP');
this.printTplList=newList;
}
// console.log("====onGetPrintTpl==>",this.printTplList);
if(this.printTplList&&this.printTplList.length>0){
this.selTplCode=this.printTplList[0].tplCode;
}
},
getDefaultPrinter(){
return this.tanexPrinter.getDefaultPrinter();
},
getSelectPrinter(){
return this.selPrinterName;
},
refreshPrintTpl(){
this.printTplList=[];
this.selTplCode=null;
this.tanexPrinter.refreshPrintTpl()
},
lpadZero(val,len) {
return (new Array(len + 1).join('0') + val).slice(-len);
},
/*
高级批量打印
[
{
"tplCode":tplCode,
"printerName":printerName,
"orderList":orderList.toString()
}
]
*/
batchPrintAdv(printDataList){
if(this.printerStatus !=1){
this.$modal.msgError("打印机未连接");
return;
}
this.isInitA4=false;
this.tanexPrinter.batchPrintAdv(printDataList,this.printManCode);
},
batchPrint(orderList,tplCode,orderDetailList){
if(this.printerStatus !=1){
this.$modal.msgError("打印机未连接");
return;
}
this.isInitA4=false;
this.orderDetailMap=new Map();
if(orderDetailList){
orderDetailList.map(v=>{
this.orderDetailMap[v.billCode]=v;
});
}
let that = this;
let newOrderList=[];
if(orderList){
this.orderList=[...orderList]
}
if(tplCode){
this.selTplCode=tplCode
}
// 打印子单
if(this.selTplCode=="TAN-GC-130" || this.selTplCode=="TAN-ENGC-130"){
for(let i=0;i<this.orderList.length;i++){
let billCode=this.orderList[i];
let subBillStart=1;
if(this.subBillStart&&this.subBillStart>0){
subBillStart=this.subBillStart;
}
let billDetail = this.orderDetailMap[billCode];
let subBillEnd=parseInt(billDetail.parcelQty);
if(this.subBillEnd&&this.subBillEnd!=''){
subBillEnd = parseInt(this.subBillEnd)<=parseInt(billDetail.parcelQty)?parseInt(this.subBillEnd):parseInt(billDetail.parcelQty);
}
// 循环子单范围
for(let i=subBillStart;i<=subBillEnd;i++){
let subBillCode = billCode+""+this.lpadZero(i,4);
newOrderList.push(subBillCode);
}
}
}else{
newOrderList=[...orderList];
}
// console.log("tnprint===>",newOrderList);
// console.log("tnprint===>",that.selTplCode);
this.printOrderList=newOrderList;
that.tanexPrinter.batchPrint(that.selTplCode,that.selPrinterName,newOrderList,this.printManCode)
},
printPreviewAdv(printDataList,billCount){
if(this.printerStatus !=1){
this.$modal.msgError("打印机未连接");
return;
}
this.isInitA4=false;
let that = this;
const loadingInstance = this.$loading({
text: '正在生成预览...'
})
let orderSeqMap = new Map();
console.log("printPreviewAdv===>",printDataList);
let j=0;
printDataList.forEach(item=>{
let billArr = item.orderList.split(",");
billArr.forEach(objBillCodeStr=>{
if(!(objBillCodeStr in orderSeqMap)){
orderSeqMap[objBillCodeStr]=j+1;
console.log("orderSeqMap===>",objBillCodeStr,j+1);
j++;
}
});
});
that.pdfList=[];
that.showCounter=0;
this.tanexPrinter.batchGetPDFAdv(printDataList,billCount,this.printManCode,async function(code,msg,data){
loadingInstance.close();
if(code == 500){
that.$modal.msgError("打印出错:"+msg);
}
else if(code == 200){
const pdfData=deepClone(data);
let pdfInfo={
"billCode":pdfData.ydSn,
"pdfUrl":null,
"pdfBase64":pdfData.file,
"tplCode":pdfData.tplCode,
"seqNo":orderSeqMap[pdfData.ydSn]
}
that.pdfList.push(pdfInfo);
if(!that.openPdfView && that.showCounter==0){
that.openPdfView = true;
}
// if(that.pdfList.length == newOrderList.length){
// that.isLoadComplete=true;
// }
that.showCounter++;
}
else if(code == 800){
// 全部获取完成
console.log("===全部获取完成ADV====>");
that.pdfList.sort((one, two) => {
let v=one.seqNo-two.seqNo;
if(one.seqNo==two.seqNo){
return one.tplCode.localeCompare(two.tplCode)
}else{
return v;
}
});
that.refreshKey();
}
});
},
printPreview(orderList,isA4,tplCode,orderDetailList){
this.refreshKey();
if(this.printerStatus !=1){
this.$modal.msgError("打印机未连接");
return;
}
this.orderDetailMap=new Map();
if(orderDetailList){
orderDetailList.map(v=>{
this.orderDetailMap[v.billCode]=v;
});
}
// console.log("====>orderDetailMap==>",this.orderDetailMap);
if(tplCode){
this.selTplCode=tplCode;
}
if(isA4){
this.isInitA4=isA4;
}
let that = this;
let newOrderList=[];
if(orderList){
this.orderList=[...orderList]
}
// 打印子单
if(this.selTplCode=="TAN-GC-130"|| this.selTplCode=='TAN-ENGC-130'){
for(let i=0;i<this.orderList.length;i++){
let billCode=this.orderList[i];
let subBillStart=1;
if(this.subBillStart&&this.subBillStart>0){
subBillStart=this.subBillStart;
}
let billDetail = this.orderDetailMap[billCode];
// console.log("====>billDetail==>",billDetail);
let subBillEnd=parseInt(billDetail.parcelQty);
if(this.subBillEnd){
subBillEnd = parseInt(this.subBillEnd)<=parseInt(billDetail.parcelQty)?parseInt(this.subBillEnd):parseInt(billDetail.parcelQty);
}
// 循环子单范围
for(let i=subBillStart;i<=subBillEnd;i++){
let subBillCode = billCode+""+this.lpadZero(i,4);
newOrderList.push(subBillCode);
}
}
}else{
newOrderList=[...orderList];
}
const loadingInstance = this.$loading({
text: '正在生成预览...'
})
this.printOrderList=newOrderList;
let orderSeqMap = new Map();
for(let i=0; i<newOrderList.length; i++){
let billCodeItem=newOrderList[i];
orderSeqMap[billCodeItem]=i+1;
}
that.pdfList=[];
that.showCounter=0;
// that.openPdfView = true;
// if(!that.openPdfView && that.showCounter==0){
// that.openPdfView = true;
// }
// that.showCounter++;
this.tanexPrinter.batchGetPDF(this.selTplCode,this.selPrinterName,newOrderList,this.printManCode,async function(code,msg,data){
loadingInstance.close();
if(code == 500){
that.$modal.msgError("打印出错:"+msg);
}
else if(code == 200){
const pdfData=deepClone(data);
let pdfInfo={
"billCode":pdfData.ydSn,
"pdfUrl":null,
"pdfBase64":pdfData.file,
"tplCode":pdfData.tplCode,
"seqNo":orderSeqMap[pdfData.ydSn]
}
that.pdfList.push(pdfInfo);
if(!that.openPdfView && that.showCounter==0){
that.openPdfView = true;
}
that.showCounter++;
}
else if(code == 800){
// 全部获取完成
console.log("===全部获取完成2====>");
that.pdfList.sort((one, two) => {
let v=one.seqNo-two.seqNo;
if(one.seqNo==two.seqNo){
return one.tplCode.localeCompare(two.tplCode)
}else{
return v;
}
});
console.log("===that.pdfList====>",that.pdfList);
that.refreshKey();
}
});
},
downPDF(orderList,isA4,tplCode,orderDetailList){
if(this.printerStatus !=1){
this.$modal.msgError("打印机未连接");
return;
}
this.orderDetailMap=new Map();
if(orderDetailList){
orderDetailList.map(v=>{
this.orderDetailMap[v.billCode]=v;
});
}
if(tplCode){
this.selTplCode=tplCode;
}
if(isA4){
this.isInitA4=isA4;
}
let that = this;
let newOrderList=[];
if(orderList){
this.orderList=[...orderList]
}
// 打印子单
if(this.selTplCode=="TAN-GC-130" || this.selTplCode=='TAN-ENGC-130'){
for(let i=0;i<this.orderList.length;i++){
let billCode=this.orderList[i];
// 循环子单范围
let subBillStart=1;
if(this.subBillStart&&this.subBillStart>0){
subBillStart=this.subBillStart;
}
let billDetail = this.orderDetailMap[billCode];
// console.log("====>billDetail==>",billDetail);
let subBillEnd=parseInt(billDetail.parcelQty);
if(this.subBillEnd){
subBillEnd = parseInt(this.subBillEnd)<=parseInt(billDetail.parcelQty)?parseInt(this.subBillEnd):parseInt(billDetail.parcelQty);
}
for(let i=subBillStart;i<=subBillEnd;i++){
let subBillCode = billCode+""+this.lpadZero(i,4);
newOrderList.push(subBillCode);
}
}
}else{
newOrderList=[...orderList];
}
const loadingInstance = this.$loading({
text: '正在下载...'
})
let orderSeqMap = new Map();
for(let i=0; i<newOrderList.length; i++){
let billCodeItem=newOrderList[i];
orderSeqMap[billCodeItem]=i+1;
}
// 清空以前的数据
this.pdfList=[];
this.tanexPrinter.batchGetPDF(this.selTplCode,this.selPrinterName,newOrderList,this.printManCode,function(code,msg,data){
if(code == 500){
loadingInstance.close();
that.$modal.msgError("下载出错:"+msg);
}
else if(code == 200){
const pdfData=deepClone(data);
let pdfInfo={
"billCode":pdfData.ydSn,
"pdfUrl":null,
"pdfBase64":pdfData.file,
"tplCode":pdfData.tplCode,
"seqNo":orderSeqMap[pdfData.ydSn]
}
that.pdfList.push(pdfInfo);
}
else if(code == 800){
loadingInstance.close();
// console.log("===全部获取完成====>");
// A4下载
if(that.isInitA4){
that.downA4PDF();
}else{
that.downSinglePDF();
}
}
});
},
downSinglePDF(){
this.$prompt('请输入保存文件名', "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
closeOnClickModal: false,
inputValidator (value) {
if (value) {
return true;
}else{
return false;
}
},
inputErrorMessage: "文件名不能为空"
}).then(async ({ value }) => {
if(!value){
this.$modal.msgError("文件名不能为空" );
return
}
let needGenList=[];
let newTmpList=[...this.pdfList];
newTmpList.sort((one, two) => {
let v=one.seqNo-two.seqNo;
if(one.seqNo==two.seqNo){
return one.tplCode.localeCompare(two.tplCode)
}else{
return v;
}
});
for(let j=0;j<newTmpList.length;j++){
needGenList.push(newTmpList[j].pdfBase64)
}
// let fileName=new Date().getTime()+".pdf";
let fileName=value+".pdf";
this.pdfUtils.mergePdf3(needGenList,fileName,true);
}).catch(() => {});
},
downA4PDF(){
this.$prompt('请输入保存文件名', "提示", {
confirmButtonText: "确定",
cancelButtonText: "取消",
closeOnClickModal: false,
inputValidator (value) {
if (value) {
return true;
}else{
return false;
}
},
inputErrorMessage: "文件名不能为空"
}).then(async ({ value }) => {
if(!value){
this.$modal.msgError("文件名不能为空" );
return
}
let cacheA4FileList=[];
const newList = Object.assign([], this.pdfList);
if(newList&&newList.length>4){
let spliteArr = (array = [], subGroupLength = 0) => {
let index = 0;
const newArray = [];
while (index < array.length) {
newArray.push(array.slice(index, index += subGroupLength));
}
return newArray;
}
const subPdfList=spliteArr(newList, 4);
for (let i=0; i < subPdfList.length;i++){
let fileItem={
"billCode":"page-"+(i+1),
"pdfUrl":null,
"childreen":subPdfList[i]
}
cacheA4FileList.push(fileItem);
}
}else{
let fileItem={
"billCode":"page-"+1,
"pdfUrl":null,
"childreen":newList
}
cacheA4FileList.push(fileItem);
}
// 下载
// let mergeA4List=[];
let needGenList=[];
for(let i=0;i<cacheA4FileList.length;i++){
let thisSubList=cacheA4FileList[i].childreen;
for(let j=0;j<thisSubList.length;j++){
needGenList.push(thisSubList[j].pdfBase64)
}
}
// let pdfUrl=await this.pdfUtils.mergePdfToA4(needGenList);
// mergeA4List.push(pdfUrl);
// let fileName=new Date().getTime()+".pdf";
let fileName=value+".pdf";
this.pdfUtils.mergePdfToA4(needGenList,fileName,true);
}).catch(() => {});
},
// 响应PDF预览
onPdfViewPrint(printCnt){
this.$emit("onPrint",printCnt);
},
handleClose(){
this.openPdfView=false;
this.$emit("onPrintComplete");
}
}
};
</script>
<style lang="scss">
.printPreDialog .el-dialog__body {
margin: 0px!important;
}
// .printPreDialog .el-dialog__header {
// display: none;
// }
.subBillRange .el-input__inner {
padding:0px;
text-align: center;
}
</style>