路由守卫 store 登录

router.beforeEach((to, from, next) =>  {
  if(to.path === "/login"){
    next()
  }else{
    let token = localStorage.getItem('Authorization');
    if(token === null || token === ""){
      next('/login')
    }else{
      next()
    }
  }
})

import Vue from ‘vue’
import Vuex from ‘vuex’

Vue.use(Vuex)

export default new Vuex.Store({
state: {
user:{},
// 存储token
Authorization: localStorage.getItem(‘Authorization’) ? localStorage.getItem(‘Authorization’) : ‘’

},
mutations: {
changeLogin(state,user){
state.Authorization =user.Authorization;
localStorage.setItem(‘Authorization’, user.Authorization)
}

},
actions: {
},
modules: {
},
getters:{//监听数据变化
getStorage(state){ // 获取本地存储的登录信息
if(!state.token){
state.token =JSON.parse(localStorage.getItem())
}
return state.token
}

}
})
// 存储localstorage
export const setStore = (key, data) => {
//存储之前先清除这样保持始终只有一个账户的信息
window.localStorage.clear();
//定义一个时间,用来设置过期则清除
let curTime = new Date().getTime();
//存储数据和存储时间
let setdata = {
data: data,
time: curTime,
};
//将数据存储为字符串类型
setdata = JSON.stringify(setdata)
window.localStorage.setItem(key, setdata)
// console.log(setdata)
}

//获取locastorage
export const getStore = () => {
// 对当前的localStorage进行遍历查找数据
if (localStorage.length > 0) {
for (let i = 0; i < localStorage.length; i++) {
let keyname = localStorage.key(i);
let str = localStorage.getItem(keyname);
str = JSON.parse(str)
//对时间进行比较看是否过期
let curTime = new Date().getTime();
let tiem = 7 * 24 * 60 * 60 * 1000;
if (curTime - str.time > tiem) {
window.localStorage.clear();
} else {
return str.data
}
}
}
}
import axios from “axios”;
import {
Message
} from “element-ui”;
import Vue from ‘vue’;
axios.defaults.timeout = 5000;
axios.defaults.baseURL = ‘http://192.168.3.164:xxx’;
const {
$message
} = Vue.prototype;
//http request 拦截器
axios.interceptors.request.use(
//在发送请求之前做了什么
config => {
console.log(config)
// if(localStorage.getItem(“token”)){
// config.headers.Authorization = localStorage.getItem(“token”);//把localStorage的token放在Authorization里
// }
// config.headers[‘x-token’] = localStorage.getItem(‘token’);
//const token = localStorage.getItem(‘token’)
// if (token) {
// token ? config.headers.Authorization = token : null;
// }
if(localStorage.getItem(‘Authorization’)){
config.headers.Authorization = localStorage.getItem(‘Authorization’);
}
return config;
},
error => {
return Promise.reject(error)
}

);
// http response 拦截器
// axios.interceptors.response.use(
// response => {
// if (response.data === 401) {
// if (response.data == 10001) {
// Message({
// message: ‘token不存在或已过期,请重新登录’,
// type: ‘warning’,
// duration: 2000
// });
// }
// this.$router.replace(’/login’);
// localStorage.removeItem(‘token’)
// }
// return response;
// },
// error => {
// Message({
// message: error.message,
// type: ‘error’,
// duration: 2000
// })
// return Promise.reject(error) // 返回接口返回的错误信息
// })
export function get(url, params = {}) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
})
.then(response => {
resolve(response.data);
})
.catch(err => {
reject(err)
})
})
}

export function post(url, data = {}) {
return new Promise((resolve, reject) => {
axios.post(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
export function put(url,data={}){
return new Promise ((resolve,reject)=>{
axios.put(url,data).then(response=>{
resolve(response.data)
},error=>{
reject(error)
})
})
}
// function http(method, url, data = {}) {
// let configData = {};
// let type = method === ‘get’ ? ‘params’ : ‘data’
// configData.method = method;
// configData.url = url;
// configData[type] = data;
// configData.headers = {
// versions: ‘1.0.0’,
// source: ‘’
// }
// }
// return axios(configData).catch(err => {
// if (err.response.status === 500) return $message.error(‘服务器错误’)
// if (err.response.status === 404) return $message.error(‘接口不存在’)
// })

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

故事只若初见

坚持就是胜利

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值