DDR爱好者之家 Design By 杰米
最近接手的B端项目选择了vue来做,此项目使用element ui Message等为组件 望周知
需求
- 登陆成功后跳转至首页
- 首页不能手动跳转至登陆页
- 登陆后跳转至目标页面
此次B端SPA项目把ak存在localstorage中
1.登陆的跳转利用全局钩子router.beforeEach
//router.js
router.beforeEach((to, from, next) => {
// 若userkey不存在并且前往页面不是登陆页面,进入登陆
// 若userkey存在并且前往登陆页面,进入主页
const userKey = localStorage.getItem('userKey')
if (!userKey && to.path !== '/login') {
next({
path: '/login',
query: { redirect: to.fullPath }
})
} else if (userKey && to.path === '/login') {
next({ path: '/' })
} else {
next()
}
})
上面使用了query带上目标参数
例子:#/login"htmlcode">
//若验证成功跳转
var redirect = decodeURIComponent(this.$route.query.redirect || '/')
self.$router.push({
// 你需要接受路由的参数再跳转
path: redirect
})
需求
若ak失效后发送请求时弹出失效弹出框返回到登陆页面
以下做了个简单的例子若请求返回的参数带0则登陆失效
// respone拦截器
axios.interceptors.response.use(
response => {
console.log(response)
const data = response.data
if (data.status === 0) {
MessageBox.alert('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
confirmButtonText: '确定',
type: 'warning'
}).then(() => {
localStorage.clear()
router.replace({
path: '/login'
})
return
}).catch(() => {
localStorage.clear()
router.replace({
path: '/login'
})
})
} else {
return response
}
},
error => {
if (error && error.response) {
switch (error.response.status) {
case 400:
error.message = '请求错误'
break
case 401:
error.message = '未授权,请登录'
break
case 403:
error.message = '拒绝访问'
break
case 404:
error.message = (process.env.NODE_ENV === 'production' "htmlcode">
loginOut() {
var self = this
this.$confirm('您确定要退出吗?', '退出管理平台', {
confirmButtonText: '确定',
cancelButtonText: '取消'
}).then(() => {
const info = {
'userkey': localStorage.getItem('userKey')
}
self.$store.dispatch('LogOut', info).then(() => {
self.$router.push({ path: '/login' })
}).catch(() => {
})
}).catch(() => {
})
}
简单的登陆登出结束啦~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
更新日志
2025年10月24日
2025年10月24日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]