赞
踩
const app = getApp(); // 接口对应表 const apiList = [ { path: "../pages/index/mock",// 页面挡板文件地址 url: "getDataByService",// 接口方法名 fnName: "getData"// mock文件中接口对应的方法名 }, ] // 封装http 请求方法 const http = (params, resolve, reject) => { wx.showLoading({ mask: true }); wx.request({ url: app.globalData.apiUrl + params.url,// 拼接全局ip + 方法名 header: { "Content-Type": "application/x-www-form-urlencoded" }, method: 'POST', data: params.data, success: (res) => { wx.hideLoading(); resolve(res); }, fail: (e) => reject(e) }) } // mock const mock = (params, resolve, reject) => { for (let item of apiList) { if (item.url === params.url) { let path = require(item.path); resolve(path[item.fnName]); } } reject({ errMsg: `未发现url:${params.url},请先在apiList中定义` }); } export const post = (params) => { return new Promise((resolve, reject) => { app.globalData.isMock ? mock(params, resolve, reject) : http(params, resolve, reject); }) }
App({
globalData: {
apiUrl: "http://www.xxx.com/",
isMock: true
}
})
module.exports = {
getData: {
"code": "200",
"msg'": "success",
"data": "mock调试成功!"
}
}
const http = require("../../utils/http");
http.post({ url: "getDataByService", data: {} })
.then( res => {
console.log("success: ", res.data);
})
.catch( err => console.error(err) )
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。