70 lines
2.3 KiB
TypeScript
70 lines
2.3 KiB
TypeScript
import WuhuBase from "../WuhuBase";
|
|
import Alert from "./Alert";
|
|
import ISidebarData from "../../interface/ISidebarData";
|
|
import Log from "../Log";
|
|
import CommonUtils from "./CommonUtils";
|
|
import FetchUtils from "./FetchUtils";
|
|
|
|
export default class InfoUtils extends WuhuBase {
|
|
className = 'InfoUtils';
|
|
|
|
/**
|
|
* 返回玩家信息的对象 { playername: string, userID: number }
|
|
* @return {PlayerInfo} rs
|
|
*/
|
|
public getPlayerInfo(): PlayerInfo {
|
|
const node = document.querySelector('script[uid]');
|
|
if (node) {
|
|
return {
|
|
playername: node.getAttribute('name'),
|
|
userID: parseInt(node.getAttribute('uid')),
|
|
}
|
|
} else {
|
|
new Alert('严重错误:芜湖助手无法获取用户数据,已退出');
|
|
throw '芜湖助手无法获取用户数据';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 返回玩家的部分实时数据
|
|
*/
|
|
public async getSessionData(): Promise<ISidebarData> {
|
|
let field: string = 'sidebarData' + this.getPlayerInfo().userID;
|
|
let ret: ISidebarData = {};
|
|
return new Promise(async resolve => {
|
|
let c = 0;
|
|
while (!sessionStorage.getItem(field) && c < 50) {
|
|
c++;
|
|
await CommonUtils.getInstance().sleep(10);
|
|
}
|
|
if (sessionStorage.getItem(field)) {
|
|
ret = JSON.parse(sessionStorage.getItem(field));
|
|
} else {
|
|
Log.info('无法从sessionStorage获取数据')
|
|
ret = await (await FetchUtils.getInstance().ajaxFetch({
|
|
url: window.addRFC('/sidebarAjaxAction.php?q=getSidebarData'),
|
|
method: 'POST',
|
|
})).json();
|
|
sessionStorage.setItem(field, JSON.stringify(ret));
|
|
}
|
|
ret.headerData = JSON.parse(sessionStorage.getItem('headerData'));
|
|
resolve(ret);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 返回玩家当前的边栏菜单列表 如gym crimes
|
|
*/
|
|
public async getSidebarData() {
|
|
return (await this.getSessionData()).areas;
|
|
}
|
|
|
|
/**
|
|
* 玩家的基础信息
|
|
* {status: string, isLoggedIn: boolean, isDonator: boolean, isTravelling: boolean, isAbroad: boolean}
|
|
*/
|
|
public async getUserState() {
|
|
return (await this.getSessionData()).headerData.user.state;
|
|
}
|
|
}
|