更新记录
1.0.0(2025-09-19)
安卓局域网服务端UTS原生插件可以为为局域网提供http服务,支持自定义接口返回数据,插件UTS开发,支持uniapp和uniapp x
平台兼容性
uni-app(4.51)
Vue2 | Vue2插件版本 | Vue3 | Vue2插件版本 | Chrome | Safari | app-vue | app-vue插件版本 | app-nvue | app-nvue插件版本 | Android | Android插件版本 | iOS | 鸿蒙 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
√ | 1.0.0 | √ | 1.0.0 | × | × | √ | 1.0.0 | √ | 1.0.0 | 5.0 | 1.0.0 | × | × |
微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 快应用-华为 | 快应用-联盟 |
---|---|---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × | × | × |
uni-app x(4.51)
Chrome | Safari | Android | Android插件版本 | iOS | 鸿蒙 | 微信小程序 |
---|---|---|---|---|---|---|
× | × | 5.0 | 1.0.0 | × | × | × |
其他
多语言 | 暗黑模式 | 宽屏模式 |
---|---|---|
× | × | √ |
插件介绍
安卓局域网服务端UTS原生插件可以为为局域网提供http服务,支持自定义接口返回数据,插件UTS开发,支持uniapp和uniapp x
使用场景
比如浏览器输入局域网网址,该网址的页面需要读取机器的硬件信息就需要借助http服务提供接口供页面调用,通过插件开发app为页面提供接口服务读取硬件数据并回调给页面 具体示例:比如开发一个h5游戏,玩家通过一关后需要通过设备打印小票等
插件使用流程
- 开启服务监听
所有的请求都会在监听中回调,开发者可以通过请求的uri来开发接口
- 开启http服务
只有开启了http服务才能通过postman或apipost等接口调试软件访问服务
- 向请求者发送数据
通过调用发送数据的方法向请求者发送他需要的数据,如果不发送数据默认请求等待时间为15秒(插件支持自定义超时时间),发送的数据支持自定义数据
用法:
在需要使用插件的页面加载以下代码
import * as module from "@/uni_modules/leven-uts-http"
使用文档
- 您也可以参考以下链接的使用文档
- 使用说明文档
插件方法
- 服务监听
- 开启http服务
- 关闭服务
- 获取ip地址
- 发送数据
具体方法的使用请参考使用说明文档
页面内容参考
<template>
<view class="content">
<view class="running-status">
<view>当前服务运行状态:</view>
<view :class="{
'running-status-open':isOpen,
'running-status-close':!isOpen
}">{{isOpen ? "运行中" : "已关闭"}}</view>
<view style="margin-left: 10px;"><button size="mini" @click="checkService">{{isOpen ? "停止服务" : "开启服务"}}</button>
</view>
</view>
<view class="title">运行日志</view>
<view class="content-body" v-html="logStr"></view>
</view>
</template>
<script>
import * as module from "@/uni_modules/leven-uts-http"
import {
dateFormat
} from "@/utils/index.js"
export default {
data() {
return {
logStr: "",
isOpen: false
}
},
onLoad() {
},
onReady() {
this.startService();
},
methods: {
//开启服务
startService() {
//注册监听
this.httpListener();
setTimeout(() => {
//开启服务
this.startHttp();
}, 300)
},
checkService() {
if (this.isOpen) {
this.stopHttp();
} else {
this.startService();
}
},
getIpAddress() {
module.getIpAddress(res => {
console.log(res)
})
},
stopHttp() {
module.stopHttp(res => {
console.log(res)
})
},
startHttp() {
module.startHttp({
port: 8080
}, res => {})
},
httpListener() {
module.httpListener(res => {
if (res) {
if (res.code != 0) {
this.writeLog(res.message);
} else {
//成功
let status = res.data.status;
switch (status) {
case "onStart":
this.writeLog("开启服务成功");
this.isOpen = true;
break;
case "onStop":
this.writeLog("关闭服务成功");
this.isOpen = false;
break;
case "onReceive":
this.writeLog("收到请求数据:" + JSON.stringify(res.data));
this.dealRequest(res.data);
break;
}
}
}
})
},
//处理请求数据
dealRequest(data) {
switch (data.uri) {
case "/getIpAddress":
//获取ip地址
this.getIpAddress();
break;
default:
module.sendData({
"error": "无效请求"
}, res => {
console.log(res)
})
}
},
//获取ip地址
getIpAddress() {
module.getIpAddress(res => {
module.sendData(res.data, resSend => {
console.log(resSend)
});
})
},
writeLog(str) {
let logStr = "<p>" + dateFormat(null, "yyyy-mm-dd hh:MM:ss") + " " + str + "</p>";
this.logStr = logStr + this.logStr;
}
}
}
</script>
<style scoped>
.running-status {
display: flex;
flex-direction: row;
align-items: center;
margin-bottom: 8px;
padding: 0 16px;
}
.running-status-open {
color: #51e617;
}
.running-status-close {
color: "#e6151f"
}
.title {
height: 40px;
background-color: #bbbcbd;
color: #FFFFFF;
display: flex;
flex-direction: row;
align-items: center;
padding: 0 16px;
}
.content-body {
padding: 16px;
}
</style>
购买说明
- 购买插件前请先试用,试用通过再购买。在试用中如果遇到任何问题,可与作者联系,将全力协助你使用本插件。