123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <template>
- <view class="appUpdate">
- <view>
- <u-overlay :show="showDownloadState" :opacity="0.7">
- <view class="warp">
- <view class="warp_content">
- <u-line-progress :percentage="parseInt(progressVal)" activeColor="#ff0000"></u-line-progress>
- <view class="warp_font">{{textLoading2}}</view>
- </view>
- </view>
- </u-overlay>
- </view>
- </view>
- </template>
- <script>
- import * as API_item from '@/api/item.js';
- export default {
- props:{
-
- },
- data() {
- return {
- showProgress:false,
- showDownloadState:false,
- textLoading2:'版本更新中,请稍等...',
- progressVal: 0, //进度值
- }
- },
- created() {
- console.log('页面是读到了');
- setTimeout(()=>{
- this.checkUpdate();
- },20)
- },
- methods: {
- // 版本号对比
- versionCompare(a, b) {
- //a>b 返回true
- console.log('对比版本号了');
- let res = false;
- let arrA = a.split(".");
- let arrB = b.split(".");
- arrA[0] = arrA[0]*1
- arrA[1] = arrA[1]*1
- arrA[2] = arrA[2]*1
- arrB[0] = arrB[0]*1
- arrB[1] = arrB[1]*1
- arrB[2] = arrB[2]*1
- if (arrA[0] > arrB[0]) {
- res = true;
- } else if (arrA[0] == arrB[0] && arrA[1] > arrB[1]) {
- res = true;
- }else if(arrA[0] == arrB[0] && arrA[1] == arrB[1] && arrA[2] > arrB[2]){
- res = true;
- }
- return res;
- },
- // 检查更新
- checkUpdate(){
- console.log('开始检查更新了');
- let _this = this
- let sys = plus.os.name.toLowerCase();
- var VERSION
- plus.runtime.getProperty(plus.runtime.appid, function(inf) {
- console.log('获取版本号',inf);
- VERSION = inf.version;
- })
- API_item.getUpdateJson().then(function(res){
- console.log('获取到结果了吗',res);
- let data = res;
- //安装式更新
- // if (sys == "ios") {
- // //版本判断,提示更新
- // this.$http.jsonp("http://itunes.apple.com/cn/lookup?id=1232107347")
- // .then(
- // function(Res) {
- // _this.versionCompare(Res.body.results[0].version, VERSION)? _this.showNeedUpdate('ios',data.isMustUpdate) : void 0
- // }
- // )
- // } else
- console.log('获取终端机类型',sys);
- if (sys == "android") {
- console.log('分别是参数',data.version, VERSION,_this.versionCompare(data.version, VERSION));
- _this.versionCompare(data.version, VERSION)? _this.showNeedUpdate('android',data.isMustUpdate,data.packageDown) : void 0
- }
- })
- },
- // 强制更新
- showNeedUpdate(func,isMust,downUrl){ //isMust 是否必须更新
- console.log('得强制更新了')
- let _this = this
- let connectionStatus = plus.networkinfo.getCurrentType(); //在Ios上获取网络类型正常,但是只能获取app打开之前的网络状态,
- // 强制退出
- uni.showModal({
- title: '更新提示',
- content: func==='android'?'有新版本,点击更新':'有新版本,点击更新<br><span style="font-size:12px;">(确定后5s自动退出)</span>',
- confirmText:"立即更新",
- cancelText:isMust==1?"退出应用":"取消",
- success:function(res){
- if (res.confirm) { //点击了确定更新
- if(func==='hot'){
- if(connectionStatus == 2 || connectionStatus == 3){
- _this.downWgt()
- }else{
- _this.$vux.confirm.show({
- title: '网络提示',
- content:'您正在使用移动流量,更新大约需20M',
- confirmText:"打开wifi(免流量)",
- cancelText:"继续",
- onCancel () {
- _this.downWgt()
- },
- onConfirm (msg) {
- _this.openSysSet()
-
- }
- })
- }
- if(isMust == 1){
- _this.afterQuit(func) //退出app
- }
- }else if(func==='ios'){
- plus.runtime.openURL("https://itunes.apple.com/cn/app/id1232107347")
- if(isMust == 1){
- _this.afterQuit(func) //退出app
- }
- }else if(func==='android'){
- _this.downWgt(downUrl,isMust)
-
- }
- } else if (res.cancel) { //点击了取消
- if(isMust == 1){
- if(func === 'ios'){
- const threadClass = plus.ios.importClass("NSThread");
- const mainThread = plus.ios.invoke(threadClass, "mainThread");
- plus.ios.invoke(mainThread, "exit");
- }else if(func === 'android'){
- plus.runtime.quit()
- }
- }else{
- void 0
- }
- }
- }
- })
- },
- // 下载wgt文件
- downWgt (downUrl,isMust){
- console.log('转到下载文件',downUrl,isMust);
- let _this = this
- let timer = (new Date).valueOf()
- let changeTime = 300
- if(downUrl){
- // _this.showProgress = true
- _this.showDownloadState = true
- var downloader = plus.downloader.createDownload(downUrl)
- downloader.addEventListener("statechanged", function(dtask,status){
- console.log('下载得一些状态',dtask,status);
- if(dtask.state == 4 && status == 200){
- _this.textLoading2 = "恭喜您,下载成功"
- uni.showModal({
- title: '安装提示',
- content: "下载已完成,是否安装?",
- confirmText:"立即安装",
- cancelText:isMust==1?"退出应用":"取消",
- success:function(res){
- if (res.confirm) { //点击了确定更新
- plus.runtime.openFile(dtask.getFileName());
- }else if(res.cancel){
- if(isMust == 1){
- plus.runtime.quit()
- }else{
- plus.runtime.quit()
- }
- }
- }
- })
- }else{
- switch(dtask.state) {
- case 1: // 开始
- _this.textLoading2 = "开始下载..."
- break;
- case 2: // 已连接到服务器
- _this.textLoading2 = "连接到服务器.."
- break;
- case 3: // 已接收到数据
- let _time = (new Date).valueOf()
- if(_time-timer>changeTime){
- timer = _time
- // _this.showLoading = true
- _this.progressVal = ((dtask.downloadedSize/dtask.totalSize).toFixed(2)*(100))
- _this.textLoading2 = "下载更新文件..."
- }
- break;
- case 4: // 下载完成
- break;
- }
- }
- })
- downloader.start();
- }else if(_this.wgtUrl){
- var downloader = plus.downloader.createDownload( _this.wgtUrl, {filename:"_doc/update/"}
- , function(d,status){ //下载完成执行的回调函数
- if ( status == 200 ) {
- // alert("下载wgt成功:"+d.filename);
- _this.installWgt(d.filename); // 安装wgt包
- } else {
- // console.log("下载wgt失败!");
- plus.nativeUI.alert("下载wgt失败!");
- }
- }
- )
- downloader.addEventListener("statechanged", function(dtask,status){
- if(!dtask){return;}
- switch(dtask.state) {
- case 1: // 开始
- _this.showLoading = true
- _this.textLoading = "开始下载..."
- break;
- case 2: // 已连接到服务器
- _this.showLoading = true
- _this.textLoading = "连接到服务器.."
- break;
- case 3: // 已接收到数据
- let _time = (new Date).valueOf()
- if(_time-timer>changeTime){
- timer = _time
- _this.showLoading = true
- _this.progressVal = ((dtask.downloadedSize/dtask.totalSize).toFixed(2)*(100))
- _this.textLoading = "下载更新文件 "+progressVal.substr(0,4)+'%'
- }
- break;
- case 4: // 下载完成
- _this.showLoading = false
- break;
- }
- })
- downloader.start();
- }else{
- return false
- }
- },
- //打开wifi,用WiFi下载
- openSysSet(){
- let sys = plus.os.name.toLowerCase();
- this.backFromSetWIFI = true
- if(sys == "ios"){ //操作苹果手机
- plus.runtime.launchApplication({action:'App-Prefs:root=WIFI'}, function(e){
- console.log(JSON.stringify(e));
- }); //WIFI
- } else { //操作安卓收集
- var main = plus.android.runtimeMainActivity();
- var Intent = plus.android.importClass("android.content.Intent");
- var mIntent = new Intent('android.settings.WIFI_SETTINGS');
- main.startActivity(mIntent);
- }
- },
- // 避免旧版本使用退出
- afterQuit(func){
- setTimeout(()=>{
- if(func==='ios'){
- const threadClass = plus.ios.importClass("NSThread");
- const mainThread = plus.ios.invoke(threadClass, "mainThread");
- plus.ios.invoke(mainThread, "exit");
- }else if(func==='android'){
- plus.runtime.quit()
- }else if(func==='hot'){
- plus.runtime.quit();
- const threadClass = plus.ios.importClass("NSThread");
- const mainThread = plus.ios.invoke(threadClass, "mainThread");
- plus.ios.invoke(mainThread, "exit");
- }
- },5000)
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .appUpdate {
- .warp {
- display: flex;
- align-items: center;
- justify-content: center;
- height: 100%;
- .warp_content{
- width: 90%; margin: 0 auto; text-align: center;
- .warp_font{
- margin-top: 30rpx; color: #f9f9f9;
- }
- }
- }
- }
- </style>
|