root-filelist.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <view>
  3. <view v-if="stack.length > 1" class="file-item" @click="openParent(stackLast)">
  4. <view>
  5. <image src="../../static/phone.png" class="icon" mode="widthFix"></image>
  6. </view>
  7. <view class="item-content"><text>...上一层</text></view>
  8. </view>
  9. <view v-for="(item, index) in fileList" :key="index" @click="onFileSelected(item)" class="file-item">
  10. <view>
  11. <image v-if="item.type === 'file'" :src="fileType(item.name)" class="icon" mode="widthFix"></image>
  12. <image v-else src="../../static/phone.png" class="icon" mode="widthFix"></image>
  13. </view>
  14. <view class="item-content">
  15. <text>{{ item.name }}</text>
  16. </view>
  17. </view>
  18. </view>
  19. </template>
  20. <script>
  21. import {
  22. onLoad,
  23. onReady,
  24. onShow
  25. } from '@dcloudio/uni-app';
  26. import pdf from '../../static/icon_pdf.png';
  27. import word from '../../static/icon_word.png';
  28. // 导入 java.io.File 类
  29. const File = plus.android.importClass('java.io.File');
  30. export default {
  31. data() {
  32. return {
  33. fileList: [],
  34. stack: [],
  35. pdf,
  36. word,
  37. fileInfo: {}
  38. };
  39. },
  40. computed: {
  41. stackLast() {
  42. return {
  43. type: 'dir',
  44. path: this.stack[this.stack.length - 2]
  45. };
  46. }
  47. },
  48. methods: {
  49. fileType(type) {
  50. return /.docx$/.test(type) ? this.word : this.pdf;
  51. },
  52. openParent(item) {
  53. this.stack = this.stack.slice(0, this.stack.length - 1);
  54. this.fileList = [];
  55. this.getPrivateDir(item.path);
  56. },
  57. onFileSelected(item) {
  58. if (item.type === 'dir') {
  59. this.stack.push(item.path);
  60. this.fileList = [];
  61. this.getPrivateDir(item.path);
  62. } else {
  63. this.fileInfo.name = item.name
  64. this.fileInfo.path = item.path
  65. uni.setStorageSync('filePath', item.path);
  66. uni.setStorageSync('fileName', item.name);
  67. uni.switchTab({
  68. url: "/pages/index",
  69. });
  70. }
  71. },
  72. getPrivateDir(dirPath) {
  73. uni.showLoading({
  74. title: '加载中'
  75. });
  76. let dir = new File(dirPath);
  77. if (!dir.exists()) {
  78. console.log('目录不存在');
  79. uni.hideLoading();
  80. return;
  81. }
  82. const files = dir.listFiles();
  83. if (files == null) {
  84. uni.hideLoading();
  85. return;
  86. }
  87. const lists = [];
  88. for (let i = 0; i < files.length; i++) {
  89. let json = {
  90. name: '',
  91. type: '',
  92. time: '',
  93. size: '',
  94. path: ''
  95. };
  96. let file = files[i];
  97. if (file.isDirectory()) {
  98. let dirName = file.getName();
  99. let dirPath = file.getAbsolutePath();
  100. if (/^\./.test(dirName)) continue;
  101. json.type = 'dir';
  102. json.path = dirPath;
  103. json.name = dirName;
  104. lists.push(json);
  105. } else {
  106. let fileName = file.getName();
  107. let fileSize = file.length();
  108. let filePath = file.getAbsolutePath();
  109. if (/^\./.test(fileName)) continue;
  110. json.type = 'file';
  111. json.name = fileName;
  112. json.size = fileSize;
  113. json.path = filePath;
  114. json.time = file.lastModified();
  115. lists.push(json);
  116. }
  117. }
  118. this.fileList = lists;
  119. uni.hideLoading();
  120. }
  121. },
  122. mounted() {
  123. const dirPath = '/storage/emulated/0/';
  124. this.stack.push(dirPath);
  125. this.getPrivateDir(dirPath);
  126. }
  127. };
  128. </script>
  129. <style lang="scss" scoped>
  130. .file-item {
  131. display: flex;
  132. padding: 20rpx;
  133. align-items: center;
  134. border-bottom: 1px solid #e7e7e7;
  135. .icon {
  136. width: 50rpx;
  137. margin-left: 20rpx;
  138. }
  139. .item-content {
  140. display: flex;
  141. flex-direction: column;
  142. padding-left: 26rpx;
  143. .item-content-text {
  144. margin: 12rpx;
  145. }
  146. }
  147. }
  148. </style>