TOKEN 从localStorage移至cookie
This commit is contained in:
parent
8c6ad81219
commit
0ace9a1c0e
|
@ -36,7 +36,7 @@ router.beforeEach(async (to, from, next) => {
|
||||||
//动态标题
|
//动态标题
|
||||||
document.title = to.meta.title ? `${to.meta.title} - ${config.APP_NAME}` : `${config.APP_NAME}`
|
document.title = to.meta.title ? `${to.meta.title} - ${config.APP_NAME}` : `${config.APP_NAME}`
|
||||||
|
|
||||||
let token = tool.data.get("TOKEN");
|
let token = tool.cookie.get("TOKEN");
|
||||||
|
|
||||||
if(to.path === "/login"){
|
if(to.path === "/login"){
|
||||||
//删除路由(替换当前layout路由)
|
//删除路由(替换当前layout路由)
|
||||||
|
|
|
@ -11,7 +11,7 @@ axios.defaults.timeout = sysConfig.TIMEOUT
|
||||||
// HTTP request 拦截器
|
// HTTP request 拦截器
|
||||||
axios.interceptors.request.use(
|
axios.interceptors.request.use(
|
||||||
(config) => {
|
(config) => {
|
||||||
let token = tool.data.get("TOKEN");
|
let token = tool.cookie.get("TOKEN");
|
||||||
if(token){
|
if(token){
|
||||||
config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token
|
config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/*
|
/*
|
||||||
* @Descripttion: 工具集
|
* @Descripttion: 工具集
|
||||||
* @version: 1.1
|
* @version: 1.2
|
||||||
* @LastEditors: sakuya
|
* @LastEditors: sakuya
|
||||||
* @LastEditTime: 2021年7月20日10:58:41
|
* @LastEditTime: 2022年5月24日00:28:56
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import CryptoJS from 'crypto-js';
|
import CryptoJS from 'crypto-js';
|
||||||
|
@ -11,44 +11,34 @@ const tool = {}
|
||||||
|
|
||||||
/* localStorage */
|
/* localStorage */
|
||||||
tool.data = {
|
tool.data = {
|
||||||
/**
|
set(key, data, datetime = 0) {
|
||||||
* 设置缓存
|
let cacheValue = {
|
||||||
* @param {*} key
|
content: data,
|
||||||
* @param {*} data
|
datetime: parseInt(datetime) === 0 ? 0 : new Date().getTime() + parseInt(datetime) * 1000
|
||||||
* @param {*} time 过期时间 单位秒 (默认:0 不过期)
|
}
|
||||||
* @returns
|
return localStorage.setItem(key, JSON.stringify(cacheValue))
|
||||||
*/
|
|
||||||
set(key, data, time = 0) {
|
|
||||||
let cacheVal = {
|
|
||||||
val: data,
|
|
||||||
time: parseInt(time) === 0 ? 0 : parseInt(time) * 1000 + new Date().getTime()
|
|
||||||
};
|
|
||||||
return localStorage.setItem(key, JSON.stringify(cacheVal));
|
|
||||||
},
|
},
|
||||||
get(key) {
|
get(key) {
|
||||||
try {
|
try {
|
||||||
const value = JSON.parse(localStorage.getItem(key))
|
const value = JSON.parse(localStorage.getItem(key))
|
||||||
if (value) {
|
if (value) {
|
||||||
let now = new Date().getTime();
|
let nowTime = new Date().getTime()
|
||||||
if (now > value.time && value.time != 0) {//缓存过期
|
if (nowTime > value.datetime && value.datetime != 0) {
|
||||||
localStorage.removeItem(key)
|
localStorage.removeItem(key)
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return value.val
|
return value.content
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
} catch (e) {
|
} catch (err) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
del (key) {
|
remove(key) {
|
||||||
return localStorage.removeItem(key);
|
return localStorage.removeItem(key)
|
||||||
},
|
|
||||||
remove(table) {
|
|
||||||
return localStorage.removeItem(table);
|
|
||||||
},
|
},
|
||||||
clear() {
|
clear() {
|
||||||
return localStorage.clear();
|
return localStorage.clear()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,6 +65,46 @@ tool.session = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*cookie*/
|
||||||
|
tool.cookie = {
|
||||||
|
set(name, value, config={}) {
|
||||||
|
var cfg = {
|
||||||
|
expires: null,
|
||||||
|
path: null,
|
||||||
|
domain: null,
|
||||||
|
secure: false,
|
||||||
|
httpOnly: false,
|
||||||
|
...config
|
||||||
|
}
|
||||||
|
var cookieStr = `${name}=${escape(value)}`
|
||||||
|
if(cfg.expires){
|
||||||
|
var exp = new Date()
|
||||||
|
exp.setTime(exp.getTime() + parseInt(cfg.expires) * 1000)
|
||||||
|
cookieStr += `;expires=${exp.toGMTString()}`
|
||||||
|
}
|
||||||
|
if(cfg.path){
|
||||||
|
cookieStr += `;path=${cfg.path}`
|
||||||
|
}
|
||||||
|
if(cfg.domain){
|
||||||
|
cookieStr += `;domain=${cfg.domain}`
|
||||||
|
}
|
||||||
|
document.cookie = cookieStr
|
||||||
|
},
|
||||||
|
get(name){
|
||||||
|
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"))
|
||||||
|
if(arr != null){
|
||||||
|
return unescape(arr[2])
|
||||||
|
}else{
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
},
|
||||||
|
remove(name){
|
||||||
|
var exp = new Date()
|
||||||
|
exp.setTime(exp.getTime() - 1)
|
||||||
|
document.cookie = `${name}=;expires=${exp.toGMTString()}`
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Fullscreen */
|
/* Fullscreen */
|
||||||
tool.screen = function (element) {
|
tool.screen = function (element) {
|
||||||
var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
|
var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement);
|
||||||
|
|
|
@ -169,7 +169,9 @@
|
||||||
//获取token
|
//获取token
|
||||||
var user = await this.$API.auth.token.post(data)
|
var user = await this.$API.auth.token.post(data)
|
||||||
if(user.code == 200){
|
if(user.code == 200){
|
||||||
this.$TOOL.data.set("TOKEN", user.data.token)
|
this.$TOOL.cookie.set("TOKEN", user.data.token, {
|
||||||
|
expires: this.ruleForm.autologin? 24*60*60 : 0
|
||||||
|
})
|
||||||
this.$TOOL.data.set("USER_INFO", user.data.userInfo)
|
this.$TOOL.data.set("USER_INFO", user.data.userInfo)
|
||||||
}else{
|
}else{
|
||||||
this.islogin = false
|
this.islogin = false
|
||||||
|
|
Loading…
Reference in New Issue