<script>
	export default {
		data() {
			return {}
		},
		onLoad() {},
		onShow() {},
		onLaunch() {
			const that = this;
			// that.checkVersion()
		},
		methods: {
			// 检查版本
			checkVersion() {
				const that = this;
				plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => {
					uni.request({
						url: `${that.$config.serverUrl}/files/project/appVersion.json`,
						method: 'GET',
						success: (res) => {
							if (wgtinfo.version === res.data.version) return;
							uni.showModal({
								title: '更新',
								content: "有新版本,是否下载?",
								success: (oneres) => {
									if (oneres.confirm) {
										// 下载app
										this.toUpdateAPK(res.data.url);
									}
								}
							})
						}
					})
				})
			},
			toUpdateAPK(url) {
				if (!url) return;
				uni.showLoading({
					title: '更新中……',
					mask: true,
				})
				uni.downloadFile({
					url: url,
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode == 200) {
							uni.showModal({
								content: '更新成功，确定现在重启吗？',
								confirmText: '重启',
								confirmColor: '#EE8F57',
								success: modalRes => {
									if (modalRes.confirm) {
										plus.runtime.install(
											res.tempFilePath, {
												force: true
											},
											function(res) {
												plus.runtime.restart();
											})
									}
								}
							})
						}
					}
				})
			}
		},
	}
</script>

<style>
	@import url('@/uni.scss');
</style>
