南骏 池
3 天以前 6f59fd655854adf8130a2e67a638181958d2e2b3
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
<template>
    <web-view  class="myView"   src="http://192.168.1.145:86/UserLogin.aspx" @message="onMessage" id="webviewRef" ref="webviewRef" name="webviewRef"
></web-view>
<!--     "http://192.168.1.145:86/UserLogin.aspx" -->
</template>
<script>
    import {queue} from '@/utils/printSupport'
    export default {
        data() {
            return {
                messageTest:"1",
                ip: "111",
                port: "9100",
                info: ['']
            }
        },
        onLoad() {
         this.ddd=";s";
         // #ifdef H5
                 // uniapp webview h5 通信 window.postMessage 方式
                 // https://blog.csdn.net/qq_42313445/article/details/127068570
                 window.addEventListener("message", (event) => {
                       // 全量打印
                      // console.log(event)
                       // 只看关键消息
                      // console.log(event.data)
                       // hybrid页面通过uni.webView.postMessage传递的data结构
                     //  let event_data = event.data.data.arg;
                     //  this.ddd=event_data
                     //  console.log(event_data)
                 }, false)
                 // #endif
        },
        methods: {
            
             onMessage(e) {
                  console.log('收到消息:', e.detail.data);
                  this.messageTest=e.detail.data;
                   var _obj=JSON.parse(this.messageTest);
                   ///
                   console.log(_obj.Ip);
                   console.log(_obj.Port);
                   console.log(_obj.Detail);
                   console.log(_obj.Type);
                   
                   
                   this.ip = _obj.Ip;
                   this.port = _obj.Port;
                   this.info = _obj.Detail;
                   /////////////////////////访问服务器
                   if(_obj.Type === "Bar")
                   {
                       this.printHandle();
                   }
                   else if(_obj.Type === "Scan")
                   {
                       //读取扫码beg 2025-7-28
                       console.log(_obj.txtName);
                           var currentWebview = this.$refs.webviewRef; 
                           let _funName='msgFromUniapp';
                           let _fundata = { msg:'',txtName:''};
                           currentWebview = this.$scope.$getAppWebview().children()[0];
                           uni.scanCode({
                                onlyFromCamera: true, //为true只允许相机扫码,不加允许相册扫码
                               success: function(res) {
                               console.log(JSON.stringify(res));
                               _fundata.msg=res.result;
                            _fundata.txtName=_obj.txtName;
                               currentWebview.evalJS(`${_funName}(${JSON.stringify(_fundata)})`);
                               uni.showToast({
                                    title: '扫码成功'
                                   });
                               },
                                fail: function(err) {
                                   console.log('扫码失败', err)
                                        }
                               });
                        //读取扫码end 2025-7-28
                   }
                   
                   
                   
                },
                callH5Method(){
                    uni.postMessage({
                        data: {
                            action: 'someAction',
                            content: 'Hello from uniapp'
                        }
                    }, (response) => {
                        console.log('收到H5页面的响应:', response);
                    });
                },
                async printHandle() {
                 const arr = this.info;
                     uni.showLoading({
                         title: '发送中...',
                         mask: true
                     });
                     try {
                        console
                         // 发送数组处理
                         await queue(arr,this.ip,this.port);
                     } catch (error) {
                         // console.error('Error during queue processing:', error);
                         uni.showToast({
                             title: error,
                             icon: "error",
                             duration: 2000
                         })
                     } finally {
                         uni.hideLoading();
                         this.printTxt = "已发送"
                         uni.showToast({
                             title: "发送完成",
                             icon: "success",
                             duration: 2000
                         })
                     }
                 },      
        }
    }
</script>
<style>
</style>