5770ee1d by renchao@pashanhoo.com

style:cas

1 parent 2159a36b
<!--
* @Description:
* @Autor: renchao
* @LastEditTime: 2023-05-16 09:50:40
* @LastEditTime: 2023-05-26 16:57:47
-->
<!DOCTYPE html>
<html>
......@@ -18,8 +18,19 @@
</title>
</head>
<script>
window.baseUrl = location.origin || location.protocol + '//' + location.host
const authorization = "bearer AT-4-MxSrO29Coe7VTazx8uuixtqqgO-hvCB6"
window._config = {
// 是否微服务模式,业务系统根据需要读取
cloudEnable: false,
// 是否启用单点登录
casEnable: true,
// cas 基地址
casBaseURL: 'http://192.168.2.38/cas',
services: {
// 配置到 contextPath 前一级
management: 'http://192.168.2.38',
business: 'http://localhost:7001'
}
}
fetch('<%= BASE_URL %>config.json')
.then(response => response.json())
.then(config => {
......
/*
* @Description: 项目权限
* @Autor: renchao
* @LastEditTime: 2023-05-16 14:10:26
* @LastEditTime: 2023-05-26 17:17:34
*/
import Vue from 'vue'
import router from './router'
import store from './store'
import axios from 'axios'
import { getMenuInfo } from '@/api/user'
import { getUrlParam } from '@/utils/operation'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'
......@@ -14,31 +16,82 @@ import Cookies from 'js-cookie'
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
if (!hasAddDict) {
store.dispatch('dict/generateDic')
}
if (hasAddRoute) {
const token = localStorage.getItem("token")
debugger
if (to.path === '/login') {
if (token) {
next('/')
} else {
next()
// next({ ...to, replace: true })
}
return
}
if (window._config.casEnable === true) {
let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
if (!token) {
let ticket = getUrlParam('ticket');
if (ticket) {
axios.get("http://192.168.2.38/management/cas/validate", {
params: {
'ticket': ticket,
'service': locationUrl
}
}).then((res) => {
localStorage.setItem('token', res.data.content.id)
window.location.href = localStorage.getItem('location')
}).catch(e => {
console.log(e)
})
} 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 })
localStorage.setItem("location", window.location.href)
window.location.href = window._config.casBaseURL + '/login?service=' + encodeURIComponent(locationUrl);
}
} else {
next('/home')
next()
}
} else {
if (!token) {
const redirectData = {
path: '/login',
replace: true,
}
NProgress.done()
if (to.path) {
redirectData.query = {
...redirectData.query,
redirect: to.path,
};
}
next(redirectData)
return
}
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
// 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')
// }
// }
// NProgress.done()
})
router.afterEach(to => {
// 解决刷新页面报404问题
......
/*
* @Description: 全局路由
* @Autor: renchao
* @LastEditTime: 2023-05-16 14:09:37
* @LastEditTime: 2023-05-26 17:11:19
*/
import Vue from 'vue'
import Router from 'vue-router'
......
......@@ -110,3 +110,25 @@ export function down (index, data) {
data.splice(index, 0, downData);
}
}
export function getUrlParam (paraName) {
let url = document.location.toString();
let arrObj = url.split('?');
if (arrObj.length > 1) {
let arrPara = arrObj[1].split('&');
let arr;
for (let i = 0; i < arrPara.length; i++) {
arr = arrPara[i].split('=');
if (arr != null && arr[0] === paraName) {
return arr[1];
}
}
return '';
} else {
return '';
}
}
......
/*
* @Description: 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器
* @Autor: renchao
* @LastEditTime: 2023-05-16 14:07:58
* @LastEditTime: 2023-05-26 16:52:25
*/
import axios from 'axios'
import { Message } from 'element-ui'
......@@ -9,10 +9,6 @@ import { endLoadingSubCount } from './requestLoading'
// create an axios instance
const service = axios.create({
baseURL:
process.env.NODE_ENV == "development"
? process.env.VUE_APP_BASE_API
: window.baseUrl + "/",
withCredentials: true, //是否允许跨域
headers: {
'Content-Type': 'application/json; charset=utf-8'
......@@ -23,13 +19,14 @@ const service = axios.create({
// request interceptor
service.interceptors.request.use(
config => {
// do something before request is sent
if (process.env.NODE_ENV === "production") {
return config;
const token = localStorage.getItem('token')
// 添加请求头
if (token) {
config.headers['Authorization'] = "Bearer " + token
} else {
config.headers.Authorization = authorization;
return config;
config.headers.delete('Authorization')
}
return config;
},
error => {
// do something with request error
......@@ -55,6 +52,27 @@ service.interceptors.response.use(
},
error => {
endLoadingSubCount()
if (error.response.status === 401) {
//todo: 需要解决 一个页面多个请求,刷新后此处会触发多次
if (window.__isNeedLogin) {
window.__isNeedLogin = false
this.$message.error('token失效,请重新登录');
let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
localStorage.removeItem('token')
if (window._config.casEnable) {
window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl);
} else {
router.replace({
path: '/login',
query: {
redirect: router.currentRoute.value.fullPath
}
})
return false
}
}
} else {
// 对响应错误做点什么
Message({
message: '服务器异常,请联系管理员',
......@@ -62,6 +80,7 @@ service.interceptors.response.use(
duration: 5 * 1000,
customClass: 'messageIndex'
})
}
return Promise.reject(error);
}
)
......