<script>
	import _config from './common/config.js';
	import _app from './common/app_info.js';
	import $http from './common/http.js'
	import {
		MatchVersion
	} from './utils/utils.js'
	import MyRequest from 'common/MyRequest.js'
	export default {
		// 在App.vue或者其他合适的地方调用
		onLaunch: function() {
			// this.checkUpdate();
			// this.handlerLaunchMsg();
			console.log("通知应用启动---");
		},
		methods: {
			openAppPages(data) {
				if (data?.payload?.pages) {
					const {
						type,
						pages
					} = data.payload;
					console.log(type, pages)
					uni.navigateTo({
						url: pages,
					});
				} else {
					console.log("页面数据不存在");
				}
			},
			// 处理Launch信息
			handlerLaunchMsg() {
				const platform = uni.getSystemInfoSync().platform;
				switch (platform) {
					case "ios":
						uni.onPushMessage((res) => {
							if (res?.data?.payload?.syczuanNotice) {
								const dataJson = res.data.payload.syczuanNotice;
								this.openAppPages(dataJson);
							}
						});
						break;
					case "android":
						// app未在后台时
						this.getArgumentsMsg();
						// app在后台时
						plus.globalEvent.addEventListener("newintent", (e) => {
							this.getArgumentsMsg();
						});
						break;
					default:
						break;
				}
			},
			getArgumentsMsg() {
				const args = plus.runtime.arguments;
				if (args) {
					try {
						const data = JSON.parse(args);
						// 在此处理点击通知后的操作
						if (data.syczuanNotice) {
							const dataJson = JSON.parse(data.syczuanNotice);
							this.openAppPages(dataJson);
						}
						// 在此处理点击自定义按钮后的操作,如清除通知
						else if (data.syczuanNoticeButton) {
							const dataButtonJson = JSON.parse(data.syczuanNoticeButton);
						}
					} catch (error) {}
				}
			},
			checkUpdate() {
				const that = this;
				this.$MyRequest('/pdaDownload/getVersion', {
					vision: 1
				}).then(res => {
					console.log("运行版本" + plus.runtime.version)
					const version = res.data.resultObj.PDA_VERSION;
					console.log("服务器版本" + res.data.resultObj.PDA_VERSION)
					const downloadUrl = res.data.resultObj.PDA_DOWNLOADURL;
					const currentVersion = plus.runtime.version;
					if (version > currentVersion) {
						uni.showModal({
							title: '发现新版本',
							content: '是否更新应用？',
							success: function(res) {
								if (res.confirm) {
									const downObj = plus.downloader.createDownload(downloadUrl, {
										method: 'GET'
									}, function(d, status) {
										if (status == 200) {
											plus.runtime.install(d.filename, {}, function() {
												plus.runtime.restart();
											});
										} else {
											uni.showToast({
												title: '下载失败，请重试',
												icon: 'none'
											});
										}
									});
									downObj.start();
								}

							}
						});

					}
				}).catch(err => {
					this.$showMessage(err)
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "uview-ui/index.scss";
	@import "./style/main.css";
	@import "./style/icon.css";
</style>