style:cas
Showing
5 changed files
with
140 additions
and
35 deletions
| 1 | <!-- | 1 | <!-- |
| 2 | * @Description: | 2 | * @Description: |
| 3 | * @Autor: renchao | 3 | * @Autor: renchao |
| 4 | * @LastEditTime: 2023-05-16 09:50:40 | 4 | * @LastEditTime: 2023-05-26 16:57:47 |
| 5 | --> | 5 | --> |
| 6 | <!DOCTYPE html> | 6 | <!DOCTYPE html> |
| 7 | <html> | 7 | <html> |
| ... | @@ -18,8 +18,19 @@ | ... | @@ -18,8 +18,19 @@ |
| 18 | </title> | 18 | </title> |
| 19 | </head> | 19 | </head> |
| 20 | <script> | 20 | <script> |
| 21 | window.baseUrl = location.origin || location.protocol + '//' + location.host | 21 | window._config = { |
| 22 | const authorization = "bearer AT-4-MxSrO29Coe7VTazx8uuixtqqgO-hvCB6" | 22 | // 是否微服务模式,业务系统根据需要读取 |
| 23 | cloudEnable: false, | ||
| 24 | // 是否启用单点登录 | ||
| 25 | casEnable: true, | ||
| 26 | // cas 基地址 | ||
| 27 | casBaseURL: 'http://192.168.2.38/cas', | ||
| 28 | services: { | ||
| 29 | // 配置到 contextPath 前一级 | ||
| 30 | management: 'http://192.168.2.38', | ||
| 31 | business: 'http://localhost:7001' | ||
| 32 | } | ||
| 33 | } | ||
| 23 | fetch('<%= BASE_URL %>config.json') | 34 | fetch('<%= BASE_URL %>config.json') |
| 24 | .then(response => response.json()) | 35 | .then(response => response.json()) |
| 25 | .then(config => { | 36 | .then(config => { | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Description: 项目权限 | 2 | * @Description: 项目权限 |
| 3 | * @Autor: renchao | 3 | * @Autor: renchao |
| 4 | * @LastEditTime: 2023-05-16 14:10:26 | 4 | * @LastEditTime: 2023-05-26 17:17:34 |
| 5 | */ | 5 | */ |
| 6 | import Vue from 'vue' | 6 | import Vue from 'vue' |
| 7 | import router from './router' | 7 | import router from './router' |
| 8 | import store from './store' | 8 | import store from './store' |
| 9 | import axios from 'axios' | ||
| 9 | import { getMenuInfo } from '@/api/user' | 10 | import { getMenuInfo } from '@/api/user' |
| 11 | import { getUrlParam } from '@/utils/operation' | ||
| 10 | import NProgress from 'nprogress' // progress bar | 12 | import NProgress from 'nprogress' // progress bar |
| 11 | import 'nprogress/nprogress.css' // progress bar style | 13 | import 'nprogress/nprogress.css' // progress bar style |
| 12 | import getPageTitle from '@/utils/get-page-title' | 14 | import getPageTitle from '@/utils/get-page-title' |
| ... | @@ -14,31 +16,82 @@ import Cookies from 'js-cookie' | ... | @@ -14,31 +16,82 @@ import Cookies from 'js-cookie' |
| 14 | NProgress.configure({ showSpinner: false }) // NProgress Configuration | 16 | NProgress.configure({ showSpinner: false }) // NProgress Configuration |
| 15 | 17 | ||
| 16 | router.beforeEach(async (to, from, next) => { | 18 | router.beforeEach(async (to, from, next) => { |
| 17 | Vue.prototype.$currentRoute = to | 19 | const token = localStorage.getItem("token") |
| 18 | NProgress.start() | 20 | debugger |
| 19 | document.title = getPageTitle(to.meta.title) | 21 | if (to.path === '/login') { |
| 20 | let hasAddDict = store.state.dict.addDict | 22 | if (token) { |
| 21 | let hasAddRoute = store.state.permission.addRoutes | 23 | next('/') |
| 22 | if (!hasAddDict) { | 24 | } else { |
| 23 | store.dispatch('dict/generateDic') | ||
| 24 | } | ||
| 25 | if (hasAddRoute) { | ||
| 26 | next() | 25 | next() |
| 27 | // next({ ...to, replace: true }) | 26 | } |
| 27 | return | ||
| 28 | } | ||
| 29 | if (window._config.casEnable === true) { | ||
| 30 | let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname; | ||
| 31 | if (!token) { | ||
| 32 | let ticket = getUrlParam('ticket'); | ||
| 33 | if (ticket) { | ||
| 34 | axios.get("http://192.168.2.38/management/cas/validate", { | ||
| 35 | params: { | ||
| 36 | 'ticket': ticket, | ||
| 37 | 'service': locationUrl | ||
| 38 | } | ||
| 39 | }).then((res) => { | ||
| 40 | localStorage.setItem('token', res.data.content.id) | ||
| 41 | window.location.href = localStorage.getItem('location') | ||
| 42 | }).catch(e => { | ||
| 43 | console.log(e) | ||
| 44 | }) | ||
| 28 | } else { | 45 | } else { |
| 29 | const { result: getMenuData } = await getMenuInfo() | 46 | localStorage.setItem("location", window.location.href) |
| 30 | const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData) | 47 | window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl); |
| 31 | // 获取用户信息 | 48 | } |
| 32 | await store.dispatch('user/getUserInfo') | ||
| 33 | router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }]) | ||
| 34 | const routeTo = Cookies.get('routerTo') | ||
| 35 | if (routeTo && routeTo !== '/') { | ||
| 36 | next({ ...to, replace: true }) | ||
| 37 | } else { | 49 | } else { |
| 38 | next('/home') | 50 | next() |
| 39 | } | 51 | } |
| 52 | } else { | ||
| 53 | if (!token) { | ||
| 54 | const redirectData = { | ||
| 55 | path: '/login', | ||
| 56 | replace: true, | ||
| 40 | } | 57 | } |
| 41 | NProgress.done() | 58 | if (to.path) { |
| 59 | redirectData.query = { | ||
| 60 | ...redirectData.query, | ||
| 61 | redirect: to.path, | ||
| 62 | }; | ||
| 63 | } | ||
| 64 | next(redirectData) | ||
| 65 | return | ||
| 66 | } | ||
| 67 | next() | ||
| 68 | } | ||
| 69 | |||
| 70 | // Vue.prototype.$currentRoute = to | ||
| 71 | // NProgress.start() | ||
| 72 | // document.title = getPageTitle(to.meta.title) | ||
| 73 | // let hasAddDict = store.state.dict.addDict | ||
| 74 | // let hasAddRoute = store.state.permission.addRoutes | ||
| 75 | // if (!hasAddDict) { | ||
| 76 | // store.dispatch('dict/generateDic') | ||
| 77 | // } | ||
| 78 | // if (hasAddRoute) { | ||
| 79 | // next() | ||
| 80 | // // next({ ...to, replace: true }) | ||
| 81 | // } else { | ||
| 82 | // const { result: getMenuData } = await getMenuInfo() | ||
| 83 | // const accessRoutes = await store.dispatch('permission/generateRoutes', getMenuData) | ||
| 84 | // // 获取用户信息 | ||
| 85 | // await store.dispatch('user/getUserInfo') | ||
| 86 | // router.addRoutes([...accessRoutes, { path: '*', redirect: '/404', hidden: true }]) | ||
| 87 | // const routeTo = Cookies.get('routerTo') | ||
| 88 | // if (routeTo && routeTo !== '/') { | ||
| 89 | // next({ ...to, replace: true }) | ||
| 90 | // } else { | ||
| 91 | // next('/home') | ||
| 92 | // } | ||
| 93 | // } | ||
| 94 | // NProgress.done() | ||
| 42 | }) | 95 | }) |
| 43 | router.afterEach(to => { | 96 | router.afterEach(to => { |
| 44 | // 解决刷新页面报404问题 | 97 | // 解决刷新页面报404问题 | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Description: 全局路由 | 2 | * @Description: 全局路由 |
| 3 | * @Autor: renchao | 3 | * @Autor: renchao |
| 4 | * @LastEditTime: 2023-05-16 14:09:37 | 4 | * @LastEditTime: 2023-05-26 17:11:19 |
| 5 | */ | 5 | */ |
| 6 | import Vue from 'vue' | 6 | import Vue from 'vue' |
| 7 | import Router from 'vue-router' | 7 | import Router from 'vue-router' | ... | ... |
| ... | @@ -110,3 +110,25 @@ export function down (index, data) { | ... | @@ -110,3 +110,25 @@ export function down (index, data) { |
| 110 | data.splice(index, 0, downData); | 110 | data.splice(index, 0, downData); |
| 111 | } | 111 | } |
| 112 | } | 112 | } |
| 113 | |||
| 114 | |||
| 115 | export function getUrlParam (paraName) { | ||
| 116 | let url = document.location.toString(); | ||
| 117 | let arrObj = url.split('?'); | ||
| 118 | |||
| 119 | if (arrObj.length > 1) { | ||
| 120 | let arrPara = arrObj[1].split('&'); | ||
| 121 | let arr; | ||
| 122 | |||
| 123 | for (let i = 0; i < arrPara.length; i++) { | ||
| 124 | arr = arrPara[i].split('='); | ||
| 125 | |||
| 126 | if (arr != null && arr[0] === paraName) { | ||
| 127 | return arr[1]; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | return ''; | ||
| 131 | } else { | ||
| 132 | return ''; | ||
| 133 | } | ||
| 134 | } | ... | ... |
| 1 | /* | 1 | /* |
| 2 | * @Description: 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器 | 2 | * @Description: 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器 |
| 3 | * @Autor: renchao | 3 | * @Autor: renchao |
| 4 | * @LastEditTime: 2023-05-16 14:07:58 | 4 | * @LastEditTime: 2023-05-26 16:52:25 |
| 5 | */ | 5 | */ |
| 6 | import axios from 'axios' | 6 | import axios from 'axios' |
| 7 | import { Message } from 'element-ui' | 7 | import { Message } from 'element-ui' |
| ... | @@ -9,10 +9,6 @@ import { endLoadingSubCount } from './requestLoading' | ... | @@ -9,10 +9,6 @@ import { endLoadingSubCount } from './requestLoading' |
| 9 | 9 | ||
| 10 | // create an axios instance | 10 | // create an axios instance |
| 11 | const service = axios.create({ | 11 | const service = axios.create({ |
| 12 | baseURL: | ||
| 13 | process.env.NODE_ENV == "development" | ||
| 14 | ? process.env.VUE_APP_BASE_API | ||
| 15 | : window.baseUrl + "/", | ||
| 16 | withCredentials: true, //是否允许跨域 | 12 | withCredentials: true, //是否允许跨域 |
| 17 | headers: { | 13 | headers: { |
| 18 | 'Content-Type': 'application/json; charset=utf-8' | 14 | 'Content-Type': 'application/json; charset=utf-8' |
| ... | @@ -23,13 +19,14 @@ const service = axios.create({ | ... | @@ -23,13 +19,14 @@ const service = axios.create({ |
| 23 | // request interceptor | 19 | // request interceptor |
| 24 | service.interceptors.request.use( | 20 | service.interceptors.request.use( |
| 25 | config => { | 21 | config => { |
| 26 | // do something before request is sent | 22 | const token = localStorage.getItem('token') |
| 27 | if (process.env.NODE_ENV === "production") { | 23 | // 添加请求头 |
| 28 | return config; | 24 | if (token) { |
| 25 | config.headers['Authorization'] = "Bearer " + token | ||
| 29 | } else { | 26 | } else { |
| 30 | config.headers.Authorization = authorization; | 27 | config.headers.delete('Authorization') |
| 31 | return config; | ||
| 32 | } | 28 | } |
| 29 | return config; | ||
| 33 | }, | 30 | }, |
| 34 | error => { | 31 | error => { |
| 35 | // do something with request error | 32 | // do something with request error |
| ... | @@ -55,6 +52,27 @@ service.interceptors.response.use( | ... | @@ -55,6 +52,27 @@ service.interceptors.response.use( |
| 55 | }, | 52 | }, |
| 56 | error => { | 53 | error => { |
| 57 | endLoadingSubCount() | 54 | endLoadingSubCount() |
| 55 | if (error.response.status === 401) { | ||
| 56 | //todo: 需要解决 一个页面多个请求,刷新后此处会触发多次 | ||
| 57 | if (window.__isNeedLogin) { | ||
| 58 | window.__isNeedLogin = false | ||
| 59 | this.$message.error('token失效,请重新登录'); | ||
| 60 | let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname; | ||
| 61 | localStorage.removeItem('token') | ||
| 62 | if (window._config.casEnable) { | ||
| 63 | window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl); | ||
| 64 | } else { | ||
| 65 | router.replace({ | ||
| 66 | path: '/login', | ||
| 67 | query: { | ||
| 68 | redirect: router.currentRoute.value.fullPath | ||
| 69 | } | ||
| 70 | }) | ||
| 71 | return false | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | } else { | ||
| 58 | // 对响应错误做点什么 | 76 | // 对响应错误做点什么 |
| 59 | Message({ | 77 | Message({ |
| 60 | message: '服务器异常,请联系管理员', | 78 | message: '服务器异常,请联系管理员', |
| ... | @@ -62,6 +80,7 @@ service.interceptors.response.use( | ... | @@ -62,6 +80,7 @@ service.interceptors.response.use( |
| 62 | duration: 5 * 1000, | 80 | duration: 5 * 1000, |
| 63 | customClass: 'messageIndex' | 81 | customClass: 'messageIndex' |
| 64 | }) | 82 | }) |
| 83 | } | ||
| 65 | return Promise.reject(error); | 84 | return Promise.reject(error); |
| 66 | } | 85 | } |
| 67 | ) | 86 | ) | ... | ... |
-
Please register or sign in to post a comment