1.方法封装
在utils文件夹下创建request.js文件,用于对方法封装
/**
* url:接口
* data:参数
* method:请求方法
*/
let timeStart = Date.now();
module.exports.requestServerData = function (url, method, data) {
return new Promise(function (resolve, reject) {
wx.request({
url: "https://xxx/" + url, //采用字符串拼接的方式
data: data,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
method: method,
success: function (res) {
resolve(res)
console.log(`耗时${Date.now() - timeStart}`);
},
fail: function (res) {
reject(res)
}
})
})
}
2.在对应的页面js文件里引入该接口
var call = require("../../utils/request.js")
onLoad: function () {
var that = this;
//调用封装的方法
call.requestServerData("url", "POST", {
x: '', //请求参数
y: ''
}).then(function (res) {
})
},