uniapp 封装uni.request请求
module.exports = (params) => {let baseUrl = "http://jzapi.56gou.com/api/v1"let url = baseUrl + params.url;let method = params.method;let header = params.header || {};let data = params.data || {};//
·
module.exports = (params) => {
let baseUrl = "http://jzapi.56gou.com/api/v1"
let url = baseUrl + params.url;
let method = params.method;
let header = params.header || {};
let data = params.data || {};
//封装token
header = {'token': uni.getStorageSync('token')}
// 请求方式 GET POST
if (method) {
method = method.toUpperCase(); // 小写转大写
if (method == "POST") {
header = {"content-type":"application/x-www-form-urlencoded",'token': uni.getStorageSync('token')}
}
}
return new Promise((reslove, reject) => {
// 发起网络请求
uni.request({
url:url,
method:method || "GET",
header:header,
data:data,
dataType:"json",
sslVerify:false, // 是否验证ssl证书
success: res => {
reslove(res.data)
},
fail: err => {
uni.showModal({
content:err.msg
})
reject(err.data)
},
complete: (e) => {
return;
}
})
})
}
使用
更多推荐
已为社区贡献2条内容
所有评论(0)