api.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. import Taro from '@tarojs/taro'
  2. import { Game } from '@/stores/game'
  3. // 控制是否使用云函数(true)或mock数据(false)
  4. const USE_CLOUD = false
  5. // Mock数据
  6. const mockGames: Game[] = [
  7. {
  8. id: '1',
  9. title: '海龟汤',
  10. image: 'https://images.unsplash.com/photo-1582845512747-e42001c95638?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  11. players: '3-10',
  12. duration: '30-60',
  13. rating: 4.8,
  14. isNew: true,
  15. isHot: true,
  16. description: '海龟汤是一种猜谜游戏,游戏开始时,主持人会讲述一个故事的结果,参与者需要通过提问来猜测故事的真相。',
  17. rules: '1. 主持人讲述一个故事的结果\n2. 参与者通过提问来猜测故事的真相\n3. 主持人只能回答"是"、"否"或"不重要"',
  18. category: '推理',
  19. tips: [
  20. '提问时要注意逻辑思维',
  21. '尝试从多个角度思考问题',
  22. '注意主持人的回答,特别是"不重要"的部分'
  23. ],
  24. examples: [
  25. {
  26. question: '一个人走进酒吧,点了一杯水,喝完就离开了。为什么?',
  27. answer: '这个人有打嗝的困扰,想通过喝水来缓解。'
  28. }
  29. ]
  30. },
  31. {
  32. id: '2',
  33. title: '剧本杀',
  34. image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  35. players: '4-8',
  36. duration: '120-180',
  37. rating: 4.5,
  38. isNew: false,
  39. isHot: true,
  40. description: '剧本杀是一种角色扮演游戏,每个玩家扮演一个角色,通过阅读剧本和相互交流来解决一个谜题。',
  41. rules: '1. 每个玩家扮演一个角色\n2. 阅读剧本并获取个人信息\n3. 通过交流和推理解决谜题',
  42. category: '角色扮演',
  43. tips: [
  44. '认真阅读你的角色背景',
  45. '注意收集和分析信息',
  46. '积极参与角色扮演和讨论'
  47. ],
  48. examples: [
  49. {
  50. question: '谁是凶手?',
  51. answer: '根据线索和证据,管家是凶手。'
  52. }
  53. ]
  54. },
  55. {
  56. id: '3',
  57. title: '狼人杀',
  58. image: 'https://images.unsplash.com/photo-1529156069898-49953e39b3ac?ixlib=rb-1.2.1&auto=format&fit=crop&w=400&h=100&q=80',
  59. players: '6-12',
  60. duration: '30-45',
  61. rating: 4.7,
  62. isNew: false,
  63. isHot: true,
  64. description: '狼人杀是一种桌游,玩家分为狼人和村民两个阵营,狼人试图消灭村民,村民则要找出并消灭狼人。',
  65. rules: '1. 玩家分为狼人和村民两个阵营\n2. 夜晚狼人选择一名玩家"杀死"\n3. 白天所有人讨论并投票处决一名玩家',
  66. category: '桌游',
  67. tips: [
  68. '仔细观察其他玩家的言行',
  69. '理性分析每晚的死亡情况',
  70. '善用自己的角色技能'
  71. ],
  72. examples: [
  73. {
  74. question: '如何判断谁是狼人?',
  75. answer: '通过分析发言逻辑、投票行为和表情变化,找出可疑的玩家。'
  76. }
  77. ]
  78. }
  79. ]
  80. // Mock用户数据
  81. const mockUser = {
  82. nickname: '测试用户',
  83. avatar: 'https://example.com/avatar.jpg',
  84. openid: 'mock_openid_123456',
  85. }
  86. // API方法
  87. export const gameAPI = {
  88. async getGames(): Promise<Game[]> {
  89. if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
  90. try {
  91. // 云函数实现
  92. const result = await Taro.cloud.callFunction({
  93. name: 'getGames',
  94. })
  95. // 使用类型断言处理结果
  96. if (result && typeof result === 'object' && 'result' in result) {
  97. const cloudResult = result.result as any
  98. return cloudResult?.data || mockGames
  99. }
  100. return mockGames
  101. } catch (error) {
  102. console.error('获取游戏列表失败:', error)
  103. return mockGames
  104. }
  105. } else {
  106. // Mock实现
  107. return new Promise(resolve => {
  108. setTimeout(() => {
  109. resolve(mockGames)
  110. }, 500) // 模拟网络延迟
  111. })
  112. }
  113. },
  114. async getGameDetail(id: string): Promise<Game | null> {
  115. if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
  116. try {
  117. // 云函数实现
  118. const result = await Taro.cloud.callFunction({
  119. name: 'getGameDetail',
  120. data: { id }
  121. })
  122. // 使用类型断言处理结果
  123. if (result && typeof result === 'object' && 'result' in result) {
  124. const cloudResult = result.result as any
  125. return cloudResult?.data || null
  126. }
  127. return null
  128. } catch (error) {
  129. console.error('获取游戏详情失败:', error)
  130. return null
  131. }
  132. } else {
  133. // Mock实现
  134. return new Promise(resolve => {
  135. setTimeout(() => {
  136. const game = mockGames.find(g => g.id === id)
  137. resolve(game || null)
  138. }, 300) // 模拟网络延迟
  139. })
  140. }
  141. }
  142. }
  143. export const userAPI = {
  144. async getUserInfo() {
  145. if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
  146. try {
  147. // 云函数实现
  148. const result = await Taro.cloud.callFunction({
  149. name: 'getUserInfo',
  150. })
  151. // 使用类型断言处理结果
  152. if (result && typeof result === 'object' && 'result' in result) {
  153. const cloudResult = result.result as any
  154. return cloudResult?.data || mockUser
  155. }
  156. return mockUser
  157. } catch (error) {
  158. console.error('获取用户信息失败:', error)
  159. return mockUser
  160. }
  161. } else {
  162. // Mock实现
  163. return new Promise(resolve => {
  164. setTimeout(() => {
  165. resolve(mockUser)
  166. }, 300)
  167. })
  168. }
  169. },
  170. // 新增方法:获取OpenID
  171. async getOpenId(code: string) {
  172. if (USE_CLOUD && process.env.TARO_ENV === 'weapp') {
  173. try {
  174. const result = await Taro.cloud.callFunction({
  175. name: 'getOpenId',
  176. data: { code }
  177. })
  178. if (result && result.result) {
  179. return result.result
  180. }
  181. } catch (error) {
  182. console.error('获取OpenID失败:', error)
  183. }
  184. }
  185. // Mock实现
  186. return { openid: 'mock_openid_' + Date.now() }
  187. }
  188. }
  189. // 导出roomAPI
  190. export { roomAPI } from './api/room'