// router.js
import {
  RouterMount,
  createRouter
} from 'uni-simple-router';
import store from '@/store/index.js'
import {
  isWeixin,
  getQueryVariable
} from '@/common/utils/common';

import {
  setStorage,
  getStorage,
  removeStorage
} from "@/common/utils/storage.js"

function goJianquan(url) {
  const href =
    `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxaddd13c267e81e70&redirect_uri=${url}&response_type=code&scope=snsapi_userinfo#wechat_redirect`;
  window.location.href = href;
}

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  //H5跳转路由锁
  routerErrorEach: (error, router) => {
    if (error.type === 3) {
      /*
      error.type
      0 表示 next(false)
      1 表示 next(unknownType)
      2 表示加锁状态，禁止跳转
      3 表示在获取页面栈的时候，页面栈不够level获取
      */
      router.$lockStatus = false;
    }
  },
  //小程序跳转路由锁
  applet: {
    animationDuration: 300 //默认 300ms  v2.0.6+
  },
  routes: [...ROUTES]
});

//全局路由前置守卫
router.beforeEach((to, from, next) => {
  // 不需要验证登录的页面
  let whitePages = [
    '/pages/login/index',
    '/pages/login/indexs',
    '/pages/index/index',
	'/pages/apply/personage/examApply',
  ]
  next();
  // if (whitePages.indexOf(to.path) >= 0) {
  //   next();
  // } else {
  //   if (isWeixin()) {
  //     if (store.getters.token) {
  //       next();
  //     } else {
  //       next('/pages/login/index');
  //       // var code = getQueryVariable('code');
  //       // if (code) {
  //       //   // 去登入
  //       // } else if (!getStorage('code')) {
  //       //   goJianquan(location.href)
  //       // }
  //     }
  //   } else {
  //     if (store.getters.token) {
  //       next();
  //     } else {
  //       next('/pages/login/index');
  //     }
  //   }
  // }
});

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

export {
  router,
  RouterMount
}
