# 9、请求多服务空间

# 前端请求多服务空间

# 方式一

const myCloud = uniCloud.init({
  provider: 'aliyun', // aliyun、tencent
  spaceId: 'xxxx-yyy', // 服务空间id
  clientSecret: 'xxxx', // 腾讯云无需填写此参数,阿里云必填
  endpoint: "https://api.next.bspapp.com", // 腾讯云无需填写此参数,阿里云必填,阿里云填 https://api.next.bspapp.com
});
vk.callFunction({
  url: 'template/db_api/pub/count',
  title:'请求中...',
  uniCloud: myCloud,
  success: (data) => {
    console.log(data);
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 方式二

方式二需要 vk-unicloud 核心库版本 >= 2.8.1

app.config.js 中新增配置 uniCloud 属性,如下

{
  uniCloud:{
    // 自定义默认环境(一般无需配置,除非你知道这么配置带来的意义)
    envs:{
      // 当有设置 default 的环境时,不传env,会自动用此环境调用云函数。
      "default":{
        provider:"aliyun", // 阿里云:aliyun 腾讯云:tencent
        spaceId:"", // 从 https://unicloud.dcloud.net.cn/home 获取,服务空间ID,注意是服务空间ID,不是服务空间名称
        clientSecret:"", // 腾讯云无需填写此参数,阿里云必填
        endpoint:"" // 腾讯云无需填写此参数,阿里云必填,阿里云填 https://api.next.bspapp.com
      },
      // a环境
      "a":{
        provider:"aliyun", // 阿里云:aliyun 腾讯云:tencent
        spaceId:"", // 从 https://unicloud.dcloud.net.cn/home 获取,服务空间ID,注意是服务空间ID,不是服务空间名称
        clientSecret:"", // 腾讯云无需填写此参数,阿里云必填
        endpoint:"" // 腾讯云无需填写此参数,阿里云必填,阿里云填 https://api.next.bspapp.com
      },
      // b环境
      "b":{
        provider:"aliyun", // 阿里云:aliyun 腾讯云:tencent
        spaceId:"", // 从 https://unicloud.dcloud.net.cn/home 获取,服务空间ID,注意是服务空间ID,不是服务空间名称
        clientSecret:"", // 腾讯云无需填写此参数,阿里云必填
        endpoint:"" // 腾讯云无需填写此参数,阿里云必填,阿里云填 https://api.next.bspapp.com
      }
    }
  }
}
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
28

前端调用

使用 default 环境调用云函数(如果没有配置 default 环境,则使用项目绑定的云环境调用云函数)

vk.callFunction({
  url: '',
  title: '请求中...',
  data: {

  },
  success: (data) => {

  }
});
1
2
3
4
5
6
7
8
9
10

使用 a 环境调用云函数

vk.callFunction({
  url: '',
  title: '请求中...',
  env: "a", // 使用a环境调用云函数
  data: {

  },
  success: (data) => {

  }
});
1
2
3
4
5
6
7
8
9
10
11

使用 b 环境调用云函数

// 使用 b 环境调用云函数
vk.callFunction({
  url: '',
  title: '请求中...',
  env: "b", // 使用b环境调用云函数
  data: {

  },
  success: (data) => {

  }
});
1
2
3
4
5
6
7
8
9
10
11
12

当有设置 default 的环境时,不传env,会自动用此环境调用云函数。

vk.callFunction 参数说明:点击查看 (opens new window)

# uniCloud.init 参数说明

参数名 类型 必填 默认值 说明
provider String - aliyun、tencent
spaceId String - 服务空间ID,注意是服务空间ID,不是服务空间名称
clientSecret String - 仅阿里云支持,可以在uniCloud控制台服务空间列表中查看
endpoint String https://api.next.bspapp.com 服务空间地址,仅阿里云侧支持 https://api.next.bspapp.com

# 云函数端请求多服务空间

# 方式一

云函数请求另外一个服务空间的数据库

目前仅限腾讯云支持

const myDB = uniCloud.database({
  provider: 'tencent', // aliyun、tencent
  spaceId: 'xxxx-yyy',
  clientSecret: 'xxxx', // 腾讯云无需填写此参数
})
res = await vk.baseDao.select({
  db:myDB,
  dbName:"vk-test",
  pageIndex:1,
  pageSize:100,
  whereJson:{

  },
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# uniCloud.database 参数说明

参数名 类型 必填 默认值 说明
provider String - aliyun、tencent
spaceId String - 服务空间ID,注意是服务空间ID,不是服务空间名称
clientSecret String - 仅阿里云支持,可以在uniCloud控制台服务空间列表中查看
endpoint String https://api.next.bspapp.com 服务空间地址,仅阿里云侧支持 https://api.next.bspapp.com

# 方式二

云函数请求另外一个服务空间的云函数

目前仅限腾讯云支持

const myCloud = uniCloud.init({
  provider: 'tencent', // 目前只支持tencent
  spaceId: 'xxxx-yyy'
})
//通过 myCloud 实例调用API
myCloud.callFunction({
  name:"router", // 大云函数名称
  data:{
    $url:"云函数路由的路径",
    data:{
      // 请求参数
    }
  }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14