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);
  })

 

posted @ 2020-06-28 10:51  五环  阅读(3124)  评论(0编辑  收藏  举报