Passwd/miniprogram/pages/openapi/serverapi/serverapi.js
Saturneric fb74e7395f Add
2020-09-01 01:19:39 +08:00

110 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

Page({
data: {
templateMessageResult: '',
wxacodeSrc: '',
wxacodeResult: '',
showClearWXACodeCache: false,
},
submitTemplateMessageForm(e) {
this.setData({
templateMessageResult: '',
})
wx.cloud.callFunction({
name: 'openapi',
data: {
action: 'sendTemplateMessage',
formId: e.detail.formId,
},
success: res => {
console.warn('[云函数] [openapi] templateMessage.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: '清除成功',
})
},
})