|
@@ -1,132 +1,117 @@
|
|
-import router from './router'
|
|
|
|
-import store from './store'
|
|
|
|
-import { Message } from 'element-ui'
|
|
|
|
-import NProgress from 'nprogress' // progress bar
|
|
|
|
-import 'nprogress/nprogress.css' // progress bar style
|
|
|
|
-import { getToken, setUserUrl, setUseType, setWebSiteId, hashParams, setLoginStatus, getLoginStatus } from '@/utils/auth' // get token from cookie
|
|
|
|
-import getPageTitle from '@/utils/get-page-title'
|
|
|
|
-import URL from '@/utils/baseUrl';
|
|
|
|
-import { getInfo } from '@/api/user'
|
|
|
|
-NProgress.configure({ showSpinner: false }) // NProgress Configuration
|
|
|
|
|
|
+//1.引入vue-element-admin 依赖 start ---------------------------------------->
|
|
|
|
+import router from './router'//引入路由
|
|
|
|
+const whiteList = ['/login', '/auth-redirect','/no-permission']//配置路由白名单
|
|
|
|
+import store from './store'//引入vuex
|
|
|
|
+import { Message } from 'element-ui'//引入element的弹框提示
|
|
|
|
+import getPageTitle from '@/utils/get-page-title'//获得系统标题
|
|
|
|
+import NProgress from 'nprogress'//引入进度条库
|
|
|
|
+import 'nprogress/nprogress.css'//引入进度条样式
|
|
|
|
+NProgress.configure({ showSpinner: false })
|
|
|
|
+//1.引入vue-element-admin依赖 end ---------------------------------------->
|
|
|
|
|
|
-const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist
|
|
|
|
|
|
+//2.引入单点登录需要的方法 start ---------------------------------------->
|
|
|
|
+import { getToken, setUserUrl, getUseType, getUserUrl,setUseType,setWebSiteId,
|
|
|
|
+hashParams,removeToken } from '@/utils/auth' //设置cookie的方法
|
|
|
|
+import URL from '@/utils/baseUrl'; //获得请求路径
|
|
|
|
+import { getInfo } from '@/api/user' //获得用户信息
|
|
|
|
+//2.引入单点登录需要的方法 end ---------------------------------------->
|
|
|
|
|
|
|
|
+//3.设置路由守卫 start ---------------------------------------->
|
|
router.beforeEach(async (to, from, next) => {
|
|
router.beforeEach(async (to, from, next) => {
|
|
- // start progress bar
|
|
|
|
|
|
+ //3.1 当路由开始改变时,加载进度条
|
|
NProgress.start()
|
|
NProgress.start()
|
|
-
|
|
|
|
- // set page title
|
|
|
|
|
|
+ //3.2 设置系统标题
|
|
document.title = getPageTitle(to.meta.title)
|
|
document.title = getPageTitle(to.meta.title)
|
|
-
|
|
|
|
- // determine whether the user has logged in
|
|
|
|
|
|
+ //3.3 检查cookie中的token是否已经存在
|
|
const hasToken = getToken()
|
|
const hasToken = getToken()
|
|
- //检测token的状态
|
|
|
|
- await store.dispatch('user/logoutStatus')
|
|
|
|
-
|
|
|
|
- //获得用户操作管理系统的权限
|
|
|
|
- const loginStatus = getLoginStatus()
|
|
|
|
-
|
|
|
|
|
|
|
|
|
|
+ //3.4 如果token存在
|
|
if (hasToken) {
|
|
if (hasToken) {
|
|
- if (loginStatus == 0 || loginStatus == null || loginStatus == undefined) {
|
|
|
|
- if (whiteList.indexOf(to.path) !== -1) {
|
|
|
|
- console.log("用户没有权限操作管理系统,使其保持在登录页面");
|
|
|
|
- next();
|
|
|
|
|
|
+ //3.4.1 如果有token,而且是在登录页面
|
|
|
|
+ if (to.path === '/login') {
|
|
|
|
+ // Retrieve userurl
|
|
|
|
+ const userurl = hashParams();
|
|
|
|
+ if (userurl) {
|
|
|
|
+ setUserUrl(userurl, 86400)
|
|
|
|
+ }
|
|
|
|
+ const userInfo = await getInfo();
|
|
|
|
+ console.log("User Info:", userInfo);
|
|
|
|
+ if (userInfo.code === 200) {
|
|
|
|
+ console.log("####")
|
|
|
|
+ //每次返回login的时候都获取一下用户类型和网站id
|
|
|
|
+ setUseType(userInfo.data.type_id, 86400)
|
|
|
|
+ setWebSiteId(userInfo.data.website_id, 86400)
|
|
|
|
+ next({ path: '/' })
|
|
|
|
+ NProgress.done()
|
|
} else {
|
|
} else {
|
|
- next(`/login?redirect=${to.path}`);
|
|
|
|
- NProgress.done();
|
|
|
|
|
|
+ if(userInfo.code == -1){
|
|
|
|
+ next({ path: '/' })
|
|
|
|
+ NProgress.done()
|
|
|
|
+ }else{
|
|
|
|
+ next({ path: '/' })
|
|
|
|
+ NProgress.done()
|
|
|
|
+ }
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
- if (to.path === '/login') {
|
|
|
|
- // Retrieve userurl
|
|
|
|
- const userurl = hashParams();
|
|
|
|
- if (userurl) {
|
|
|
|
- setUserUrl(userurl, 86400)
|
|
|
|
- }
|
|
|
|
|
|
+ //3.4.2 如果有token,但是没在登录页面
|
|
|
|
+ const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
|
|
|
+ //此时分为两种情况,一种是用户通过路由访问,一种是用户从url直接访问(刷新)时执行
|
|
|
|
+ if (hasRoles) {
|
|
const userInfo = await getInfo();
|
|
const userInfo = await getInfo();
|
|
- console.log("User Info:", userInfo);
|
|
|
|
- if (userInfo.code === 200) {
|
|
|
|
- console.log("####")
|
|
|
|
- setUseType(userInfo.data.userType, 86400)
|
|
|
|
- setWebSiteId(userInfo.data.siteId, 86400)
|
|
|
|
- next({ path: '/' })
|
|
|
|
- NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
|
|
|
- } else {
|
|
|
|
- if(userInfo.code == -1){
|
|
|
|
- setLoginStatus(0,86400)
|
|
|
|
- next({ path: '/' })
|
|
|
|
- NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
|
|
|
- }else{
|
|
|
|
- next({ path: '/' })
|
|
|
|
- NProgress.done() // hack: https://github.com/PanJiaChen/vue-element-admin/pull/2939
|
|
|
|
|
|
+ //如果用户没有权限登录该网站,返回登录页面
|
|
|
|
+ if(userInfo.code == -1){
|
|
|
|
+ console.log("用户没有访问权限,将跳转到无权限页面!")
|
|
|
|
+ if (to.path !== '/no-permission') {
|
|
|
|
+ //避免重复跳转
|
|
|
|
+ next({path:'/no-permission'})
|
|
|
|
+ } else {
|
|
|
|
+ //已经在无权限页面,不再跳转
|
|
|
|
+ next()
|
|
}
|
|
}
|
|
|
|
+ NProgress.done()
|
|
|
|
+ }else{
|
|
|
|
+ setUseType(userInfo.data.type_id, 86400)
|
|
|
|
+ setWebSiteId(userInfo.data.website_id, 86400)
|
|
|
|
+ console.log("用户有访问权限,访问继续!")
|
|
|
|
+ next()
|
|
}
|
|
}
|
|
- // if is logged in, redirect to the home page
|
|
|
|
} else {
|
|
} else {
|
|
- // determine whether the user has obtained his permission roles through getInfo
|
|
|
|
- const hasRoles = store.getters.roles && store.getters.roles.length > 0
|
|
|
|
- if (hasRoles) {
|
|
|
|
- next()
|
|
|
|
- } else {
|
|
|
|
- try {
|
|
|
|
- // get user info
|
|
|
|
- // note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
|
|
|
|
- const { roles } = await store.dispatch('user/getInfo')
|
|
|
|
- // generate accessible routes map based on roles
|
|
|
|
- const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
|
|
|
- // dynamically add accessible routes
|
|
|
|
- router.addRoutes(accessRoutes)
|
|
|
|
- // hack method to ensure that addRoutes is complete
|
|
|
|
- // set the replace: true, so the navigation will not leave a history record
|
|
|
|
- next({ ...to, replace: true })
|
|
|
|
- // Call getInfo to fetch user information
|
|
|
|
- const userInfo = await getInfo();
|
|
|
|
- console.log("User Info:", userInfo);
|
|
|
|
- console.log("33333333333")
|
|
|
|
- } catch (error) {
|
|
|
|
- console.log("444444444444")
|
|
|
|
- // remove token and go to login page to re-login
|
|
|
|
- await store.dispatch('user/resetToken')
|
|
|
|
- Message.error(error || 'Has Error')
|
|
|
|
- next(`/login?redirect=${to.path}`)
|
|
|
|
- NProgress.done()
|
|
|
|
- }
|
|
|
|
|
|
+ //页面刷新的时候执行
|
|
|
|
+ try {
|
|
|
|
+ const { roles } = await store.dispatch('user/getInfo')
|
|
|
|
+ const accessRoutes = await store.dispatch('permission/generateRoutes', roles)
|
|
|
|
+ router.addRoutes(accessRoutes)
|
|
|
|
+ next({ ...to, replace: true })
|
|
|
|
+ } catch (error) {
|
|
|
|
+ await store.dispatch('user/resetToken')
|
|
|
|
+ Message.error(error || 'Has Error')
|
|
|
|
+ next(`/login?redirect=${to.path}`)
|
|
|
|
+ NProgress.done()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
} else {
|
|
} else {
|
|
- /* has no token*/
|
|
|
|
|
|
+ //3.5 如果token不存在
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
if (whiteList.indexOf(to.path) !== -1) {
|
|
const userurl = hashParams();
|
|
const userurl = hashParams();
|
|
if(userurl){
|
|
if(userurl){
|
|
- setUserUrl(userurl, 86400)
|
|
|
|
|
|
+ //更新backurl
|
|
|
|
+ setUserUrl(userurl, 86400)
|
|
|
|
+ }else{
|
|
|
|
+ //设置来源网站为当前域名
|
|
|
|
+ setUserUrl(window.location.host,86400);
|
|
}
|
|
}
|
|
- // in the free login whitelist, go directly
|
|
|
|
next()
|
|
next()
|
|
} else {
|
|
} else {
|
|
- const userType = getUseType()
|
|
|
|
- if(userType==10000){
|
|
|
|
- next(`/login?redirect=${to.path}`)
|
|
|
|
|
|
+ next(`/login`)
|
|
NProgress.done()
|
|
NProgress.done()
|
|
- setUserUrl(URL.webUrl, 86400)
|
|
|
|
- }else{
|
|
|
|
- const userurl = getUserUrl();
|
|
|
|
- let url=''
|
|
|
|
- if (!userurl.startsWith('http://') && !userurl.startsWith('https://')) {
|
|
|
|
- // 如果不是,则拼接上 http://
|
|
|
|
- url = 'http://' + userurl;
|
|
|
|
- }else{
|
|
|
|
- url = userurl;
|
|
|
|
- }
|
|
|
|
- next(`/login?backurl=${url}`)
|
|
|
|
- NProgress.done()
|
|
|
|
- setUserUrl(userurl, 86400)
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
router.afterEach(() => {
|
|
router.afterEach(() => {
|
|
- // finish progress bar
|
|
|
|
|
|
+ //路由改变完毕后,结束进度条
|
|
NProgress.done()
|
|
NProgress.done()
|
|
-})
|
|
|
|
|
|
+})
|
|
|
|
+//3.设置路由守卫 end ---------------------------------------->
|