router.ts 718 B

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