2024-03-26 21:09:41 +08:00
|
|
|
import {createContext, Dispatch} from "react";
|
2024-04-02 18:45:46 +08:00
|
|
|
import {get} from "../util/AjaxUtils.ts";
|
|
|
|
import {MessageInstance} from "antd/es/message/interface";
|
2024-03-26 21:09:41 +08:00
|
|
|
|
|
|
|
export enum GlobalDataActionType {
|
|
|
|
REFRESH_SELF
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface User {
|
|
|
|
balance: string;
|
|
|
|
nickname: string;
|
2024-04-19 18:20:51 +08:00
|
|
|
userId: string;
|
2024-03-26 21:09:41 +08:00
|
|
|
username: string;
|
|
|
|
hasUserInfo: boolean;
|
|
|
|
}
|
2024-03-26 11:54:30 +08:00
|
|
|
|
2024-04-02 18:45:46 +08:00
|
|
|
export function reloadUser(messageApi: MessageInstance, globalDispatchContext: Dispatch<GlobalDataAction>) {
|
|
|
|
return new Promise<any>(resolve => {
|
|
|
|
get<any>({
|
|
|
|
messageApi,
|
|
|
|
url: '/api/user-info/get-user-self',
|
|
|
|
onSuccess({data}) {
|
|
|
|
globalDispatchContext({
|
|
|
|
type: GlobalDataActionType.REFRESH_SELF,
|
|
|
|
user: {
|
|
|
|
balance: (Math.floor(data.accountMoney) / 100).toFixed(2),
|
|
|
|
nickname: data.nickname,
|
2024-04-19 18:20:51 +08:00
|
|
|
userId: data.userId,
|
2024-04-02 18:45:46 +08:00
|
|
|
username: data.username,
|
|
|
|
hasUserInfo: data.hasUserInfo,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
resolve(data);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-26 11:54:30 +08:00
|
|
|
export interface GlobalData {
|
2024-03-26 21:09:41 +08:00
|
|
|
user: User;
|
2024-03-26 11:54:30 +08:00
|
|
|
}
|
|
|
|
|
2024-03-26 21:09:41 +08:00
|
|
|
export interface GlobalDataAction {
|
|
|
|
type: number;
|
|
|
|
user?: User;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const GlobalContext = createContext<GlobalData>({
|
|
|
|
user: {
|
|
|
|
balance: '0',
|
|
|
|
nickname: '',
|
2024-04-19 18:20:51 +08:00
|
|
|
userId: '',
|
2024-03-26 21:09:41 +08:00
|
|
|
username: '',
|
|
|
|
hasUserInfo: false
|
|
|
|
}
|
|
|
|
});
|
2024-04-02 18:45:46 +08:00
|
|
|
export const GlobalDispatchContext = createContext<Dispatch<GlobalDataAction>>(() => {
|
|
|
|
});
|