Newer
Older
VueSharedPrinting / src / components / global.vue
hanfei on 30 Jan 2024 9 KB han
<template>
  <div>
    <!--    打印中提示-->
    <div class="dyzTip" v-if="DYZshow&&DYZlist">
      <div class="curtain"></div>
      <div class="content">
        <img src="../assets/printing.png" alt="">
        <div v-if="ztyc" class="flex-center" style="padding: 0 .3rem;margin: 0;">小天很抱歉通知您,您使用中的打印设备发生异常,请您联系客服人员(电话{{ cite.facility.servicePhone }})为您处理订单善后事宜。</div>
<!--        <div v-else class="name">正在进行{{ DYZlist[0].printFileType=='PDF'?'文档':'照片' }}打印...</div>-->
        <div v-else class="name">正在打印...</div>
        <!--        <div class="flex-center">{{ DYZlist.length }}/{{ DYZlistNum }}</div>-->
<!--        <div class="flex-center name">您的资料打印中-->
<!--          <span v-for="(item,index) in DYZlist2" :key="index" :style="Progress.taskCode==item.code?'':'display:none;'">第{{index+1}}份</span>-->
<!--          {{ Progress.rate || '0%' }}</div>-->
        <div class="JDT"><span :style="'width:'+Progress.rate"></span></div>
<!--        <div class="flex-center">您的资料打印中{{ Progress.rate || '0%' }}</div>-->

        <div v-if="ztyc" class="btn flex-center" @click="onCloneZt()">我已知晓({{numZt}}秒)</div>
      </div>
    </div>

    <!--    提示-->
    <div class="tkTip flex-center" v-if="state.sta">
      <div>{{ state.name }}</div>
    </div>

    <!--    小程序支付-->
    <div class="dyzTip" v-if="zfShow">
      <div class="curtain"></div>
      <div class="content content2">
        <VueQr v-show="src" :auto-color="true" :dot-scale="1" :logo-src="require('@/assets/logo.png')"
               :logoMargin="0" :logoScale=".2" :margin="0" :size="300" :text="src"
               logoBackgroundColor="white"></VueQr>
        <p>
          <img @click="onChange('zfShow',false)" class="icon" src="../assets/demo10.png" alt="">
        </p>
      </div>
    </div>
  </div>
</template>
<script>
import mqtt from "mqtt/dist/mqtt"
import {getAdvertisement, getDeviceByCode} from "@/api/resultApi";
import VueQr from 'vue-qr'
import { getDeviceCode } from "@/api/bdResultApi";

export default {
  name: "global",
  components: {
    VueQr
  },
  data() {
    return {
      client: null,
      code: '',

      DYZshow: false, // 是否显示打印中
      downloadPath: 'D:\\TZDYSHARE\\TEMPFILES\\',

      state: '',

      src: '',
      zfShow: false,

      DYZlist: [],
      DYZlist2: [],
      DYZlistNum: 1,
      Progress: {},
      numZt: 120,
      ztyc: false,
    };
  },
  created() {
    this.toLoad()
  },
  methods: {
    onCloneZt() {
      this.numZt=0
      this.onNum()
    },
    onNum() {
      if(this.numZt==0) {
        this.cite.ztyc = false
        this.DYZshow = false
        this.DYZlist = []
      }else{
        this.numZt = this.numZt-1
        setTimeout(()=>{
          this.onNum()
        },1000)
      }
      this.ztyc = this.cite.ztyc
    },
    onChange(name,val){
      this[name] = val
    },
    toLoad() {
      // this.cite.code = 'SNJ-XX-EPSON-X001-00001'
      // this.cite.code = 'TZLC001'
      // this.code = this.cite.code
      // this.load()
      getDeviceCode().then(res =>{
        this.cite.code = res.data
        this.code = res.data
        this.load()
      })
      setTimeout(()=>{
        if(this.code=='') {
          this.toLoad()
        }
      },1000)
    },
    load() {
      if (this.cite.facility === "") {
        this.getDeviceByCode();
      }
      // 判断是否是广告屏
      if(this.cite.name != 'advertising') {
        this.initMqtt()
        setInterval(()=>{
          if(!this.state.sta&&this.cite.state.sta) {
            setTimeout(()=>{
              this.cite.state.sta = false
            },3000)
          }
          this.state = this.cite.state
        },1000)
      }
    },
    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=='HEARTBEAT') {
          if(data.deviceStatus=='ABNORMAL'&&this.DYZshow) {
            if(!this.cite.ztyc) {
              this.numZt=120
              this.onNum();
            }
            this.cite.ztyc = true
          }
        }else if(data.cmd=='ORDER_INFO') {
          //小程序支付
          this.DYZshow = true
          this.zfShow = false
          this.DYZlist=data.printTaskPushList
          this.DYZlist2 = data.printTaskPushList
          this.DYZlistNum = this.DYZlist.length
          // this.cite.url = data.payCodeUrl
          // if(this.cite.selectedFile.length<1) {
          //   this.src = data.payCodeUrl
          //   this.zfShow=true
          // }
        }else if(data.cmd=='LOGIN') {
          //登录
        }else if(data.cmd=='LOGOUT'){
          //登出
        }else if(data.cmd=='PRINT'){
          //打印任务
          // this.DYZshow = true
          // this.zfShow = false
          // this.DYZlist.push(data)
          // this.DYZlistNum = this.DYZlist.length
        }else if(data.cmd=='PRINT_STATUS'){
          //打印任务完成
          console.log(this.DYZlist)
          if(data.taskStatus=='COMPLETE') {
            if(this.DYZlist.length>1) {
              this.DYZlist.forEach((item,index)=>{
                if(item.code == data.taskCode) {
                  this.DYZlist.splice(index, 1)
                }
              })
            }else{
              if(this.DYZshow) {
                this.DYZshow = false
                this.DYZlist = []
                this.DYZlistNum = 1
                this.Progress = {}
                this.$router.push({
                  name: 'complete'
                })
                getDeviceByCode({ code: this.code }).then((res) => {
                  this.cite.facility = res;
                });
              }
            }
          }else if(data.taskStatus=='INTERRUPT') {
            if(!this.cite.ztyc) {
              this.numZt=120
              this.onNum();
            }
            this.cite.ztyc = true
          }
        }else if(data.cmd=='PRINT_PROGRESS'&&!this.cite.ztyc) {
          console.log(data)
          this.Progress = data
          if(data.rate=='99%') {
            setTimeout(()=>{
              if(this.Progress.rate=='99%') {
                this.Progress.rate = '100%'
              }
            },1500)
          }
        }
      })
    },
    //订阅多个主题
    subscribes() {
      const arr = ['/tzdy/send/device/'+this.code]
      this.client.subscribe(arr, { qos: 0 }, (err) => {
        if (!err) {
          console.log(`消息订阅成功`)
        } else {
          console.log('消息订阅失败')
        }
      })
    },
    //获取设备信息
    getDeviceByCode() {
      if(this.code) {
        getDeviceByCode({ code: this.code }).then((res) => {
          console.log(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 || ''
      })
    },
  }
}
</script>

<style scoped lang="less">
.dyzTip {
  position: fixed;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  z-index: 99;
  .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;
    }
    .btn {
      width: 18rem;
      height: 3rem;
      margin: 1rem auto;
      color: #fff;
      background: linear-gradient(132deg, #52A39D 0%, #A1C553 50%, #81D012 100%);
      border-radius: 1rem;
    }
    .JDT {
      width: 80%;
      margin: 1rem auto 0;
      height: 1rem;
      border-radius: 1rem;
      position: relative;
      border: 1px solid #323232;
      span {
        position: absolute;
        background: linear-gradient(132deg, #52A39D 0%, #A1C553 50%, #81D012 100%);
        top: 0;
        left: 0;
        bottom: 0;
      }
    }
  }
  .content2 {
    background: transparent;
    height: auto;
    img {
      width: 18rem;
    }
    .icon {
      width: 3rem;
      margin-top: 3rem;
    }
  }
}
.tkTip {
  position: fixed;
  top: 5%;
  width: 100%;
  z-index: 99;
  div {
    background: rgba(51, 51, 51, 0.5);
    color: #fff;
    padding: 1rem 2rem;
    border-radius: 1rem;
  }
}
</style>