import store from '../store/index.js' import { baseURL } from '../config/app' const getUpAppUrl = baseURL + '/api/index/version' const iosAppStroeUrl = 'https://apps.apple.com/cn/app/%E5%BE%AE%E4%B9%90%E7%A7%8D/id1628702591'; // 获取app是否需要升级 export function getUpApp() { // 获取当前运行系统 let hj = uni.getSystemInfoSync().platform; // 获取仓库app数据对象 let app = store.state.isShowIllegality; // 当前系统为安卓则显示数据 if (hj === 'android') { // 设置默认显示数据 app = true; // store.commit('changeState', { // name: 'app', // value: app // }); } console.log(getUpAppUrl); uni.request({ url: getUpAppUrl, method: 'GET', data: {}, success: res => { console.log(res, '版本号'); let r = res.data.data; plus.runtime.getProperty(plus.runtime.appid, function(wgtinfo) { // 保存线上版本号 let version = r.app_android; // 获取线上版本 const arr = r.version.split('.'); // 获取当前系统 const arr1 = wgtinfo.version.split('.'); console.log(arr, arr1) for (let i = 0; i < arr.length; i++) { // 线上版本号 const x = +arr[i]; // 线下版本号 const y = +arr1[i]; console.log(arr, arr1, "版本号") // 判断当前版本是否为小于等于线上版本 if (x >= y) { console.log('dashabi2020') console.log(x, y) // 只有ios才需要显示 if (hj === 'ios') { // 设置显示数据 app = true; store.commit('changeState', { name: 'app', value: app }); } // 当版本较低时更新数据 if (x > y) { uni.showModal({ title: '提示', content: '请更新应用', showCancel: false, success(e) { if (hj === 'ios') { plus.runtime.openURL( iosAppStroeUrl ); } if (hj === 'android') { // plus.runtime.openURL('http://lxscimg.liuniu946.com/lxscV' + version + '.apk'); downApp(version); } } }); return; } } } }); }, fail: res => { console.log(res, 'shib'); } }); } // 下载app export function downApp(version) { console.log(version); uni.navigateTo({ url: '/pages/index/downLoad?apk=' + version, fail(err) { console.log(err); } }) // dtask = plus.downloader.createDownload(version); // dtask.addEventListener("statechanged", onStateChanged, false); // dtask.start(); // uni.showLoading({ // title: '下载中...', // mask: true // }); // plus.nativeUI.showWaiting('下载升级包...'); // plus.downloader // .createDownload(version, {}, (d, status) => { // if (status == 200) { // installApp(d.filename); // 安装app // } else { // plus.nativeUI.alert('下载升级包失败!'); // } // plus.nativeUI.closeWaiting(); // }) // .start(); } // 监听下载任务状态 function onStateChanged(download, status) { switch (download.state) { case 1: // showLoading.setTitle("正在下载"); break; case 2: // showLoading.setTitle("已连接到服务器"); break; case 3: prg = parseInt( //下载的进度 (parseFloat(download.downloadedSize) / parseFloat(download.totalSize)) * 100 ); // showLoading.setTitle("版本更新,正在下载" + prg + "% "); this.percent = prg; console.log(prg); break; case 4: // plus.nativeUI.closeWaiting(); //关闭系统提示框 this.showProcess = false; //下载完成 break; } } // 安装app export function installApp(path) { plus.nativeUI.showWaiting('安装升级包...'); plus.runtime.install( path, {}, function() { plus.nativeUI.closeWaiting(); uni.showToast({ icon: 'none', title: '升级完成,准备重新载入' }); setTimeout(_ => { uni.hideToast(); plus.runtime.restart(); }, 1000); }, function(e) { plus.nativeUI.closeWaiting(); plus.nativeUI.alert('安装升级包失败[' + e.code + ']:' + e.message); } ); }