<script>
import { config } from '@/common/config.js';
export default {
	data() {
		return {
			downloadUrl: ''
		};
	},
	onLaunch: function() {
		console.log('App Launch');
	},
	onShow: function() {
		console.log('App Show');
		this.getDownloadUrl();
	},
	methods: {
		/**
		 * 通过参数设置获取最新版本信息
		 */
		getVersion() {
			const _this = this;
			uni.$u.api
				.getParameterApi({
					key: 'parking.operation.version'
				})
				.then(res => {
					if (res.code === 200) {
						if (res.msg !== config.version) {
							uni.getNetworkType({
								success: function(res) {
									if (res.networkType === 'wifi') {
										uni.showModal({
											title: '版本发布更新',
											content: '发现新版本且您使用的是无限网络,是否进行更新?',
											confirmText: '更新',
											confirmColor: '#EE8F57',
											success: res => {
												if (res.confirm) {
													// _this.downloadAppFile()
													_this.downloadAppFileByBrowser();
												}
											}
										});
									} else {
										uni.showModal({
											title: '版本发布更新',
											content: '发现新版本,是否继续消耗流量进行更新?',
											confirmText: '更新',
											confirmColor: '#EE8F57',
											success: res => {
												if (res.confirm) {
													// _this.downloadAppFile()
													_this.downloadAppFileByBrowser();
												}
											}
										});
									}
								}
							});
						}
					}
				});
		},
		/**
		 * 通过参数设置获取最新下载链接
		 */
		getDownloadUrl() {
			uni.$u.api
				.getParameterApi({
					key: 'parking.operation.package'
				})
				.then(res => {
					if (res.code === 200) {
						this.downloadUrl = res?.msg;
						this.getVersion();
					}
				});
		},
		/**
		 * 下载文件
		 */
		downloadAppFile() {
			const _this = this
			// uni.showLoading({
			// 	mask: true,
			// 	title: '下载中...'
			// })
			// let downloadTask = uni.downloadFile({ // 执行下载
			// 	url: _this.downloadUrl,
			// 	success: downloadResult => {
			// 		// 下载成功
			// 		if (downloadResult.statusCode === 200) {
			// 			uni.showModal({
			// 				title: '',
			// 				content: '下载成功,确定现在安装吗?',
			// 				confirmText: '安装',
			// 				confirmColor: '#EE8F57',
			// 				success: function(res) {
			// 					if (res.confirm) {
			// 						plus.runtime.install( // 安装
			// 							downloadResult.tempFilePath, {
			// 								force: true
			// 							},
			// 							function() {
			// 								plus.runtime.restart();
			// 								uni.navigateBack()
			// 							},
			// 							function(e) {
			// 								utils.showToast('安装失败');
			// 							}
			// 						);
			// 					}
			// 				}
			// 			});
			// 		}
			// 	},
			// 	complete: (res) => {
			// 		uni.hideLoading();
			// 	}
			// });
			// downloadTask.onProgressUpdate((res) => {
			// 	if (res.progress === 100) {
			// 		uni.hideLoading();
			// 	}
			// });
			// #ifdef APP-PLUS
			const dtask = plus.downloader.createDownload(_this.downloadUrl, { force: true }, function(d, status) {
				// 下载完成
				if (status == 200) {
					var path = d.filename; //文件安装路径
					plus.runtime.install(
						path,
						{},
						function() {
							plus.nativeUI.closeWaiting(); //关闭系统等待对话框
							if (name == 'wgt') {
								console.log('安装wgt文件成功!');
							} else {
								console.log('安装apk文件成功!');
							}
							plus.nativeUI.alert('安装成功!', function() {
								plus.runtime.restart();
							});
						},
						function(e) {
							plus.nativeUI.closeWaiting(); //关闭系统等待对话框
							if (name == 'wgt') {
								console.log('安装wgt文件失败[' + e.code + ']:' + e.message);
								plus.nativeUI.alert('安装wgt文件失败[' + e.code + ']:' + e.message);
							} else {
								console.log('安装apk文件失败[' + e.code + ']:' + e.message);
								plus.nativeUI.alert('安装apk文件失败[' + e.code + ']:' + e.message);
							}
						}
					);
				} else {
					alert('下载失败:' + status);
				}
			});
			// #endif
		},
		/**
		 * 下载app通过浏览器
		 */
		downloadAppFileByBrowser() {
			// #ifdef APP-PLUS
			plus.runtime.openURL(this.downloadUrl);
			// #endif
		}
	},
	onHide: function() {
		console.log('App Hide');
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
@import '@/uni_modules/uview-ui/index.scss';
</style>