|
@@ -1,6 +1,32 @@
|
|
//处理通用游戏列表和游戏详情的获取
|
|
//处理通用游戏列表和游戏详情的获取
|
|
import Taro from '@tarojs/taro'
|
|
import Taro from '@tarojs/taro'
|
|
-import { Game } from '@/stores/game'
|
|
|
|
|
|
+
|
|
|
|
+// 游戏数据接口移动到API层
|
|
|
|
+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;
|
|
|
|
+ }[];
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// API返回结果接口
|
|
|
|
+export interface ApiResult<T> {
|
|
|
|
+ success: boolean;
|
|
|
|
+ data?: T;
|
|
|
|
+ message?: string;
|
|
|
|
+}
|
|
|
|
|
|
// 控制是否使用云函数(true)或mock数据(false)
|
|
// 控制是否使用云函数(true)或mock数据(false)
|
|
const USE_CLOUD = false
|
|
const USE_CLOUD = false
|
|
@@ -83,9 +109,10 @@ const mockGames: Game[] = [
|
|
|
|
|
|
// 游戏通用API
|
|
// 游戏通用API
|
|
export const gameAPI = {
|
|
export const gameAPI = {
|
|
- async getGames(): Promise<Game[]> {
|
|
|
|
- if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
|
|
|
|
- try {
|
|
|
|
|
|
+ // 获取游戏列表
|
|
|
|
+ async getGames(): Promise<ApiResult<Game[]>> {
|
|
|
|
+ try {
|
|
|
|
+ if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
|
|
// 云函数实现
|
|
// 云函数实现
|
|
const result = await Taro.cloud.callFunction({
|
|
const result = await Taro.cloud.callFunction({
|
|
name: 'getGames',
|
|
name: 'getGames',
|
|
@@ -94,26 +121,33 @@ export const gameAPI = {
|
|
// 使用类型断言处理结果
|
|
// 使用类型断言处理结果
|
|
if (result && typeof result === 'object' && 'result' in result) {
|
|
if (result && typeof result === 'object' && 'result' in result) {
|
|
const cloudResult = result.result as any
|
|
const cloudResult = result.result as any
|
|
- return cloudResult?.data || mockGames
|
|
|
|
|
|
+ return {
|
|
|
|
+ success: true,
|
|
|
|
+ data: cloudResult?.data || mockGames
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return mockGames
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error('获取游戏列表失败:', error)
|
|
|
|
- return mockGames
|
|
|
|
|
|
+ return { success: false, message: '获取游戏列表失败' }
|
|
|
|
+ } else {
|
|
|
|
+ // Mock实现
|
|
|
|
+ return new Promise(resolve => {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ resolve({ success: true, data: mockGames })
|
|
|
|
+ }, 500) // 模拟网络延迟
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error('获取游戏列表失败:', error)
|
|
|
|
+ return {
|
|
|
|
+ success: false,
|
|
|
|
+ message: error.message || '获取游戏列表失败'
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- // Mock实现
|
|
|
|
- return new Promise(resolve => {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- resolve(mockGames)
|
|
|
|
- }, 500) // 模拟网络延迟
|
|
|
|
- })
|
|
|
|
}
|
|
}
|
|
},
|
|
},
|
|
|
|
|
|
- async getGameDetail(id: string): Promise<Game | null> {
|
|
|
|
- if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
|
|
|
|
- try {
|
|
|
|
|
|
+ // 获取游戏详情
|
|
|
|
+ async getGameDetail(id: string): Promise<ApiResult<Game | null>> {
|
|
|
|
+ try {
|
|
|
|
+ if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
|
|
// 云函数实现
|
|
// 云函数实现
|
|
const result = await Taro.cloud.callFunction({
|
|
const result = await Taro.cloud.callFunction({
|
|
name: 'getGameDetail',
|
|
name: 'getGameDetail',
|
|
@@ -123,21 +157,27 @@ export const gameAPI = {
|
|
// 使用类型断言处理结果
|
|
// 使用类型断言处理结果
|
|
if (result && typeof result === 'object' && 'result' in result) {
|
|
if (result && typeof result === 'object' && 'result' in result) {
|
|
const cloudResult = result.result as any
|
|
const cloudResult = result.result as any
|
|
- return cloudResult?.data || null
|
|
|
|
|
|
+ return {
|
|
|
|
+ success: true,
|
|
|
|
+ data: cloudResult?.data || null
|
|
|
|
+ }
|
|
}
|
|
}
|
|
- return null
|
|
|
|
- } catch (error) {
|
|
|
|
- console.error('获取游戏详情失败:', error)
|
|
|
|
- return null
|
|
|
|
|
|
+ return { success: false, message: '获取游戏详情失败' }
|
|
|
|
+ } else {
|
|
|
|
+ // Mock实现
|
|
|
|
+ return new Promise(resolve => {
|
|
|
|
+ setTimeout(() => {
|
|
|
|
+ const game = mockGames.find(g => g.id === id)
|
|
|
|
+ resolve({ success: true, data: game || null })
|
|
|
|
+ }, 300) // 模拟网络延迟
|
|
|
|
+ })
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ console.error('获取游戏详情失败:', error)
|
|
|
|
+ return {
|
|
|
|
+ success: false,
|
|
|
|
+ message: error.message || '获取游戏详情失败'
|
|
}
|
|
}
|
|
- } else {
|
|
|
|
- // Mock实现
|
|
|
|
- return new Promise(resolve => {
|
|
|
|
- setTimeout(() => {
|
|
|
|
- const game = mockGames.find(g => g.id === id)
|
|
|
|
- resolve(game || null)
|
|
|
|
- }, 300) // 模拟网络延迟
|
|
|
|
- })
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|