封装请求地址 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}`
// let token = wx.getStorageSync('token') ? wx.getStorageSync('token') : ''
//(wx.showLoading)显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
wx.showLoading({ title: "数据请求中" });
return new Promise((resolve,reject)=>{
wx.request({
url: fullUrl,
method:methodType,
data:data,
header: {
'content-type': 'application/json', // 默认值
// 'x-api-key': token,
},
success(res){
resolve(res)
// if (res.data.status == 200) { //根据自己返回数据状态进行更改
// resolve(res.data)
// wx.hideLoading()
// }else{
//手动关闭loading提示框
wx.hideLoading()
// wx.showToast({
// title: res.data.msg,
// icon:'none'
// })
// reject(res.data.message)
// }
},
fail(){
wx.showToast({
title: '接口请求错误',
icon:'none'
})
reject('接口请求错误')
}
})
})
}
}
自己的js文件构造
import { request } from './request'
module.exports = {
// 获取采购list
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);
}
})
},
本文来自博客园,作者:depressiom,转载请注明原文链接:https://www.cnblogs.com/depressiom/p/17700415.html