60 lines
1.1 KiB
JavaScript
60 lines
1.1 KiB
JavaScript
import config from '@/config'
|
|
|
|
export default class Http {
|
|
|
|
async get(params) {
|
|
uni.showLoading({
|
|
title: '加载中...'
|
|
});
|
|
return new Promise((suc, err) => {
|
|
uni.request({
|
|
url: config.checkUrl() + params.url,
|
|
data: params.data,
|
|
method: 'GET',
|
|
header: Object.assign({
|
|
token: uni.getStorageSync('token')
|
|
}, params.headers),
|
|
success: (res) => {
|
|
this.interceptors(suc, res.data)
|
|
}
|
|
});
|
|
})
|
|
}
|
|
|
|
async post(params) {
|
|
uni.showLoading({
|
|
title: '加载中...'
|
|
});
|
|
return new Promise((suc, err) => {
|
|
uni.request({
|
|
url: config.checkUrl() + params.url,
|
|
data: params.data,
|
|
method: 'POST',
|
|
header: Object.assign({
|
|
token: uni.getStorageSync('token')
|
|
}, params.headers),
|
|
success: (res) => {
|
|
this.interceptors(suc, res.data)
|
|
}
|
|
});
|
|
})
|
|
}
|
|
|
|
interceptors(callback,data) {
|
|
uni.hideLoading();
|
|
switch (data.status) {
|
|
case 200:
|
|
callback(data.result)
|
|
break;
|
|
case 404:
|
|
uni.showToast({
|
|
title: data.msg,
|
|
duration: 2000,
|
|
icon: 'error'
|
|
});
|
|
throw new Error(data.msg)
|
|
break;
|
|
}
|
|
}
|
|
}
|