axios封装
1 import JsonP from 'jsonp' 2 import axios from 'axios' 3 import { Modal } from 'antd' 4 export default class Axios { 5 static jsonp(options) { 6 return new Promise((resolve, reject) => { 7 JsonP(options.url, { 8 param: 'callback' 9 }, function (err, response) { 10 if (response.status == 'success') { 11 resolve(response); 12 } else { 13 reject(response.messsage); 14 } 15 }) 16 }) 17 } 18 19 static ajax(options){ 20 let loading; 21 if (options.data && options.data.isShowLoading !== false){ 22 loading = document.getElementById('ajaxLoading'); 23 loading.style.display = 'block'; 24 } 25 let baseApi = 'https://www.xxxxxx.com/xx/5a7278e28d0c633b9c4adbd7/api'; 26 return new Promise((resolve,reject)=>{ 27 axios({ 28 url:options.url, 29 method:'get', 30 baseURL:baseApi, 31 timeout:5000, 32 params: (options.data && options.data.params) || '' 33 }).then((response)=>{ 34 if (options.data && options.data.isShowLoading !== false) { 35 loading = document.getElementById('ajaxLoading'); 36 loading.style.display = 'none'; 37 } 38 if (response.status == '200'){ 39 let res = response.data; 40 if (res.code == '0'){ 41 resolve(res); 42 }else{ 43 Modal.info({ 44 title:"提示", 45 content:res.msg 46 }) 47 } 48 }else{ 49 reject(response.data); 50 } 51 }) 52 }); 53 } 54 }
调用:
1 import axios from './../../axios/index' 2 3 4 request =()=>{ 5 axios.ajax({ 6 url:'/table/list', 7 data:{ 8 params:{ 9 page:this.params.page 10 }, 11 // isShowLoading:false 12 } 13 }).then((res)=>{ 14 console.log("res1",res) 15 if(res.code == 0){ 16 this.setState({ 17 dataSource2:res.result 18 }) 19 } 20 }) 21 }