Blame view

src/permission.js 4.05 KB
1 2 3
/*
 * @Description: 项目权限
 * @Autor: renchao
yangwei committed
4
 * @LastEditTime: 2023-06-20 10:07:56
5
 */
6
import Vue from 'vue'
7
import axios from 'axios'
赵千 committed
8 9
import router from './router'
import store from './store'
10
import Cookies from 'js-cookie'
renchao@pashanhoo.com committed
11
import { getMenuInfo } from '@/api/user'
赵千 committed
12 13 14
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
yangwei committed
15
import {getToken, getUrlParam, setToken} from "@/utils/util";
苗菁 committed
16

renchao@pashanhoo.com committed
17
NProgress.configure({ showSpinner: false }) // NProgress Configuration
赵千 committed
18 19

router.beforeEach(async (to, from, next) => {
苗菁 committed
20 21 22 23 24 25
  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操作
yangwei committed
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  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) {
苗菁 committed
50
      next()
yangwei committed
51 52 53 54 55 56 57 58
      // 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')
xiaomiao committed
59
      if (to.fullPath||routeTo && routeTo !== '/') {
yangwei committed
60 61 62 63
        next({ ...to, replace: true })
      } else {
        next('/home')
      }
赵千 committed
64
    }
苗菁 committed
65 66 67
  }
  if (window._config.casEnable === true) {
    if (!token) {
yangwei committed
68
      let ticket = getUrlParam("ticket");
苗菁 committed
69
      if (ticket) {
yangwei committed
70
        casValidate(ticket)
苗菁 committed
71 72 73 74
      } else {
        localStorage.setItem("location", window.location.href)
        window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl);
      }
renchao@pashanhoo.com committed
75
    } else {
苗菁 committed
76 77 78
      permission()
    }
  } else {
yangwei committed
79
    // 使用自定义页面实现单点登录
苗菁 committed
80
    if (!token) {
yangwei committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      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`
苗菁 committed
101
      }
yangwei committed
102 103 104 105 106 107 108 109 110 111
    }else{
      if (to.path === '/login') {
        const redirectUrl = getUrlParam('redirectUrl');
        if (redirectUrl && redirectUrl !== '') {
            window.location.href = redirectUrl
            return
        } else {
            next('/');
            return
        }
苗菁 committed
112
      }
yangwei committed
113
      permission()
任超 committed
114
    }
苗菁 committed
115 116
  }
  NProgress.done()
赵千 committed
117 118
})
router.afterEach(to => {
苗菁 committed
119 120 121
  // 解决刷新页面报404问题
  Cookies.set("routerTo", to.fullPath)
  NProgress.done()
赵千 committed
122
})