-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoFetch.js
52 lines (42 loc) · 1.48 KB
/
doFetch.js
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
import axios from 'axios';
import {getItem} from './storage';
import * as types from '../redux/mutation-types';
global.accessToken = null
async function _fetch(url,method,headers,params,data){
try {
// await 是在等待一个async函数完成, 它会阻塞后面的代码
//等着 Promise 对象 resolve,然后得到 resolve 的值,可以继续执行
//这就是 await 必须用在 async 函数中的原因。async 函数调用不会造成阻塞,
//它内部所有的阻塞都被封装在一个 Promise 对象中异步执行。
//所以_fetch不会阻塞
if(global.accessToken == null || global.accessToken == undefined || global.accessToken == ''){
global.accessToken = await getItem(types.ACCESSTOKEN)
}
return axios({
url,
method,
baseURL:'http://127.0.0.1:9090',
headers:{"accessToken":global.accessToken,...headers},
params,
data
})
} catch (error) {
console.log(error)
}
}
function post(url,data,headers=null){
return _fetch(url,'post',headers,null,data)
}
function get(url,params,headers=null){
return _fetch(url,'get',headers,params,null);
}
function put(url,data,headers=null){
return _fetch(url,'put',headers,null,data);
}
function deleteAxios(url,headers=null){
return _fetch(url,'delete',headers,null,null);
}
global.post = post;
global.get = get;
global.put = put;
global.delete = deleteAxios;