65 lines
1.5 KiB
Plaintext
65 lines
1.5 KiB
Plaintext
export class HttpRequest {
|
|
|
|
/***普通请求方法***/
|
|
static Request(url: string, method: "GET" | "POST" | "PUT" | undefined, data: object | any) {
|
|
return new Promise(function(resolve, reject) {
|
|
let header = {
|
|
'content-type': method == 'POST' || method == 'PUT' ? 'application/x-www-form-urlencoded' : 'application/json; charset=utf-8',
|
|
'Access-Control-Allow-Origin': '*'
|
|
};
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: header,
|
|
success(res: any) {
|
|
if (res.statusCode == "200") {
|
|
resolve(res.data);
|
|
}
|
|
else
|
|
{
|
|
resolve(res);
|
|
}
|
|
},
|
|
fail(err) {
|
|
//请求失败
|
|
uni.showToast({
|
|
title: '无法连接到服务器',
|
|
icon: 'none'
|
|
})
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
};
|
|
|
|
/***带Token的请求方法***/
|
|
static RequestWithToken(url: string, method: "GET" | "POST" | "PUT" | undefined, token: string, data: object | any) {
|
|
return new Promise(function(resolve, reject) {
|
|
let header = {
|
|
'content-type': method == 'POST' || method == 'PUT' ? 'application/x-www-form-urlencoded' : 'application/json; charset=utf-8',
|
|
'Authorization': 'Bearer ' + token,//token获取
|
|
'Access-Control-Allow-Origin': '*'
|
|
};
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: header,
|
|
success(res: any) {
|
|
resolve(res);
|
|
},
|
|
fail(err) {
|
|
//请求失败
|
|
uni.showToast({
|
|
title: '无法连接到服务器',
|
|
icon: 'none'
|
|
})
|
|
reject(err)
|
|
}
|
|
})
|
|
})
|
|
|
|
}
|
|
}
|