update.vue 4.6 KB
<template>
	<u-popup :show="show" bgColor="transparent" mode="center" :safeAreaInsetBottom="false" borderRadius="20">
		<view class="new-update">
			<view class="new-update-1">
				<image class="new-update-1-i" src="@/static/imagesV2/icon70.png" mode="widthFix"></image>
				<view class="new-update-1-c">
					<view class="new-update-1-c-1">
						发现新版本
					</view>
				</view>
			</view>
			<view class="new-update-2">
				<view class="new-update-2-1">新版本更新内容</view>
				<scroll-view :scroll-y="true" style="height: 300rpx;">
					<view style="font-size: 28rpx !important;">
						<view v-html="content"></view>
					</view>
				</scroll-view>
				<view class="immediate"  @click="update" v-if="!is_load">立即升级</view>
				<view class="progress" v-else>
					<u-line-progress :percentage="progressOf" activeColor="#1b60b2"></u-line-progress>
					<view class="progress-t">安装包下载中,请稍后({{ size.schedule }}M/{{ size.total }}M)</view>
				</view>
			</view>
		</view>
	</u-popup>
</template>

<script>
	export default {
		data() {
			return {
				show: true,
				content: '更新内容',
				is_load: false,
				progressOf: 0,
				urlObj: {
					ios: '',
					apk: ''
				},
				size: {
					total: 0,
					schedule: 0
				}
			}
		},
		onBackPress(e) {
			// 这里可以自定义返回逻辑
			return true   // return true 表示禁止默认返回
		},
		onLoad() {
			this.getData()
		},
		methods: {
			getData() {
				let version = plus.runtime.version
				this.$service.P_post('/version/index', {
					client: uni.getSystemInfoSync().platform,
					version: version
				}).then(res => {
					this.content = res.msg.content
					if(plus.os.name.toLowerCase() == 'ios') {
						this.urlObj.ios = res.msg.url
					} else {
						this.urlObj.apk = res.msg.url
					}
				})
			},
			update() {
				let that = this
				if (plus.os.name.toLowerCase() == 'ios') {
					plus.runtime.openURL(this.urlObj.ios)
				} else {
					this.is_load = true
					var dtask = this.getCreateDownload()
					try {
						dtask.start();
						var prg = 0;
						dtask.addEventListener('statechanged', (task, status) => {
							switch (task.state) {
								case 3:
									that.progressOf = parseInt(
										(parseFloat(task.downloadedSize) /
											parseFloat(task.totalSize)) *
										100
									);
									that.size.total = (task.totalSize / (1024 * 1024)).toFixed(2)
									that.size.schedule = (task.downloadedSize / (1024 * 1024)).toFixed(2)
									break;
								case 4:
									that.is_load = false
									//下载完成
									break;
							}
						})
					} catch (err) {
						console.log('err', err)
						// plus.nativeUI.closeWaiting();
						that.is_load = false
						uni.showToast({
							title: '更新失败-03',
							mask: false,
							duration: 1500
						});
					}
				}
			},
			// 获取下载实例
			getCreateDownload() {
				return plus.downloader.createDownload(this.urlObj.apk, {},
						function(d, status) {
							// 下载完成
							if (status == 200) {
								plus.runtime.install(plus.io.convertLocalFileSystemURL(d.filename), {}, e => e,
									function(error) {
										uni.showToast({
											title: '安装失败-01',
											mask: false,
											duration: 1500
										});
									})
							} else {
								uni.showToast({
									title: '更新失败-02',
									mask: false,
									duration: 1500
								});
							}
					});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.new-update{
		width: 611rpx;
		.new-update-1{
			position: relative;
			height: 260rpx;
			.new-update-1-i{
				width: 100%;
				position: absolute;
			}
			.new-update-1-c{
				padding-top: 50rpx;
				position: relative;
				display: flex;
				flex-direction: column;
				justify-content: center;
				height: 100%;
				box-sizing: border-box;
				padding-left: 54rpx;
				.new-update-1-c-1{
					font-size: 36rpx;
					font-weight: 800;
					color: #FFFFFF;
					margin-bottom: 10rpx;
				}
				.new-update-1-c-2{
					font-size: 26rpx;
					color: #fff;
				}
			}
		}
		.new-update-2{
			background: #fff;
			border-radius:0 0 40rpx 40rpx;
			padding: 50rpx;
			padding-top: 20rpx;
			box-sizing: border-box;
			.new-update-2-1{
				margin-bottom: 20rpx;
				color: #1b60b2;
				font-weight: bold;
			}
			.immediate{
				line-height: 90rpx;
				background: linear-gradient(0deg, #098ad5 0%, #1b60b2 100%);
				color: #fff;
				margin: 60rpx 0 0 0;
				border-radius: 500rpx;
				text-align: center;
			}
			.progress{
				height: 80rpx;
				margin: 60rpx 0 0 0;
				.progress-t{
					font-size: 26rpx;
					color: #323232;
					margin-top: 15rpx;
					text-align: center;
				}
			}
		}
	}
</style>