// 路由
import {
	RouterMount,
	createRouter
} from './uni-simple-router.js'
import store from '@/shopro/store'
const router = createRouter({
	platform: process.env.VUE_APP_PLATFORM,
	applet: {
		animationDuration: 0 //默认 300ms 
	},
	routerErrorEach: ({
		type,
		msg
	}) => {
		switch (type) {
			case 3: // APP退出应用
				// #ifdef APP-PLUS
				router.$lockStatus = false;
				uni.showModal({
					title: '提示',
					content: '您确定要退出应用吗？',
					success: function(res) {
						if (res.confirm) {
							plus.runtime.quit();
						}
					}
				});
				// #endif
				break;
			case 2:
			case 0:
				router.$lockStatus = false;
				break;
			default:
				break;
		}

	},
	// 通配符，非定义页面，跳转404
	routes: [...ROUTES,
		{
			path: '*',
			redirect: (to) => {
				return {
					name: '404'
				}
			}
		},
	]
});
function nextPath (path,errCode,next){
	if(errCode == 2001) {
		if(path == "/pages/user/enterprise" ){   //重点***放行
			next()
		}else{
			next({
				name: 'enterprise',
				NAVTYPE: 'replaceAll'
			});
		}
	}else if(errCode == 1001) {
	} 
}
//全局路由前置守卫
router.beforeEach((to, from, next) => {
	 console.log(to)
	// #ifdef MP-WEIXIN
	// if(to.meta.auth) {
	// 	let token = uni.getStorageSync('token');
	// 	// 如果有缓存信息
	// 	if(token){
	// 		let authCheck = store.dispatch('authCheck');
	// 		authCheck.then(resolve =>{
	// 			console.log(to.path)
	// 			if(to.path == "/pages/index/index" ){   //重点***放行
	// 				next()
	// 			}else{
	// 				// next({
	// 				// 	name: 'index',
	// 				// 	NAVTYPE: 'replaceAll'
	// 				// });
	// 				next({
	// 					name: 'orderDetail',
	// 					NAVTYPE: 'replaceAll'
	// 				});
	// 			}
	// 		}).catch(errCode =>{
	// 			console.log(errCode)
	// 			nextPath(to.path,errCode,next)
	// 		})
	// 	}else{
	// 		let getUserInfo = store.dispatch('getUserInfo');
	// 		getUserInfo.then(resolve =>{
	// 		 if(to.path == "/pages/index/index" || to.path == "pages/index/load"){   //重点***放行
	// 		 	next()
	// 		 }else{
	// 		 	next({
	// 		 		name: 'index',
	// 		 		NAVTYPE: 'replaceAll'
	// 		 	});
	// 		 }
	// 		}).catch(errCode =>{
	// 			nextPath(to.path,errCode,next)
	// 		})
	// 	}
	 
	 
		

	// }else{
		next();
	// }
	// #endif
	
	// #ifdef H5
	next();
	// #endif


	
	// 权限控制登录
	// if (to.meta && to.meta.auth && !store.getters.isLogin) {
	// 	store.dispatch('showAuthModal');
	// 	next(false);
	// } else if (store.getters.initRecharge.enable !== '1' && to.path === '/pages/user/wallet/top-up') {
	// 	// 充值入口控制
	// 	next(false);
	// } else {
	// }
});

export {
	router,
	RouterMount
}
