admin 管理员组文章数量: 1184232
1、 登录时设置cookie标志位
try {
const result = await login({
account: values.mobile,
password: shajs('sha1').update(values.password).digest('hex'),
verifyCode: values.code,
randomNum: this.uuidNumber })
this.$ls.set(ACCESS_TOKEN, result.data.access_token, 24 * 60 * 60 * 1000)
this.$ls.set(MEMBER_ID, result.data.member_id, 24 * 60 * 60 * 1000)
this.$ls.on(ACCESS_TOKEN, (val, oldVal, uri) => {
if (oldVal) {
setTimeout(() => {
window.location.reload()
}, 1500)
}
})
if (result.data.corp_id) {
this.$ls.set(CORP_ID, result.data.corp_id, 24 * 60 * 60 * 1000)
} else {
this.$ls.remove(CORP_ID)
}
this.$router.push({ path: '/' })
this.$message.success('登录成功')
// 添加cookie监测登陆状态
document.cookie = 'is_online=yes; path=/; ' + 'expires=' + -1
}
2、每次进入时在入口文件App.vue监测is_online是否存在。
// 退出浏览器或关闭窗口自动重置登陆状态
beforeUpdate () {
this.cleanCookie()
},
mounted () {
this.cleanCookie()
},
methods: {
cleanCookie () {
if (document.cookie.indexOf('is_online') === -1) {
localStorage.removeItem('pro__Access-Token')
}
}
}
【解释】
3、只要每次是从登陆入口进来的都是有cookie标志位的,没有标识位说明没走登录流程;
4、Expires,该属性用来设置Cookie的有效期。Cookie中的maxAge用来表示该属性,单位为秒。
将maxAge设置为负数,Cookie仍然会存在一段时间直到关闭浏览器或者重新打开浏览器。
版权声明:本文标题:关闭浏览器时实现用户登出(关闭页面时不登出)——全网最有用 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1766357659a3452259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论