微信小程序云開發(fā)SDK文檔 開發(fā)能力

2022-05-12 16:35 更新

Cloud.openapi

云調用 API 對象。


Cloud.CloudID(cloudID: string)

支持端:小程序 2.7.0

聲明字符串為 CloudID(開放數(shù)據(jù) ID),該接口傳入一個字符串,返回一個 CloudID 特殊對象,將該對象傳至云函數(shù)可以獲取其對應的開放數(shù)據(jù)。詳見通過云調用獲取開放數(shù)據(jù)

參數(shù)

cloudID: string

通過開放能力在小程序端獲取得到的 CloudID

示例代碼

小程序端調用

wx.cloud.callFunction({
  name: 'myFunction',
  data: {
    weRunData: wx.cloud.CloudID('xxx'), // 這個 CloudID 值到云函數(shù)端會被替換
    obj: {
      shareInfo: wx.cloud.CloudID('yyy'), // 非頂層字段的 CloudID 不會被替換,會原樣字符串展示
    }
  }
})

在云函數(shù)端接收到的 event 將會包含對應開放數(shù)據(jù)的對象,其中 event.weRunData 會因為符合規(guī)則而包含開放數(shù)據(jù),event.shareInfo 則不會,event 結構將如下:

{
  "weRunData": {
    "cloudID": "27_Ih-9vxDaOhIbh48Bdpk90DUkUoNMAPaNtg7OSGM-P2wPEk1NbspjKGoql_g",
    "data": {
      "stepInfoList": [
        {
          "step": 9103,
          "timestamp": 1571673600
        },
        {
          "step": 9783,
          "timestamp": 1571760000
        }
      ],
      "watermark": {
        "appid": "wx3d289323f5900f8e",
        "timestamp": 1574338655
      }
    }
  },
  "obj": {
    "shareInfo": "xxx"
  }
}

Cloud.getOpenData(list: string[]): Object

支持端:云函數(shù)

獲取 CloudID 對應的開放數(shù)據(jù)

參數(shù)

list: string[]

要獲取對應開放數(shù)據(jù)的 CloudID 列表

返回值

Object

屬性 類型 說明
list Array.<Object> 開放數(shù)據(jù)列表,與傳入的 CloudID 列表一一對應

list 的結構

屬性 類型 說明
cloudID string 開放數(shù)據(jù) CloudID
data Object 開放數(shù)據(jù)

說明

詳見通過云調用獲取開放數(shù)據(jù)

示例代碼

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const res = await cloud.getOpenData({
    list: event.openData.list, // 假設 event.openData.list 是一個 CloudID 字符串列表
  })
  return res.list
}

返回的結果結構類似如下(假設 list 長度為 1,其中的 CloudID 是微信運動數(shù)據(jù)的 CloudID):

[{
  "cloudID": "27_Ih-9vxDaOhIbh48Bdpk90DUkUoNMAPaNtg7OSGM-P2wPEk1NbspjKGoql_g",
  "data": {
    "stepInfoList": [
      {
        "step": 9103,
        "timestamp": 1571673600
      },
      {
        "step": 9783,
        "timestamp": 1571760000
      }
    ],
    "watermark": {
      "appid": "wx3d289323f5900f8e",
      "timestamp": 1574338655
    }
  }

Cloud.getVoIPSign(options: Object): Promise<Object>

支持端:云函數(shù)

獲取實時語音簽名

參數(shù)

options: Object

屬性類型默認值必填說明
groupIdstring游戲房間的標識
noncestring隨機字符串,長度應小于 128
timestampnumber生成這個隨機字符串的 UNIX 時間戳(精確到秒)

返回值

Promise.<Object>

屬性類型說明
signaturestring簽名

示例代碼

const cloud = require('wx-server-sdk')
cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

exports.main = async (event, context) => {
  const result = await cloud.getVoIPSign({
    groupId: 'xxx',
    timestamp: 1557056066,
    nonce: 'yyy'
  })
  return result.fileListt
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號