<style lang="scss">
	/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
	@import "uview-ui/index.scss";
</style>
<script>
	import store from "@/store";
	import socket from "@/config/socket";
	import DB from "@/common/sqlite";
	// #ifdef APP-PLUS
	import checkUpdate from "@/uni_modules/uni-upgrade-center-app/utils/check-update"
	// #endif
	export default {
		onLaunch: function(e) {
			//取出缓存数据
			store.commit("setCacheData");
			if (store.state.token) {
				socket.init();
			} else {
				var url = window.location.href;
				if ((url.indexOf("pages/carInsure/quoteDetail1") > 0) || (url.indexOf("pages/carInsure/quoteDetail") >
						0) || (url.indexOf("pages/register/register") > 0) ||
					(url.indexOf("pages/register/registerSuccess") > 0) || (url.indexOf("pages/carInsure/payVerify") >
						0) || (url.indexOf("pages/login/mimi") > 0) || (url.indexOf("pages/login/xieyi") > 0) || (url
						.indexOf("pages/carInsure1/yonganCode") > 0)) {
					uni.navigateTo({
						url: url
					})
				} else {
					
				}
			}
			// #ifdef H5
			if (store.state.token) {
				socket.init();
			} else {
				var url = window.location.href;
				if ((url.indexOf("pages/carInsure/quoteDetail") > 0) || (url.indexOf("pages/register/register") > 0) ||
					(url.indexOf("pages/register/registerSuccess") > 0) || (url.indexOf("pages/carInsure/payVerify") >
						0) || (url.indexOf("pages/login/mimi") > 0) || (url.indexOf("pages/login/xieyi") > 0) || (url
						.indexOf("pages/carInsure1/yonganCode") > 0)) {
					uni.navigateTo({
						url: url
					})
				} else {
					
				}
			}
			// #endif
			// #ifdef APP-PLUS
			this.launch()
			if (store.state.token) {
				socket.init();
			} else {
				var url = window.location.href;
				if ((url.indexOf("pages/carInsure/quoteDetail1") > 0) || (url.indexOf("pages/carInsure/quoteDetail") >
						0) || (url.indexOf("pages/register/register") > 0) ||
					(url.indexOf("pages/register/registerSuccess") > 0) || (url.indexOf("pages/carInsure/payVerify") >
						0) || (url.indexOf("pages/login/mimi") > 0) || (url.indexOf("pages/login/xieyi") > 0) || (url
						.indexOf("pages/carInsure1/yonganCode") > 0)) {
					uni.navigateTo({
						url: url
					})
				} else {
					
				}
			}
			// #endif
			//#ifdef APP-PLUS
			var info = plus.push.getClientInfo()
			
			// 使用5+App的方式进行监听消息推送
			plus.push.addEventListener("click", function(msg) {
				console.log("click:" + JSON.stringify(msg));
				console.log(msg.payload);
				console.log(JSON.stringify(msg));
				// onLaunch 生命周期里，页面跳转有问题,跳不过去
				// 应该是页面还没加载，加上定时后，就可以了；
				setTimeout(() => {
					uni.navigateTo({
						url: `pages/charging/chargeCoupon?data=${JSON.parse(msg.payload)}`
					})
				}, 1000)
			}, false);
			// 监听在线消息事件    
			plus.push.addEventListener("receive", function(msg) {
				//业务代码
				console.log("recevice:" + JSON.stringify(msg))
			}, false);
			//#endif

		},
		onShow: function(e) {
			// #ifdef APP-PLUS
			DB.openSqlite()
				.then(res => {
				})
				.catch(error => {
				});
			// #endif

			// #ifdef MP-WEIXIN
			//获取二维码携带的参数
			let scene = decodeURIComponent(e.query.scene);
			scene = scene.split("&");
			let data = {
				//场景值
				scene: e.scene
			};
			scene.forEach(item => {
				let arr = item.split("=");
				if (arr.length == 2) {
					data[arr[0]] = arr[1];
				}
			});
			store.commit("setChatScenesInfo", Object.assign(e.query, data));
			//小程序更新
			if (uni.getUpdateManager) {
				const updateManager = uni.getUpdateManager();
				updateManager.onCheckForUpdate(function(res) {
					// 请求完新版本信息的回调
					// console.log(res.hasUpdate);
				});
				updateManager.onUpdateReady(function(res) {
					uni.showModal({
						title: "更新提示",
						content: "新版本已经准备好，是否重启应用？",
						success(res) {
							if (res.confirm) {
								// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
								updateManager.applyUpdate();
							}
						}
					});
				});
				updateManager.onUpdateFailed(function(res) {
					// 新的版本下载失败
					uni.showModal({
						title: "已经有新版本了哟~",
						content: "新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~",
						showCancel: false
					});
				});
			}
			// #endif
		},
		onHide: function() {},

		methods: {
			//  TODO 开屏广告 后续优化添加
			launch() {
				try {
					var date = new Date();
					var year = date.getFullYear();
					var month = date.getMonth() + 1;
					var strDate = date.getDate();
					var currentdate = year + '' + month + '' + strDate + '' + plus.runtime.version;

					// 获取本地存储中launchFlag标识(不可动)
					const value = uni.getStorageSync("launchFlag");
					if (value == currentdate) {
						checkUpdate();
					} else {
						//app启动时打开启动广告页
						var w = plus.webview.open(
							"/hybrid/html/advertise/advertise.html",
							"本地地址", {
								top: 0,
								bottom: 0,
								zindex: 999,
							},
							"fade-in",
							500
						);
						//设置定时器，4s后关闭启动
						setTimeout(function() {
							uni.setStorage({
								key: "launchFlag",
								data: currentdate
							});
							checkUpdate();
							plus.webview.close(w);
						}, 3000);
					}
				} catch (e) {
					// error
					uni.setStorage({
						key: "launchFlag",
						data: currentdate,
						success: function() {
							console.log("error时存储launchFlag");
						},
					});
				}
			},
		}
	};
</script>

<style>
	/* #ifndef APP-NVUE */
	@import "/style/uni.css";
	/* 官方ui库 */
	@import "/style/animate.min.css";
	/* 第三方动画库 */
	/* @import "/style/icon.css";	 */
	/* 自定义图标库 */
	@import 'http://at.alicdn.com/t/font_1807257_t3nqcuysmns.css';
	@import "/style/common.css";
	/* 公共样式库 */
	@import "/style/zcm-main.css";
	/* UI基础库 */
	/* @import "/style/input.scss"; */
	/* 基础表单样式*/
	/* #endif */


	/* #ifdef H5 */
	/* 修复H5底部导航挡住内容bug */
	uni-app {
		height: auto;
	}

	/* 修复H5输入框上下不居中bug */
	.uni-input-form {
		height: 100%;
	}

	/* 去除地图上高德地图标识符 */
	.amap-copyright {
		display: none !important;
	}

	.amap-logo {
		display: none !important;
	}

	.amap-ui-control-zoom {
		width: 60upx !important;
	}

	.amap-ui-control-zoom>* {
		width: 60upx !important;
		height: 60upx !important;
		line-height: 60upx !important;
	}

	.amap-ui-control-theme-dark {
		display: none !important;
	}

	/* #endif */
</style>
