cmd-progress.vue

<!-- 版本升级弹窗开始 -->
		<uni-popup ref="promotion" type="center">
			<view class="promotion">
				<view class="operates" v-if="showBtns==true">
					<text @click="cancel">取消</text>
					<text @click="sure">确认</text>
				</view>
				<view class="operates" v-else>
					<cmd-progress :percent="percentage" stroke-color="linear-gradient(to right, #ef32d9, #89fffd)"></cmd-progress>
				</view>
			</view>
		</uni-popup>
		<!-- 版本升级弹窗结束 -->
<script>
import cmdProgress from "@/components/cmd-progress/cmd-progress.vue" //进度条
export default {
		components: {
			cmdProgress
		},
		data() {
			return {
				downloadUrl: "", //安卓app下载链接
				percentage: 0, //下载进度
				showBtns: true,
			}
		},
		onShow() {
			this.query(); //版本升级
		},
		watch: {
			//监听进度条
			percentage(e) {
				if (e >= 100) {
					this.$refs.promotion.close()
				}
			}
		},
		methods: {
			//查询当前版本号
			query() {
				//#ifdef APP-PLUS
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					var version = wgtinfo.version //客户端版本号
					this.check(version) //检测是否需要更新
				})
				//#endif
			},
			//检测是否需要更新
			async check(version) {
				let res = await edition.edition();
				let resp = res.data;
				if (resp) {
					const obj = resp.data.filter(item => {
						return item.groupName == 'version'
					})
					//获取当前版本号
					const versionNum = obj[0].title //服务端版本号
					this.downloadUrl = obj[0].content //app下载链接
					if (version != versionNum) {
						this.$refs.promotion.open();
					} else {
						console.log('当前已是最新版本')
					}
				}
			},
			//确认更新
			sure() {
				//关闭按钮
				this.showBtns = false;
				let sys = uni.getSystemInfoSync().platform //检查系统
				if (sys == 'ios') {
					this.getIosInfo()
				} else if (sys == "android") {
					this.updateAPP() // 调用下载方法app内下载
				}
			},
			//取消更新
			cancel() {
				this.$refs.promotion.close();
			},
			//链接苹果商店下载
			getIosInfo() {
				let appleId = “你的appid”
				plus.runtime.launchApplication({
					action: "itms-apps://itunes.apple.com/cn/app/id" + appleId + "?mt=8"
				}, function(e) {
					console.log('Open system default browser failed: ' + e.message);
				});
			},
			//安卓更新
			updateAPP() {
				let _this = this;
				let url = this.downloadUrl;
				// 官方API
				var dtask = plus.downloader.createDownload(url, {}, function(d, status) {


					//d为下载的文件对象
					if (status == 200) {
						//下载成功,d.filename是文件在保存在本地的相对路径,使用下面的API可转为平台绝对路径
						var fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
						plus.runtime.openFile(d.filename); //选择软件打开文件
						uni.showToast({
							icon: 'none',
							title: '更新成功'
						})
					} else {
						//下载失败
						plus.downloader.clear(); //清除下载任务
						uni.showToast({
							icon: 'none',
							title: '更新失败'
						})
					}
				})
				//开始下载
				dtask.start();
				//监听下载进度
				dtask.addEventListener('statechanged', function(task) {
					_this.percentage = parseInt(
						(parseFloat(task.downloadedSize) /
							parseFloat(task.totalSize)) *
						100
					);
					if (_this.percentage == 100) {
						this.$refs.promotion.close();
					}
					// console.log('下载进度:' + _this.percentage)
				})
			},
		}
</script>
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐