From 4ab207445944abe56229a0f082d84a70eaad019f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E6=9D=B0?= Date: Wed, 29 Oct 2025 22:05:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E9=85=8D=E7=BD=AE=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .hbuilderx/launch.json | 2 +- api/tcpConnection.js | 92 +++++++++++++++++++++++++------------------------- utils/http.js | 4 +-- vue.config.js | 24 ++++++------- 4 files changed, 61 insertions(+), 61 deletions(-) diff --git a/.hbuilderx/launch.json b/.hbuilderx/launch.json index f1dfc07..de6a674 100644 --- a/.hbuilderx/launch.json +++ b/.hbuilderx/launch.json @@ -3,7 +3,7 @@ "configurations" : [ { "customPlaygroundType" : "local", - "playground" : "standard", + "playground" : "custom", "type" : "uni-app:app-android" } ] diff --git a/api/tcpConnection.js b/api/tcpConnection.js index 819051f..c31188e 100644 --- a/api/tcpConnection.js +++ b/api/tcpConnection.js @@ -6,7 +6,7 @@ */ // 引用TCP插件 -//const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin'); +const TCPSocket = uni.requireNativePlugin('Aimer-TCPPlugin'); // const TCPSocket = uni.requireNativePlugin("Aimer-TCPPlugin"); // TCP连接配置 @@ -67,52 +67,52 @@ class TCPConnection { } console.log('开始建立TCP连接:', connectionConfig); - // TCPSocket.connect( - // connectionConfig, - // result => { - // /** - // * status : 0 连接成功 - // * status : 1 断开连接 - // * receivedMsg : 服务器返回字符串(普通的字符串交互) - // * receivedHexMsg : 服务器返回字节数组(单片机、智能家居等硬件数据交互) - // */ - // if (result.status == '0') { - // // TCP连接成功 - // this.channelConnections.set(connectionConfig.channel, true); - // console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`); - // this._notifyConnectionCallbacks('connected', result, connectionConfig.channel); - // } else if (result.status == '1') { - // // TCP断开连接 - // this.channelConnections.set(connectionConfig.channel, false); - // console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`); - // this._notifyConnectionCallbacks('disconnected', result, connectionConfig.channel); - // } + TCPSocket.connect( + connectionConfig, + result => { + /** + * status : 0 连接成功 + * status : 1 断开连接 + * receivedMsg : 服务器返回字符串(普通的字符串交互) + * receivedHexMsg : 服务器返回字节数组(单片机、智能家居等硬件数据交互) + */ + if (result.status == '0') { + // TCP连接成功 + this.channelConnections.set(connectionConfig.channel, true); + console.log(`TCP连接成功 - Channel ${connectionConfig.channel}`); + this._notifyConnectionCallbacks('connected', result, connectionConfig.channel); + } else if (result.status == '1') { + // TCP断开连接 + this.channelConnections.set(connectionConfig.channel, false); + console.log(`TCP断开连接 - Channel ${connectionConfig.channel}`); + this._notifyConnectionCallbacks('disconnected', result, connectionConfig.channel); + } - // if (result.receivedMsg) { - // // 服务器返回字符串 - // console.log('收到字符串消息:', result.receivedMsg); - // this._notifyMessageCallbacks('string', result.receivedMsg, null, connectionConfig.channel); - // } + if (result.receivedMsg) { + // 服务器返回字符串 + console.log('收到字符串消息:', result.receivedMsg); + this._notifyMessageCallbacks('string', result.receivedMsg, null, connectionConfig.channel); + } - // // if (result.receivedHexMsg) { - // // // 硬件服务器返回16进制数据 - // // console.log('收到16进制消息:', result.receivedHexMsg); - // // let msg = result.receivedHexMsg; - // // let sum = msg.length / 2; - // // let arr = []; - // // for (let k = 0; k < sum; k++) { - // // let i = msg.substring(k * 2, k * 2 + 2); - // // arr.push(i); - // // } - // // console.log('解析后的16进制数组:', arr); - // // this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr); - // // } + // if (result.receivedHexMsg) { + // // 硬件服务器返回16进制数据 + // console.log('收到16进制消息:', result.receivedHexMsg); + // let msg = result.receivedHexMsg; + // let sum = msg.length / 2; + // let arr = []; + // for (let k = 0; k < sum; k++) { + // let i = msg.substring(k * 2, k * 2 + 2); + // arr.push(i); + // } + // console.log('解析后的16进制数组:', arr); + // this._notifyMessageCallbacks('hex', result.receivedHexMsg, arr); + // } - // // 执行回调函数 - // if (callback && typeof callback === "function") { - // callback(result); - // } - // }); + // 执行回调函数 + if (callback && typeof callback === "function") { + callback(result); + } + }); } /** @@ -144,7 +144,7 @@ class TCPConnection { sendConfig.charsetname = config.charsetname; } - //TCPSocket.send(sendConfig); + TCPSocket.send(sendConfig); console.log("js成功发送TCP消息:", messageStr); return true; } @@ -159,7 +159,7 @@ class TCPConnection { channel: channel }; - //TCPSocket.disconnect(disconnectConfig); + TCPSocket.disconnect(disconnectConfig); this.channelConnections.set(channel, false); console.log(`TCP连接已断开 - Channel ${channel}`, disconnectConfig); } diff --git a/utils/http.js b/utils/http.js index b85b36c..5e0c9aa 100644 --- a/utils/http.js +++ b/utils/http.js @@ -2,8 +2,8 @@ import { useUserStore } from "../stores/modules/userInfo" import { useDeviceStore } from "../stores/modules/deviceInfo" import { useLoginStore } from "../stores/modules/login" -// const baseURL = "https://dbqb.nfdxy.net/testApi" -const baseURL = "http://192.168.40.8:9000" +const baseURL = "https://dbqb.nfdxy.net/testApi" +// const baseURL = "http://192.168.40.8:9000" const httpInterceptor = { // 拦截前触发 diff --git a/vue.config.js b/vue.config.js index 0345309..5f02fc9 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,6 +1,6 @@ module.exports = { devServer: { - /* proxy: { + proxy: { '/api': { // 你的目标服务器的请求路径前缀 target: 'https://hwjb.homilychart.com', // 目标服务器的地址 changeOrigin: true, // 是否跨域 @@ -9,16 +9,16 @@ module.exports = { '^/api': '' // 将 /api 替换为 /testApi,以便正确请求目标服务器的资源 } } - } */ - proxy: { - '/api': { // 你的目标服务器的请求路径前缀 - target: 'http://192.168.40.8:9000', // 目标服务器的地址 - changeOrigin: true, // 是否跨域 - secure: false, // 如果是https接口,需要配置这个参数 - pathRewrite: { - '^/api': '' // 将 /api 替换为 /testApi,以便正确请求目标服务器的资源 - } - } - } + } + // proxy: { + // '/api': { // 你的目标服务器的请求路径前缀 + // target: 'http://192.168.40.8:9000', // 目标服务器的地址 + // changeOrigin: true, // 是否跨域 + // secure: false, // 如果是https接口,需要配置这个参数 + // pathRewrite: { + // '^/api': '' // 将 /api 替换为 /testApi,以便正确请求目标服务器的资源 + // } + // } + // } } } \ No newline at end of file