index.js 1.02 KB
/*
 * @Date: 2025-03-20 20:36:36
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2025-06-12 10:37:19
 * @FilePath: /mlaj/src/router/index.js
 * @Description: 路由实例创建和导出
 */
import { createRouter, createWebHashHistory } from 'vue-router'
import { routes } from './routes'
import { checkWxAuth, checkAuth } from './guards'

const router = createRouter({
  history: createWebHashHistory(import.meta.env.VITE_BASE || '/'),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 每次路由切换后,页面滚动到顶部
    // return savedPosition || { top: 0, left: 0 }
    return { top: 0, left: 0 }
  },
})

// 导航守卫
router.beforeEach(async (to, from, next) => {
  // 微信授权检查
  const wxAuthResult = await checkWxAuth()
  if (wxAuthResult !== true) {
    next(wxAuthResult)
    return
  }

  // 检查用户是否已登录

  // 登录权限检查
  const authResult = checkAuth(to)
  if (authResult !== true) {
    next(authResult)
    return
  }

  next()
})

export default router