scf-openapi.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-server-api/database/
  2. Page({
  3. onShareAppMessage() {
  4. return {
  5. title: '云函数中使用微信开放能力',
  6. path: 'packageCloud/pages/scf/scf-openapi/scf-openapi'
  7. }
  8. },
  9. data: {
  10. theme: 'light',
  11. sendTemplateMessageResult: '',
  12. sendTemplateMessageError: false,
  13. getWXACodeResult: '',
  14. getWXACodeError: false,
  15. sendTemplateMessageLoading: false,
  16. getWXACodeLoading: false,
  17. theme: 'light'
  18. },
  19. onLoad() {
  20. this.setData({
  21. theme: wx.getSystemInfoSync().theme || 'light'
  22. })
  23. if (wx.onThemeChange) {
  24. wx.onThemeChange(({theme}) => {
  25. this.setData({theme})
  26. })
  27. }
  28. this.setData({
  29. theme: wx.getSystemInfoSync().theme || 'light'
  30. })
  31. if (wx.onThemeChange) {
  32. wx.onThemeChange(({theme}) => {
  33. this.setData({theme})
  34. })
  35. }
  36. },
  37. sendTemplateMessageViaCloudFunction(e) {
  38. this.setData({
  39. sendTemplateMessageResult: '',
  40. sendTemplateMessageError: false,
  41. sendTemplateMessageLoading: true,
  42. })
  43. wx.cloud.callFunction({
  44. name: 'openapi',
  45. data: {
  46. action: 'sendTemplateMessage',
  47. formId: e.detail.formId,
  48. },
  49. // eslint-disable-next-line
  50. }).then((res) => {
  51. this.setData({
  52. sendTemplateMessageResult: res,
  53. sendTemplateMessageLoading: false,
  54. })
  55. console.log('[云调用] [发送模板消息] 成功: ', res)
  56. }).catch(err => {
  57. this.setData({
  58. sendTemplateMessageError: true,
  59. sendTemplateMessageLoading: false,
  60. })
  61. console.error('[云调用] [发送模板消息] 失败: ', err)
  62. })
  63. },
  64. getWXACodeViaCloudFunction() {
  65. this.setData({
  66. getWXACodeResult: '',
  67. getWXACodeError: false,
  68. getWXACodeLoading: true,
  69. })
  70. wx.cloud.callFunction({
  71. name: 'openapi',
  72. data: {
  73. action: 'getWXACode'
  74. }
  75. // eslint-disable-next-line
  76. }).then(res => {
  77. this.setData({
  78. getWXACodeResult: res,
  79. getWXACodeLoading: false,
  80. })
  81. console.log('[云调用] [获取小程序码]] 成功: ', res)
  82. }).catch(err => {
  83. this.setData({
  84. getWXACodeError: true,
  85. getWXACodeLoading: false,
  86. })
  87. console.error('[云调用] [获取小程序码] 失败: ', err)
  88. })
  89. },
  90. queryServerDataViaClient() {
  91. const db = wx.cloud.database()
  92. this.setData({
  93. clientLoading: true,
  94. serverDataClient: '',
  95. serverDataClientError: false
  96. })
  97. db.collection('perm4').where({
  98. _openid: 'server'
  99. }).get({
  100. success: res => {
  101. const resFirstData = (res.data && res.data[0]) || {}
  102. this.setData({
  103. serverDataClient: resFirstData.data
  104. })
  105. console.log('[数据库] [查询记录] 成功: ', res)
  106. },
  107. fail: err => {
  108. this.setData({
  109. serverDataClientError: true
  110. })
  111. console.error('[数据库] [查询记录] 失败:', err)
  112. },
  113. complete: () => {
  114. this.setData({
  115. clientLoading: false
  116. })
  117. }
  118. })
  119. },
  120. queryServerDataViaCloudFunction() {
  121. this.setData({
  122. cloudLoading: true,
  123. serverDataCloud: '',
  124. serverDataCloudError: false
  125. })
  126. wx.cloud.callFunction({
  127. name: 'getServerDataDemo',
  128. data: {},
  129. success: res => {
  130. console.log('[云函数] [getServerDataDemo] res: ', res.result)
  131. const resFirstData = (res.result.data && res.result.data[0]) || {}
  132. this.setData({
  133. serverDataCloud: resFirstData.data
  134. })
  135. },
  136. fail: err => {
  137. this.setData({
  138. serverDataCloudError: true
  139. })
  140. console.error('[云函数] [getServerDataDemo] 调用失败', err)
  141. },
  142. complete: () => {
  143. this.setData({
  144. cloudLoading: false
  145. })
  146. }
  147. })
  148. }
  149. })