file.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Page({
  2. onShareAppMessage() {
  3. return {
  4. title: '文件',
  5. path: 'packageAPI/pages/media/file/file'
  6. }
  7. },
  8. onLoad() {
  9. this.setData({
  10. theme: wx.getSystemInfoSync().theme || 'light'
  11. })
  12. if (wx.onThemeChange) {
  13. wx.onThemeChange(({theme}) => {
  14. this.setData({theme})
  15. })
  16. }
  17. this.setData({
  18. savedFilePath: wx.getStorageSync('savedFilePath')
  19. })
  20. },
  21. data: {
  22. theme: 'light',
  23. tempFilePath: '',
  24. savedFilePath: ''
  25. },
  26. chooseImage() {
  27. const that = this
  28. wx.chooseImage({
  29. count: 1,
  30. success(res) {
  31. that.setData({
  32. tempFilePath: res.tempFilePaths[0]
  33. })
  34. }
  35. })
  36. },
  37. saveFile() {
  38. if (this.data.tempFilePath.length > 0) {
  39. const that = this
  40. wx.saveFile({
  41. tempFilePath: this.data.tempFilePath,
  42. success(res) {
  43. that.setData({
  44. savedFilePath: res.savedFilePath
  45. })
  46. wx.setStorageSync('savedFilePath', res.savedFilePath)
  47. wx.showModal({
  48. title: '保存成功',
  49. content: '下次进入应用时,此文件仍可用',
  50. })
  51. },
  52. fail() {
  53. wx.showModal({
  54. title: '保存失败',
  55. content: '应该是有 bug 吧',
  56. })
  57. }
  58. })
  59. }
  60. },
  61. clear() {
  62. wx.setStorageSync('savedFilePath', '')
  63. this.setData({
  64. tempFilePath: '',
  65. savedFilePath: ''
  66. })
  67. },
  68. onLoad() {
  69. this.setData({
  70. theme: wx.getSystemInfoSync().theme || 'light'
  71. })
  72. if (wx.onThemeChange) {
  73. wx.onThemeChange(({theme}) => {
  74. this.setData({theme})
  75. })
  76. }
  77. }
  78. })