热更新-下载安装包
downloadWgt(url, newVersion) {
plus.nativeUI.showWaiting('正在下载更新包...');
plus.downloader.createDownload(url, {filename: '_doc/update/'}, function(d, status) {
if (status === 200) {
installApp(d.filename, newVersion);
} else {
plus.nativeUI.alert('更新包下载失败,请尝试到应用市场下载安装');
}
plus.nativeUI.closeWaiting();
}).start();
},
installApp(path, newVersion) {
plus.nativeUI.showWaiting('安装文件...');
plus.runtime.install(path, {force: true}, function() {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert('应用资源更新完成!',function(){
plus.runtime.restart();
});
}, function(e) {
plus.nativeUI.closeWaiting();
plus.nativeUI.alert('安装文件失败[' + e.code + ']:' + e.message);
plus.nativeUI.alert('请尝试到应用市场下载安装');
});
},
跳转地图
openAMap(info) {
let plus = window.plus;
var url = null, id = null, f = null;
switch (plus.os.name) {
case 'Android':
url = `androidamap://viewMap?sourceApplication=${this.app}&poiname=${encodeURI(info.address)}&lat=${info.latitude}&lon=${info.longitude}&dev=0`;
f = this.androidMarket;
id = 'com.autonavi.minimap';
break;
case 'iOS':
url = `iosamap://viewMap?sourceApplication=${encodeURI(this.app)}&poiname=${encodeURI(info.address)}&lat=${info.latitude}&lon=${info.longitude}&dev=0`;
f = this.iosAppstore;
id = 'itunes.apple.com/cn/app/gao-tu-zhuan-ye-dao-hang-ban/id461703208?mt=8';
break;
default:
break;
}
plus.runtime.openURL(url, function(e) {
plus.nativeUI.confirm(`检查到您未安装"高德地图",是否到商城搜索下载?`, function(i) {
if (i.index === 0) {
f(id);
}
});
}, id);
},
openBMap(info) {
let plus = window.plus;
var url = null, id = null, f = null;
switch (plus.os.name) {
case 'Android':
url = `baidumap://map/marker?location=${info.latitude},${info.longitude}&title=${encodeURI(info.address)}&content=${encodeURI(info.stationNames)}&src=${encodeURI(this.app)}`;
f = this.androidMarket;
id = 'com.baidu.BaiduMap';
break;
case 'iOS':
url = `baidumap://map/marker?location=${info.latitude},${info.longitude}&title=${encodeURI(info.address)}&content=${encodeURI(info.stationNames)}&src=${encodeURI(this.app)}`;
f = this.iosAppstore;
id = 'itunes.apple.com/cn/app/bai-du-de-tu-yu-yin-dao-hang/id452186370?mt=8';
break;
default:
break;
}
plus.runtime.openURL(url, function(e) {
plus.nativeUI.confirm(`检查到您未安装"百度地图",是否到商城搜索下载?`, function(i) {
if (i.index === 0) {
f(id);
}
});
});
},
ios指向应用商店
plus.runtime.openURL(url);
视频播放
let videoPlay = plus.video.VideoPlayer(id, {
src: src,
showPlayBtn: false,
controls: false,
width: '100%'
});
videoPlay.play();
摄像
videoCapture() {
var cmr = plus.camera.getCamera();
var res = cmr.supportedVideoResolutions[0];
var fmt = cmr.supportedVideoFormats[0];
var _this = this;
cmr.startVideoCapture(path => {
createUpload(path);
}, error => {
}, {
resolution: res,
format: fmt,
videoMaximumDuration: 15
});
},
createUpload(file) {
let task = window.plus.uploader.createUpload(url, {method: 'POST'}, (res, status) => {
if (status === 200) {
} else {
}
});
task.addFile(file, {key: 'file'});
task.start();
},
图片下载保存
var dtask = window.plus.downloader.createDownload(url, {
filename: localImageUrl
}, function(d, status) {
if (status === 200) {
plus.gallery.save(d.filename, () => {
}, (error) => {
});
} else {
}
});
dtask.start();
图片上传
window.plus.nativeUI.actionSheet({
cancel: '取消',
buttons: [{title: '拍照'}, {title: '从相册选择'}]
}, function(e) {
if (e.index === 1) {
_this.getImage();
} else if (e.index === 2) {
_this.galleryImgs();
}
});
getImage() {
var cmr = window.plus.camera.getCamera();
var _this = this;
cmr.captureImage(function(p) {
handleImg(p);
}, function(e) {}, {
filter: 'image'
});
},
handleImg(url) {
plus.io.resolveLocalFileSystemURL(url, function(entry) {
entry.file(function(file) {
if (file.size / 1024 > 1000) {
compressImage(entry.toLocalURL(), entry.name, _this.upload);
} else {
upload(url);
}
});
}, function(e) {
});
},
compressImage(url, filename, cb, er) {
var name = '_doc/upload/' + filename;
plus.zip.compressImage({
src: url,
dst: name,
format: 'jpg',
quality: 20,
overwrite: true
}, function(zip) {
cb && cb(zip.target);
}, function() {
er && er('图片压缩失败');
});
},
galleryImgs() {
plus.gallery.pick(function(e) {
handleRestructureAlbum(e.files, 0);
}, function(e) {
}, {
filter: 'image',
multiple: true,
maximum: 2,
system: false
});
},
handleRestructureAlbum(list, index) {
if (index > list.length - 1) {return;};
let url = list[index];
plus.io.resolveLocalFileSystemURL(url, function(entry) {
entry.file(function(file) {
if (file.size / 1024 > 1000) {
_this.compressImage(entry.toLocalURL(), entry.name, (zip) => {
_this.upload(zip).then(() => {
index++;
handleRestructureAlbum(list, index);
}).catch(() => {
}, () => {
} else {
_this.upload(url).then(() => {
index++;
handleRestructureAlbum(list, index);
}).catch(() => {
}
});
}, function(e) {
});
},
upload(url) {
return new Promise((resolve, reject) => {
let serverUrl = process.env.SERVER_HOST + '/common/uploadImage.do';
let _this = this;
let task = plus.uploader.createUpload(serverUrl, {
method: 'POST'
}, function(res, status) {
if (status === 200) {
resolve();
} else {
reject('图片上传失败');
}
});
task.addFile(url, {key: 'file'});
task.start();
});
},