permission.js 4.05 KB
/*
 * @Description:
 * @Autor: renchao
 * @LastEditTime: 2023-06-26 11:11:23
 */
import Vue from 'vue'
import axios from 'axios'
import store from "./store";
import router from "./router";
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 getTheme from "@/utils/theme";
import Cookies from "js-cookie";
import { getToken, getUrlParam, setToken } from "@/utils/util";

NProgress.configure({ showSpinner: false });

router.beforeEach(async (to, from, next) => {
  getTheme()
  NProgress.start();
  document.title = getPageTitle(to.meta.title);
  let hasAddDict = store.state.dict.addDict;
  let hasUser = store.state.user.hasUser;
  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('sjsb-location') + '#' + localStorage.getItem('hash')
      } else {
        alert(res.data.message)
      }
    }).catch((e) => {
      console.log(e);
    });
  }
  async function permission () {
    if (!hasUser) {
      store.dispatch("user/getUserInfo");
      if (!hasAddDict) {
        await store.dispatch("dict/generateDic");
      }
    }
    if (hasAddRoute) {
      next();
    } else {
      //请求菜单
      const { result: getMenuData } = (await getMenuInfo(Vue.prototype.BASE_API.CODE)) || [];
      const accessRoutes = await store.dispatch(
        "permission/generateRoutes",
        getMenuData
      );
      let path = JSON.parse(getMenuData[0].metadata)?.path + JSON.parse(getMenuData[0].children[0].metadata)?.path
      router.addRoutes([
        ...accessRoutes,
        { path: "*", redirect: "/404", hidden: true }
      ]);
      next(path)
    }
  }
  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('sjsb-location', locationUrl)
            localStorage.setItem('hash', to.fullPath)
            window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/sjsb/&hash=/login&`
            return
          }
        }
        localStorage.setItem('sjsb-location', locationUrl)
        localStorage.setItem('hash', to.fullPath)
        //todo: loginUrl 需要业务系统根据登录页面路由地址获取,这里只是简写
        window.location.href = window._config.services.management + `/management/cas/status?loginUrl=${window._config.baseUrl}/sjsb/&hash=/login`
      }
    } else {
      permission()
      if (to.path === '/login') {
        const redirectUrl = getUrlParam('redirectUrl');
        if (redirectUrl && redirectUrl !== '') {
          window.location.href = redirectUrl
          return
        } else {
          next('/');
          return
        }
      }
    }
  }
  NProgress.done()
});
router.afterEach((to) => {
  // 解决刷新页面报404问题
  Cookies.set("routerTo", to.fullPath);
  NProgress.done();
})