封装aixos拦截器
import axios from 'axios'; import router from 'vue-router' import { Message } from 'element-ui' // axios.defaults.timeout = 5000; // axios.defaults.baseURL = 'http://10.128.128.124:5000' // axios.defaults.baseURL = 'http://10.128.128.118:8086' // axios.defaults.baseURL = 'http://wxmini.nat300.top' // axios.defaults.baseURL = 'http://t200shool.zhengtoon.com/edu_order' //测试环境 axios.defaults.baseURL = 'http://jyfwyy.efzhou.com/api' // 正式环境地址 //http request 拦截器 axios.interceptors.request.use( config => { // const token = getCookie('名称');注意使用的时候需要引入cookie方法,推荐js-cookie config.data = JSON.stringify(config.data); config.headers['Content-Type'] = 'application/json;charset=UTF-8' return config; }, error => { return Promise.reject(error) } ); //http response 拦截器 axios.interceptors.response.use( response => { // 请求错误提示语 if (response.data.meta.code != 0) { Message({ showClose: true, message: response.data.meta.message, type: 'warning' }); } // if (response.data.errCode == 2) { // router.push({ // path: "/", // query: { // redirect: router.currentRoute.fullPath // } //从哪个页面跳转 // }) // } return response; }, error => { this.$message.error('抱歉!请求失败...'); return Promise.reject(error) } ) /** * 封装get方法 * @param url * @param data * @returns {Promise} */ export function fetch(url, params = {}) { return new Promise((resolve, reject) => { axios.get(url, { params: params }) .then(response => { resolve(response.data); }) .catch(err => { reject(err) }) }) } /** * 封装post请求 * @param url * @param data * @returns {Promise} */ export function post(url, data = {}) { return new Promise((resolve, reject) => { axios.post(url, data) .then(response => { resolve(response.data); }, err => { reject(err) }) }) } /** * 封装patch请求 * @param url * @param data * @returns {Promise} */ export function patch(url, data = {}) { return new Promise((resolve, reject) => { axios.patch(url, data) .then(response => { resolve(response.data); }, err => { reject(err) }) }) } /** * 封装put请求 * @param url * @param data * @returns {Promise} */ export function put(url, data = {}) { return new Promise((resolve, reject) => { axios.put(url, data) .then(response => { resolve(response.data); }, err => { reject(err) }) }) }