| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import axios from 'axios';
- const uniAdapter = (config) => {
- return new Promise((resolve, reject) => {
- uni.request({
- method: config.method.toUpperCase(),
- url: config.baseURL + config.url,
- data: config.data,
- header: config.headers,
- timeout: config.timeout,
-
- success: (res) => {
- resolve({
- data: res.data,
- status: res.statusCode,
- config: config,
- headers: res.header
- })
- },
-
- fail: (err) => {
- reject(new Error(err.errMsg || '请求失败'))
- }
- })
- })
- }
- const instance = axios.create({
- baseURL: 'https://m.douban.com/',
- timeout: 100000,
- adapter: uniAdapter
- });
- // 添加请求拦截器
- instance.interceptors.request.use(function (config) {
- // 在发送请求之前做些什么
- return config;
- }, function (error) {
- // 对请求错误做些什么
- return Promise.reject(error);
- });
- // 添加响应拦截器
- instance.interceptors.response.use(function (response) {
- // 2xx 范围内的状态码都会触发该函数。
- // 对响应数据做点什么
- return response;
- }, function (error) {
- // 超出 2xx 范围的状态码都会触发该函数。
- // 对响应错误做点什么
- return Promise.reject(error);
- });
-
-
- export default instance;
|