1、创建配置文件
// utils/cloudConfig.js
// 云开发环境配置// 当前小程序配置
const currentConfig = {env: "cloudbase-6goxxxxxxd6c75e0", // 当前小程序环境 IDappid: "wxdexxxxx5dbcf04", // 当前小程序 AppID
};// 共享云开发环境配置
const sharedConfig = {resourceAppid: "wx9xxxxxxxx142c", // 资源方小程序 AppIDresourceEnv: "xxve-0gxxxxx521f5", // 资源方环境 ID
};module.exports = {currentConfig,sharedConfig,
};
2、封装请求(云函数为例)
提示:不会用云函数可看之前的文章:
// utils/cloudRequest.js
// 云函数请求封装工具const app = getApp();/*** 调用共享云函数* @param {string} functionName - 云函数名称* @param {object} data - 传递的数据* @returns {Promise} 返回云函数调用结果*/
async function callSharedFunction(functionName, data = {}) {try {// 等待共享云开发环境初始化完成,下一步展示具体内容await app.waitForSharedCloudReady();if (!app.globalData.sharedCloud) {throw new Error("共享云开发环境未初始化");}const result = await app.globalData.sharedCloud.callFunction({name: functionName,data: data,});console.log(`共享云函数 ${functionName} 调用成功:`, result);return result;} catch (error) {console.error(`共享云函数 ${functionName} 调用失败:`, error);// 显示错误提示wx.showToast({title: "获取数据失败",icon: "none",});throw error;}
}/*** 通用数据获取方法* @param {object} data - 配置选项* @param {object} data.gather - 集合名称,必填* @param {object} data.page - 页码,默认1* @param {object} data.pageSize - 每页数量,默认20,pageSize和size二选一* @returns {Promise} 返回数据*/
async function getCloudFunctionData(data = {}) {try {const res = await callSharedFunction("云函数名", data);if (res && res.result) {return res.result;} else {// console.warn(`获取 ${functionName} 数据为空或格式不正确:`, res.result);return {};}} catch (error) {// console.error(`获取 ${functionName} 数据失败:`, error);throw error;}
}module.exports = {callSharedFunction,getCloudFunctionData
};
提示:以下是本篇文章正文内容,下面案例可供参考
3、初始化云开发
// app.js
const { currentConfig, sharedConfig } = require("./utils/cloudConfig");App({onLaunch() {// 初始化当前小程序的云开发环境wx.cloud.init({env: currentConfig.env, // 当前小程序环境 IDappid: currentConfig.appid, // 当前小程序 AppIDtraceUser: true, // 开启用户追踪(可选)});// 初始化共享云开发环境this.initSharedCloud();},// 初始化共享云开发环境async initSharedCloud() {try {// 创建共享云开发实例const sharedCloud = new wx.cloud.Cloud({resourceAppid: sharedConfig.resourceAppid, // 资源方小程序 AppIDresourceEnv: sharedConfig.resourceEnv, // 资源方环境 ID});// 初始化共享云开发环境await sharedCloud.init();// 将共享云开发实例保存到全局数据中this.globalData.sharedCloud = sharedCloud;this.globalData.sharedCloudReady = true; // 标记初始化完成console.log("共享云开发环境初始化成功");// 触发初始化完成事件this.triggerSharedCloudReady();} catch (error) {console.error("共享云开发环境初始化失败:", error);this.globalData.sharedCloudReady = false;}},// 触发共享云开发环境初始化完成事件triggerSharedCloudReady() {if (this.globalData.sharedCloudReadyCallbacks &&this.globalData.sharedCloudReadyCallbacks.length > 0) {this.globalData.sharedCloudReadyCallbacks.forEach((callback) => {try {callback();} catch (error) {console.error("执行共享云开发环境就绪回调失败:", error);}});this.globalData.sharedCloudReadyCallbacks = [];}},// 等待共享云开发环境初始化完成waitForSharedCloudReady() {return new Promise((resolve, reject) => {if (this.globalData.sharedCloudReady) {resolve();} else {// 添加回调到队列if (!this.globalData.sharedCloudReadyCallbacks) {this.globalData.sharedCloudReadyCallbacks = [];}this.globalData.sharedCloudReadyCallbacks.push(resolve);// 设置超时setTimeout(() => {reject(new Error("共享云开发环境初始化超时"));}, 10000); // 10秒超时}});},globalData: {sharedCloud: null,sharedCloudReady: false, // 共享云开发环境是否初始化完成sharedCloudReadyCallbacks: [], // 等待初始化的回调队列},
});
4、使用
// index.js
const { getCloudFunctionData } = require("../../utils/cloudRequest.js");Page({/*** 页面的初始数据*/data: {},// 共享数据async getTopicList() {try {const res = await getCloudFunctionData({ gather: "你的集合名" });console.log(res);} catch (err) {console.log(err);}},/*** 生命周期函数--监听页面加载*/onLoad(options) {this.getTopicList();},
});