封装request
1. utils/util文件下封装 util.js
var api = require('../../config/api.js');
function request(url, data = {}, method = "GET") {
return new Promise(function (resolve, reject) {
wx.request({
url: url,
data: data,
method: method,
header: {
'Content-Type': 'application/json',
'X-Nideshop-Token': wx.getStorageSync('token')
},
success: function (res) {
resolve(res)
},
fail: function (err) {
reject(err)
}
})
});
}
let findTypes = (data) => {
return new Promise((resolve,reject) => {
resolve(request(api.baseUrl,data,'GET'))
})
}
let findLocations = (data) => {
return new Promise((resolve) => {
resolve(request(api.baseUrl,data,'GET'))
})
}
module.exports = {
request,
findType,
findLocations,
}
2. 引入使用
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
Page({
onLoad: function () {
util.request(api.baseUrl, {
act: 'findTypes',
userId:'001'
}).then(res => {
console.log(res)
})
let params ={
act:'findTypes',
userId:'001'
}
util.findTypes(params).then((res) => {
cosole.log(res)
})
util.findLocations({
act:'findLocations',
userId:'001'
}).then((res) => {
console.log(res)
})
},
})
2-1. config/api api.js
const baseUrl = 'https://www.hiolabs.com/api/';
module.exports = {
IndexUrl: ApiRootUrl + 'index/appInfo',
}