var UserInfo = require('./userInfo');
const BaseUrl = "http://192.168.1.128:8080/";
const BaseImgUrl = "http://192.168.1.118:8080/"
function configHeader(contentType) {
//请求头 'application/x-www-form-urlencoded',application/json;
var authorization = UserInfo.getTokenType() + " " + UserInfo.getToken();
var header = {
'content-type': contentType,
'Authorization': authorization,
'TENANT-ID': 1,
'encrypt':false,
'VERSION':'ceshi'
}
return header;
}
/**
* 供外部post请求调用
* application/x-www-form-urlencoded
*/
function postForm(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "POST")
request(url, params, "application/x-www-form-urlencoded", "POST", onSuccess, onFailed);
}
/**
* 供外部post请求调用
* application/json
*/
function postJson(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "POST")
request(url, params, "application/json", "POST", onSuccess, onFailed);
}
/**
* 供外部get请求调用
* application/x-www-form-urlencoded
*/
function getForm(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "GET")
request(url, params, "application/x-www-form-urlencoded", "GET", onSuccess, onFailed);
}
/**
* 供外部get请求调用
* application/json
*/
function getJson(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "GET")
request(url, params, "application/json", "GET", onSuccess, onFailed);
}
function getNewJson(url, params, onSuccess,oncomplete,onFailed) {
//console.log("请求方式:", "GET")
request(url, params, "application/json", "GET", onSuccess, oncomplete,onFailed);
}
/**
* 供外部put请求调用
* application/json
*/
function putJson(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "PUT")
request(url, params, "application/json", "PUT", onSuccess, onFailed);
}
/**
* 供外部DELETE请求调用
* application/json
*/
function deleteJson(url, params, onSuccess, onFailed) {
//console.log("请求方式:", "DELETE")
request(url, params, "application/json", "DELETE", onSuccess, onFailed);
}
/**
* function: 封装网络请求
* @url URL地址
* @params 请求参数
* @contentType 请求类型: application/x-www-form-urlencoded / application/json
* @method 请求方式:GET/POST
* @onSuccess 成功回调
* @onFailed 失败回调
*/
function request(url, params, contentType, method, onSuccess, onFailed) {
if (url == 'api/driver/order-info'){
//不显示加载圈
}else{
wx.showLoading({
title: "正在加载中...",
})
}
var header = configHeader(contentType);
console.log("请求地址:", BaseUrl + url);
console.log("请求头:", header);
console.log("请求参数:", params);
wx.request({
url: BaseUrl + url,
data: dealParams(params),
method: method,
header: header,
success: function(res) {
wx.hideLoading();
if (res.data) {
console.log('请求响应:', (BaseUrl + url), res.data);
// application/json类型请求,失败统一处理
if (contentType == 'application/json') {
// 错误提示
if (res.data.code != 0) {
/// 登录超时,退出登录
if (res.data.msg == '用户凭证已过期') {
UserInfo.logout();
wx.reLaunch({
url: '/subPackagesLogin/pages/login/oauthLogin/oauthLogin',
})
return;
}
var url_list=['support/approvalProcess/getAuth','support/qcc/search'];
if(url_list.indexOf(url)!=-1){
onSuccess(res.data);
return;
}
wx.showToast({
title: res.data.msg ? res.data.msg : "接口返回失败",
icon:'error'
})
if(onFailed){
onFailed();
}
return;
}
}
onSuccess(res.data);
} else {
wx.showToast({
title: "请求结果为空",
icon: 'error',
})
}
},
fail: function(error) {
wx.hideLoading();
onFailed(error); //failure for other reasons
wx.showToast({
title: "连接服务器失败",
icon: 'error',
})
}
})
}
/**
* function: 根据需求处理请求参数:添加固定参数配置等
* @params 请求参数
*/
function dealParams(params) {
var tempParams = JSON.parse(JSON.stringify(params))
// tempParams.openId = wx.getStorageSync('openId');;
//console.log("请求参数:", tempParams)
return tempParams;
}
// 1.通过module.exports方式提供给外部调用
module.exports = {
configHeader:configHeader,
postFormRequest: postForm,
postJsonRequest: postJson,
getFormRequest: getForm,
getJsonRequest: getJson,
getNewJsonRequest: getNewJson,
putJsonRequest: putJson,
deleteRequest: deleteJson,
BaseUrl: BaseUrl,
BaseImgUrl:BaseImgUrl
}
小程序 封装网络请求
该代码段展示了在微信小程序中对HTTP请求的封装,包括GET、POST、PUT、DELETE等方法,使用了wx.request进行网络调用,并处理了请求头(headers),如Authorization、content-type等,同时包含了错误处理和特定情况下的特殊逻辑,如登录超时重定向。
摘要由CSDN通过智能技术生成