|
@@ -1,73 +1,104 @@
|
|
|
-//管理游戏列表和通用游戏详情
|
|
|
+// 管理游戏列表和通用游戏详情
|
|
|
import { defineStore } from 'pinia'
|
|
|
-import { gameAPI } from '@/services/api/game'
|
|
|
+import { ref, computed } from 'vue'
|
|
|
+import { gameAPI, type Game } from '@/services/api/game'
|
|
|
|
|
|
-// 游戏数据接口
|
|
|
-export interface Game {
|
|
|
- id: string;
|
|
|
- title: string;
|
|
|
- image: string;
|
|
|
- players: string;
|
|
|
- duration: string;
|
|
|
- rating: number;
|
|
|
- isNew: boolean;
|
|
|
- isHot: boolean;
|
|
|
- description: string;
|
|
|
- rules: string;
|
|
|
- category: string;
|
|
|
- tips: string[];
|
|
|
- examples: {
|
|
|
- question: string;
|
|
|
- answer: string;
|
|
|
- }[];
|
|
|
-}
|
|
|
-
|
|
|
-export const useGameStore = defineStore('game', {
|
|
|
- state: () => ({
|
|
|
- games: [] as Game[],
|
|
|
- loading: false,
|
|
|
- currentGame: null as Game | null,
|
|
|
- }),
|
|
|
+export const useGameStore = defineStore('game', () => {
|
|
|
+ // 状态
|
|
|
+ const games = ref<Game[]>([])
|
|
|
+ const currentGame = ref<Game | null>(null)
|
|
|
+ const loading = ref(false)
|
|
|
+ const error = ref<string | null>(null)
|
|
|
+
|
|
|
+ // 计算属性
|
|
|
+ const hotGames = computed(() => games.value.filter(game => game.isHot))
|
|
|
+ const newGames = computed(() => games.value.filter(game => game.isNew))
|
|
|
|
|
|
- actions: {
|
|
|
- // 获取游戏列表
|
|
|
- async fetchGames() {
|
|
|
- this.loading = true;
|
|
|
- try {
|
|
|
- this.games = await gameAPI.getGames();
|
|
|
- } catch (error) {
|
|
|
- console.error('获取游戏列表失败:', error);
|
|
|
- this.games = [];
|
|
|
- } finally {
|
|
|
- this.loading = false;
|
|
|
+ // 获取游戏列表
|
|
|
+ async function fetchGames() {
|
|
|
+ loading.value = true
|
|
|
+ error.value = null
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await gameAPI.getGames()
|
|
|
+
|
|
|
+ if (response.success && response.data) {
|
|
|
+ games.value = response.data
|
|
|
+ } else {
|
|
|
+ error.value = response.message || '获取游戏列表失败'
|
|
|
+ games.value = []
|
|
|
}
|
|
|
- },
|
|
|
+ } catch (e) {
|
|
|
+ console.error('获取游戏列表失败:', e)
|
|
|
+ error.value = '获取游戏列表失败'
|
|
|
+ games.value = []
|
|
|
+ } finally {
|
|
|
+ loading.value = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取游戏详情
|
|
|
+ async function getGameDetail(id: string): Promise<Game | null> {
|
|
|
+ // 先从缓存中查找
|
|
|
+ const cachedGame = games.value.find(game => game.id === id)
|
|
|
+ if (cachedGame) {
|
|
|
+ currentGame.value = cachedGame
|
|
|
+ return cachedGame
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果缓存中没有,从API获取
|
|
|
+ loading.value = true
|
|
|
+ error.value = null
|
|
|
|
|
|
- // 获取游戏详情
|
|
|
- async getGameDetail(id: string) {
|
|
|
- try {
|
|
|
- // 查找本地缓存
|
|
|
- const game = this.games.find(game => game.id === id);
|
|
|
- if (game) {
|
|
|
- this.currentGame = game;
|
|
|
- return game;
|
|
|
- }
|
|
|
-
|
|
|
- // 如果本地没有,从API获取
|
|
|
- const gameData = await gameAPI.getGameDetail(id);
|
|
|
- if (gameData) {
|
|
|
- this.currentGame = gameData;
|
|
|
- return gameData;
|
|
|
- }
|
|
|
-
|
|
|
- // 如果没有找到,返回null
|
|
|
- this.currentGame = null;
|
|
|
- return null;
|
|
|
- } catch (error) {
|
|
|
- console.error('获取游戏详情失败:', error);
|
|
|
- this.currentGame = null;
|
|
|
- return null;
|
|
|
+ try {
|
|
|
+ const response = await gameAPI.getGameDetail(id)
|
|
|
+
|
|
|
+ if (response.success && response.data) {
|
|
|
+ currentGame.value = response.data
|
|
|
+ return response.data
|
|
|
+ } else {
|
|
|
+ error.value = response.message || '获取游戏详情失败'
|
|
|
+ currentGame.value = null
|
|
|
+ return null
|
|
|
}
|
|
|
+ } catch (e) {
|
|
|
+ console.error('获取游戏详情失败:', e)
|
|
|
+ error.value = '获取游戏详情失败'
|
|
|
+ currentGame.value = null
|
|
|
+ return null
|
|
|
+ } finally {
|
|
|
+ loading.value = false
|
|
|
}
|
|
|
}
|
|
|
-})
|
|
|
+
|
|
|
+ // 设置当前游戏
|
|
|
+ function setCurrentGame(game: Game | null) {
|
|
|
+ currentGame.value = game
|
|
|
+ }
|
|
|
+
|
|
|
+ // 清除游戏数据
|
|
|
+ function clearGames() {
|
|
|
+ games.value = []
|
|
|
+ currentGame.value = null
|
|
|
+ error.value = null
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ // 状态
|
|
|
+ games,
|
|
|
+ currentGame,
|
|
|
+ loading,
|
|
|
+ error,
|
|
|
+ hotGames,
|
|
|
+ newGames,
|
|
|
+
|
|
|
+ // 方法
|
|
|
+ fetchGames,
|
|
|
+ getGameDetail,
|
|
|
+ setCurrentGame,
|
|
|
+ clearGames
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+// 重新导出Game类型,方便使用
|
|
|
+export type { Game } from '@/services/api/game'
|