uni-app 封装请求

1、npm install uni-request --save

2、uniRequest.defaults.baseURL = '请求路径基地址';

3、uniRequest.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';

4、

// 添加请求拦截器

uniRequest.interceptors.request.use(function(config) {

// 在发送请求之前做些什么

if (uni.getStorageSync('atoken')) {

config.headers['Authorization'] = uni.getStorageSync('atoken')

}

return config;

}, function(error) {

// 对请求错误做些什么

return Promise.reject(error);

});

5、

// 添加响应拦截器

uniRequest.interceptors.response.use(function(response) {

// 对响应数据做点什么

if (response.data.code === 200) {

 

} else if (response.data.code === 500) {

uni.showToast({

    title: response.data.message,

    duration: 2000,

icon:"none"

});

return;

} else if (response.data.code === 404) {

uni.showToast({

    title: '参数检验失败',

    duration: 2000,

icon:"none"

});

return;

} else if (response.data.code === 401) {

uni.showToast({

    title: '暂未绑定手机号',

    duration: 2000,

icon:"none"

});

return;

}

return response;

}, function(error) {

// 对响应错误做点什么

return Promise.reject(error);

});

 

请求的api

import uniRequest from 'uni-request';

//get请求

export function content(id, type) {

return uniRequest({

url: '  接口   ' + id + '/' + type,

method: 'get'

})

}

//post 请求

export function getSettlementConfirmOrder(data) {

return uniRequest({

url: '  接口  ',

method: 'post',

data: data

})

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章