记录一下axios封装

125 阅读1分钟

新建一个request.js

/****   request.js   ****/
// 导入axios
import axios from 'axios'
import qs from 'qs'
import {
	Toast
} from 'vant';
// import CryptoJs from 'crypto-js'
 


 
const service = axios.create({
	 
	baseURL: 'http://10.143.138.113:8080',//process.env.BASE_API,
	// 超时时间 单位是ms,这里设置了3s的超时时间
	timeout: 300 * 1000
})
// 2.请求拦截器
service.interceptors.request.use(config => {
	 
		
		if (config.method === 'get') {
			
			config.paramsSerializer = function(params) {
				return qs.stringify(params, {
					arrayFormat: 'comma'
				})
			}
		}
		
 
	return config
}, error => {
	Promise.reject(error)
})

// response interceptor
service.interceptors.response.use((config) => {
	return config
}, (error) => {
	 
	if (error.response) {
		const errorMessage = error.response.data === null ? '系统内部异常,请联系网站管理员' : error.response.data.message
		switch (error.response.status) {
			case 404:
				Toast('很抱歉,资源未找到')

				break
			case 403:
				Toast('很抱歉,您暂无该操作权限')


				break
			case 401:
				Toast('很抱歉,认证已失效,请重新登录')
				let aid=getQueryVariable('corpId')
				localStorage.removeItem(aid)
				// alert(aid)

				break
			default:
				if (errorMessage === 'refresh token无效') {
					Toast('登录已过期,请重新登录')
						let aid=getQueryVariable('corpId')
						localStorage.removeItem(aid)

				} else {
					Toast(errorMessage)


				}
				break
		}
	}
	return Promise.reject(error)
})

export default service

http.js

/****   http.js   ****/
// 导入封装好的axios实例
import request from './request'
import qs from 'qs'
const http ={
    /**
     * methods: 请求
     * @param url 请求地址 
     * @param params 请求参数
     */
    get(url,params){
        const config = {
            method: 'get',
            url:url
        }
        if(params) config.params = params
        return request(config)
    },
    post(url,params){
        const config = {
            method: 'post',
            url:url
        }
        if(params) config.data = params
        return request(config)
    },
	postUncode(url,params){
		const config = {
		    method: 'post',
		    url:url,
			headers:{
			    'Content-Type': 'application/x-www-form-urlencoded'
			}
		}
		
		if(params) config.data = qs.stringify(params)
		
		return request(config)
	},
	upFile(url,data){
		return request({
			method: 'post',
			url: url,
			data: data,
			headers:{
					"Content-Type": "multipart/form-data"
				}
		}) 
	},
    put(url,params){
        const config = {
            method: 'put',
            url:url
        }
        if(params) config.params = params
        return request(config)
    },
    delete(url,params){
        const config = {
            method: 'delete',
            url:url
        }
        if(params) config.params = params
        return request(config)
    }
}
//导出
export default http

api.js

import http from './http.js'

 
export default{
	// 文件上传
	uploadFile(param){
		return http.upFile('http://mz.xxxxmaterial/uploadAll',param)
	},
	 
	isWorker(param){
		return http.get(`/path`,param)
	},
      }