HttpService.ts
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {AxiosInstance, AxiosRequestConfig} from 'axios';
import qs from 'qs';
/**
* @title http请求封装
* @desc
*/
export class HttpService {
constructor(public axios: AxiosInstance) {
}
/**
* get请求, json形式返回
* @param url 接口地址
* @param paramMap 参数
*/
public get(url: string, paramMap?: Record<string, unknown>): Promise<unknown> {
const request: AxiosRequestConfig = {
params: paramMap,
};
return this.axios.get(url, request);
}
/**
* post请求
* @param url 接口地址
* @param body 参数
*/
public post(url: string, body: string | Record<string, unknown>): Promise<unknown> {
const requestConfig = {
withCredentials: true,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
};
return this.axios.post(url, body, requestConfig);
}
/**
* 以表单方式提交
* @ param {string} url
* @ param body
* @ returns {Promise<any>}
*/
public postForm(url: string, body: string | Record<string, unknown>): Promise<unknown> {
const options = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
};
return this.axios.post(url, qs.stringify(body), options);
}
/**
* 以参数和json一起提交数据
* @param url
* @param body
* @param paramMap
*/
public postParams(url: string, body: string | Record<string, unknown> | null, paramMap: string | Record<string, unknown>): Promise<any> {
const options = {
params: paramMap,
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
};
return this.axios.post(url, body, options);
}
/**
* put方式提交
* @param url
* @param body
*/
public put(url: string, body: Record<string, unknown> | string): Promise<any> {
const options = {};
return this.axios.put(url, body, options);
}
/**
* delete方式提交
* @param url
* @param paramMap
*/
public delete(url: string, paramMap?: Record<string, unknown> | string): Promise<any> {
const options = {
params: paramMap,
};
return this.axios.delete(url, options);
}
/**
* patch方式提交
* @param url
* @param body
*/
public patch(url: string, body: Record<string, unknown> | string): Promise<any> {
const options = {};
return this.axios.patch(url, body, options);
}
/**
* post请求下载文件
* @param url
* @param body
* @param fileName
* @param contentType
*/
public postDownload(url: string, body = null, fileName: string, contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
this.axios.post(url, body, {
responseType: 'blob',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
}).then(data => {
HttpService.downloadFile(data, fileName, contentType);
});
}
/**
* get方式请求下载文件
* @param url
* @param paramMap
* @param fileName
* @param contentType
*/
public getDownload(url: string, paramMap = null, fileName: string, contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
this.axios.get(url, {
responseType: 'blob',
headers: {
'Content-Type': 'application/json; charset=UTF-8',
},
params: paramMap,
}).then(data => {
HttpService.downloadFile(data, fileName, contentType);
});
}
/**
* 将文件下载下来
* @param data
* @param fileName
* @param contentType
*/
private static downloadFile(data: any, fileName: string, contentType: string) {
const blob = new Blob([data], {type: contentType});
const url = window.URL.createObjectURL(blob);
// 以动态创建a标签进行下载
const a = document.createElement('a');
a.href = url;
a.download = fileName;
a.click();
window.URL.revokeObjectURL(url);
}
/**
* 以formData方式提交
* @ param {string} url
* @ param body
* @ returns {Promise<any>}
*/
public postFormData(url: string, formData: any): Promise<any> {
const options = {
headers: {
'Content-Type': 'multipart/form-data; charset=UTF-8',
},
};
return this.axios.post(url, formData, options);
}
}