// router.js
import { RouterMount, createRouter } from 'uni-simple-router';
import tools from '@/common/js/tools'
import store from '@/store/index'
import { getPlatform } from '@/common/js/utils'
const router = createRouter({
    platform: process.env.VUE_APP_PLATFORM,
    routes: [...ROUTES]
});
//全局路由前置守卫
const whiteList = ['/','/pages/index/index','/pages/text/index']

router.beforeEach((to, from, next) => {
    let token = to.query.token;
    let m_id = to.query.m_id;
    if (token && m_id) {
        // store.commit("setCookie", { 'token': token, 'm_id': m_id });
        uni.setStorageSync('token',token);
        uni.setStorageSync('m_id',m_id);
        next('/pages/index/index');
    }
    const userToken = uni.getStorageSync('token')
    const userMid = uni.getStorageSync('m_id')
    console.log('登陆信息-------'+userToken+'--------------'+userMid)
    if((!userToken || !userMid)  && whiteList.indexOf(to.fullPath)<0){
        console.log('暂无登陆信息')
        console.log(to.fullPath )
        if(to.fullPath!=='/pages/login/index'){
            next('/pages/login/index');
        }
    }
    console.log('前置守卫to----------', to)
    next();

});
// 全局路由后置守卫
router.afterEach((to, from) => {
    console.log('后置守卫to------', to)
})

export {
    router,
    RouterMount
}