微信云开发官网
在小程序中尝试通过 wx.cloud.database() 访问云数据库集合,持续收到错误码 -601002 (SERVICE_FAIL_AUTH_INVALID_USER_TICKET) 。此问题在确认云环境 ID 正确无误、已在 app.js 中正确使用 wx.cloud.init() 初始化,并且对目标集合设置了“所有用户可读,仅创建者可写/所有用户可读可写”以及开启了环境的“未登录用户访问权限”后依然存在。即使使用最简化的数据库读取测试代码,也无法成功。 操作步骤: 1. 在微信开发者工具中创建一个新的小程序项目 2. 开通云开发,并获取云环境 ID。 3. 在云数据库中创建一个名为 pocket_chat_data 的集合 4. 设置 pocket_chat_data 集合的数据库权限为“所有用户可读,仅创建者可写”。 5. 在云开发控制台“概览”->“设置”中,打开环境的“未登录用户访问权限”。 6. 在 `app.js` 的 onLaunch 方法中添加如下初始化和测试代码: // app.js App({ onLaunch() { // 本地存储能力 const logs = wx.getStorageSync('logs') || [] logs.unshift(Date.now()) wx.setStorageSync('logs', logs) // 登录 wx.login({ success: res => { // 发送 res.code 到后台换取 openId, sessionKey, unionId } }) // 初始化云开发环境 if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力') } else { wx.cloud.init({ env: 'cloudbase-8gghdoel1548bd3', traceUser: true, }); const db = wx.cloud.database(); db.collection('pocket_chat_data').limit(1).get({ success: function(res) { console.log('测试数据库读取成功:', res.data); }, fail: function(err) { console.error('测试数据库读取失败:', err); } }); } }, globalData: { userInfo: null, message: [] //缓存消息数据 } }) 编译报错 [图片]
// 公众号基本信息:请修改下列字段使用本示例 请换成自己的 const WX_RESOURCE_APPID = 'wxebbfb0c332a64879' /* 资源方 AppID */ const WX_RESOURCE_ENVID = 'y-xxxx' /* 资源方云开发环境 ID */ const WX_OFFICIAL_ACCOUNT_APPID = 'wxdeb11c5649d3bff6'/* 公众号 AppID */ const WX_AUTH_TYPE = 'snsapi_base' /* 选择需要的授权方式,snsapi_base 或 snsapi_userinfo */ const WX_REDIRECT_URI = 'https://shouyin.yeepay.com/nc-cashier-wap/sccanpay/auth2Callback/wechatLogin' /* 回调 URL */ // // 示例配置 const isPersistLoginInfo = false // 是否需要保留登录态信息到 window 中,只有 snsapi_userinfo 方式需要 if (window.wx) { window.cloud = wx.cloud } var urlSearch = new URLSearchParams(location.search) var accessToken = urlSearch.get('access_token') var refreshToken = urlSearch.get('refresh_token') /** * 检查/发起登录 * 1. 函数会检查当前是否已登录(checkLogin) * 2. 如果未登录,会 10s 后自动发起登录(startLogin) * 3. 如果已登录,会初始化实例,使用指定的微信云开发资源 */ window.doLogin = async () => { try { const checkLoginOptions = { provider: 'OfficialAccount', appid: WX_OFFICIAL_ACCOUNT_APPID, } if (accessToken) { checkLoginOptions.accessToken = accessToken } if (refreshToken) { checkLoginOptions.refreshToken = refreshToken } console.log(`checkLogin options: `, checkLoginOptions) const result = await cloud.checkLogin(checkLoginOptions) console.log(`checkLogin result: `, result) if (isPersistLoginInfo) { window.checkLoginRes = result } if (result.errCode === 0 && result.loggedIn) { console.log(`checkLogin success`) const instance = window.instance = new cloud.Cloud({ appid: WX_OFFICIAL_ACCOUNT_APPID, resourceAppid: WX_RESOURCE_APPID, resourceEnv: WX_RESOURCE_ENVID, }) const initResult = await instance.init() console.log(`instance inited`, initResult) console.log(`can use cloud instance to access resource now !`) return instance } else { console.log(`checkLogin with sdk errCode ${result.errCode} errMsg ${result.errMsg}, will start oauth in 10s`) setTimeout(() => { try { cloud.startLogin({ provider: 'OfficialAccount', appid: WX_OFFICIAL_ACCOUNT_APPID, scope: WX_AUTH_TYPE, redirectUri: WX_REDIRECT_URI+'?redirectUrl='+window.location.href, }) } catch (e) { console.error(`startLogin fail: ${e}`) console.warn(`will start OfficialAccount OAuth login in 10s.`) } }, 3000) } } catch (e) { console.error(e) } } async function getJSSDKSignature(url = window.location.href) { const instance = window.instance let res = {} try { // 在完成云开发登录之后,可以使用云开发 Web SDK 提供的 getJSSDKSignature 方法完成获取网页 wx.config 所需签名 res = await instance.getJSSDKSignature({ url: url // '要签名的网页 URL' }) } catch (error) { console.error(error); } console.log(res, 'res------<'); return res } 调用 checkLogin 获取登录, 提示 redirect_uri 参数错误 [图片]
代码: app.globalData.db.collection('Subjects').where({ _id: "48fe51f767c90ee6007354ad79be3c4a" }).update({ data: { author: "123" } }).then(res => { console.log('更新数据成功',res); return }).catch((e) => { console.log('数据更新失败'); return }) 控制台提示: 更新数据成功 {stats: {…}, errMsg: "collection.update:ok"}errMsg: "collection.update:ok"stats: {updated: 0}__proto__: Objectupdate显示ok,为啥updated显示0 ? 云控制台也显示数据库数据没有更新
我在小程序中使用微信官方自定义 tabBar(tabBar.custom = true,路径为 /custom-tab-bar/index)。在多款真机环境下(如安卓荣耀、微信最新版),都存在同样现象:小程序冷启动进入首页后,用户第一次从首页切到“我的”等 tab 时,底部自定义 tabBar 会短暂整条消失一下再出现,此后在同一进程内多次切换 tab 都正常,不再复现。 目前已按官方文档和社区建议做了如下优化,但问题仍存在: 1)在 lifetimes.attached 中根据当前 route 初始化 selected,并尝试过仅在各页面 onShow 中通过 getTabBar().setData({ selected }) 控制高亮,同时在 custom-tab-bar/index.js 中彻底移除所有对 selected 的 setData,保证状态只由页面 onShow 驱动。 2)在 onSwitchTab 中尝试过多种写法: a. 直接 wx.switchTab({ url }); b. 先 this.setData({ selected: index }) 再用 wx.nextTick(() => wx.switchTab({ url })); c. 按官方建议使用 setTimeout 延迟 50ms 再 wx.switchTab。 3)tabBar 根节点样式已固定:position: fixed; bottom: 0; height: 110rpx; background-color 固定为浅灰,z-index 设为较高值,页面中也未使用 wx.hideTabBar,未叠加 map、video 等原生组件和高 z-index 遮罩层。 4)图标资源为本地文件,已采用双 image + hidden 的预加载方式,且在 app.js 的 onLaunch 中通过 wx.getImageInfo 预加载 tabBar 所需图标,减少首次渲染抖动。 5)已尝试精简 custom-tab-bar 组件初始化逻辑,避免在创建阶段做大量计算,只保留必要的数据和方法。 在上述所有优化组合下,问题表现依旧一致:仅在冷启动后第一次切换 tab 时,底部自定义 tabBar 会短暂整条消失,再重新出现且选中态正确;之后再次切换则完全正常。想请官方帮忙确认: 1)这是否为当前微信自定义 tabBar 在冷启动场景下的已知渲染问题或限制? 2)是否有推荐的官方写法或内部配置,能在继续使用官方自定义 tabBar 的前提下,彻底避免这次首次切换时的底栏“短暂消失”现象?
在云开发创建云函数getProducts的时候,为什么在微信开发者工具没有同步创建文件夹
调用云数据库的代码全删了,本来就写了一句话,还没调试成功,所以一定是删干净了。之后定时去数据概览那里查看次数,次数会定时增加?电脑的数据概览和小程序数据概览都会增加10-30次不等,这是正常现象吗?还是在偷偷计算?正常的话数据概览凭什么算次数呢?你们有遇到过类似的事情吗? [图片]
使用在腾讯微搭调用云函数时,中间需要去发送模板消息通知用户,但是报错 errCode: -501007 invalid parameters | errMsg: officialAccount.messageTemplate.send:fail missing wxCloudApiToken,不知道什么问题? [图片][图片]
[图片][图片] [图片] 如图1当点击头像或者昵称时,报错如图2,工具是1.05版本的,电脑是w7系统升级不了1.05以上版本,之前node.js装的9.6.0,能够正常使用。现在换了appid之后,云函数需要的node.js12.16版本,如图3,电脑重装了系统之后,安装的node.js是12.16.0版本的。现在点击头像或者昵称时就报错,globalthis is not defined.这个错误要怎么解决?
云函数本地调试可以用,但是上传了就报错 云函数报错 日志内容 Request ID: 2630a398-8b5e-487b-8d82-e42d95cd33a4 执行时间: 0ms内存使用: Zero KB 返回结果 (空) 日志 TypeError: Cannot read properties of undefined (reading 'toString') at writeRuntimeFile (/data/scf/frame/node16/runtime.js:65:37) at main (/data/scf/frame/node16/runtime.js:259:7) at Object.<anonymous> (/data/scf/frame/node16/runtime.js:268:1) at Module._compile (node:internal/modules/cjs/loader:1101:14) at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10) at Module.load (node:internal/modules/cjs/loader:981:32) at Function.Module._load (node:internal/modules/cjs/loader:822:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:17:47 START RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Init Report RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Coldstart: 7ms (InitRuntime: 7ms InitFunction: 0ms) Memory: 256MB MemUsage: 0.00MB ERROR RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Result: {"errorCode": -1, "errorMessage": "145 code exit unexpected", "statusCode": 443} END RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Report RequestId: 2630a398-8b5e-487b-8d82-e42d95cd33a4 Duration: 0ms Memory: 256MB MemUsage: 0.000000MB //云函数代码 const sharp = require('sharp'); const fs = require('fs'); const path = require('path'); const cloud = require('wx-server-sdk'); cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); const db = cloud.database(); const processImage = async (tempPath, savePath, targetSizeKB = 100) => { const outputPath = path.join(savePath, `${Date.now()}.jpg`); const maxSize = targetSizeKB * 1024; try { const metadata = await sharp(tempPath).metadata(); let quality = 80; let buffer = await sharp(tempPath) .flatten({ background: '#ffffff' }) // 移除透明背景 .jpeg({ quality }) .toBuffer(); while (buffer.length > maxSize && quality > 30) { quality -= 10; buffer = await sharp(buffer) .jpeg({ quality }) .toBuffer(); } fs.writeFileSync(outputPath, buffer); return outputPath; } catch (err) { return new Error(`图片处理失败: ${err.message}`); } }; exports.main = async (event, context) => { var cloudImgPath = processImage(event.img, event.path) if (typeof (cloudImgPath) === String) { return { code: "100", img: cloudImgPath, msg: "success" } } else { return { code: "500", img: cloudImgPath, msg: "fail" } } }
今天一打开数据库里的图片都显示不出来了,但是文字可以,而且重新上传页显示加载失败,看了云开发,明明还没到期啊😭😭 [图片]
https://developers.weixin.qq.com/miniprogram/dev/devtools/download.html[图片]
项目用的是云开发,开发过程中,会参生很多配置项,我目前的做法是专门创建了一个config的云函数,把配置项都直接写死在config云函数里,小程序打开后,就直接调用config云函数来获取配置信息。如果需要修改配置,直接修改config云函数即可 另外一个方案是在mongodb建一个config表,把配置信息存在config表里。小程序打开后,读取config表的配置 请问哪个方案好呢?
如题,如果不能的话,也太霸王条款了吧……而且标准版199元/月超级贵啊,都够买配置不错的云服务器了!
// 调用微信云开发的短信发送接口 const result = await cloud.openapi.cloudbase.sendSms({ env: 'cloud1xxxx', "phone_number_list": [ phone // 使用传入的手机号 ], "sms_type": "Notification", // 短信类型,可根据需要修改 // 移除content字段,避免与模板发送冲突 "template_id": "844110", // 使用通用的短信模板 // "template_param_list": [ // '验证码是:' + code, // 直接传入验证码,不要添加"验证码"前缀 // "5" // 有效期(分钟) // ], "path": "", "template_param_list": ["能力上新", "/cloud-admin/jump-mp.html"], "resource_appid":"xxxx" });
在云函数中如何处理公众号用户发的Gif图呀,在云函数的日志里根本看不到,哪位大佬教一下~~~
云开发最近在什么都没改动的情况下,突然有开始偶尔报错,查询相关信息后说是数据库访问超时,但目前这张表里只有2.4W条数据,求问有可能是什么原因导致的?使用的是云开发的文档型数据库,大约6月20日开始的,偶现。 [图片]
1图是云函数手动云端测试后,2图是过了一会 [图片][图片][图片]
2024年3月29号通过微信认证审核的境外主体,目前使用“云服务”时被显示“无法开通云服务”。想咨询如何解决这个问题
如题,当前wx.cloud.callFunction不支持流式调用(分块传输),调用大模型,上下文经常会超60s。暂时不想切新版3.7.12去用ai专用接口,来不及测试可能带来的影响。 所以请教下,能否 (1)提高超时上限到120s? (2)支持流式传输
wx.getWeRunData有少数用户表示微信app内显示有步数,在云函数日志系统查看该批次用户的步数基本的都是0,只有偶尔几天是有步数信息的。这是为什么,iOS和安卓都有出现
小程序示例
小程序官方示例Demo,包含云开发示例。
小程序示例
扫码体验