permission.js 4.04 KB
/*
 * @Description: 项目权限
 * @Autor: renchao
 * @LastEditTime: 2023-06-20 10:07:56
 */
import Vue from 'vue'
import axios from 'axios'
import router from './router'
import store from './store'
import Cookies from 'js-cookie'
import { getMenuInfo } from '@/api/user'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
import {getToken, getUrlParam, setToken} from "@/utils/util";

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async (to, from, next) => {
  Vue.prototype.$currentRoute = to
  NProgress.start()
  document.title = getPageTitle(to.meta.title)
  let hasAddDict = store.state.dict.addDict
  let hasAddRoute = store.state.permission.addRoutes
  // cas操作
  const token = getToken()
  let locationUrl = window.location.origin + window.location.pathname;
  function casValidate (ticket){
    axios.get(window._config.services.management + "/management/cas/validate", {
      params: {
        ticket: ticket,
        service: locationUrl,
      },
    }).then(async (res) => {
      if (res.data.status === 1) {
          setToken(res.data.content.accessToken)
          window.location.href = localStorage.getItem('dj-location') + '#' + localStorage.getItem('hash')
      } else {
          alert(res.data.message)
      }
    }).catch((e) => {
      console.log(e);
    });
  }
  async function permission () {
    if (!hasAddDict) {
      store.dispatch('dict/generateDic')
    }
    if (hasAddRoute) {
      next()
      // next({ ...to, replace: true })
    } else {
      const { result: getMenuData } = await getMenuInfo()
      const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData)
      // 获取用户信息
      await store.dispatch('user/getUserInfo')
      router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }])
      const routeTo = Cookies.get('routerTo')
      if (routeTo && routeTo !== '/') {
        next({ ...to, replace: true })
      } else {
        next('/home')
      }
    }
  }
  if (window._config.casEnable === true) {
    if (!token) {
      let ticket = getUrlParam("ticket");
      if (ticket) {
        casValidate(ticket)
      } else {
        localStorage.setItem("location", window.location.href)
        window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl);
      }
    } else {
      permission()
    }
  } else {
    // 使用自定义页面实现单点登录
    if (!token) {
      let ticket = getUrlParam('ticket');
      if (ticket) {
        casValidate(ticket)
      } else {
          if (to.path === '/login') {
              if (getUrlParam('_flag') === '1') {
                  next();
                  return
              } else {
                  //todo: loginUrl 需要业务系统根据登录页面路由地址获取,这里只是简写
                  localStorage.setItem('dj-location',locationUrl)
                  localStorage.setItem('hash',to.fullPath)
                  window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/dj/&hash=/login&`
                  return
              }
          }
          localStorage.setItem('dj-location',locationUrl)
          localStorage.setItem('hash',to.fullPath)
          //todo: loginUrl 需要业务系统根据登录页面路由地址获取,这里只是简写
          window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/dj/&hash=/login`
      }
    }else{
      if (to.path === '/login') {
        const redirectUrl = getUrlParam('redirectUrl');
        if (redirectUrl && redirectUrl !== '') {
            window.location.href = redirectUrl
            return
        } else {
            next('/');
            return
        }
      }
      permission()
    }
  }
  NProgress.done()
})
router.afterEach(to => {
  // 解决刷新页面报404问题
  Cookies.set("routerTo", to.fullPath)
  NProgress.done()
})