Files
ClassContent/历届学生/fontendsix/项目练习/上课项目/uniapp-project/http/index.js
2024-09-27 02:06:13 +08:00

69 lines
1.2 KiB
JavaScript

import config from "@/config"
export default class Http {
constructor() {}
async get(params) {
uni.showLoading({
title: '加载中...'
});
return new Promise((success, error) => {
uni.request({
url: config.CheckBaseUrl() + params.url,
method: "GET",
data: params.data,
header: Object.assign({
"Content-Type": "application/json; charset=utf-8",
}, params.headers),
success: (res) => {
this.interceptors(success, error, res.data)
}
});
})
}
async post(params) {
uni.showLoading({
title: '加载中...'
});
return new Promise((success, error) => {
uni.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) {
uni.hideLoading();
switch (data.status) {
case 200:
s(data.result)
break;
case 404:
uni.showToast({
title: data.msg,
duration: 2000
});
throw new Error(data.msg);
break;
default:
break;
}
}
}