<!--

App.vue本身不是页面，这里不能编写视图元素，也就是没有<template>
-->
<script setup>
	// 更新 1111
	import {
		ref,
		reactive, provide,onMounted
	} from 'vue';
	import appConfig from '@/config/appConfig.js';
	import {
		onLaunch,onHide
	} from '@dcloudio/uni-app';
	import WebsocketUtil  from '@/http/websock.js'
	const websocketUtil = new WebsocketUtil(appConfig.wss, 5000); // 创建 WebSocket 工具类实例  
	provide('websocketUtil', websocketUtil); // 提供给所有子组件
	
	
	import { getFindBySource } from '@/http/yskApi/version.js'
	onHide(()=>{
		
	})
	onLaunch(() => {
		uni.hideTabBar()
		//#ifdef APP-PLUS
		//获取当前系统版本信息 
		plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
			//请求后台接口 解析数据 对比版本
			console.log("widgetInfo==",widgetInfo)
			getFindBySource({source:'点餐宝'}).then(res => {
				if (res.url && widgetInfo.version < res.version) {
					let downloadLink = res.url;
					console.log(downloadLink)
					let ready = false; 
					// 校验是否强制升级
					if (res.isUp == 1) {
						uni.showModal({
							showCancel: false,
							title: '发现新版本',
							confirmText: '立即更新',
							content: res.message,
							success: res => {
								if (res.confirm) {
									uni.showLoading({title:'下载中...'});
									if (uni.getSystemInfoSync().platform ==
										'android') {
											uni.hideLoading()
										uni.downloadFile({
											url: downloadLink,
											success: downloadResult => {
												uni.hideLoading()
												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({title:'下载中...'});
									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) {
									uni.hideLoading()
									console.log('取消');
								}
							}
						});
					}
				}
			}).catch((res)=>{
				console.log(res)
			})
		});
		
		// #endif
		
	});
</script>
<style lang="scss">
	/** 每个页面公共css */
	@import '@/commons/style/common.scss';
	@import '@/commons/style/ipad.scss';

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

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