<template>
<div>
<!-- 打印中提示-->
<div class="dyzTip" v-show="DYZshow">
<div class="curtain"></div>
<div class="content">
<img src="../assets/printing.png" alt="">
<div class="name">正在进行{{ listData.printFileType=='PDF'?'文档':'照片' }}打印...</div>
<div class="flex-center">{{ listData.printFileName }}</div>
<div class="flex-center">{{ cite.selectedFile.length||1 }}/{{ cite.fileNum||1 }}</div>
</div>
</div>
</div>
</template>
<script>
import mqtt from "mqtt/dist/mqtt"
import {downloadPDF, getAdvertisement, getDeviceByCode} from "@/api/resultApi";
import bdRequest from "@/utils/bdRequest";
export default {
name: "global",
data() {
return {
client: null,
code: '',
listData: '',
listData2: {
code: "20240115143608553416-0",
copies: 2,
printFileId: 404,
printFileName: "2177370079086126.pdf",
printFileType: "PDF",
printingColor: "BW",
printingFaces: "DOUBLE",
},
DYZshow: false, // 是否显示打印中
};
},
created() {
this.load()
},
methods: {
load() {
// 获取名为code的查询参数值
if (this.cite.code === "") {
this.code = this.$route.query.code;
this.cite.code = this.code;
}else{
this.code = this.cite.code;
}
if (this.cite.facility === "") {
this.getDeviceByCode();
}
// 判断是否是广告屏
console.log(this.cite)
if(this.cite.name != 'advertising') {
this.initMqtt()
}
},
initMqtt() {
// 连接配置选项
let options = {
connectTimeout: 4000, // 超时时间
// 认证信息
clientId: '', //不填默认随机生成一个ID
username: 'admin', //用户名
password: 'public' //密码
}
this.client = mqtt.connect('wss://mps.xlmalls.com:8084/mqtt', options) //调用连接的api
//连接成功
this.client.on('connect', (e) => {
console.log('连接成功', e)
this.subscribes()
})
//重连提醒
this.client.on('reconnect', (error) => {
console.log('正在重连', error)
})
//连接失败提醒
this.client.on('error', (error) => {
console.log('连接失败', error)
})
//接收消息
this.client.on('message', (topic, message) => {
const data = JSON.parse(message) //接受到控制信号的数据
console.log('接收消息',data)
if(data.cmd=='pay') {
//小程序支付
this.cite.url = data.payCodeUrl
}else if(data.login) {
//登录
this.cite.memberLoginVo = data.memberLoginVo
}else if(data.code){
//打印任务
this.listData = data
this.dayin()
}else{
//登出
this.cite.memberLoginVo = ''
}
})
},
//订阅多个主题
subscribes() {
const arr = ['/tzdy/send/device/'+this.code]
this.client.subscribe(arr, { qos: 0 }, (err) => {
if (!err) {
console.log(`消息订阅成功`)
} else {
console.log('消息订阅失败')
}
})
},
//获取设备信息
getDeviceByCode() {
getDeviceByCode({ code: this.code }).then((res) => {
this.cite.facility = res;
this.getAdvertisement()
});
},
// 获取广告链接
getAdvertisement() {
let name = 'DeviceScreen'
if(this.cite.name=='advertising') {
name = 'AdvertisingScreen'
}
getAdvertisement({deviceId:this.cite.facility.id,screen: name}).then((res) =>{
// console.log(res)
this.cite.advertisement = res || ''
})
},
// 小程序下载
downloadPDF(id,name) {
downloadPDF({id: id}).then(res => {
console.log(res)
const fileName = name
const blob = new Blob([res], {type: 'application/octet-stream'})
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
} else {
let link = document.createElement('a')
link.href = window.URL.createObjectURL(blob)
link.download = fileName
link.click()
window.URL.revokeObjectURL(link.href)
}
}).catch(() => {
})
},
async dayin() {
let _this = this
let {listData} = _this
await this.downloadPDF(listData.printFileId,listData.printFileName)
setTimeout(() =>{
_this.DYZshow = true
_this.setDy(listData,listData.copies)
},2000)
},
setDy(listData,num) {
let name = this.cite.memberLoginVo.printerGlossy || 'EPSON WF-C5890 Series2'
if(listData.printFileType=='PDF') {
name = this.cite.memberLoginVo.printerA4 || 'EPSON WF-C5890 Series'
}
console.log('设备名称',name)
let res = bdRequest({
method: 'post',
url: '/api/Printer/Print',
post: '8080',
data: {
"PrinterName": name,
// "SourceFile": 'D:\\TZDYSHARE\\TEMPFILES\\'+listData.printFileName,
"SourceFile": 'C:\\datasource\\'+listData.printFileName,
"FileType": "pdf",
"PrintColor": listData.printingColor || "",
"DblPrt": listData.printingFaces || ""
}
})
console.log(res)
num = num-1
if(num>0) {
this.setDy(listData,num)
}else{
setTimeout(()=>{
this.$router.push({
name: 'complete'
})
this.DYZshow = false
},1000)
}
},
}
}
</script>
<style scoped lang="less">
.dyzTip {
position: fixed;
width: 100%;
height: 100%;
top: 0;
left: 0;
.curtain {
background: #000;
width: 100%;
height: 100%;
opacity: .3;
}
.content {
position: absolute;
left: 50%;
margin-left: -19rem;
top: 50%;
margin-top: -14.5rem;
width: 38.56rem;
height: 29rem;
background: #FFFFFF;
border-radius: 1.25rem;
overflow: hidden;
img {
width: 100%;
}
div {
font-size: 1.38rem;
font-family: PingFang SC, PingFang SC;
font-weight: bold;
color: #323232;
line-height: 1.88rem;
margin-top: 1.3rem;
}
.name {
font-size: 1rem;
font-family: PingFang SC, PingFang SC;
font-weight: 400;
color: #323232;
line-height: 1.38rem;
text-align: left;
padding: 0 3rem;
}
}
}
</style>