<script>
	import Vue from 'vue'
	import {
		updateMini
	} from '@/util/sqma.js'
	import {
		isWechat
	} from '@/util/squ.js'
	import { login, LoginAlready, LoginSuccess, LoginUnBind, LoginRedirect } from '@/util/login.js'
	import squni from '@/util/squni.js'
	import config from '@/common/config.js'
	export default {
		async onLaunch(e) {
			this.vueStylePrototype()

			// #ifdef MP-WEIXIN
			this.$store.commit('setPlatform', 'WxMa')
			// 微信小程序打开场景
			this.$store.commit('setScene', e.scene)
			// 微信小程序更新检查
			updateMini();
			// #endif
			
			// #ifdef H5
			this.$store.commit('setPlatform', isWechat() ? 'WxMp' : 'H5')
			// #endif
		},
		async onShow() {
			// 如果写在onLaunch里面，用户重新进入小程序Token可能过期，需要做Token刷新逻辑
			await this.checkReady()
			
			// 保证onLaunch执行完后，再执行页面级别的onShow/onLoad(两个需要分别进行await this.$ready)
			this.$emitReady()
		},
		onHide() {
			
		},
		methods: {
			async checkReady() {
				if (config.jumpDefaultBrowser && isWechat()) {
					squni.routeWithParams('/pages/main/jump-default-browser')
					return
				}
				
				// 登录
				await login().then(async res => {
					if (res === LoginAlready || res === LoginSuccess) {
						await this.$store.dispatch('GetUserInfo')
					} else if (res === LoginUnBind) {
						await this.$store.dispatch('GetUserInfo')
					} else if (res === LoginRedirect) {
						// do nothing...
					} else {
						squni.toast((res && res.data && res.data.message) || '登录失败')
					}
				})
			},
			vueStylePrototype() {
				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

						// ====== 可用窗口高度 =====//
						// #ifdef MP-WEIXIN
						Vue.prototype.AvailableHeight = e.windowHeight + e.statusBarHeight;
						// #endif
						// #ifdef H5
						Vue.prototype.AvailableHeight = e.windowHeight + e.statusBarHeight - Vue.prototype
							.CustomBar;
						// #endif
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "@/uni_modules/colorui/main.css";
	@import "@/uni_modules/colorui/icon.css";
	// @import "@/uni_modules/uview-ui/index.scss";
	@import "common/common.scss";
</style>
