333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<template>
  <div class="page-list-container">
    <el-card class="scan-card">
      <div slot="header" class="clearfix">
        <span>分拣机打印</span>
      </div>
      <el-form ref="form" :model="formData" label-width="120px">
        <!-- <el-form-item label="选择打印机">
          <el-select v-model="formData.printName" placeholder="请选择打印机" class="w-300">
            <el-option v-for="(item, index) in printList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="打印机地址">
          <el-input ref="ServerDomain" v-model="formData.ServerDomain" class="w-300" @keyup.native.enter.stop="getWayBillPrinter"></el-input>
          <el-button type="primary" @click="connectSocketServer('showMsg')">连接服务器</el-button>
          <el-tag :type="serverStatus==='已连接服务器'?'success':'danger'">{{ serverStatus }}</el-tag>
        </el-form-item>
        <el-form-item label="运单号">
          <el-input ref="waybillCode" v-model="formData.WayBillCode" class="w-300" @keyup.native.enter.stop="getWayBillPrinter"></el-input>
          <el-button type="primary" @click="getWayBillPrinter">手工打印</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <!-- 显示打印模板 -->
    <div id="mount-print"></div>
  </div>
</template>
 
<script>
import Vue from "vue";
import { getLodop } from "@/utils/LodopFuncs";
import print from "@/views/sys/print/base-fenjianji";
 
export default {
  name: "tms-panel-sorting-printer",
  components: {
    print
  },
  data() {
    return {
      formData: {
        WayBillCode: null, // 运单号
        printName: null, // 打印及名称
        ServerDomain: "ws://127.0.0.1:2017/", // 服务器IP地址
        directPort: null // 指定口岸
      },
      showprint: false, // 显示print
      wayBill_Id: 0, // 订单Id
      menu_Id: 0, // 模板Id
      panelResult: true, // 组板结果
      panelMsg: null, // 组板信息
      wayBillList: [], // 正常运单
      abnormalWaybillList: [], // 异常运单
      printList: [], // 打印机设备列表
      printName: null, // 打印及名称
      // 口岸名称下拉框数据列表
      portOptions: [],
      // websockect
      ws: null,
      // 服务器状态
      serverStatus: "未连接"
    };
  },
  mounted() {
    this.getPrintList();
    this.connectSocketServer();
    setInterval(() => {
      if (!this.ws || this.ws.readyState === 2 || this.ws.readyState === 3) {
        this.connectSocketServer();
      }
    }, 20 * 1000);
  },
  activated() {},
  methods: {
    // 打印
    btnClickPrint() {},
    // 加载数据
    getData() {},
    // 获取运单信息和打印机名称
    getWayBillPrinter() {
      var url = "/api/TMS_PDAPrint/getWayBillPrinter";
      var params = {
        Code: this.formData.WayBillCode
      };
      this.common.ajax(
        url,
        params,
        res => {
          this.common.showMsg(res);
          if (res.result) {
            var Profile = Vue.extend(print);
            var m = new Profile({
              propsData: {
                vueData: JSON.parse(res.data.vueData),
                billDataInfo: res.data.billDataInfo,
                printInfo: res.data.printInfo,
                billCodeField: "WaybillCode"
              }
            }).$mount();
            var mountPrint = document.getElementById("mount-print");
            var firstChild = mountPrint.firstChild;
            mountPrint.insertBefore(m.$el, firstChild);
            m.lodopPrint();
            window.setTimeout(() => {
              document.getElementById(res.data.billDataInfo.mainInfo.WayBillCode).remove();
            }, 2 * 60 * 1000);
          }
          this.$refs.waybillCode.select();
          this.$refs.waybillCode.focus();
        },
        true
      );
    },
    // 获得打印机列表
    getPrintList() {
      window.setTimeout(() => {
        const LODOP = getLodop();
        var iPrinterCount = LODOP.GET_PRINTER_COUNT();
        for (var i = 0; i < iPrinterCount; i++) {
          this.printList.push(LODOP.GET_PRINTER_NAME(i));
        }
      }, 1000);
    },
    // 连接服务器
    connectSocketServer(type) {
      const userInfo = this.common.getUserInfo();
      const support = "MozWebSocket" in window ? "MozWebSocket" : "WebSocket" in window ? "WebSocket" : null;
 
      if (support == null) {
        alert("Your browser cannot support WebSocket!");
        return;
      }
 
      // 0 (CONNECTING), 1 (OPEN), 2 (CLOSING),3 (CLOSED)
      if (!this.ws || this.ws.readyState === 2 || this.ws.readyState === 3) {
        this.ws = new window[support](this.formData.ServerDomain);
      } else {
        this.$message.warning("已连接,无需重复连接");
      }
 
      this.ws.onerror = function(e) {
        // console.log(e);
      };
 
      this.ws.onmessage = evt => {
        var data = JSON.parse(evt.data);
        if (data.content) {
          this.formData.WayBillCode = data.content;
          this.formData.directPort = data.printNo; // 指定口岸
          this.getWayBillPrinter();
        }
      };
 
      // when the connection is established, this method is called
      this.ws.onopen = () => {
        var msg = {};
        msg.Action = "SetOnline";
        msg.FromMobile = userInfo.Mobile;
        this.ws.send(JSON.stringify(msg));
        this.serverStatus = "已连接服务器";
        if (type === "showMsg") {
          if (this.serverStatus === "已连接服务器") {
            this.$message.success(this.serverStatus);
          } else {
            this.$message.error(this.serverStatus);
          }
        }
      };
 
      // when the connection is closed, this method is called
      this.ws.onclose = () => {
        this.serverStatus = "未连接服务器";
      };
    }
  }
};
</script>
 
<style lang="scss" scoped>
@import "../../../styles/scan.scss";
.successClass {
  color: green;
  font-size: 40px;
  margin-left: 200px;
}
.errorClass {
  color: red;
  font-size: 40px;
  margin-left: 200px;
}
</style>