对uni-request的共同参数进行简单的封装,减少重复性请求代码。
创建utils文件夹存放工具类文件,之后在utils中创建request.js文件存放uni.request的请求方法。
request.js代码
//手机调试
let remote_ip = uni.getStorageSync("remote_ip")
remote_ip = remote_ip ? remote_ip : "默认IP"
let remote_port = uni.getStorageSync("remote_port")
remote_port = remote_port ? remote_port : "默认端口"
//#ifdef APP-PLUS
let serverUrl = `http://${remote_ip}:${remote_port}/api`
//#endif
//#ifdef H5
//web端调试
let serverUrl = '/dev-api'
//#endif
const $http = {
request: function(method, url, data, option = {
showLoading: false,
noHeader: false,
cancelLogin: false,
ignoreError: false
}) {
var headers = {
"Content-Type": "application/json",
// "authorization": `Token ${uni.getStorageSync('token')}`,
"Authorization": `${uni.getStorageSync('token')}`
};
if (option.noHeader) {
headers = {
"Content-Type": "application/json",
};
} else {
headers = {
"Content-Type": "application/json",
// "authorization": `Token ${uni.getStorageSync('token')}`,
"Authorization": `${uni.getStorageSync('token')}`
};
}
return new Promise((resolve, reject) => {
if (option.showLoading) {
uni.showLoading({
title: "加载中",
mask: true
});
}
if (url.indexOf('undefined') > -1 || url.indexOf('null') > -1) {
if (option.showLoading) {
uni.hideLoading()
}
reject()
}
uni.request({
url: url,
header: headers,
data: data,
method: method,
dataType: 'json',
success: function(res) {
if (option.showLoading) {
uni.hideLoading()
}
if (res.statusCode !== 200) {
uni.showToast({
title: "后台服务调用失败:" + res.statusCode,
icon: "none"
})
} else {
// console.log('res.data.errorCode:', res.data.errorCode)
if ("000015" === res.data.errorCode || "000013" === res.data
.errorCode || "000014" === res
.data.errorCode) {
// if ("000000" != res.data.errorCode){//经需求确认,只要状态码不是000000都强制退出
uni.showToast({
title: "登录信息失效,请重新登录",
icon: "none"
})
setTimeout(() => {
uni.reLaunch({
url: "/pages/login/index"
})
}, 500)
} else if ('32050930' == res.data.errorCode) {
uni.showToast({
title: res.data.errorMsg,
icon: "none"
})
} else if (res.data.status !== 'SUCCEED') {
resolve(res.data)
uni.showToast({
title: res.data.errorMsg,
icon: "none"
})
} else {
resolve(res.data);
}
}
},
fail: function(err) {
if (option.showLoading) {
uni.hideLoading()
}
// 与用户输入的IP地址无法建立连接时,给出提示,超时时长大概在20秒左右
if(url.includes('/auth/auth/login')){
uni.showToast({
title: `${uni.getStorageSync("remote_ip")}:${uni.getStorageSync("remote_port")}"服务地址访问失败"`,
icon: "none",
duration: 3000
})
resolve(err)
}else{
reject(err)
}
}
})
})
},
get: function(url, data, option = {
showLoading: false,
noHeader: false,
cancelLogin: false,
ignoreError: false
}) {
let allurl = this.geturl(url);
return this.request('GET', allurl, data, option)
},
post: function(url, data, option = {
showLoading: false,
noHeader: false,
cancelLogin: false,
ignoreError: false
}) {
let allurl = this.geturl(url);
return this.request('POST', allurl, data, option)
},
delete: function(url, data, option = {
showLoading: false,
noHeader: false,
cancelLogin: false,
ignoreError: false
}) {
let allurl = this.geturl(url);
return this.request('DELETE', allurl, data, option)
},
put: function(url, data, option = {
showLoading: false,
noHeader: false,
cancelLogin: false,
ignoreError: false
}) {
let allurl = this.geturl(url);
return this.request('PUT', allurl, data, option)
},
geturl: function(url) {
//#ifdef APP-PLUS
if(uni.getStorageSync("remote_ip")){
serverUrl = `http://${uni.getStorageSync("remote_ip")}:${uni.getStorageSync("remote_port")}/api`
}
//#endif
console.log(url,"url");
return serverUrl + url
}
};
module.exports = $http
页面使用
async suree() {
let res = await this.$http.post('/auth/auth/login', {
userAccount:user,
password:123,
})
if(res.status === "SUCCEED"){
}else{
}
},