scf-storage.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const demoImageFileId = require('../../../../config').demoImageFileId
  2. // 参考文档:https://developers.weixin.qq.com/miniprogram/dev/wxcloud/reference-server-api/storage/
  3. const app = getApp()
  4. Page({
  5. onShareAppMessage() {
  6. return {
  7. title: '云函数操作存储',
  8. path: 'packageCloud/pages/scf/scf-storage/scf-storage'
  9. }
  10. },
  11. data: {
  12. theme: 'light',
  13. fileTempURLDone: false,
  14. fileId: '',
  15. tempFileURL: '',
  16. loading: false
  17. },
  18. onLoad() {
  19. this.setData({
  20. theme: wx.getSystemInfoSync().theme || 'light'
  21. })
  22. if (wx.onThemeChange) {
  23. wx.onThemeChange(({theme}) => {
  24. this.setData({theme})
  25. })
  26. }
  27. this.setData({
  28. fileId: app.globalData.fileId || demoImageFileId
  29. })
  30. },
  31. getTempFileURL() {
  32. const fileId = this.data.fileId
  33. if (!fileId) {
  34. return
  35. }
  36. this.setData({
  37. loading: true
  38. })
  39. wx.cloud.callFunction({
  40. name: 'getTempFileURL',
  41. data: {
  42. fileIdList: [fileId]
  43. },
  44. success: res => {
  45. console.log('[云函数] [getTempFileURL] res: ', res.result)
  46. if (res.result.length) {
  47. this.setData({
  48. fileTempURLDone: true,
  49. tempFileURL: res.result[0].tempFileURL
  50. })
  51. }
  52. },
  53. fail: err => {
  54. console.error('[云函数] [getTempFileURL] 调用失败', err)
  55. },
  56. complete: () => {
  57. this.setData({
  58. loading: false
  59. })
  60. }
  61. })
  62. }
  63. })