// app.js App({ onLaunch() { // 初始化云开发环境 if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力') wx.showToast({ title: '请升级微信开发者工具', icon: 'none' }) return } // 检查云开发环境是否可用 wx.cloud.init({ appid: 'wxc45a0df13aec69ae', // 请替换为创建云开发环境的小程序AppID envid: 'cloud1', traceUser: true, success: res => { console.log('云开发初始化成功', res) // 检查云函数是否可用 wx.cloud.callFunction({ name: 'getTestData', data: {}, success: () => console.log('云函数连接测试成功'), fail: err => { console.error('云函数连接测试失败', err) wx.showToast({ title: '云函数未部署,请上传云函数', icon: 'none', duration: 3000 }) } }) }, fail: err => { console.error('云开发初始化失败', err) let errMsg = '云开发初始化失败' if (err.errCode === -601002) { errMsg = '环境ID无效或未开通云服务' } else if (err.errCode === -502001) { errMsg = '网络连接失败,请检查网络' } else if (err.errCode === -601003) { errMsg = 'AppID与云环境不匹配' } wx.showToast({ title: errMsg, icon: 'none', duration: 3000 }) } }) // 展示本地存储能力 const logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId } }) }, globalData: { userInfo: null } })