<template>
	<view>
		<index v-if="curPageCode === 'indexPage'" @changeCurPageCode="changeCurPageCode"></index>
		<goods-manage v-else-if="curPageCode === 'supplyChainPage'"></goods-manage>
		<my-home v-else-if="curPageCode === 'myPage'"></my-home>
		<msg-home v-else-if="curPageCode === 'msgPage'"></msg-home>
		<module-bar ref="moduleBar" :moduleBarInfos="moduleBarInfos" @getCurPageInfo="getCurPageInfo"></module-bar>
		
		<!-- 首页引导 -->
		<vertify-worker-type ref="vertifyWorkerType"></vertify-worker-type>
		<!-- 登录校验弹窗 -->
		<vertify-login ref="vertifyLogin" @reload="reloadForwardPage"></vertify-login>
		<!-- <vertify-phone ref="vertifyPhone" @reload="reloadForwardPage"></vertify-phone> -->
	</view>
</template>

<script>
	import moduleBar from '@/components/custom-bar/module-bar.vue';
	import index from '@/pages/index/index-home.vue';
	import myHome from '@/pages/index/my-home.vue';
	import msgHome from '@/pages/index/msg-home.vue';
	import goodsManage from '@/pages/index/goods-manage.vue';

	export default {
		components: {
			moduleBar,
			index,
			myHome,
			msgHome,
			goodsManage
		},
		data() {
			return {
				moduleBarInfos: [],
				curPageCode: 'indexPage',
				forwardingPageCode: null
			}
		},
		onLoad(options) {
			this.loadData();
			if (options && options.menuCode) {
				this.changeCurPageCode(options.menuCode)
			}
		},
		methods: {
			async loadData() {
				// uni.getSystemInfo({
				// 	success: function(res) {
				// 		console.log('DPR：' + res.pixelRatio)
				// 		console.log('宽度：' + res.screenWidth)
				// 		console.log('高度：' + res.screenHeight)
				// 	}
				// })
				this.moduleBarInfos = await this.$api.data('moduleBarInfos');
			},
			reloadForwardPage() {
				this.getCurPageInfo({
					curPageCode: this.forwardingPageCode
				})
			},
			async getCurPageInfo(data) {
				let pageCode = data.curPageCode;
				this.forwardingPageCode = pageCode;
				// 登录校验
				if (['supplyChainPage', 'myPage'].indexOf(pageCode) >= 0) {
					let loginRes = await this.authLogin();
					if (!loginRes) {
						return;
					}
				}
				this.forwardingPageCode = null;
				
				if (pageCode === 'publishPage') {
					this.isShowPublish = true;
				} else {
					this.isShowPublish = false;
					this.curPageCode = pageCode;
				}
			},
			async authLogin() {
				// 更新缓存中的userInfo
				let res = await this.$request.storageExistUser();
				// 获取缓存中的userInfo
				let curUserInfo = this.$request.getCurUserInfo();
				// 校验提示登录
				if (!curUserInfo || !curUserInfo.openId) {
					this.$refs.vertifyLogin.showModal();
					return false;
				} else {
					this.$refs.vertifyLogin.hideModal();
				}
				// 校验是否注册过
				if (!curUserInfo.phone) {
					uni.reLaunch({
						url: '/pages/login/login?operType=1'
					})
					return false;
				}
				// 校验提示获取手机号码
				// if (!curUserInfo.phone) {
				// 	this.$refs.vertifyPhone.showModal();
				// 	return false;
				// } else {
				// 	this.$refs.vertifyPhone.hideModal();
				// }
				// 提示选择师傅类型
				if (curUserInfo.type == null) {
					this.$refs.vertifyWorkerType.showModal();
					return false;
				}
				return true;
			},
			changeCurPageCode(targetPageCode) {
				// #ifdef MP
				this.$nextTick(() => {
					this.$refs.moduleBar.navChangeByCode(targetPageCode, true);
				})
				// #endif
			}
		}
	}
</script>

<style scoped>
</style>
