在utils目录下创建network.js文件封装请求
封装的network.js:
//模块一,全局变量
let urlList = {
host: 'http://47.106.25.53/',
loginHost: "http://47.106.25.53/",
webModelUrl: 'ipp-web/', //登陆模块
mobileModelUrl: 'ipp-mobile/', //业务模块
authModelUrl: 'ipp-auth-web/', //上传图片
},
requestHandler = {
title: '',
url: '',
success: function (res) { },
fail: function () { }
},
domain = urlList.host + urlList.mobileModelUrl;
//--------end------------ //模块二,处理请求
function request(method, requestHandler) {
const token = wx.getStorageSync('token');
if (!requestHandler.title) requestHandler.title = "加载中";
wx.showLoading({
title: requestHandler.title,
})
setTimeout(function () {
wx.hideLoading()
}, 10000);
wx.request({
url: domain + requestHandler.url,
data: requestHandler.data,
method: method,
header: {
"X-Requested-With": "XMLHttpRequest",
'Content-Type': 'application/json',
'token': token
},
dataType: 'json',
success: function (res) {
if (res.data.msgCode == 'F0006' || res.data.msgCode == 'F0007') {
/*wx.showModal({
title: '温馨提示',
content: '登录已失效,请重新登录!',
cancelText: '取消',
confirmText: '确定',
success: function (res) {
wx.reLaunch({
url: '../logs/logs',
})
}
})*/
}
wx.hideLoading()
requestHandler.success(res)
},
fail: function () {
wx.hideLoading()
requestHandler.fail()
}
})
}
//---------end----------- //模块三,请求方法
//GET请求
function GET(requestHandler) {
request('GET', requestHandler)
};
//POST请求
function POST(requestHandler) {
request('POST', requestHandler)
};
//--------end----------- //模块四,输出方法
module.exports = {
GET: GET,
POST: POST,
urlList: urlList
};
//-----end----------
在页面上使用require引入输出的封装对象:
const network = require('../../utils/network.js');
使用方法:
network.POST({
url: 'asnorder/getPackingUnitInfo.shtml',
data: {},
success: function (res) { },
})