axios的一种使用方式

Author Avatar ZTFtrue 发表于 • 2018年02月05日 10:21 • 共 • 265 • 次浏览
import axios from 'axios';
import { session } from '../libs/util.js';
let base = '/api/';
axios.defaults.baseURL = base;
// axios.defaults.timeout = 3000;
axios.interceptors.request.use(config => { // 通过创建实例好像会导致拦截无效
    let reg = new RegExp('(^|)csrfToken=([^;]*)(;|$)');
    let x = document.cookie.match(reg)[2];
    config.headers['test-token'] = x;// 添加属性test-token
    config.headers.authorization = session(); //添加headers.authorization
    return config;
}, error => { // 在此处可全局处理异常
    if (error.response) {
        return Promise.reject(error.response.status);
    } else {
        let a = -1;
        return Promise.reject(a);
    }
});
axios.interceptors.response.use(data => {
    if (data.headers.authorization) {
        session(data.headers.authorization);
    }
    return data;
}, error => { // 在此处可全局处理异常
    if (error.response) {
        return Promise.reject(error.response.status);
    } else {
        let a = -1;
        return Promise.reject(a);
    }
});

export const postRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: params,
        timeout: 5000,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};
export const uploadFileRequest = (url, params, source) => {
    return axios({
        method: 'post',
        url: `${url}`,
        data: params,
        timeout: 30000, //
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    }, {
        cancelToken: source.token
    });
};
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${url}`,
        data: params,
        timeout: 3000,
        transformRequest: [function (data) {
            let ret = '';
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&';
            }
            return ret;
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
        }
    });
};
export const deleteRequest = (url) => {
    return axios({
        method: 'delete',
        timeout: 3000,
        url: `${url}`
    });
};
export const getRequest = (url) => {
    return axios({
        method: 'get',
        timeout: 3000,
        url: `${url}`
    });
};
import CryptoJS from 'crypto-js';
import {postRequest, uploadFileRequest, getRequest} from './methods';
// base64混淆
 let words = CryptoJS.enc.Utf8.parse(password);
 let base64 = CryptoJS.enc.Base64.stringify(words);

post请求

export const submitReviewPost = function (name , pwd) { 
    let params = {
        'name': name,
        'pwd': pwd
    };
    return postRequest('/user/register/2', params);
};

文件上传

export const postImage = function (file, source) {
    let formData = new FormData(); // FormData形式上传
    formData.append('file', file);
    return uploadFileRequest('/upload', formData, source);
};
最后编辑于 • 2018年02月17日 21:16 •  

你尚未登录,无法进行回复。