Login & Register

This commit is contained in:
Limo Saplf
2019-09-01 21:56:21 +08:00
parent d9a8408535
commit 4480fc8e19
12 changed files with 231 additions and 23 deletions

View File

@@ -15,11 +15,29 @@ const successLabel = 'color:white;background-color:green;padding: 1px 2px';
const failureLabel = 'color:white;background-color:red;padding: 1px 2px';
const originLabel = 'color:unset;background-color:unset;padding:unset';
const baseURL = 'http://yapi.faronear.org/mock/35';
const baseURL = 'https://server.fiv.tacever.org/v1';
// const baseURL = '//yapi.faronear.org/mock/20/v1';
const instance = axios.create({
baseURL,
});
function replaceId(obj: any) {
if (obj instanceof Array) {
obj.forEach(replaceId);
} else if (typeof obj === 'object') {
const id = obj['_id'];
if (id) {
delete obj['_id'];
obj.id = id;
}
_.entries(obj).forEach(([_, value]) => {
if (typeof value === 'object') {
replaceId(value);
}
});
}
}
async function filter<T>(
method: keyof AxiosInstance,
...args: any[]
@@ -29,13 +47,14 @@ async function filter<T>(
const { user } = globalState().user;
if (user && user.token) {
instance.defaults.headers = {
...instance(instance.defaults.headers),
Authorization: user.token,
...(instance.defaults.headers || {}),
token: user.token,
};
} else {
instance.defaults.headers = _.omit(instance.defaults.headers, 'Authorization');
instance.defaults.headers = _.omit(instance.defaults.headers, 'token');
}
const { status, data } = await (instance[method] as (...args: any[]) => Promise<AxiosResponse<AppResp>>)(...args);
replaceId(data);
ret = {
ok: status >= 200 && status < 300 && data && data.code === 0,
code: data && data.code,