// 存储的token的KEY
export const TOKEN_KEY = 'token-user';
// 获取token 接口url
export const TOKEN_URL = '';
// 跳转登录地址
export const LOGIN_URL = '';
// 获取 token
export function getToken() {
const token = localStorage.getItem(TOKEN_KEY);
return token;
}
// 设置token
export function setToken(value) {
if (value) {
localStorage.setItem(TOKEN_KEY, value);
}
}
// 清除token
export function clearToken() {
localStorage.removeItem(TOKEN_KEY);
}
// 跳转到登录
export function toLogin() {
window.location.href = LOGIN_URL;
}
// 空值: [undefined, null, NaN, [], {}], 注意非空:0, false;
export function isEmpty(value) {
switch (Object.prototype.toString.call(value)) {
case '[object Undefined]':
// eslint-disable-next-line no-void
return value === void 0;
case '[object Null]':
return value === null;
case '[object Number]':
return isNaN(value);
case '[object String]':
return value === '';
case '[object Boolean]':
return false;
case '[object Object]':
return Object.keys(value).length === 0;
case '[object Array]':
return value.length === 0;
default:
return false;
}
}
// 拦截是否登录
const token = getToken();
if (isEmpty(token)) {
toLogin();
}
标签:case,存储,return,前端,object,value,token,export From: https://blog.51cto.com/u_15311558/7985365