123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-server-api/database/
- Page({
- onShareAppMessage() {
- return {
- title: '云函数中使用微信开放能力',
- path: 'packageCloud/pages/scf/scf-openapi/scf-openapi'
- }
- },
- data: {
- theme: 'light',
- sendTemplateMessageResult: '',
- sendTemplateMessageError: false,
- getWXACodeResult: '',
- getWXACodeError: false,
- sendTemplateMessageLoading: false,
- getWXACodeLoading: false,
- theme: 'light'
- },
- onLoad() {
- this.setData({
- theme: wx.getSystemInfoSync().theme || 'light'
- })
- if (wx.onThemeChange) {
- wx.onThemeChange(({theme}) => {
- this.setData({theme})
- })
- }
- this.setData({
- theme: wx.getSystemInfoSync().theme || 'light'
- })
- if (wx.onThemeChange) {
- wx.onThemeChange(({theme}) => {
- this.setData({theme})
- })
- }
- },
- sendTemplateMessageViaCloudFunction(e) {
- this.setData({
- sendTemplateMessageResult: '',
- sendTemplateMessageError: false,
- sendTemplateMessageLoading: true,
- })
- wx.cloud.callFunction({
- name: 'openapi',
- data: {
- action: 'sendTemplateMessage',
- formId: e.detail.formId,
- },
- // eslint-disable-next-line
- }).then((res) => {
- this.setData({
- sendTemplateMessageResult: res,
- sendTemplateMessageLoading: false,
- })
- console.log('[云调用] [发送模板消息] 成功: ', res)
- }).catch(err => {
- this.setData({
- sendTemplateMessageError: true,
- sendTemplateMessageLoading: false,
- })
- console.error('[云调用] [发送模板消息] 失败: ', err)
- })
- },
- getWXACodeViaCloudFunction() {
- this.setData({
- getWXACodeResult: '',
- getWXACodeError: false,
- getWXACodeLoading: true,
- })
- wx.cloud.callFunction({
- name: 'openapi',
- data: {
- action: 'getWXACode'
- }
- // eslint-disable-next-line
- }).then(res => {
- this.setData({
- getWXACodeResult: res,
- getWXACodeLoading: false,
- })
- console.log('[云调用] [获取小程序码]] 成功: ', res)
- }).catch(err => {
- this.setData({
- getWXACodeError: true,
- getWXACodeLoading: false,
- })
- console.error('[云调用] [获取小程序码] 失败: ', err)
- })
- },
- queryServerDataViaClient() {
- const db = wx.cloud.database()
- this.setData({
- clientLoading: true,
- serverDataClient: '',
- serverDataClientError: false
- })
- db.collection('perm4').where({
- _openid: 'server'
- }).get({
- success: res => {
- const resFirstData = (res.data && res.data[0]) || {}
- this.setData({
- serverDataClient: resFirstData.data
- })
- console.log('[数据库] [查询记录] 成功: ', res)
- },
- fail: err => {
- this.setData({
- serverDataClientError: true
- })
- console.error('[数据库] [查询记录] 失败:', err)
- },
- complete: () => {
- this.setData({
- clientLoading: false
- })
- }
- })
- },
- queryServerDataViaCloudFunction() {
- this.setData({
- cloudLoading: true,
- serverDataCloud: '',
- serverDataCloudError: false
- })
- wx.cloud.callFunction({
- name: 'getServerDataDemo',
- data: {},
- success: res => {
- console.log('[云函数] [getServerDataDemo] res: ', res.result)
- const resFirstData = (res.result.data && res.result.data[0]) || {}
- this.setData({
- serverDataCloud: resFirstData.data
- })
- },
- fail: err => {
- this.setData({
- serverDataCloudError: true
- })
- console.error('[云函数] [getServerDataDemo] 调用失败', err)
- },
- complete: () => {
- this.setData({
- cloudLoading: false
- })
- }
- })
- }
- })
|