import { appCheckUpdate } from '../api/request2.js'; export function appUpdate(isShowHint) { let curversion = 0; plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) { const data = { action: 'checkVersion', appid: plus.runtime.appid, appVersion: plus.runtime.version, wgtVersion: widgetInfo.version, versionCode: widgetInfo.versionCode } curversion = data.versionCode appCheckUpdate().then(res => { console.log("当前版本提示", curversion) var json = JSON.stringify(res) console.log("获取更新数据", json) if (res.data) { if (res.data.installPackageVersion > Number(curversion)) { var downUrl = res.data.installPackageUrl; var content = res.data.updateContent; var version = res.data.installPackageVersion; var isForcedUpdate = res.data.isForcedUpdate var versionName = res.data.installPackageName console.log("新版本提示") showDialog(versionName, content, isForcedUpdate, downUrl) // uni.showModal({ // title: "发现新版本:(" + version + ")", // content: content, // confirmText: "更新", // showCancel: isForcedUpdate == "TRUE" ? false : true, // cancelText: "取消", // success: (res) => { // if (res.confirm) { // confirm(downUrl); // console.log('comfirm') //点击确定之后执行的代码 // } else { // console.log('cancel') //点击取消之后执行的代码 // if (isForcedUpdate == "TRUE") { // } // } // } // }) } else { if (isShowHint) { uni.showToast({ title: "当前是最新版本" }) } console.log("没有新版本") } } }).catch(error => { console.log("版本错误", error) }) }) } export function showDialog(versionName, content, isForcedUpdate, downUrl) { uni.showModal({ title: "发现新版本:(" + versionName + ")", content: content, confirmText: "更新", showCancel: isForcedUpdate == "TRUE" ? false : true, cancelText: "取消", success: (res) => { if (res.confirm) { confirm(downUrl); console.log( 'comfirm') //点击确定之后执行的代码 } else { console.log( 'cancel') //点击取消之后执行的代码 if (isForcedUpdate == "TRUE") { showDialog(versionName, content, isForcedUpdate, downUrl) } } } }) } export function confirm(downUrl) { var downloadApkUrl = downUrl var dtask = plus.downloader.createDownload(downloadApkUrl, {}, function(d, status) { // 下载完成 if (status == 200) { plus.runtime.install(plus.io.convertLocalFileSystemURL( d.filename), {}, {}, function(error) { uni.showToast({ title: '安装失败', duration: 1500 }); }) } else { uni.showToast({ title: '更新失败', duration: 1500 }); } }); try { dtask.start(); // 开启下载的任务 var prg = 0; var showLoading = plus.nativeUI.showWaiting( "正在下载"); //创建一个showWaiting对象 dtask.addEventListener('statechanged', function( task, status ) { // 给下载任务设置一个监听 并根据状态 做操作 switch (task.state) { case 1: showLoading.setTitle("正在下载"); break; case 2: showLoading.setTitle("已连接到服务器"); break; case 3: if (task.totalSize && task.totalSize > 0) { prg = parseInt( (parseFloat(task.downloadedSize) / parseFloat(task.totalSize)) * 100 ); showLoading.setTitle("正在下载" + prg + "% "); }else { showLoading.setTitle("正在下载"); } break; case 4: plus.nativeUI.closeWaiting(); //下载完成 break; } }); } catch (err) { plus.nativeUI.closeWaiting(); uni.showToast({ title: '更新失败-03', mask: false, duration: 1500 }); } }