更新记录
1.0.5(2023-12-15)
- 修复Android setCustomConentType接口个别后缀不生效问题
1.0.4(2023-12-13)
- demo更新vue2兼容性问题
1.0.3(2023-12-13)
- 更新iOS支持的最低版本,设为iOS 9.0
平台兼容性
Vue2 | Vue3 |
---|---|
√ | √ |
App | 快应用 | 微信小程序 | 支付宝小程序 | 百度小程序 | 字节小程序 | QQ小程序 |
---|---|---|---|---|---|---|
HBuilderX 3.6.8,Android:4.4,iOS:9 | × | × | × | × | × | × |
钉钉小程序 | 快手小程序 | 飞书小程序 | 京东小程序 |
---|---|---|---|
× | × | × | × |
H5-Safari | Android Browser | 微信浏览器(Android) | QQ浏览器(Android) | Chrome | IE | Edge | Firefox | PC-Safari |
---|---|---|---|---|---|---|---|---|
× | × | × | × | × | × | × | × | × |
前言
HttpServe是Android、iOS搭建APP本地web service的插件
支持定制QQ252797991
功能
- app本地浏览多个静态web网站,支持自定义路径、_www 、_doc、_documents、_downloads等路径
- 支持get、post等请求,自定义返回数据,数据类型支持json、html、file
- 支持文件上传下载
- 搭建本地服务播放本地hls/m3u8视频
- 支持跨域
- 支持后台运行,后台运行配置请使用插件https://ext.dcloud.net.cn/plugin?id=10450
不熟悉UTS插件集成步骤的同学可以参考https://www.jianshu.com/p/830ccc503e29或官网https://doc.dcloud.net.cn/uni-app-x/plugin/
引入声明变量
建议变量声明成全局单例变量,避免开发阶段vue热更新后引起启动服务端口被占用导致失败的问题
import {
UTSHttpServer
} from "@/uni_modules/wrs-uts-httpserver";
let httpServer = new UTSHttpServer()
场景 1 app本地浏览多个静态web网站或下载某个目录下的文件
let urlPath = "/loginSys/.*?"
let fileDir = absPath + '/static/loginSys'
httpServer.addWebsite(urlPath, fileDir, () => {
// 添加addWebsite完成
});
启动成功后,在浏览器输入host + port + urlPath + 网站文件,如:
http://192.168.6.31:8888/loginSys/index.html
场景 2 搭建本地服务播放本地hls/m3u8视频
let urlPath = "/video/hls/.*?"
let fileDir = plus.io.convertLocalFileSystemURL("_www/static/video/hls")
httpServer.addWebsite(urlPath, fileDir, () => {
// 添加addWebsite完成
});
播放地址
http://172.16.11.44:8888/video/hls/index.m3u8
场景 3 下载某个特定文件
let urlPath = "/downloadFile"
let filePath = plus.io.convertLocalFileSystemURL("_www/static/video/movie.mp4")
httpServer.downloadFile(urlPath, filePath, (request) => {
}, () => {
// 添加downloadFile完成
});
以下功能接口涉及到动态返回数据,目前只支持Android,iOS当前受限当前版本UTS语法,将在下个版本支持
- Get请求
let urlPath = "/get"
httpServer.get(urlPath, (request) => {
console.log("Get request:" + JSON.stringify(request))
// 返回给客服端的数据
this.sendJSONResponse(request)
// android:
// {"url":"/get","headers":{"accept":"*/*","user-agent":"PostmanRuntime/7.35.0","accept-encoding":"gzip, deflate, br","host":"172.16.11.21:8888","connection":"keep-alive","postman-token":"bd55de40-aae8-47a6-b971-83205df665f7"},"path":"/get","method":"GET","urlParams":{},"requestID":"/get_514aff025303-4c6a-93dc-1affef73c579"}
}, () => {
// 添加get完成
});
- Post请求
let urlPath = "/post"
httpServer.post(urlPath, (request) => {
console.log("Post request:" + JSON.stringify(request))
this.sendJSONResponse(request)
}, () => {
// 添加post完成
});
- Post请求-FormData
let urlPath = "/postFormData"
var saveDir = plus.io.convertLocalFileSystemURL('_downloads'); // 文件保存目录
httpServer.postFormData(urlPath, saveDir, (request) => {
console.log("Post request:" + JSON.stringify(request))
this.sendJSONResponse(request)
}, () => {
// 添加post完成
});
- 上传文件
let urlPath = "/uploadFile"
var saveDir = plus.io.convertLocalFileSystemURL('_downloads'); // 文件保存目录
httpServer.uploadFile(urlPath, saveDir, (request) => {
console.log("uploadFile request:" + JSON.stringify(request))
console.log("上传文件保存目录:" + saveDir)
this.sendJSONResponse(request)
// ios:
// {"remoteAddress":"172.16.11.60:59430","body":{"age":{"isFile":false,"value":"20"},"name":{"isFile":false,"value":"wrs"},"file":{"fileName":"text.txt","isFile":true,"path":"/var/mobile/Containers/Data/Application/0365A8A9-B926-4571-9C4D-C251DC7AAB26/Library/Caches/Pandora/downloads/text.txt"}},"urlParams":{},"url":"http://172.16.11.44:8888/uploadFile","localAddress":"172.16.11.44:8888","method":"POST","headers":{"User-Agent":"PostmanRuntime/7.32.3","Accept":"*/*","Content-Length":"651","Content-Type":"multipart/form-data; boundary=--------------------------445338919225193221741331","Host":"172.16.11.44:8888","Postman-Token":"d1501ccd-0e2e-407d-89ad-6604f3e3b76f","Accept-Encoding":"gzip, deflate, br","Connection":"keep-alive"},"path":"/uploadFile"}
}, () => {
// 添加uploadFile完成
});
返回给客服端的数据
- 返回JSON
let requestID = request.requestID
let resp = {
type: "json", // 返回类型
value: { // 返回值,即响应数据
"name": "张三",
"info": {
"age": 12
},
teams: [{
"name": "xxx"
}]
},
headers: { // 返回头
token: "xxxx-xx"
}
}
httpServer.sendResponse(requestID, resp)
- 返回html或text
let requestID = request.requestID
let resp = {
type: "html", // html或text
value: '<!DOCTYPE html><html><head><meta charset="utf-8"></head><body>你好,我是html</body></html>',
headers: {
token: "xxxx-xx"
}
}
httpServer.sendResponse(requestID, resp)
- 返回文件,即下载文件
var filePath = plus.io.convertLocalFileSystemURL("_www/static/video/movie.mp4")
let requestID = request.requestID
let resp = {
type: "file",
value: filePath,
headers: {
token: "xxxx-xx"
}
}
httpServer.sendResponse(requestID, resp)
自定义ContentFile
httpServer.setCustomConentType({
".css": "text/css;charset=utf-8",
".js": "application/javascript"
})