Page({ data: { templateId: '', subscribeMessageResult: '', requestSubscribeMessageResult: '', wxacodeSrc: '', wxacodeResult: '', showClearWXACodeCache: false, }, async getSubscribeMessageTemplate() { try { const { result } = await wx.cloud.callFunction({ name: 'openapi', data: { action: 'requestSubscribeMessage', }, }) const templateId = result console.warn('[云函数] [openapi] 获取订阅消息模板 调用成功:', templateId) this.setData({ templateId, }) } catch (err) { wx.showToast({ icon: 'none', title: '调用失败', }) console.error('[云函数] [openapi] 获取订阅消息模板 调用失败:', err) } }, async requestSubscribeMessage() { const templateId = this.data.templateId if (!templateId) { wx.showModal({ title: '发送失败', content: '请先获取模板 ID', showCancel: false, }) } wx.requestSubscribeMessage({ tmplIds: [templateId], success: (res) => { if (res[templateId] === 'accept') { this.setData({ requestSubscribeMessageResult: '成功', }) } else { this.setData({ requestSubscribeMessageResult: `失败(${res[templateId]})`, }) } }, fail: (err) => { this.setData({ requestSubscribeMessageResult: `失败(${JSON.stringify(err)})`, }) }, }) }, sendSubscribeMessage(e) { this.setData({ subscribeMessageResult: '', }) wx.cloud.callFunction({ name: 'openapi', data: { action: 'sendSubscribeMessage', templateId: this.data.templateId, }, success: res => { console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res) wx.showModal({ title: '发送成功', content: '请返回微信主界面查看', showCancel: false, }) wx.showToast({ title: '发送成功,请返回微信主界面查看', }) this.setData({ subscribeMessageResult: JSON.stringify(res.result) }) }, fail: err => { wx.showToast({ icon: 'none', title: '调用失败', }) console.error('[云函数] [openapi] subscribeMessage.send 调用失败:', err) } }) }, submitSubscribeMessageForm(e) { this.setData({ subscribeMessageResult: '', }) wx.cloud.callFunction({ name: 'openapi', data: { action: 'sendSubscribeMessage', formId: e.detail.formId, }, success: res => { console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res) wx.showModal({ title: '发送成功', content: '请返回微信主界面查看', showCancel: false, }) wx.showToast({ title: '发送成功,请返回微信主界面查看', }) this.setData({ templateMessageResult: JSON.stringify(res.result) }) }, fail: err => { wx.showToast({ icon: 'none', title: '调用失败', }) console.error('[云函数] [openapi] templateMessage.send 调用失败:', err) } }) }, onGetWXACode() { this.setData({ wxacodeSrc: '', wxacodeResult: '', showClearWXACodeCache: false, }) // 此处为演示,将使用 localStorage 缓存,正常开发中文件 ID 应存在数据库中 const fileID = wx.getStorageSync('wxacodeCloudID') if (fileID) { // 有云文件 ID 缓存,直接使用该 ID // 如需清除缓存,选择菜单栏中的 “工具 -> 清除缓存 -> 清除数据缓存”,或在 Storage 面板中删掉相应的 key this.setData({ wxacodeSrc: fileID, wxacodeResult: `从本地缓存中取得了小程序码的云文件 ID`, showClearWXACodeCache: true, }) console.log(`从本地缓存中取得了小程序码的云文件 ID:${fileID}`) } else { wx.cloud.callFunction({ name: 'openapi', data: { action: 'getWXACode', }, success: res => { console.warn('[云函数] [openapi] wxacode.get 调用成功:', res) wx.showToast({ title: '调用成功', }) this.setData({ wxacodeSrc: res.result, wxacodeResult: `云函数获取二维码成功`, showClearWXACodeCache: true, }) wx.setStorageSync('wxacodeCloudID', res.result) }, fail: err => { wx.showToast({ icon: 'none', title: '调用失败', }) console.error('[云函数] [openapi] wxacode.get 调用失败:', err) } }) } }, clearWXACodeCache() { wx.removeStorageSync('wxacodeCloudID') this.setData({ wxacodeSrc: '', wxacodeResult: '', showClearWXACodeCache: false, }) wx.showToast({ title: '清除成功', }) }, })