<script>
	export default {
		globalData: {
			msg: {},
			verno:'',
			vername:'',
			version:''
		},
		onLaunch() {
			let that = this

			//获取平台信息
			uni.getSystemInfo({
				success(res) {
					uni.setStorageSync('platform', res.platform)
				}
			})
			// #ifdef APP-PLUS

			//获取版本号
			let v = plus.runtime.version
			let c = plus.runtime.versionCode
			let version = v + '(' + c + ')'
			that.globalData.version = version
			that.globalData.vername = v
			that.globalData.verno = c

			//获取设备uuid
			plus.device.getInfo({
				success(res) {
					let uuid = res.uuid
					that.globalData.clientid = uuid
				}
			})

			//锁定屏幕
			plus.screen.lockOrientation("portrait-primary")

			//引入插件
			const KJJPush = uni.requireNativePlugin('KJ-JPush');
			//设置应用角标，android只支持华为手机 或者使用 plus.runtime.setBadgeNumber(0);
			KJJPush.setApplicationIconBadgeNumber(0);

			let platform = uni.getStorageSync('platform')
			if (platform == 'ios') {
				//ios重置极光推送服务器的角标
				KJJPush.ios_resetJPushBadge();
			}

			//清除所有通知消息
			KJJPush.clearAllNotifications();
			//判断用户(应用设置界面)是否允许接收通知
			KJJPush.isNotificationEnabled(result => {
				var str = JSON.stringify(result);
				// '0'未开启通知  '1'已开启
				if (str == '0') {
					uni.showModal({
						title: '提示',
						content: '您还未开启通知,可能无法获取推送消息,是否现在去开启?',
						success: function(res) {
							if (res.confirm) {
								KJJPush.openSettingsForNotification();
							}
						}
					});
				}
			});

			let uid = uni.getStorageSync("userid")
			//设置Alias
			KJJPush.setAlias(uid ? uid : 'tourist', 1, result => {
				console.log("setAlias:" + JSON.stringify(result));
			});

			//监听推送打开通知，（前台、后台、ios app完全退出）都可以监听
			KJJPush.addNotifyMessageOpened(result => {
				console.log(result)
			});
			//  #endif

			//判断token是否过期
			let token = uni.getStorageSync("token")
			//let isFirstTimeEnterApp = uni.getStorageSync("isFirstTimeEnterApp")
			//console.log(token)
			//console.log("isFirstTimeEnterApp",isFirstTimeEnterApp)
			if (!token) {
				//if (isFirstTimeEnterApp) { //判断是否第一次进入App 先入引导页
				uni.reLaunch({
					url: "/pages/sub_basic/login"
				})
				//}
			} else {
				let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
				let curRoute = ''
				if (routes.length != 0) {
					curRoute = routes[routes.length - 1].route //获取当前页面路由
				}
				//console.log(routes)
				that.$u.post("/v1/infor", {}).then((res) => {
					if (res.token) {
						uni.setStorageSync("token", res.token)
					}
					if (curRoute != 'pages/sub_tabbar/index') {
						let hands = uni.getStorageSync("hands")
						if (hands) {
							uni.setStorageSync("login", 2)
							uni.reLaunch({
								url: "/pages/sub_mine/lock"
							})
						} else {
							uni.switchTab({
								url: "/pages/sub_tabbar/index"
							})
						}
					}

				}).catch(res => {
					uni.setStorageSync('token', '')
					if (routes.length != 0 || curRoute != 'pages/sub_basic/login') {
						uni.reLaunch({
							url: "/pages/sub_basic/login"
						})
					}

				})
			}

			uni.onNetworkStatusChange(function(res) {
				if (res.networkType == '4g') {
					uni.showToast({
						title: "您已切换至4g网络,请注意流量使用情况",
						icon: "none",
						duration: 1500
					})
				} else if (res.networkType == 'wifi') {
					uni.showToast({
						title: "您已切换至wifi网络",
						icon: "none",
						duration: 1500
					})
				} else
				if (!res.isConnected || res.networkType == '2g' || res.networkType == 'none') {
					let routes = getCurrentPages()
					let curRoute = routes[routes.length - 1].route
					if (curRoute == 'pages/sub_basic/login' || curRoute == 'pages/sub_basic/register' || curRoute ==
						'pages/sub_basic/forgetPwd') {
							uni.showToast({
								title:'暂无可用网络。请开启网络',
								icon:'none'
							})
					} else {
						uni.navigateTo({
							url: "/pages/sub_basic/network"
						})
					}
				}
			});
		},
		onShow() {

		}
	}
</script>

<style lang="scss">
	/* #ifndef APP-NVUE */
	@import "uview-ui/index.scss";
	@import "/static/css/iconfont.css";

	/*覆盖文本编辑器里面的图片的大小*/
	uni-rich-text {
		img {
			max-width: 100% !important;
		}
	}

	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	page {
		background-color: #F3F3F3;
	}

	/* #endif */



	/*每个页面公共css */
</style>
