//http.js
/*************************start*********************************************************/
const request = function(url, method, data, headers, is_show_loading) {
return new Promise((resolve, reject) => {
if (is_show_loading) {
uni.showLoading({
title: '加载中...',
//是否显示透明遮罩层,防止触摸穿透,默认:false
mask: true,
})
}
uni.request({
method: method,
url: url,
data: data,
header: headers,
success: function(res) {
// console.log('请求结果', res);
resolve(res.data);
},
fail: function(err) {
uni.showToast({
title: '网络异常',
duration: 2000,
icon: 'none'
});
reject('请求失败', err);
},
complete: function() {
if (is_show_loading) {
uni.hideLoading();
}
}
})
})
}
const post = function(options) {
// console.log(options)
let headers = {
'Content-Type': 'application/json'
};
let is_show_loading = true;
if (options.is_show_loading != undefined) {
is_show_loading = options.is_show_loading;
}
return request(options.url, 'POST', options.data, headers, is_show_loading);
}
export default {
request,
post
}
//***************************end******************************************************//
//api.js 方法库
import http from './http.js';
function getData(data){
return http.post({
url: url.request['url'],
data:data
});
}
export default {
getData:getData
}
//页面调用
that.api.getData(params).then(function(res) {})
uni-app的request封装
最新推荐文章于 2024-07-16 17:38:41 发布