美文网首页小知识前端
react中使用axios拦截并处理不同http状态码

react中使用axios拦截并处理不同http状态码

作者: imxiaochao | 来源:发表于2018-09-26 09:56 被阅读4279次

在react和vue都一般都会使用一个全局的request方法进行http请求
在该方法中需要对200之外的状态码进行单独处理

参考ant pro项目中代码 改写的axios配置

状态码设置

// axiosSetting.js
import axios from 'axios'
import { message } from 'antd'
import { routerRedux} from 'dva/router'
import { getToken } from './auth'
import store from '../index'

axios.defaults.withCredentials = true
axios.defaults.timeout = 10000

const codeMessage = {
  200: '服务器成功返回请求的数据。',
  201: '新建或修改数据成功。',
  202: '一个请求已经进入后台排队(异步任务)。',
  204: '删除数据成功。',
  400: '发出的请求有错误,服务器没有进行新建或修改数据的操作。',
  401: '用户没有权限(令牌、用户名、密码错误)。',
  403: '用户得到授权,但是访问是被禁止的。',
  404: '发出的请求针对的是不存在的记录,服务器没有进行操作。',
  406: '请求的格式不可得。',
  410: '请求的资源被永久删除,且不会再得到的。',
  422: '当创建一个对象时,发生一个验证错误。',
  500: '服务器发生错误,请检查服务器。',
  502: '网关错误。',
  503: '服务不可用,服务器暂时过载或维护。',
  504: '网关超时。',
}

axios拦截

// axiosSetting.js
// request拦截器
axios.interceptors.request.use(
    config => {
        // Do something before request is sent
        const token = getToken()
        // console.log(window.location.href.indexOf('/user/login') > -1)
        if(config.url.indexOf('/auth/oauth/token') > 0){
            config.headers.Authorization = 'Basic dnVlOnZ1ZQ==' // 增加客户端认证
        }else{
            config.headers.Authorization = token // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
        }
        const {url} = config
        if(/^\/api\//.test(url) && !token && !window.location.href.indexOf('user') > -1){
            const { dispatch } = store
            dispatch(routerRedux.replace('/user/login')) // 跳转到登录页
        }
        return config
    },
    error => {
        // Do something with request error
        console.log(error) // for debug
        Promise.reject(error)
    }
)
// respone拦截器
axios.interceptors.response.use(
    response => {
        /**
         * 下面的注释为通过response自定义code来标示请求状态,当code返回如下情况为权限有问题,登出并返回到登录页
         * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
         */
        const res = response.data
        // if (response.status === 401 || res.status === 40101) {
        //   MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
        //     confirmButtonText: '重新登录',
        //     cancelButtonText: '取消',
        //     type: 'warning'
        //   }).then(() => {
        //     store.dispatch('LogOut').then(() => {
        //       location.reload() // 为了重新实例化vue-router对象 避免bug
        //     })
        //   })
        //   return Promise.reject('error')
        // }
        // if (res.status === 30101) {
        //   Message({
        //     message: res.message,
        //     type: 'error',
        //     duration: 5 * 1000
        //   })
        //   return Promise.reject('error')
        // }
        // if (res.status === 40301) {
        //   Message({
        //     message: '当前用户无相关操作权限!',
        //     type: 'error',
        //     duration: 5 * 1000
        //   })
        //   return Promise.reject('error')
        // }
        if (response.status !== 200 && res.status !== 200) {
            message.error(response.data.message)
            // notification.error({
            //     message: res.status,
            //     description: res.message,
            // })
        } else {
            return response.data
        }
    },
    error => {
        // console.log(JSON.stringify(error)) // for debug
        if (error === undefined || error.code === 'ECONNABORTED') {
            message.warning('服务请求超时')
            return Promise.reject(error)
        }
        const { response: { status, statusText, data: { msg = '服务器发生错误' } }} = error
        const { response } = error
        const { dispatch } = store
        const text = codeMessage[status] || statusText || msg

        if (status === 400) {
            // message.warning('账户或密码错误!')
            dispatch(routerRedux.push('/user/login'))
        }
        const info = response.data
        if (status === 401 || info.status === 40101) {
            dispatch({
                type: 'login/logout',
            })
            // MessageBox.confirm('你已被登出,可以取消继续留在该页面,或者重新登录', '确定登出', {
            //     confirmButtonText: '重新登录',
            //     cancelButtonText: '取消',
            //     type: 'warning',
            // }).then(() => {
            //     store.dispatch('LogOut').then(() => {
            //         location.reload() // 为了重新实例化vue-router对象 避免bug
            //     })
            // })
        }
        if (status === 403) {
            dispatch(routerRedux.push('/exception/403'))
            // Notification.warning({
            //     title: '禁止',
            //     message: info.message,
            //     type: 'error',
            //     duration: 2 * 1000,
            // })
        }
        if (info.status === 30101) {
            dispatch(routerRedux.push('/exception/500'))
            // Notification.warning({
            //     title: '失败',
            //     message: info.message,
            //     type: 'error',
            //     duration: 2 * 1000,
            // })
        }
        if (response.status === 504) {
            dispatch(routerRedux.push('/exception/500'))
            // Message({
            //     message: '后端服务异常,请联系管理员!',
            //     type: 'error',
            //     duration: 5 * 1000,
            // })
        }
        message.error(`${status}:${text}`)
        // throw error
        // return error
        return Promise.reject(error)
    }
)

调用并导出axios

// request.js
import axios from 'axios'
import { merge } from 'lodash'

import './axiosSetting'

const request = async (_options) => {
    // 默认GET方法
    const method = _options.method || 'GET'
    const options = merge(
        { ..._options },
        {
            method,
        }
    )
    return axios(options)
}

/**
 * 封装get请求
 * @param { String } url 请求路径
 * @param { Object } 请求参数
 *  params GET请求参数
 */
const get = (url, params, _options) => {
    return request({ ..._options, params, url })
}
/**
   * 封装post请求
   * @param { Object } 请求参数
   *  data POST请求请求参数,对象形式
   */
const post = (url, data, _options) => {
    return request({ ..._options, data, url }, 'POST')
}
  
export { get, post }
export default request

相关文章

  • react中使用axios拦截并处理不同http状态码

    在react和vue都一般都会使用一个全局的request方法进行http请求在该方法中需要对200之外的状态码进...

  • axios 简单化

    为什么选择axios? 1.使用axios可以统一做请求-响应拦截,例如响应时我们将响应信息拦截起来,判断状态码,...

  • refresh_token代码实现简图

    token 失效的 refresh_token 使用机制 axios 响应拦截器 的错误处理函数中,如果 响应状态...

  • vue的axios拦截器使用

    axios拦截器 下载并使用axios后可以对全局进行拦截器设置。拦截器在发送请求前或响应返回时做一些特殊的处理。...

  • 是否应当在axios的响应拦截器里设置弹出toast?

    前言 通常,我们在axios的响应拦截器里写的最多的就是根据HTTP状态码来做出对应的响应,最常见的就是状态码为5...

  • axios 响应拦截器,状态码判断

    // axios 响应拦截器,状态码判断 axiosIns.interceptors.response.use(f...

  • axios的封装使用

    axios是经常使用到的网络请求库,因此我们需要简单的封装一下。主要的作用就是统一http状态码处理,统一错误处理...

  • React 使用 Axios

    Axios 如何发送, 端口不一致, 使用 proxy 配置转发 axios 拦截器, 同意 loading 处理...

  • HTTP协议状态码

    HTTP协议的状态码 使用php或者javascript都会用到http的不同状态,一些常见的状态码为: 200 ...

  • axios拦截器

    为什么要使用axios拦截器? 页面发送http请求,很多情况我们要对请求和其响应进行特定的处理;例如每个请求都附...

网友评论

    本文标题:react中使用axios拦截并处理不同http状态码

    本文链接:https://www.haomeiwen.com/subject/tduloftx.html