<!--

App.vue本身不是页面，这里不能编写视图元素，也就是没有<template>
-->
<script setup>
	import {
		onLaunch
	} from '@dcloudio/uni-app';
	import { getFindBySource } from '@/http/yskApi/version.js'
	onLaunch(() => {
		let that = this 
		uni.hideTabBar()
		// #ifdef MP-WEIXIN
		const updateManager = wx.getUpdateManager() // 小程序版本更新管理器
		updateManager.onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调
		})
		updateManager.onUpdateReady(function() {
			// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			updateManager.applyUpdate()

		})
		updateManager.onUpdateFailed(function() {
			// 新版本下载失败
		})
		// #endif
		
		
		
		//#ifdef APP-PLUS
		//获取当前系统版本信息 
		plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
			//请求后台接口 解析数据 对比版本
			console.log("widgetInfo==",widgetInfo)
			getFindBySource({source:'APP'}).then(res => {
				console.log("selectNewApp==",res)   
				console.log("version===",res.url && widgetInfo.version < res.version)
				if (res.url && widgetInfo.version < res.version) {
					console.log("version===222")
					let downloadLink = res.url;
					// let downloadLink = "https://short-video.hnsiyao.cn/app/sy-duanju.apk";
					console.log(downloadLink)
					// let androidLink = res.androidWgtUrl;
					// let iosLink = res.iosWgtUrl;
					let ready = false; 
					// 校验是否强制升级
					if (res.isUp == 1) {
						uni.showModal({
							showCancel: false,
							title: '发现新版本',
							confirmText: '立即更新',
							content: res.message,
							success: res => {
								if (res.confirm) {
									uni.showLoading('下载中...');
									if (uni.getSystemInfoSync().platform ==
										'android') {
										uni.downloadFile({
											url: downloadLink,
											success: downloadResult => {
												if (downloadResult.statusCode === 200) { 
													plus.io.resolveLocalFileSystemURL(downloadResult.tempFilePath, entry => {
														entry.getParent(_oldFile=>{
															entry.moveTo(_oldFile,'.apk',newFilePath=>{
																console.log('newFilePath',newFilePath.fullPath)
																plus.runtime
																	.install(newFilePath.fullPath, { force: false },
																		d => {
																			console
																				.log(
																					'install success...'
																				);
																			plus.runtime
																				.restart();
																		},
																		e => {
																			console.log(e)
																			console
																				.error(
																					'install fail...'
																				);
																		}
																	);
															})
														}) 
													})
												}
											}
										});
									}
									if (uni.getSystemInfoSync().platform ==
										'ios') {
										plus.runtime.openURL(downloadLink, function(
											res) {});
									}
								} else if (res.cancel) {
									console.log('取消');
								}
							}
						});
					} else { 
						uni.showModal({
							title: '发现新版本',
							confirmText: '立即更新',
							cancelText: '下次更新',
							content: res.message, 
							success: res => {
								if (res.confirm) {
									uni.showLoading('下载中...');
									if (uni.getSystemInfoSync().platform ==
										'android') {
										uni.downloadFile({
											url: downloadLink,
											success: downloadResult => {
												if (downloadResult
													.statusCode ===
													200) {
													plus.io.resolveLocalFileSystemURL(downloadResult.tempFilePath, entry => {
														entry.getParent(_oldFile=>{
															entry.moveTo(_oldFile,'.apk',newFilePath=>{
																console.log('newFilePath',newFilePath.fullPath)
																plus.runtime
																	.install(newFilePath.fullPath, { force: false },
																		d => {
																			console
																				.log(
																					'install success...'
																				);
																			plus.runtime
																				.restart();
																		},
																		e => {
																			console.log(e)
																			console
																				.error(
																					'install fail...'
																				);
																		}
																	);
															})
														}) 
													})
													
												}
											}
										});
									}
									if (uni.getSystemInfoSync().platform ==
										'ios') {
										plus.runtime.openURL(downloadLink, function(
											res) {});
									}
								} else if (res.cancel) {
									console.log('取消');
								}
							}
						});
					}
				}
			}).catch((res)=>{
				console.log(res)
			})
		});
		
		// #endif
		
	});
</script>

<style lang="scss">
	/** 每个页面公共css */
	@import '@/commons/style/global.scss';
	@import '@/commons/style/common.scss';

	/**  uni 组件样式覆盖 */
	@import '@/commons/style/uni-overwrite.scss';

	@import "uview-plus/index.scss";
</style>