我通过身份验证获得菜单操作。
async getMenu({rootState, commit}) {
try {
const { auth, lang } = rootState;
const {items} = await this.$axios.$get(`/api/${ lang.locale }/menu`, {
headers: {
'Authorization': `bearer ${auth.token}`,
'Accept-Language': `${lang.locale}`
},
});
if (items) {
// set items
commit('setMenu', items);
}
} catch (error) {
console.log({Error: error})
}
}
如果发生错误,我将提交带有刷新令牌的请求
$axios.onError(async (error) => {
const code = parseInt(error.response && error.response.status);
const message = error.response && error.response.data && error.response.data.error;
if (code === 403) {
await store.dispatch(
'auth/refreshToken',
{ refreshToken: store.state.auth.refreshToken },
{ root: true }
);
获得令牌后如何重复操作?
使用
$axios.defaults.validateStatus = (status) => {
return [200, 402].indexOf(status) !== -1;
};
$axios.onResponse(async (response) => {...}