mirror of
https://github.com/iczer/vue-antd-admin
synced 2025-04-05 19:41:37 +08:00
feat: add login blocking; 🌟
新增:登录拦截功能;
This commit is contained in:
parent
d21a2ab062
commit
4e6a060dfe
@ -15,10 +15,10 @@
|
|||||||
</a-menu-item>
|
</a-menu-item>
|
||||||
<a-menu-divider />
|
<a-menu-divider />
|
||||||
<a-menu-item>
|
<a-menu-item>
|
||||||
<router-link to="/login">
|
<a @click="logout">
|
||||||
<a-icon type="poweroff" />
|
<a-icon type="poweroff" />
|
||||||
<span>退出登录</span>
|
<span>退出登录</span>
|
||||||
</router-link>
|
</a>
|
||||||
</a-menu-item>
|
</a-menu-item>
|
||||||
</a-menu>
|
</a-menu>
|
||||||
</a-dropdown>
|
</a-dropdown>
|
||||||
@ -26,12 +26,18 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import {mapState} from 'vuex'
|
import {mapState} from 'vuex'
|
||||||
|
import {logout} from '@/services'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'HeaderAvatar',
|
name: 'HeaderAvatar',
|
||||||
computed: {
|
computed: {
|
||||||
...mapState('setting', ['weekMode']),
|
|
||||||
...mapState('account', ['user']),
|
...mapState('account', ['user']),
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
logout() {
|
||||||
|
logout()
|
||||||
|
this.$router.push('/login')
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -21,6 +21,7 @@ Mock.mock('/login', 'post', ({body}) => {
|
|||||||
result.data = {}
|
result.data = {}
|
||||||
result.data.user = user
|
result.data.user = user
|
||||||
result.data.token = 'Authorization:' + Math.random()
|
result.data.token = 'Authorization:' + Math.random()
|
||||||
|
result.data.expireAt = new Date(new Date().getTime() + 30 * 60 * 1000)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
})
|
})
|
||||||
|
@ -110,9 +110,9 @@ export default {
|
|||||||
const result = res.data
|
const result = res.data
|
||||||
if (result.code >= 0) {
|
if (result.code >= 0) {
|
||||||
const user = result.data.user
|
const user = result.data.user
|
||||||
|
setAuthorization({token: result.data.token, expireAt: new Date(result.data.expireAt)})
|
||||||
this.$router.push('/dashboard/workplace')
|
this.$router.push('/dashboard/workplace')
|
||||||
this.$store.commit('account/setUser', user)
|
this.$store.commit('account/setUser', user)
|
||||||
setAuthorization({token: result.data.token})
|
|
||||||
this.$message.success(result.message, 3)
|
this.$message.success(result.message, 3)
|
||||||
} else {
|
} else {
|
||||||
this.error = result.message
|
this.error = result.message
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import Vue from 'vue'
|
import Vue from 'vue'
|
||||||
|
import {checkAuthorization} from '@/utils/request'
|
||||||
import Router from 'vue-router'
|
import Router from 'vue-router'
|
||||||
import PageView from '@/layouts/PageView'
|
import PageView from '@/layouts/PageView'
|
||||||
import BlankView from '@/layouts/BlankView'
|
import BlankView from '@/layouts/BlankView'
|
||||||
@ -7,7 +8,7 @@ import Login from '@/pages/login/Login'
|
|||||||
|
|
||||||
Vue.use(Router)
|
Vue.use(Router)
|
||||||
|
|
||||||
export default new Router({
|
const router = new Router({
|
||||||
routes: [
|
routes: [
|
||||||
{
|
{
|
||||||
path: '/login',
|
path: '/login',
|
||||||
@ -201,3 +202,14 @@ export default new Router({
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// 登录拦截
|
||||||
|
router.beforeEach((to, from, next) => {
|
||||||
|
if (to.path !== '/login' && !checkAuthorization()) {
|
||||||
|
next({path: '/login'})
|
||||||
|
} else {
|
||||||
|
next()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export default router
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import {login} from './user'
|
import {login, logout} from './user'
|
||||||
|
|
||||||
export {
|
export {
|
||||||
login
|
login,
|
||||||
|
logout
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {LOGIN} from '@/services/api'
|
import {LOGIN} from '@/services/api'
|
||||||
import {request, METHOD} from '@/utils/request'
|
import {request, METHOD, removeAuthorization} from '@/utils/request'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 登录服务
|
* 登录服务
|
||||||
@ -14,4 +14,11 @@ async function login(name, password) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export {login}
|
/**
|
||||||
|
* 退出登录
|
||||||
|
*/
|
||||||
|
function logout() {
|
||||||
|
removeAuthorization()
|
||||||
|
}
|
||||||
|
|
||||||
|
export {login, logout}
|
||||||
|
@ -43,13 +43,13 @@ async function request(url, method, params) {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 设置认证信息
|
* 设置认证信息
|
||||||
* @param token {Object}
|
* @param auth {Object}
|
||||||
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
* @param authType {AUTH_TYPE} 认证类型,默认:{AUTH_TYPE.BEARER}
|
||||||
*/
|
*/
|
||||||
function setAuthorization(auth, authType = AUTH_TYPE.BEARER) {
|
function setAuthorization(auth, authType = AUTH_TYPE.BEARER) {
|
||||||
switch (authType) {
|
switch (authType) {
|
||||||
case AUTH_TYPE.BEARER:
|
case AUTH_TYPE.BEARER:
|
||||||
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token)
|
Cookie.set(xsrfHeaderName, 'Bearer ' + auth.token, {expires: auth.expireAt})
|
||||||
break
|
break
|
||||||
case AUTH_TYPE.BASIC:
|
case AUTH_TYPE.BASIC:
|
||||||
case AUTH_TYPE.AUTH1:
|
case AUTH_TYPE.AUTH1:
|
||||||
@ -59,9 +59,49 @@ function setAuthorization(auth, authType = AUTH_TYPE.BEARER) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 移出认证信息
|
||||||
|
* @param authType {AUTH_TYPE} 认证类型
|
||||||
|
*/
|
||||||
|
function removeAuthorization(authType = AUTH_TYPE.BEARER) {
|
||||||
|
switch (authType) {
|
||||||
|
case AUTH_TYPE.BEARER:
|
||||||
|
Cookie.remove(xsrfHeaderName)
|
||||||
|
break
|
||||||
|
case AUTH_TYPE.BASIC:
|
||||||
|
case AUTH_TYPE.AUTH1:
|
||||||
|
case AUTH_TYPE.AUTH2:
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查认证信息
|
||||||
|
* @param authType
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
|
function checkAuthorization(authType = AUTH_TYPE.BEARER) {
|
||||||
|
switch (authType) {
|
||||||
|
case AUTH_TYPE.BEARER:
|
||||||
|
if (Cookie.get(xsrfHeaderName)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
break
|
||||||
|
case AUTH_TYPE.BASIC:
|
||||||
|
case AUTH_TYPE.AUTH1:
|
||||||
|
case AUTH_TYPE.AUTH2:
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
METHOD,
|
METHOD,
|
||||||
AUTH_TYPE,
|
AUTH_TYPE,
|
||||||
request,
|
request,
|
||||||
setAuthorization
|
setAuthorization,
|
||||||
|
removeAuthorization,
|
||||||
|
checkAuthorization
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user