微信小程序跨账号环境共享教程
看了也是有点懵懵的,后面看了很多别的大佬的资料才知道代码可以这么写,难点在调用方的代码,这里给出调用方的代码,如下:
//app.js
App({
globalData: {
},
async onLaunch() {
if (!wx.cloud) {
console.error(请使用 2.2.3 或以上的基础库以使用云能力)
} else {
wx.cloud.init({
env: "xxx",
traceUser: true,
})
}
const updateManager = wx.getUpdateManager()
updateManager.onCheckForUpdate(function(res) {
console.log("小程序是否需要更新:" + res.hasUpdate)
if (res.hasUpdate) {
updateManager.onUpdateReady(function() {
wx.showModal({
title: 更新提示,
content: 新版本已经准备好,是否重启应用?,
success: function(res) {
if (res.confirm) {
updateManager.applyUpdate()
}
}
})
})
}
})
updateManager.onUpdateFailed(function() {
// 新版本下载失败
console.log("更新失败")
})
/*调用方关键代码如下*/
// 声明新的 cloud 实例
var c1 = new wx.cloud.Cloud({
// 资源方 AppID
resourceAppid: xxxx,
// 资源方环境 ID
resourceEnv: shujuku-xxxx,
})
// 跨账号调用,必须等待 init 完成
// init 过程中,资源方小程序对应环境下的 cloudbase_auth 函数会被调用,并需返回协议字段(见下)来确认允许访问、并可自定义安全规则
await c1.init()
c1.database().collection(sort).get()
.then(res => {
console.log(共享环境请求数据成功, res)
})
}
})
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
微信小程序如何发红包?
