addFunction.js 1023 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // pages/addFunction/addFunction.js
  2. const code = `// 云函数入口函数
  3. exports.main = async (event, context) => {
  4. console.log(event)
  5. console.log(context)
  6. return {
  7. sum: event.a + event.b
  8. }
  9. }`
  10. Page({
  11. data: {
  12. result: '',
  13. canIUseClipboard: wx.canIUse('setClipboardData'),
  14. },
  15. onLoad: function (options) {
  16. },
  17. copyCode: function() {
  18. wx.setClipboardData({
  19. data: code,
  20. success: function () {
  21. wx.showToast({
  22. title: '复制成功',
  23. })
  24. }
  25. })
  26. },
  27. testFunction() {
  28. wx.cloud.callFunction({
  29. name: 'sum',
  30. data: {
  31. a: 1,
  32. b: 2
  33. },
  34. success: res => {
  35. wx.showToast({
  36. title: '调用成功',
  37. })
  38. this.setData({
  39. result: JSON.stringify(res.result)
  40. })
  41. },
  42. fail: err => {
  43. wx.showToast({
  44. icon: 'none',
  45. title: '调用失败',
  46. })
  47. console.error('[云函数] [sum] 调用失败:', err)
  48. }
  49. })
  50. },
  51. })