-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhttp.ts
52 lines (46 loc) · 1.34 KB
/
http.ts
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
import { ACCESS_TOKEN } from '@/constants/user';
import Taro, { Chain } from '@tarojs/taro';
class Http {
constructor () {
Taro.addInterceptor(this.httpInterceptor);
Taro.addInterceptor(Taro.interceptors.timeoutInterceptor);
}
httpInterceptor (chain: Chain) {
console.log(chain);
const requestParams = chain.requestParams;
return chain.proceed(requestParams).then((response) => {
console.log(response);
});
}
request <T = any> (option: Taro.request.Option) {
return new Promise((resolve, reject) => {
const header: TaroGeneral.IAnyObject = {
'content-type': 'application/json',
};
//
try {
// 使用临时TOKEN
const token = Taro.getStorageSync(ACCESS_TOKEN) || '4|jzeuroVvWNRvGZzKbBkjnFLjPT1g1j0vuo4tdZ1i';
// console.log(token);
if (token) {
header.Authorization = `Bearer ${token}`;
}
} catch (error) {
console.log(error);
}
Taro.request<T>(Object.assign({
timeout: 30000,
header,
success: (response) => {
resolve(response.data as T);
},
fail: (error) => {
reject(error);
}
}, option, {
url: `${process.env.HOST}/${process.env.API_PREFIX}/${process.env.API_VERSION}${option.url}`,
}));
});
}
}
export default new Http();