export const request = (params) => {
const baseUrl = "https://api-hmugo-web.itheima.net/api/public/v1";
return new Promise((resolve, reject) => {
wx.request({
...params,
url:baseUrl+params.url,
success: (result) => {
resolve(result.data.message);
},
fail: (err) => {
reject(err);
},
});
});
};
使用
// 先引入
import { request } from '../../request/index.js';
// 使用
getSwiper() {
request({
url: '/home/swiperdata',
}).then((res) => {
this.setData({
swiperList: res,
});
});
},
// 使用async await
//获取分类数据
async getCate() {
// request({
// url:"/categories"
// }).then((res)=> {
// this.cateData = res.data.message
// // 存到本地存储上
// wx.setStorageSync('cates', {time:Date.now(),data:this.cateData})
// let leftCateList = this.cateData.map(v => v.cat_name)
// let rightCateContent = this.cateData[0].children
// this.setData({
// leftCateList,
// rightCateContent
// })
// })
// async await 的方式请求
const res = await request({url:"/categories"});
this.cateData = res;
// 存到本地存储上
wx.setStorageSync('cates', {time:Date.now(),data:this.cateData});
let leftCateList = this.cateData.map(v => v.cat_name);
let rightCateContent = this.cateData[0].children;
this.setData({
leftCateList,
rightCateContent
})
}