request.js 3 KB
Newer Older
潘琦 committed
1
import axios from 'axios'
潘琦 committed
2
import store from '../store'
潘琦 committed
3
import { Toast } from 'mint-ui'
4
// import router from '@/router/index'
潘琦 committed
5
import baseUrl from '@/util/baseUrl'
潘琦 committed
6
import { removeUrlParam } from '@/util/index'
潘琦 committed
7

潘琦 committed
8 9 10 11 12 13 14 15 16 17 18
// 创建axios实例
const service = axios.create({
  baseURL: baseUrl.voucherUrl,
  timeout: 30000 // 请求超时时间
})

// console.log('NODE_ENV' + process.env.NODE_ENV)
// if (process.env.NODE_ENV === 'production') {
//   // let root = process.env.API_ROOT
//   axios.defaults.baseURL = baseUrl.voucherUrl // 开发服
// }
潘琦 committed
19
// 返回其他状态吗
潘琦 committed
20
service.defaults.validateStatus = function (status) {
潘琦 committed
21 22 23
  return status // 默认的
}
// 跨域请求,允许保存cookie
潘琦 committed
24
service.defaults.withCredentials = true
潘琦 committed
25
// request拦截器
潘琦 committed
26
service.interceptors.request.use(config => {
潘琦 committed
27
  let token = store.getters.access_token
潘琦 committed
28 29 30 31 32 33 34 35 36 37 38
  if (token) {
    config.headers['Authorization'] = 'Bearer ' + token
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})

// respone拦截器
潘琦 committed
39
service.interceptors.response.use(response => {
40 41 42 43 44
  let res = response
  if (response.request) {
    res = response.data
  }
  // const res = response.data && (response.data.code === 0 || response.data.code === 200) ? response.data : response.data
潘琦 committed
45 46 47 48 49 50 51 52 53
  const status = Number(response.status) || 200
  console.log('response status:' + status)
  if (status !== 200) {
    if (status === 401) {
      Toast({
        message: '您因长时间逗留,验证已过期,正在刷新!',
        position: 'center',
        duration: 5 * 1000
      })
潘琦 committed
54 55 56
      store.dispatch('setToken', '')
      store.dispatch('setUserId', '')
      store.dispatch('setCode', '')
潘琦 committed
57
      setTimeout(function () {
潘琦 committed
58 59
        window.location.href = removeUrlParam('code')
      }, 4 * 1000)
潘琦 committed
60 61
    } else if (status === 403) {
      if (res.msg === '请先进行实名认证') { // 跳转至社区推荐页
62 63 64 65
        Toast({
          message: '您还未实名认证,进入实名认证页面中...',
          position: 'center',
          duration: 5 * 1000
潘琦 committed
66
        })
67 68 69 70
        setTimeout(() => {
          // router.push({
          //   path: '/recommend'
          // })
潘琦 committed
71
          window.location.href = 'http://' + location.host + '/voucher/#/recommend/list'
72
        }, 4 * 1000)
潘琦 committed
73
      }
74 75
    } else if (status === 404) {
      Toast({
潘琦 committed
76
        message: res.error,
77 78 79
        position: 'center',
        duration: 3 * 1000
      })
潘琦 committed
80
    } else {
潘琦 committed
81 82 83 84 85 86 87 88 89 90 91 92
      let message = 'Error:400'
      if (res.code && res.code === 1) {
        message = res.msg
      } else {
        message = res.message
      }
      Toast({
        message: message,
        position: 'center',
        duration: 3 * 1000
      })
      return Promise.reject(new Error(res.message))
潘琦 committed
93
    }
潘琦 committed
94 95 96 97 98
  } else if (res.code === 1) {
    if (!res.msg) {
      res.msg = '内部服务器错误[500]'
    }
    return Promise.reject(new Error(res.msg))
潘琦 committed
99 100 101 102 103 104 105 106
  }

  return res
}, error => {
  console.log('错误' + error)// for debug
  return Promise.reject(new Error(error))
})

潘琦 committed
107
export default service