/*
 * @Description: 
 * @Author: 欧阳承珺
 * @LastEditors: 欧阳承珺
 * @Date: 2022-10-19 13:58:11
 * @LastEditTime: 2022-11-03 15:36:24
 */
import { Message } from 'element-ui';

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','')
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      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
    }
  })

}