import axios from 'axios'
import store from '../store'
const instance = axios.create({
baseURL: '/api',
timeout: 5000
});
// 添加请求拦截器
instance.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
const token = store.state.userToken.token
config.headers.token = token
return config
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
// 添加响应拦截器
instance.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
const http = {
get(url, data, config) {
return instance.get(url, { params: data }, config)
},
post(url, data, config) {
return instance.post(url, data, config)
},
put(url, data, config) {
return instance.put(url, data, config)
},
patch(url, data, config) {
return instance.put(url, data, config)
},
delete(url, data, config) {
return instance.delete(url, { data }, config)
}
}
export default http;
持久化token
const state = {
token: '',
departNum: 0
}
const actions = {}
const mutations = {
//改变部门数量
departNum(state, num) {
state.departNum = num
},
setToken(state, token) {
state.token = token
},
clearToken(state) {
state.token = ''
},
}
const getters = {}
export default {
namespaced: true,
state,
actions,
mutations,
getters
}
import { createStore } from "vuex"
import userToken from "./modules/userToken.js"
import createPersistedState from 'vuex-persistedstate'
const store = createStore({
modules: {
userToken
},
plugins: [createPersistedState({
paths: ['userToken.token','userToken.departNum']
})],
})
export default store