index.ts 577 B

1234567891011121314151617181920212223
  1. import { isNull, router, useStore } from "@/cool";
  2. /**
  3. * 路由跳转前的全局钩子(如修改 pages.json 后需重新编译项目以确保路由信息生效)
  4. * @param to 跳转页
  5. * @param from 当前页
  6. * @param next 跳转函数
  7. */
  8. router.beforeEach((to, from, next) => {
  9. const { user } = useStore();
  10. // 判断是否需要登录
  11. if (to.isAuth == true || (isNull(to.meta) ? true : to.meta.isAuth == true)) {
  12. // 如果用户信息为空,则跳转到登录页
  13. if (!user.isNull()) {
  14. next();
  15. } else {
  16. router.login();
  17. }
  18. } else {
  19. next();
  20. }
  21. });