Reinit project
This commit is contained in:
40
src/services/common.ts
Normal file
40
src/services/common.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { AreaCodes, User } from '@/types/common';
|
||||
import { loadAreaCode, storeAreaCode, loadUser, storeUser } from '@/utils/storage';
|
||||
import http from '@/utils/http';
|
||||
|
||||
export function globalState() {
|
||||
return (window as any).g_app._store.getState();
|
||||
}
|
||||
|
||||
export async function getAreaCodes(): Promise<AreaCodes> {
|
||||
const areaCodeCache = loadAreaCode();
|
||||
if (areaCodeCache) {
|
||||
return areaCodeCache;
|
||||
}
|
||||
|
||||
const res = await http.get<AreaCodes>('/sign/code');
|
||||
if (res.ok && res.data) {
|
||||
storeAreaCode(res.data);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
throw Error(res.msg);
|
||||
}
|
||||
|
||||
export async function getUserInfo(refresh = false): Promise<User> {
|
||||
if (!refresh) {
|
||||
const userCache = loadUser() as User;
|
||||
if (userCache) {
|
||||
return userCache;
|
||||
}
|
||||
}
|
||||
|
||||
const { ok, msg, data } = await http.get<User>('/user/info');
|
||||
if (ok && data) {
|
||||
data.token = globalState().user.user.token;
|
||||
storeUser(data);
|
||||
return data;
|
||||
}
|
||||
|
||||
throw Error(msg);
|
||||
}
|
||||
Reference in New Issue
Block a user