前言
wrs-httpserver是Android、iOS搭建APP本地web service的插件,常用的场景有: <br />
- app本地离线播放m3u8视频
- app本地浏览web网站,支持绝对路径,_www ,_doc,_documents,_downloads等相对路径需要转成本地绝对路径,下面有转换方法。
功能
- 启动http service,可配置多个web站点服务
wrs-httpserver插件
场景 1. 直接访问在uniapp项目源码里的web站点
var httpServer = uni.requireNativePlugin("wrs-httpserver");
// 把_www相对路径转为绝对路径
var absPath = plus.io.convertLocalFileSystemURL('_www');
// Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
if (absPath.endWith('/')) {
absPath = absPath.substring(0, absPath.length - 1);
}
var params = {
port: 8030, // http service的端口
staticWebs: [
{
urlPath: '/register', // 站点请求的url
directoryPath: absPath + '/static/registerSys' // web站点文件绝对路径,这个路径是绝对路径
},
{
urlPath: '/login',
directoryPath: absPath + '/static/loginSys'
}
]
};
httpServer.startServer(params, (resp) => {
if (resp.code == 0) {
console.log("httpServer启动成功:" + resp.serverHost);
} else {
console.log("httpServer启动失败:" + resp.msg);
}
});
启动成功后,在浏览器输入resp.serverHost + port + urlPath + 网站文件,如:
http://192.168.6.31:8030/register/index.html
http://192.168.6.31:8030/login/index.html
loginSys、loginSys这两个站点的源码放到HBuildx项目里的static文件夹里
![](https://img.haomeiwen.com/i3531360/83a982dab97678e5.png)
场景 2. 通过接口网络下载web站点源码并访问
// 下载web站点压缩文件
uni.downloadFile({
url: "http://192.168.6.89:8080/video/registerSys.zip", //仅为示例,并非真实的资源
success: (res) => {
if (res.statusCode === 200) {
var zipfile = res.tempFilePath;
// 下载后的压缩文件解压到_downloads目录
var targetPath = '_downloads/';
var absPath = plus.io.convertLocalFileSystemURL(targetPath);
// 经测试,修正在华为Android 10的系统上解压的解压目录,Android上使用绝对路径
switch (uni.getSystemInfoSync().platform) {
case 'android':
targetPath = plus.io.convertLocalFileSystemURL(targetPath);
break;
case 'ios':
targetPath = '_downloads/';
break;
default:
break;
}
plus.zip.decompress(zipfile, targetPath,
function() {
var path = plus.io.convertLocalFileSystemURL(targetPath);
console.log('解压成功绝对路径:' + path);
},
function(error) {
console.log('解压解压失败:' + error);
});
} else {
console.log('下载失败');
}
}
});
解压后_downloads目录已经有站点文件了,在启动http server时把_downloads下的站点也加进入即可:
// _www ,_doc,_documents,_downloads
var staticWebs = [];
var paths = ['_www', '_downloads'];
for (var i = 0; i < paths.length; i++) {
var path = paths[i];
var absPath = plus.io.convertLocalFileSystemURL(path);
// Android获取的absPath以/结尾,iOS获取的absPath不是/结尾
if (absPath.endWith('/')) {
absPath = absPath.substring(0, absPath.length - 1);
}
switch (i) {
case 0: {
staticWebs.push({
urlPath: '/staticLoginSys',
directoryPath: absPath + '/static/loginSys',
});
staticWebs.push({
urlPath: '/',
directoryPath: absPath + '/static/registerSys',
});
}
break;
case 1: {
staticWebs.push({
urlPath: '/downloadsSys',
directoryPath: absPath + '/registerSys',
});
}
break;
default:
break;
}
console.log('path:' + path + ' 绝对路径:' + absPath);
}
var appid = plus.runtime.appid;
var params = {
port: 8030,
staticWebs: staticWebs
};
this.staticWebs = staticWebs;
httpServer.startServer(params, (resp) => {
if (resp.code == 0) {
console.log("httpServer启动成功:" + resp.serverHost);
} else {
console.log("httpServer启动失败:" + resp.msg);
}
});
_downloads下web站点的访问地址是:
http://192.168.6.31:8030/downloadsSys/index.html
如果Android打包时出现如下错误,请再次提交打包,这是uniapp云打包下载库没有及时下载到造成的,需要多试几次
![](https://img.haomeiwen.com/i3531360/b79d287fba65d0fd.png)
网友评论