<script>

export default {
	data() {
		return {
			// 上一次网络状态
			lastNetworkStatus: null
		}
	},
	methods: {
		// 判断是否是第一次安装APP并且打开
		isFirstOpen() {
			let isFirstOpen = uni.getStorageSync('isFirstOpen')
			if (!isFirstOpen) {
				uni.setStorageSync('isFirstOpen', true)
				return true
			}
			return false
		},
		networkStatusChange(res) {
			console.log(res)
			if (res.isConnected) {
				// 如果是第一次安装进入,并且网络状态为有网络,则跳转到主页
				if (this.isFirstOpen()) {
					uni.redirectTo({
						url: '/pages/index/index'
					})
				}
				// 如果上一次网络状态为无网络,且当前网络状态为有网络,则跳转到首页
				if (this.lastNetworkStatus === false) {
					uni.showModal({
						title: "提示",
						content: "当前设备网络发生更改,是否刷新页面?",
						showCancel: true,
						success: function (res) {
							if (res.confirm || res.cancel) {
								uni.redirectTo({
									url: '/pages/index/index'
								})
							}
						},
					});
				}

			} else {
				uni.redirectTo({
					url: '/pages/networko/index'
				})
			}
			this.lastNetworkStatus = res.isConnected

		}
	},
	onLaunch: function () {
		console.log('onLaunch')

	},
	onShow: function () {
		uni.onNetworkStatusChange(this.networkStatusChange);
		uni.getNetworkType({
			success: (res) => {
				if (res.networkType === 'none') {
					uni.redirectTo({
						url: '/pages/networko/index'
					})

				}
			}
		})

	},
	onHide: function () {
		uni.offNetworkStatusChange(this.networkStatusChange)
	}
}
</script>

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