Newer
Older
VueSharedPrinting / src / components / global.vue
hanfei on 12 Jan 2024 4 KB han
<template>
  <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: ''
    };
  },
  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(() =>{
        let res = bdRequest({
          method: 'post',
          url: '/api/Printer/Print',
          post: '8080',
          data: {
            "PrinterName": "EPSON WF-C5890 Series",
            // "SourceFile": 'D:\\TZDYSHARE\\TEMPFILES\\'+listData.printFileName,
            "SourceFile": 'C:\\datasource\\'+listData.printFileName,
            "FileType": "pdf",
            "PrintColor": listData.printingColor || "",
            "DblPrt": listData.printingFaces || ""
          }
        })
        console.log(res)
      },2000)
    },
  }
}
</script>