import { Router, useRoute } from 'vue-router'
import useUserInfo from "@/stores/modules/user";
import tokenInfo from "@/stores/modules/token";
const route = useRoute()
/**
 * 除了注册页，当没有 token 则跳转至注册页
 * @param router
 */


export function authGuard(router: Router) {
  router.beforeEach(async (to, from, next) => {
    /* 跳过路由守卫 */

    const useUserInfoStore = useUserInfo();
    const tokenInfoStore = tokenInfo();
    if (tokenInfoStore.getToken /* 已登录 */) {
      if(!useUserInfoStore.userInfo.id){
          await useUserInfoStore.getInfo()
      }
      return next()
    } else {
      if(to.meta.noAuth)  return next()
      useUserInfoStore.updateLoginShow(true)
    }



    return next()
  })
}
