import axios from 'axios'
import { Message, MessageBox } from 'element-ui'
import {requestUrl,homeUrl} from "./common.js"
// import store from '../store'
// import { getToken,get } from '@/utils/auth'

// 创建axios实例
const service = axios.create({
  baseURL: `${requestUrl}/apis`,
  timeout: 35000 // 请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
	let token=localStorage.getItem("token")
	if(token||token!=="undefined"){
	  config.headers['token']=token;
	}
	return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
service.interceptors.response.use(
  response => {
  /**
  * code为非200是抛错 可结合自己业务进行修改
  */
    const res = response
    if (res.status !== 200) {
      alert('接口错误')	
    }else {
      return res.data
    }
  },
  error => {
	  console.log(error.response.status)
    if(error.response.status == 401){
    	 Message.error("登陆失效,请重新登录!")
       localStorage.removeItem('token');
       setTimeout(function () {
		  location.href = `${homeUrl}/login/user/`;
       }, 2000);
    }
  }
)	
export default service
