直接上代码吧,你竟然搜到了我的文章就应该知道了,为什么要热更新
1//app热更新下载 2//假定字符串的每节数都在5位以下 3function toNum(a) { 4 //也可以这样写 var c=a.split(/\./); 5 var c = a.split('.'); 6 var num_place = ["", "0", "00", "000", "0000"], 7 r = num_place.reverse(); 8 for(var i = 0; i < c.length; i++) { 9 var len = c[i].length; 10 c[i] = r[len] + c[i]; 11 } 12 var res = c.join(''); 13 return res; 14} 15 16var btn = ["确定升级", "取消"]; 17//获取app系统更新[是否手动点击获取更新] 18function appUpdate(Index) { 19 console.log('appUpdate'); 20 mui.plusReady(function() { 21 plus.runtime.getProperty(plus.runtime.appid, function(inf) { 22 ver = inf.version + ''; 23 console.log('ver:' + ver); 24 var client; 25 var ua = navigator.userAgent.toLowerCase(); 26 if(/iphone|ipad|ipod/.test(ua)) { //苹果手机 27 mui.ajax({ 28 type: "get", 29 dataType: 'json', 30 url: "https://itunes.apple.com/lookup?id=1462614850", //获取当前上架APPStore版本信息 31 data: { 32 id: 1462614850 //APP唯一标识ID 33 }, 34 contentType: 'application/x-www-form-urlencoded;charset=UTF-8', 35 success: function(data) { 36 console.log('data:' + JSON.stringify(data)); 37 var resultCount = data.resultCount; 38 for(var i = 0; i < resultCount; i++) { 39 var normItem = data.results[i].version; 40 console.log('normItem:' + normItem) 41 if(normItem > ver) { 42 var _msg = "发现新版本:V" + normItem; 43 //plus.nativeUI.alert("发现新版本:V" + normItem); 44 mui.confirm(_msg, '升级确认', btn, function(e) { 45 if(e.index == 0) { //执行升级操作 46 document.location.href = 'https://itunes.apple.com/cn/app/%E5%AD%A9%E5%84%BF%E6%AC%A2/id1462614850?mt=8'; //上新APPStore下载地址 47 } 48 }); 49 return; 50 } 51 } 52 if(ismanual) { 53 mui.toast('当前版本号已是最新'); 54 } 55 return; 56 } 57 }); 58 } else if(/android/.test(ua)) { 59 mui.ajax(ip + "APIVApp/SelectVApp", { 60 data: { 61 apkVersion: ver, 62 }, 63 dataType: 'json', 64 type: 'get', 65 timeout: 10000, 66 success: function(data) { 67 console.log('data:' + JSON.stringify(data)) 68 console.log(toNum(ver)) 69 if(toNum(data[0]._vname) > toNum(ver)) { 70 var _msg = "发现新版本:V" + data[0]._vname; 71 mui.confirm(_msg, '升级确认', btn, function(e) { 72 if(e.index == 0) { //执行升级操作 73 downWgt(); 74 } 75 }); 76 } else { 77 console.log(Index); 78 if(Index) { 79 mui.toast('当前版本号已是最新'); 80 } 81 return; 82 } 83 }, 84 error: function(xhr, type, errerThrown) { 85 mui.toast('网络异常,请稍候再试'); 86 } 87 }); 88 } 89 }); 90 }); 91} 92 93// 下载wgt文件 94function downWgt() { 95 var wgtUrl = ip + "app/H5750CDB5.wgt"; 96 plus.nativeUI.showWaiting("下载更新文件..."); 97 plus.downloader.createDownload(wgtUrl, { 98 filename: "_doc/update/" 99 }, function(d, status) { 100 if(status == 200) { 101 console.log("下载更新文件成功:" + d.filename); 102 installWgt(d.filename); //安装wgt包 103 104 } else { 105 console.log("下载失败!"); 106 plus.nativeUI.alert("下载失败!"); 107 } 108 plus.nativeUI.closeWaiting(); 109 }).start(); 110} 111 112function installWgt(path) { 113 plus.nativeUI.showWaiting("安装更新文件..."); 114 plus.runtime.install(path, {}, function() { 115 plus.nativeUI.closeWaiting(); 116 console.log("安装更新文件成功!"); 117 plus.nativeUI.alert("应用资源更新完成!", function() { 118 plus.runtime.restart(); 119 }); 120 }, function(e) { 121 plus.nativeUI.closeWaiting(); 122 console.log("安装更新文件失败[" + e.code + "]:" + e.message); 123 plus.nativeUI.alert("安装更新文件失败[" + e.code + "]:" + e.message); 124 if(e.code == 10) { 125 alert('请清除临时目录'); 126 } 127 }); 128}