基于vue、Element-UI的后台管理系统登录页的登录状态保存 通过这篇清晰思路以及掌握探索方法才是最重要的欧
1. 基础信息
cookie,session,token的区别 Vue Router
2. 思考过程
2.1 利用token登录状态保存思路
2.2 确定登录逻辑所在位置
router/index.js👇【页面path情况】 👇当我们只是复制黄色框框部分网址到新开浏览器时,会发现黄色框框会再去自动补齐后面部分网址。由此可推测,在这个过程中其实是存在一个逻辑的判断。那么同样,我们则需要找到该处逻辑位置加入对token的判断。 👇找到逻辑位置
**注意**
router.beforeEach(async(to, from, next) => {
NProgress.start()
document.title = getPageTitle(to.meta.title)
const hasToken = getToken()
if (hasToken) {
if (to.path === '/login') {
console.log(to.path)
next({ path: '/' })
NProgress.done()
} else {
const hasGetUserInfo = store.getters.nickName
if (hasGetUserInfo) {
next()
NProgress.done()
} else {
try {
await store.dispatch('user/login')
next()
} catch (error) {
await store.dispatch('user/resetToken')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
👇解析
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next(`/login?redirect=${to.path}`)
说明:NProgress是一个放在浏览器顶部的进度条
👇测试结果【登录进入前】 1.当首次登录时,用户未带token,进入登录页
- 未使用手机验证码登录时整体的跳转过程
👇测试结果【登录进入后】
1.首次通过验证登录进入
来源 👇login/index.vue
handleLogin() {
this.$refs.loginForm.validate(valid => {
if (valid) {
this.loading = true
this.$store.dispatch('user/login', this.loginForm).then(() => {
console.log('success login')
this.$router.push({
path: this.redirect || '/'
})
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
},
这一次因为nickname不再为空,因此路由拦截会使得页面也依旧保留在首页/dashboard 3. 复制url进入会需要重新登录,并且在登录页面刷新,打印会修改为
if (hasToken) {
console.log(hasToken)
if (to.path === '/login') {
console.log('司六')
console.log(to.path)
next({ path: '/' })
NProgress.done()
} else {
console.log('司六else')
console.log(from.path)
console.log(to.path)
const hasGetUserInfo = store.getters.nickName
console.log(hasGetUserInfo)
if (hasGetUserInfo) {
next()
console.log(to.path)
NProgress.done()
} else {
try {
console.log('1')
await store.dispatch('user/login')
next()
} catch (error) {
console.log('2')
await store.dispatch('user/resetToken')
console.log(error)
console.log(to.path)
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
console.info('米希')
console.info(to.path)
if (whiteList.indexOf(to.path) !== -1) {
next()
} else {
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
})
👇找到页面跳转的逻辑所在后,我们需要寻找token的定义处 【store/modules/user.js👇】
import {
login,
logout
} from '@/api/user'
import {
getToken,
setToken,
removeToken
} from '@/utils/auth'
import {
resetRouter
} from '@/router'
const getDefaultState = () => {
return {
token: getToken(),
nickName: '',
headPortrait: ''
}
}
const state = getDefaultState()
const mutations = {
RESET_STATE: (state) => {
Object.assign(state, getDefaultState())
},
SET_TOKEN: (state, token) => {
state.token = token
'注意'
console.log('3state.token处')
console.log(state.token)
},
SET_nickName: (state, nickName) => {
state.nickName = nickName
},
SET_headPortrait: (state, headPortrait) => {
state.headPortrait = headPortrait
}
}
const actions = {
login({
commit
}, userInfo) {
const {
phoneNumber,
code
} = userInfo
return new Promise((resolve, reject) => {
login({
phoneNumber: phoneNumber.trim(),
code: code
}).then(response => {
if (!response) {
return reject('Verification failed, please Login again.')
}
const {
token,
nickName,
headPortrait
} = response.data
commit('SET_TOKEN', token)
setToken(token)
commit('SET_nickName', nickName)
commit('SET_headPortrait', headPortrait)
resolve()
}).catch(error => {
reject(error)
})
})
},
'注意'
resetToken({
commit
}) {
return new Promise(resolve => {
removeToken()
commit('RESET_STATE')
resolve()
})
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
2.3目标
首次登录后,可以再利用url携带token时直接进入首页
|