<script>
	import Vue from 'vue'
	import accountApi from '@/api/account.js'
	import oilSiteApi from '@/api/oil-site.js'
	let loginTimer = null
	let onlineTimer = null

	export default {
		data() {
			return {
				loginStatus: 'online',
				msg: ''
			}
		},
		onLaunch() {
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		        // #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
			this.onShareAppMessage()

		},

		methods: {
			getFilterData() {
				oilSiteApi.getCheckInfo().then(res => {
					if (res.code == 20000) {
						let channelCodes = []
						res.data.channelCodes.map(item => {
							channelCodes.push({
								title: item.name,
								value: item.id
							})
						})
						let productCodes = []
						res.data.productCodes.map(item => {
							productCodes.push({
								title: item.name,
								value: item.id
							})
						})
						let siteBrands = []
						res.data.siteBrands.map(item => {
							siteBrands.push({
								title: item.name,
								value: item.id
							})
						})
						// console.log(channelCodes, productCodes, siteBrands)
						this.menuList[0].detailList = this.menuList[0].detailList.concat(channelCodes)
						this.menuList[1].detailList = this.menuList[1].detailList.concat(productCodes)
						this.menuList[2].detailList = this.menuList[2].detailList.concat(siteBrands)
						uni.setStorageSync('filterData', this.menuList)
						// this.$refs.slFilter.resetMenuList(this.menuList)
					}
				})
			},
			onShareAppMessage() {
				wx.onAppRoute(() => {
					let white = ['BagStation/pages/stationDetail/stationDetail']
					// console.log('当前页面路由发生变化 触发该事件onShareAppMessage')
					const pages = getCurrentPages() //获取加载的页面
					const view = pages[pages.length - 1] //获取当前页面的对象
					if (!view) return false //如果不存在页面对象 则返回
					// 若想给个别页面做特殊处理 可以给特殊页面加isOverShare为true 就不会重写了
					// const data = view.data
					// if (!data.isOverShare) {
					// data.isOverShare = true
					view.onShareAppMessage = () => { //重写分享配置
						return {
							title: '清正源油加油',
							path: "/pages/index/startPage/startPage", //若无path 默认跳转分享页
							// imageUrl:'/image/onshowMessage.png' //若无imageUrl 截图当前页面
							imageUrl: 'https://publicxingyou.oss-cn-hangzhou.aliyuncs.com/mp-oil/login-bg.png',
						}
					}
					// console.log(view.route)
					// clearTimeout(loginTimer)
					// clearTimeout(onlineTimer)

					if (view.route != 'pages/index/startPage/startPage' && view.route.indexOf('BagAuth') == -1) {
						const token = uni.getStorageSync('Authorization')
						const openid = uni.getStorageSync('openid')

						let jumpUrl = '/BagAuth/pages/login/login?loginType=true'
						let urlTarget = this.obtainUrlPathParameterTarget(view.$page.fullPath)
						// console.log('urlTarget', urlTarget)
						let target = {}
						if (urlTarget.q) {
							try {
								let originLink = decodeURIComponent(decodeURIComponent(urlTarget.q))
								target = this.obtainUrlPathParameterTarget(originLink)
								target.siteId && (jumpUrl += `&scanningParams=${decodeURIComponent(urlTarget.q)}`)
							} catch (e) {}
						}

						if (token && openid) {
							this.getAmount(jumpUrl)
						} else {
							target.siteId && uni.redirectTo({
								url: jumpUrl
							})
						}
					}
				})
			},

			obtainUrlPathParameterTarget(url) {
				let target = {}
				if (url.includes('?')) {
					let paramsText = url.split("?")[1]
					let paramsArr = paramsText.split("&")
					paramsArr.forEach(item => {
						target[item.split('=')[0]] = item.split('=')[1]
					})
				}
				// console.log('11target', target)
				return target
			},

			// 是否token过期或ip更改
			getAmount(jumpUrl) {
				accountApi.getTotalOilCardInfo().then(res => {
					if (res.code == 42011) {
						uni.clearStorageSync()
						uni.showModal({
							title: '需要您重新登录',
							content: res.msg,
							duration: '3000',
							success: (res) => {
								if (res.confirm) {
									uni.redirectTo({
										url: jumpUrl
									})
								}
							}
						})
					}
				})
			},
		},

		// onShow: function() {
		// 	const token = uni.getStorageSync('Authorization')
		// 	const openid = uni.getStorageSync('openid')

		// 	if (token && openid) {
		// 		this.getAmount()
		// 	}
		// },
	}
</script>

<style>
	/*每个页面公共css */
	@import "colorui/main.css";
	@import "colorui/icon.css";
	@import "colorui/animation.css";
	@import "common/common.css";
</style>
