axios添加公共参数

import qs from 'qs';
import axios from 'axios';

// 请求的拦截器
axios.interceptors.request.use(function (config) {
    const token = localStorage.getItem('token')
    const uid = localStorage.getItem('uid')
     // 判断请求的类型
     // 如果是post请求就把默认参数拼到data里面
     // 如果是get请求就拼到params里面
    if(config.method === 'post') {
        let data = qs.parse(config.data)

        config.data = qs.stringify({
            token: token,
            uid: uid,
            ...data
        })
    } else if(config.method === 'get') {
        config.params = {
            token: token,
            uid: uid,
            ...config.params
        }
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  })

猜你喜欢

转载自www.cnblogs.com/zwhbk/p/13201738.html