zhangwencui
5 天以前 9af9d62696a57a8ac8603e41931afbe63ae05cb8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Taro from '@tarojs/taro'
 
const isWeapp = process.env.TARO_ENV === 'weapp'
 
export async function callFunction<T = any>(
  name: string,
  data?: Record<string, any>
): Promise<T> {
  if (!isWeapp) {
    const mockModule = await import(`../data/${name}`)
    return mockModule.default(data) as T
  }
  const res = await Taro.cloud.callFunction({ name, data })
  const result = res.result as { code: number; message: string; data: T }
  if (result.code !== 0) {
    console.error(`[Cloud] ${name} failed:`, result.message)
    throw new Error(result.message || '请求失败')
  }
  return result.data
}
 
export function getDatabase() {
  if (!isWeapp) {
    return null
  }
  return Taro.cloud.database()
}