封装请求地址 https.js
let baseUrl='https://XX.XXX.com/index.php/';
export {
baseUrl
}
结构目录
封装 request.js
import { baseUrl } from './https.js'
module.exports = {
request : function(url, methodType, data){
let fullUrl = `${baseUrl}${url}`
wx.showLoading({ title: "数据请求中" });
return new Promise((resolve,reject)=>{
wx.request({
url: fullUrl,
method:methodType,
data:data,
header: {
'content-type': 'application/json',
},
success(res){
resolve(res)
wx.hideLoading()
},
fail(){
wx.showToast({
title: '接口请求错误',
icon:'none'
})
reject('接口请求错误')
}
})
})
}
}
自己的js文件构造
import { request } from './request'
module.exports = {
getPurchaseList: (data) => request('api/Api/list', 'POST', data),
}
如何调用
const api = require('../../../api/purchase');
getPurchaseList(){
var that = this;
let data ={
"openId": that.data.userInfo.app_openid,
"token": that.data.userInfo.login_token
};
api.getPurchaseList(data).then((res) => {
if (res.data.status == 1) {
that.setData({
goodsTypes: res.data
})
} else {
app.myShowToast(res.data.msg);
}
})
},