cordova plugin add cordova-plugin-appversion //获取当前版本号
cordova plugin add cordova-plugin-file-transfer //下载
判断平台
判断平台 let vm = this let browser = { versions: function () { let u = navigator.userAgent, app = navigator.appVersion; return {//移动终端浏览器版本信息 trident: u.indexOf(‘Trident‘) > -1, //IE内核 presto: u.indexOf(‘Presto‘) > -1, //opera内核 webKit: u.indexOf(‘AppleWebKit‘) > -1, //苹果、谷歌内核 gecko: u.indexOf(‘Gecko‘) > -1 && u.indexOf(‘KHTML‘) == -1, //火狐内核 mobile: !!u.match(/AppleWebKit.*Mobile/i) || !!u.match(/MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/), //是否为移动终端 ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端 android: u.indexOf(‘Android‘) > -1 || u.indexOf(‘Linux‘) > -1, //android终端或者uc浏览器 iPhone: u.indexOf(‘iPhone‘) > -1 || u.indexOf(‘Mac‘) > -1, //是否为iPhone或者QQHD浏览器 iPad: u.indexOf(‘iPad‘) > -1, //是否iPad webApp: u.indexOf(‘Safari‘) == -1 //是否web应该程序,没有头部与底部 }; } (), language: (navigator.browserLanguage || navigator.language).toLowerCase() } if (browser.versions.iPhone || browser.versions.iPad || browser.versions.ios ) { vm.platform = 1 } if (browser.versions.android) { vm.platform = 2 } //platform ==1 ios平台,2安卓平台
如果是ios,直接跳到苹果商店相应的位置就好
安卓
let vm = this window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) { let url = ‘url‘; fs.root.getFile(‘userApp.apk‘, {create: true, exclusive: false}, function (fileEntry) { download(fileEntry.toURL(), url); }, onErrorCreateFile); }, onErrorLoadFs); function download(fileEntry, url) { let ft = new FileTransfer(); let fileURL = fileEntry; ft.onprogress = function (e) { //获取进度 if (e.lengthComputable) { vm.percentShow = true //进度e.loaded / e.total
vm.$set(vm.percent,‘num‘,parseInt( e.loaded / e.total * 100, 10)) const colorMap = [‘#FFA55E‘] vm.$set(vm.percent,‘strokeColor2‘,colorMap[parseInt( e.loaded / e.total * 3, 10)]) } } ft.download(url, fileURL, function (entry) { cordova.plugins.fileOpener2.open( fileEntry, ‘application/vnd.android.package-archive‘, { error : function(e) { console.log(‘Error status: ‘ + e.status + ‘ - Error message: ‘ + e.message); }, success : function () { console.log(‘file opened successfully‘); } } ); }, function (err) { alert("下载失败!:"+JSON.stringify(err)); }, null, // or, pass false { //headers: { // "Authorization": "Basic dGVzdHVzZXJuYW1lOnRlc3RwYXNzd29yZA==" //} }); } //文件创建失败回调 function onErrorCreateFile(error) { alert("文件创建失败!") } //FileSystem加载失败回调 function onErrorLoadFs(error) { alert("文件系统加载失败!") }
时间: 2024-10-18 03:11:16