1.在utils文件夹建一个public.js
const baseUrl = '###';
const http = ({ url = '', param = {}, ...other } = {}) => {
wx.showLoading({
title: '请求中,请耐心等待..'
});
let timeStart = Date.now();
return new Promise((resolve, reject) => {
wx.request({
url: baseUrl + url,
data: param,
header: {
'content-type': 'application/json'
},
...other,
complete: (res) => {
wx.hideLoading();
console.log(`耗时${Date.now() - timeStart}毫秒`);
if (res.statusCode >= 200 && res.statusCode < 300) {
resolve(res.data)
} else {
reject(res)
}
}
})
})
}
const get = (url, param = {}) => {
return http({
url,
param
})
}
const post = (url, param = {}) => {
return http({
url,
param,
method: 'post'
})
}
const put = (url, param = {}) => {
return http({
url,
param,
method: 'put'
})
}
const _delete = (url, param = {}) => {
return http({
url,
param,
method: 'delete'
})
}
module.exports = {
baseUrl,
get,
post,
put,
_delete
}
2.发送请求
const api = require('../../utils/public.js')
Page({
data: {
ID:"0",
},
onLoad: function (options) {
api.post("pnt/getCategories").then(res => {
console.log(res,"成功请求")
}).catch(e => {
console.log(e,"失败请求")
})
api.get(`sitemsg/getSiteMsgByConId?conId=${this.data.ID}`).then(res => {
console.log(res,"成功请求")
}).catch(e => {
console.log(e,"失败请求")
})
api.post("sitemsg/getSiteMsgByConId",{conId:this.data.ID}).then(res => {
console.log(res,"成功请求")
}).catch(e => {
console.log(e,"失败请求")
})
Promise.all([
api.get("pnt/getCategories"),
api.post("sitemsg/getSiteMsgByConId",{conId:this.data.ID})
]).then(result => {
console.log(result)
}).catch(e => {
console.log(e)
})
},
})
https://www.it610.com/article/1279397810260688896.htm