<script>
export default {
	onLaunch: function(option) {
		// #ifdef APP-PLUS
		uni.getSystemInfo({
			success: res => {
				let fontsize = ((res.windowWidth * 16) / 1200) * 5.5 + 'px';
				this.$store.commit('setRootSize', fontsize);
			}
		});
		// #endif
		this.$store.dispatch('getAddonIsExit');
		this.$store.dispatch('getDefautlImg');
		if (uni.getStorageSync('cashier_token')) {
			this.$store.dispatch('getStoreInfo');
			this.$store.dispatch('getUserInfo');
			this.$store.dispatch('getUserGroup');
		}
		this.$store.commit('setCurrRoute', '/' + option.path);

		// #ifdef APP-PLUS
		plus.webview.open(this.$config.baseUrl + '/cashier/pages/index/loading', 'loading');
		uni.switchTab({
			url: '/pages/reserve/index',
			success: () => {
				uni.switchTab({
					url: '/pages/recharge/index',
					success: () => {
						uni.switchTab({
							url: '/pages/verify/index',
							success: () => {
								if (!uni.getStorageSync('cashier_token')) {
									uni.navigateTo({ url: '/pages/login/login' });
								} else {
									uni.switchTab({ url: '/pages/billing/index' });
								}
								plus.webview.close('loading');
							}
						});
					}
				});
			}
		});
		// #endif
	},
	onShow: function(option) {
		if (!uni.getStorageSync('cashier_token')) {
			// #ifdef H5
			if (location.href.indexOf('pages/index/loading') == -1) {
				this.$util.redirectTo('/pages/login/login', {}, 'redirectTo');
			}
			// #endif
			// #ifndef H5
			this.$util.redirectTo('/pages/login/login', {}, 'redirectTo');
			// #endif
		}
	},
	onHide: function() {},
	methods: {
		initRoute(route) {
			const search = function(menu, route, arr = []) {
				menu.find((item, index) => {
					if (item.path == route) {
						arr.push(index);
						return true;
					} else if (item.children) {
						arr = search(item.children, route, arr);
						if (arr.length) {
							arr.push(index);
							return true;
						} else {
							return false;
						}
					}
					return false;
				});
				return arr;
			};

			let menuIndex = search(this.menu, route).reverse();
			this.$store.commit('setMemuIndex', { level: 'first', index: menuIndex[0] });
			this.$store.commit('setMemuIndex', { level: 'second', index: menuIndex[1] ?? -1 });
			this.$store.commit('setMemuIndex', { level: 'third', index: menuIndex[2] ?? -1 });
		},
		/**
		 * 检测页面是否有权限
		 */
		checkPageAuth() {
			this.$api.sendRequest({
				url: '/cashier/storeapi/store/checkpageauth',
				data: {
					page: this.currRoute
				},
				success: res => {
					if (res.code && res.code == -10012) {
						this.$util.redirectTo('/pages/index/no_permission', {}, 'redirectTo');
					}
				}
			});
		}
	},
	computed: {
		currRoute() {
			return this.$store.state.currRoute;
		},
		menu() {
			let menu = require('@/common/menu/store.js');
			return menu.default ?? [];
		}
	},
	watch: {
		currRoute: function(nVal, oVal) {
			if (nVal) {
				this.initRoute(nVal);
				this.checkPageAuth();
			}
		},
		menu: function(nVal) {
			if (nVal.length) {
				this.initRoute(this.currRoute);
			}
		}
	}
};
</script>

<style lang="scss">
/*每个页面公共css */
@import url('/common/css/iconfont.css');
@import '/common/css/common.scss';
@import '/common/css/form.scss';
uni-toast .uni-toast__content {
	font-size: 0.16rem !important;
}
</style>
