| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import { APP_FAVO_ADD, APP_FAVO_REMOVE, APP_GAME_LIST, success } from "@scripts/api";
- import { Http } from "@scripts/request";
- import { IGameInfo, IGameListParams } from "./data";
- import { CurrencyType } from "@scripts/define/Define";
- const UPDATE_LIST_CD = 10 * 60 * 1000;
- interface IGameCaches {
- list: IGameInfo[],
- update_at: number,
- }
- class GameStore {
- private _gameListMap: { [key: string]: IGameCaches } = {};
- private _currencyNow: CurrencyType;
- private _favoriting: boolean = false;
- public get favoriting() { return this._favoriting; }
- public gameID = 0;
- public gameLink = "";
- clearGame() {
- this.gameID = 0;
- this.gameLink = "";
- }
- private findGameList(key = '') {
- if (!key) key = this._currencyNow;
- return this._gameListMap[key]?.list || [];
- }
- async reqGameList(currency: CurrencyType, cache = true) {
- this._currencyNow = currency;
- const gameCache = this._gameListMap[currency];
- if (gameCache && cache) {
- if (Date.now() - gameCache.update_at < UPDATE_LIST_CD) {
- return true;
- }
- }
- const params: IGameListParams = {
- currency: currency,
- favorite: 0
- }
- const res = await Http.call<IGameInfo[]>(APP_GAME_LIST, params as any);
- if (success(res)) {
- this._gameListMap[currency] = {
- list: res.data,
- update_at: Date.now(),
- };
- return true;
- }
- return false;
- }
- /**
- * 获取游戏列表
- * @param tagId 0 不筛选标签
- * @returns
- */
- getGameList(tagId = 0) {
- const gameList = this.findGameList();
- if (tagId === 0) {
- return gameList;
- }
- return gameList.map(v => {
- if (v.tags.find(t => t.tagId === tagId)) {
- return v;
- }
- })
- }
- getFavorites() {
- const gameList = this.findGameList();
- return gameList.map(v => v.isFavorite === 1);
- }
- async reqFavoAdd(id: number) {
- if (this._favoriting) return;
- this._favoriting = true;
- const res = await Http.call(APP_FAVO_ADD, { id });
- this._favoriting = false;
- if (success(res)) {
- const gameList = this.findGameList();
- const game = gameList.find(g => g.gameID === id);
- if (game) {
- game.isFavorite = 1;
- return game;
- }
- }
- }
- async reqFavoRemove(id: number) {
- if (this._favoriting) return;
- this._favoriting = true;
- const res = await Http.call(APP_FAVO_REMOVE, { id });
- this._favoriting = false;
- if (success(res)) {
- const gameList = this.findGameList();
- const game = gameList.find(g => g.gameID === id);
- if (game) {
- game.isFavorite = 0;
- return game;
- }
- }
- }
- }
- export const gameStore = new GameStore();
|