serverapi.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. Page({
  2. data: {
  3. templateId: '',
  4. subscribeMessageResult: '',
  5. requestSubscribeMessageResult: '',
  6. wxacodeSrc: '',
  7. wxacodeResult: '',
  8. showClearWXACodeCache: false,
  9. },
  10. async getSubscribeMessageTemplate() {
  11. try {
  12. const { result } = await wx.cloud.callFunction({
  13. name: 'openapi',
  14. data: {
  15. action: 'requestSubscribeMessage',
  16. },
  17. })
  18. const templateId = result
  19. console.warn('[云函数] [openapi] 获取订阅消息模板 调用成功:', templateId)
  20. this.setData({
  21. templateId,
  22. })
  23. } catch (err) {
  24. wx.showToast({
  25. icon: 'none',
  26. title: '调用失败',
  27. })
  28. console.error('[云函数] [openapi] 获取订阅消息模板 调用失败:', err)
  29. }
  30. },
  31. async requestSubscribeMessage() {
  32. const templateId = this.data.templateId
  33. if (!templateId) {
  34. wx.showModal({
  35. title: '发送失败',
  36. content: '请先获取模板 ID',
  37. showCancel: false,
  38. })
  39. }
  40. wx.requestSubscribeMessage({
  41. tmplIds: [templateId],
  42. success: (res) => {
  43. if (res[templateId] === 'accept') {
  44. this.setData({
  45. requestSubscribeMessageResult: '成功',
  46. })
  47. } else {
  48. this.setData({
  49. requestSubscribeMessageResult: `失败(${res[templateId]})`,
  50. })
  51. }
  52. },
  53. fail: (err) => {
  54. this.setData({
  55. requestSubscribeMessageResult: `失败(${JSON.stringify(err)})`,
  56. })
  57. },
  58. })
  59. },
  60. sendSubscribeMessage(e) {
  61. this.setData({
  62. subscribeMessageResult: '',
  63. })
  64. wx.cloud.callFunction({
  65. name: 'openapi',
  66. data: {
  67. action: 'sendSubscribeMessage',
  68. templateId: this.data.templateId,
  69. },
  70. success: res => {
  71. console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res)
  72. wx.showModal({
  73. title: '发送成功',
  74. content: '请返回微信主界面查看',
  75. showCancel: false,
  76. })
  77. wx.showToast({
  78. title: '发送成功,请返回微信主界面查看',
  79. })
  80. this.setData({
  81. subscribeMessageResult: JSON.stringify(res.result)
  82. })
  83. },
  84. fail: err => {
  85. wx.showToast({
  86. icon: 'none',
  87. title: '调用失败',
  88. })
  89. console.error('[云函数] [openapi] subscribeMessage.send 调用失败:', err)
  90. }
  91. })
  92. },
  93. submitSubscribeMessageForm(e) {
  94. this.setData({
  95. subscribeMessageResult: '',
  96. })
  97. wx.cloud.callFunction({
  98. name: 'openapi',
  99. data: {
  100. action: 'sendSubscribeMessage',
  101. formId: e.detail.formId,
  102. },
  103. success: res => {
  104. console.warn('[云函数] [openapi] subscribeMessage.send 调用成功:', res)
  105. wx.showModal({
  106. title: '发送成功',
  107. content: '请返回微信主界面查看',
  108. showCancel: false,
  109. })
  110. wx.showToast({
  111. title: '发送成功,请返回微信主界面查看',
  112. })
  113. this.setData({
  114. templateMessageResult: JSON.stringify(res.result)
  115. })
  116. },
  117. fail: err => {
  118. wx.showToast({
  119. icon: 'none',
  120. title: '调用失败',
  121. })
  122. console.error('[云函数] [openapi] templateMessage.send 调用失败:', err)
  123. }
  124. })
  125. },
  126. onGetWXACode() {
  127. this.setData({
  128. wxacodeSrc: '',
  129. wxacodeResult: '',
  130. showClearWXACodeCache: false,
  131. })
  132. // 此处为演示,将使用 localStorage 缓存,正常开发中文件 ID 应存在数据库中
  133. const fileID = wx.getStorageSync('wxacodeCloudID')
  134. if (fileID) {
  135. // 有云文件 ID 缓存,直接使用该 ID
  136. // 如需清除缓存,选择菜单栏中的 “工具 -> 清除缓存 -> 清除数据缓存”,或在 Storage 面板中删掉相应的 key
  137. this.setData({
  138. wxacodeSrc: fileID,
  139. wxacodeResult: `从本地缓存中取得了小程序码的云文件 ID`,
  140. showClearWXACodeCache: true,
  141. })
  142. console.log(`从本地缓存中取得了小程序码的云文件 ID:${fileID}`)
  143. } else {
  144. wx.cloud.callFunction({
  145. name: 'openapi',
  146. data: {
  147. action: 'getWXACode',
  148. },
  149. success: res => {
  150. console.warn('[云函数] [openapi] wxacode.get 调用成功:', res)
  151. wx.showToast({
  152. title: '调用成功',
  153. })
  154. this.setData({
  155. wxacodeSrc: res.result,
  156. wxacodeResult: `云函数获取二维码成功`,
  157. showClearWXACodeCache: true,
  158. })
  159. wx.setStorageSync('wxacodeCloudID', res.result)
  160. },
  161. fail: err => {
  162. wx.showToast({
  163. icon: 'none',
  164. title: '调用失败',
  165. })
  166. console.error('[云函数] [openapi] wxacode.get 调用失败:', err)
  167. }
  168. })
  169. }
  170. },
  171. clearWXACodeCache() {
  172. wx.removeStorageSync('wxacodeCloudID')
  173. this.setData({
  174. wxacodeSrc: '',
  175. wxacodeResult: '',
  176. showClearWXACodeCache: false,
  177. })
  178. wx.showToast({
  179. title: '清除成功',
  180. })
  181. },
  182. })