uniapp版本升级(app内部下载)

获取服务端返回的下载链接,以及版本号。

import {
	// 获取线上版本信息
	getVersonInfo
} from '@/api/config.js'

let version = '2.0.3';
export const checkUpdates = async function() {
	// 是否强制更新
	let isForced = null;
	let downlondUrl = null;
	await getVersonInfo().then(res => {
		let system = res.result.app_system;
		downlondUrl = system.download_url;
		// 对本地版本号进行截取,获取整数值;
		let developVersion = Number(version.replace('.', "").replace('.', ""));
		let ProductionVersion = Number(system.version_number.replace('.', "").replace('.', ""));
		// 是否强制更新
		if (developVersion <  ProductionVersion) {
			// 更新
			isForced = system.forced_update;
		}
	})
	return new Promise((resolve) =>{
		resolve({
			isForced,
			downlondUrl
		})
	})
}

 引入上面封装的方法并调用 isForced用于展示不同的升级提示框 (需求有强制更新和非强制更新)

import {
	checkUpdates
	} from 'util/update.js'
export default {
	name: "update-verson",
	data() {
	    return {
			isForced: null,
			renewal: false,
			downlondUrl: '',
			};
		},
	created() {
	   this.checkUpdates();
	},
	methods: {
		checkUpdates() {
			checkUpdates().then(res => {
				this.isForced = res.isForced;
				this.downlondUrl = res.downlondUrl;
				// 互斥锁
				this.renewal = uni.getStorageSync('isForcedNum') ?         
                uni.getStorageSync('isForcedNum') : false;
				uni.removeStorageSync('isForcedNum')
		    })
},

 

<view>
		<!-- 普通更新 -->
		<view v-if="isForced == 0">
			<u-modal :show="renewal" title="版本更新" cancelText="取消升级" confirmText="立即升级" :showCancelButton="true"
				@cancel="cancel" @confirm="confirm">
				<view class="slot-content">
					<span>版本更新</span>
				</view>
			</u-modal>
		</view>
		<!-- 强制更新 -->
		<view v-if="isForced == 1">
			<u-modal :show="renewal" title="版本更新" confirmText="立即升级" @confirm="confirm">
				<view class="slot-content">
					<span>版本更新</span>
				</view>
			</u-modal>
		</view>
	</view>

点击立即升级 confirm

confirm() {
				var dtask = plus.downloader.createDownload(this.downlondUrl, {}, function(d, status) {
					// 下载完成  
					if (status == 200) {
						plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, {}, function(
							error) {
							uni.showToast({
								title: '安装失败',
								mask: false,
								duration: 1500
							});
						})
					} else {
						uni.showToast({
							title: '更新失败',
							mask: false,
							duration: 1500
						});
					}
				});
				try {
					dtask.start(); // 开启下载的任务
					var prg = 0;
					var showLoading = plus.nativeUI.showWaiting(
						"正在下载"); //创建一个showWaiting对象 
					dtask.addEventListener('statechanged', function(
						task,
						status
					) {
						console.log(task, status);
						// 给下载任务设置一个监听 并根据状态  做操作
						switch (task.state) {
							case 1:
								showLoading.setTitle("正在下载");
								break;
							case 2:
								showLoading.setTitle("已连接到服务器");
								break;
							case 3:
								prg = parseInt(
									(parseFloat(task.downloadedSize) /
										parseFloat(task.totalSize)) *
									100
								);
								showLoading.setTitle("  正在下载" + prg + "%  ");
								break;
							case 4:
								plus.nativeUI.closeWaiting();
								//下载完成
								break;
						}
					});
				} catch (err) {
					plus.nativeUI.closeWaiting();
					uni.showToast({
						title: '更新失败-03',
						mask: false,
						duration: 1500
					});
				}
			}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值