1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /*
- * @Description:
- * @Author: 欧阳承珺
- * @LastEditors: 欧阳承珺
- * @Date: 2022-10-19 13:58:11
- * @LastEditTime: 2022-11-16 10:25:06
- */
- import { Message } from 'element-ui';
- import { removeToken } from '~/utils/cookie';
- export default function({store, app:{ $axios }, redirect} ) {
- $axios.onRequest(config => {
- $axios.setToken(store.state.token, 'Bearer')
- })
- $axios.onError(error => {
- const code = parseInt(error.response && error.response.status)
- if(code === 401) {
- redirect('/')
- Message.error('登录过期,请重新登录')
- store.commit('OPEN_LOGIN',false)
- store.commit('DEL_TOKEN','')
- store.commit('setUser', {})
- removeToken('Token')
- localStorage.removeItem('token')
- return Promise.resolve(false)
- }
- if (code !== 200) {
- Message.error(error.response?.data?.msg)
- }
- })
- $axios.onResponse((res) => {
- if(res?.config?.url === '/auth/getImageCode') {
- return res
- }else if(res.data && res?.data?.code !== 200) {
- Message.error(res.data.msg)
- }else {
- return res.data
- }
- })
- }
|