file.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. Page({
  2. data: {
  3. tempFilePath: '',
  4. savedFilePath: '',
  5. dialog: {
  6. hidden: true
  7. }
  8. },
  9. onLoad() {
  10. my.getStorage({
  11. success: (savedFilePath) => {
  12. this.setData({
  13. savedFilePath,
  14. });
  15. },
  16. });
  17. },
  18. chooseImage() {
  19. my.chooseImage({
  20. count: 1,
  21. success: (res) => {
  22. this.setData({
  23. tempFilePath: res.tempFilePaths[0]
  24. })
  25. }
  26. })
  27. },
  28. saveFile() {
  29. if (this.data.tempFilePath.length > 0) {
  30. var that = this
  31. my.saveFile({
  32. tempFilePath: this.data.tempFilePath,
  33. success(res) {
  34. that.setData({
  35. savedFilePath: res.savedFilePath
  36. })
  37. my.setStorage({ key: 'savedFilePath', data: res.savedFilePath })
  38. that.setData({
  39. dialog: {
  40. title: '保存成功',
  41. content: '下次进入应用时,此文件仍可用',
  42. hidden: false
  43. }
  44. })
  45. },
  46. fail(res) {
  47. that.setData({
  48. dialog: {
  49. title: '保存失败',
  50. content: '应该是有 bug 吧',
  51. hidden: false
  52. }
  53. })
  54. }
  55. })
  56. }
  57. },
  58. clear() {
  59. my.setStorage({ key: 'savedFilePath', data: '' })
  60. this.setData({
  61. tempFilePath: '',
  62. savedFilePath: ''
  63. })
  64. },
  65. confirm() {
  66. this.setData({
  67. 'dialog.hidden': true
  68. })
  69. }
  70. })