1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| import axios from "axios" import qs from "qs" import Vue from "vue"
const service = axios.create({ withCredentials: true, crossDomain: true, baseURL: Vue.prototype.baseURL, timeout: 6000 })
service.interceptors.request.use( config => { config.method === 'post' ? config.data = qs.stringify({ ...config.data }) : config.params = { ...config.params }; console.log('请求拦截成功') return config; }, error => { console.log(error); return Promise.reject(error); } );
service.interceptors.response.use(res => { if (res.data.status == 200) { return res.data } else { return Promise.reject(res.data.msg); } }, error => { return Promise.reject(error) })
axios.defaults.adapter = function(config) { return new Promise((resolve, reject) => { var settle = require('axios/lib/core/settle'); var buildURL = require('axios/lib/helpers/buildURL'); uni.request({ method: config.method.toUpperCase(), url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer), header: config.headers, data: config.data, dataType: config.dataType, responseType: config.responseType, sslVerify: config.sslVerify, complete: function complete(response) { console.log("执行完成:", response) response = { data: response.data, status: response.statusCode, errMsg: response.errMsg, header: response.header, config: config }; settle(resolve, reject, response); } }) }) } export default service
|