UNIAPP-request请求封装
export default {
// 全局配置
common: {
// 如果不是H5
// #ifndef H5
baseUrl: 'http://127.0.0.1/test/',
// #endif
// #ifdef H5
// baseUrl: 'http://127.0.0.1/test/',
baseUrl: '/api',
// #endif
header: {
'Content-Type': 'application/json;charset=UTF-8',
// 'Content-Type': 'application/x-www-form-urlencoded'
},
data: {},
method: 'GET',
dataType: 'json',
token: false
},
// 请求 返回promise
request(options = {}) {
// 组织参数
options.url = this.common.baseUrl + options.url
options.header = options.header || this.common.header
options.data = options.data || this.common.data
options.method = options.method || this.common.method
options.dataType = options.dataType || this.common.dataType
options.token = options.token === true ? true : this.common.token
// console.log(options);
// 请求
return new Promise((res, rej) => {
// 请求之前验证...
// 请求中...
uni.request({
...options,
success: (result) => {
// 返回原始数据
// console.log(result);
if (options.native) {
return res(result)
}
// 服务端失败
if (result.statusCode !== 200) {
if (options.toast !== false) {
uni.showToast({
title: result.data.data || '服务端失败',
icon: 'none'
});
}
return false;
}
// 其他验证...
// 成功
let data = result.data
res(data)
},
fail: (error) => {
uni.showToast({
title: error.errMsg || '请求失败',
icon: 'none'
});
return rej(error)
}
});
})
},
// get请求
get(url, options = {}) {
options.url = url
options.data = {}
options.method = 'GET'
return this.request(options)
},
// post请求
post(url, data = {}, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
options.header = {
'Content-Type': 'application/x-www-form-urlencoded'
}
return this.request(options)
},
// delete请求
del(url, data = {}, options = {}) {
options.url = url
options.data = data
options.method = 'DELETE'
options.header = {
'Content-Type': 'application/x-www-form-urlencoded'
}
return this.request(options)
},
// 上传文件
upload(url, data, onProgress = false) {
return new Promise((result, reject) => {
// 上传之前验证
// let token = uni.getStorageSync('token')
// if (!token) {
// uni.showToast({
// title: '请先登录',
// icon: 'none'
// });
// // token不存在时跳转
// return uni.navigateTo({
// url: '/pages/login/login',
// });
// }
// let userinfo = uni.getStorageSync('userinfo');
// if (userinfo) {
// userinfo = JSON.parse(userinfo);
// }
// let token = userinfo.token;
const uploadTask = uni.uploadFile({
url: this.common.baseUrl + url,
filePath: data.filePath,
name: data.name || "files",
// header: {
// 'Authorization': token
// },
success: (res) => {
if (res.statusCode !== 200) {
result(false)
return uni.showToast({
title: '上传失败',
icon: 'none'
});
}
let message = JSON.parse(res.data)
result(message);
},
fail: (err) => {
console.log(err);
reject(err)
}
})
uploadTask.onProgressUpdate((res) => {
if (typeof onProgress === 'function') {
onProgress(res.progress)
}
});
})
}
}
免责申明:
1. 本站所有教程、文章或资源分享目的仅供大家学习和交流!
2. 如有无法查看或链接失效,麻烦请报告联系管理员处理!
3. 本站无法保证资源或其时效性,恕不接受任何提问。
4. 在本站下载的源码严禁杜绝任何形式的正式商业用途,请去程序官方购买。 所有资料均来自于网络,版权归原创者所有!本站不提供任何保证,并不承担任何法律责任,如果对您的版权或者利益造成损害,请提供相应的资质证明,我们将于3个工作日内予以删除。