|
|
|
@ -19,21 +19,21 @@ class Proxy { |
|
|
|
|
async () => await Axios.get(url, { params, ...opt }), |
|
|
|
|
data || params |
|
|
|
|
); |
|
|
|
|
post = async (url, params, opt, data ) => |
|
|
|
|
post = async (url, params, opt, data) => |
|
|
|
|
await this.check( |
|
|
|
|
url, |
|
|
|
|
opt, |
|
|
|
|
async () => await Axios.post(url, params, opt), |
|
|
|
|
data || params |
|
|
|
|
); |
|
|
|
|
put = async (url , params , opt , data ) => |
|
|
|
|
put = async (url, params, opt, data) => |
|
|
|
|
await this.check( |
|
|
|
|
url, |
|
|
|
|
opt, |
|
|
|
|
async () => await Axios.put(url, params, opt), |
|
|
|
|
data || params |
|
|
|
|
); |
|
|
|
|
delete = async (url , params , opt , data ) => { |
|
|
|
|
delete = async (url, params, opt, data) => { |
|
|
|
|
await this.check( |
|
|
|
|
url, |
|
|
|
|
opt, |
|
|
|
@ -42,7 +42,7 @@ class Proxy { |
|
|
|
|
); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
check = async (url , { dispatch } , fetch , params ) => { |
|
|
|
|
check = async (url, { dispatch }, fetch, params) => { |
|
|
|
|
dispatch = dispatch || (() => {}); |
|
|
|
|
dispatch({ type: url.split("/")[0] + "/" + "loading" }); |
|
|
|
|
let response = await fetch(); |
|
|
|
@ -51,7 +51,8 @@ class Proxy { |
|
|
|
|
dispatch({ type: url, data: response.data.data, params }); |
|
|
|
|
return response.data.data; |
|
|
|
|
case 401: |
|
|
|
|
if (await this.refresh()) { |
|
|
|
|
let refresh = localStorage.getItem("refresh"); |
|
|
|
|
if (refresh && (await this.refresh(refresh))) { |
|
|
|
|
let response = await fetch(); |
|
|
|
|
dispatch({ type: url, data: response.data.data, params }); |
|
|
|
|
return response.data.data; |
|
|
|
@ -69,17 +70,20 @@ class Proxy { |
|
|
|
|
|
|
|
|
|
refresh = async () => { |
|
|
|
|
let refresh = localStorage.getItem("refresh"); |
|
|
|
|
if (!refresh) window.location.href = "/"; |
|
|
|
|
let login = await this.login( |
|
|
|
|
"user/login", |
|
|
|
|
{}, |
|
|
|
|
{ headers: { Authorization: `Bearer ${refresh}` } } |
|
|
|
|
); |
|
|
|
|
return login ? true : false; |
|
|
|
|
|
|
|
|
|
if (!refresh) return false; |
|
|
|
|
//window.location.href = "/";
|
|
|
|
|
// let login = await this.login(
|
|
|
|
|
// "user/login",
|
|
|
|
|
// {},
|
|
|
|
|
// { headers: { Authorization: `Bearer ${refresh}` } }
|
|
|
|
|
// );
|
|
|
|
|
// return login ? true : false;
|
|
|
|
|
return false; |
|
|
|
|
}; |
|
|
|
|
login = async (url , params , { dispatch } ) => |
|
|
|
|
login = async (url, params, { dispatch }) => |
|
|
|
|
this.post(url, params, { |
|
|
|
|
dispatch: (obj ) => { |
|
|
|
|
dispatch: (obj) => { |
|
|
|
|
let login = obj.data; |
|
|
|
|
if (!login || !login.refreshToken) return false; |
|
|
|
|
localStorage.setItem("refresh", login.refreshToken); |
|
|
|
@ -91,9 +95,9 @@ class Proxy { |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
logout = async (url , params , { dispatch } ) => { |
|
|
|
|
logout = async (url, params, { dispatch }) => { |
|
|
|
|
this.post(url, params, { |
|
|
|
|
dispatch: (obj ) => { |
|
|
|
|
dispatch: (obj) => { |
|
|
|
|
localStorage.removeItem("refresh"); |
|
|
|
|
localStorage.removeItem("access"); |
|
|
|
|
localStorage.removeItem("userData"); |
|
|
|
@ -112,7 +116,7 @@ class Proxy { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return JSON.parse(userData || ''); |
|
|
|
|
return JSON.parse(userData || ""); |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
const _proxy = new Proxy(); |
|
|
|
|