setCookie (name, value, second) {
var timestr = ''
if (second > 0) {
var exp = new Date()
exp.setTime(exp.getTime() + 1000 * second)
timestr = ';expires=' + exp.toGMTString()
}
document.cookie = name + '=' + escape(value) + timestr
},
getCookie (name) {
var arrt = document.cookie.match(new RegExp('(^| )' + name + '=([^;]*)(;|$)'))
if (arrt) {
return unescape(arrt[2])
} else {
return null
}
},
clearCookie (cname) {
var cd = new Date()
cd.setTime(cd.getTime() - 1)
document.cookie = cname + '=-1;expires=' + cd.toUTCString()
// window.document.cookie = cname + '=del;path=/;expires=-1'
}
可用于判断是否登录
rt.beforeEach((to, from, next) => {
NProgress.start()
if (to.meta.title) {
document.title = to.meta.title
}
// 判断是否有Cookie
var iflg = utils.getCookie(conf.fmCookieName)
if (to.matched.some(record => record.meta.requireLogin)) {
if (iflg === null) {
Notification.warning({
title: '提示',
message: '请先登录'
})
next('/login')
NProgress.done()
} else {
next()
}
} else {
if (iflg !== null) {
next('/welcome')
NProgress.done()
} else {
next()
}
}
})
网友评论