Files
ClassContent/项目合集/校园失物招领/min-tpl/http/index.js
2024-09-27 02:06:13 +08:00

71 lines
1.2 KiB
JavaScript

import config from "../config/index"
export default class Http {
constructor() {}
async get(params) {
wx.showLoading({
title: '加载中...'
});
return new Promise((success, error) => {
wx.request({
url: config.CheckBaseUrl() + params.url,
method: "GET",
data: params.data,
header: Object.assign({
"token": wx.getStorageSync('token'),
"Content-Type": "application/json; charset=utf-8",
}, params.headers),
success: (res) => {
this.interceptors(success, error, res.data)
}
});
})
}
async post(params) {
wx.showLoading({
title: '加载中...'
});
return new Promise((success, error) => {
wx.request({
url: config.CheckBaseUrl() + params.url,
method: "POST",
data: params.data,
header: Object.assign({
"Content-Type": "application/json; charset=utf-8",
}, params.headers),
success: (res) => {
this.interceptors(success, error, res.data)
}
});
})
}
interceptors(s, e, data) {
wx.hideLoading();
switch (data.status) {
case 200:
s(data.result)
break;
case 404:
wx.showToast({
title: data.message,
icon: "none",
duration: 2000
});
throw new Error(data.message);
break;
default:
break;
}
}
}