123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import Taro from '@tarojs/taro'
- import { Game } from '@/stores/game'
- // 控制是否使用云函数(true)或mock数据(false)
- const USE_CLOUD = false
- // Mock数据
- const mockGames: Game[] = [
- {
- id: '1',
- title: '海龟汤',
- image: 'https://images.unsplash.com/photo-1582845512747-e42001c95638?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '3-10',
- duration: '30-60',
- rating: 4.8,
- isNew: true,
- isHot: true,
- description: '海龟汤是一种猜谜游戏,游戏开始时,主持人会讲述一个故事的结果,参与者需要通过提问来猜测故事的真相。',
- rules: '1. 主持人讲述一个故事的结果\n2. 参与者通过提问来猜测故事的真相\n3. 主持人只能回答"是"、"否"或"不重要"',
- category: '推理',
- tips: [
- '提问时要注意逻辑思维',
- '尝试从多个角度思考问题',
- '注意主持人的回答,特别是"不重要"的部分'
- ],
- examples: [
- {
- question: '一个人走进酒吧,点了一杯水,喝完就离开了。为什么?',
- answer: '这个人有打嗝的困扰,想通过喝水来缓解。'
- }
- ]
- },
- {
- id: '2',
- title: '剧本杀',
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '4-8',
- duration: '120-180',
- rating: 4.5,
- isNew: false,
- isHot: true,
- description: '剧本杀是一种角色扮演游戏,每个玩家扮演一个角色,通过阅读剧本和相互交流来解决一个谜题。',
- rules: '1. 每个玩家扮演一个角色\n2. 阅读剧本并获取个人信息\n3. 通过交流和推理解决谜题',
- category: '角色扮演',
- tips: [
- '认真阅读你的角色背景',
- '注意收集和分析信息',
- '积极参与角色扮演和讨论'
- ],
- examples: [
- {
- question: '谁是凶手?',
- answer: '根据线索和证据,管家是凶手。'
- }
- ]
- },
- {
- id: '3',
- title: '狼人杀',
- image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
- players: '6-12',
- duration: '30-45',
- rating: 4.7,
- isNew: false,
- isHot: true,
- description: '狼人杀是一种桌游,玩家分为狼人和村民两个阵营,狼人试图消灭村民,村民则要找出并消灭狼人。',
- rules: '1. 玩家分为狼人和村民两个阵营\n2. 夜晚狼人选择一名玩家"杀死"\n3. 白天所有人讨论并投票处决一名玩家',
- category: '桌游',
- tips: [
- '仔细观察其他玩家的言行',
- '理性分析每晚的死亡情况',
- '善用自己的角色技能'
- ],
- examples: [
- {
- question: '如何判断谁是狼人?',
- answer: '通过分析发言逻辑、投票行为和表情变化,找出可疑的玩家。'
- }
- ]
- }
- ]
- // Mock用户数据
- const mockUser = {
- nickname: '测试用户',
- avatar: 'https://example.com/avatar.jpg',
- openid: 'mock_openid_123456',
- }
- // API方法
- export const gameAPI = {
- async getGames(): Promise<Game[]> {
- if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
- try {
- // 云函数实现
- const result = await Taro.cloud.callFunction({
- name: 'getGames',
- })
-
- // 使用类型断言处理结果
- if (result && typeof result === 'object' && 'result' in result) {
- const cloudResult = result.result as any
- return cloudResult?.data || mockGames
- }
- return mockGames
- } catch (error) {
- console.error('获取游戏列表失败:', error)
- return mockGames
- }
- } 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 {
- // 云函数实现
- const result = await Taro.cloud.callFunction({
- name: 'getGameDetail',
- data: { id }
- })
-
- // 使用类型断言处理结果
- if (result && typeof result === 'object' && 'result' in result) {
- const cloudResult = result.result as any
- return cloudResult?.data || null
- }
- return null
- } catch (error) {
- console.error('获取游戏详情失败:', error)
- return null
- }
- } else {
- // Mock实现
- return new Promise(resolve => {
- setTimeout(() => {
- const game = mockGames.find(g => g.id === id)
- resolve(game || null)
- }, 300) // 模拟网络延迟
- })
- }
- }
- }
- export const userAPI = {
- async getUserInfo() {
- if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
- try {
- // 云函数实现
- const result = await Taro.cloud.callFunction({
- name: 'getUserInfo',
- })
-
- // 使用类型断言处理结果
- if (result && typeof result === 'object' && 'result' in result) {
- const cloudResult = result.result as any
- return cloudResult?.data || mockUser
- }
- return mockUser
- } catch (error) {
- console.error('获取用户信息失败:', error)
- return mockUser
- }
- } else {
- // Mock实现
- return new Promise(resolve => {
- setTimeout(() => {
- resolve(mockUser)
- }, 300)
- })
- }
- },
-
- // 新增方法:获取OpenID
- async getOpenId(code: string) {
- if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
- try {
- const result = await Taro.cloud.callFunction({
- name: 'getOpenId',
- data: { code }
- })
-
- if (result && result.result) {
- return result.result
- }
- } catch (error) {
- console.error('获取OpenID失败:', error)
- }
- }
-
- // Mock实现
- return { openid: 'mock_openid_' + Date.now() }
- }
- }
- // 导出roomAPI
- export { roomAPI } from './api/room'
|