env.uvue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <template>
  2. <view style="margin:12px">
  3. <page-head title="环境变量 - 文件系统"></page-head>
  4. <button class="button" type="primary" @tap="getDirInfo(uni.env.USER_DATA_PATH)">USER_DATA_PATH</button>
  5. <button class="button" type="primary" @tap="getDirInfo(cachePath)">CACHE_PATH</button>
  6. <button class="button" type="primary" @tap="getDirInfo(sandboxPath)">SANDBOX_PATH</button>
  7. <!-- #ifdef APP-HARMONY -->
  8. <button class="button" type="primary" @tap="getDirInfo(tempPath)">TEMP_PATH</button>
  9. <!-- #endif -->
  10. <!-- #ifdef APP-ANDROID -->
  11. <button class="button" type="primary" @tap="getDirInfo(androidInternalSandboxPath)">ANDROID_INTERNAL_SANDBOX_PATH</button>
  12. <!-- #endif -->
  13. <boolean-data :defaultValue="false" title="是否递归获取" @change="switchRecursive"></boolean-data>
  14. </view>
  15. <scroll-view style="flex: 1; padding: 16px 0px;">
  16. <text class="result">{{result}}</text>
  17. <text class="error">{{error}}</text>
  18. <view class="stat" v-for="(stat,index) in list" :key="index" >
  19. <text class="path">{{stat.path}}</text>
  20. <text class="size">{{stat.size}}</text>
  21. </view>
  22. </scroll-view>
  23. </template>
  24. <script>
  25. type StatInfo = {
  26. path : string;
  27. size : string;
  28. };
  29. export default {
  30. data() {
  31. return {
  32. result: '',
  33. error: '',
  34. list: [] as Array<StatInfo>,
  35. recursive: false,
  36. cachePath: uni.env.CACHE_PATH as string,
  37. sandboxPath: uni.env.SANDBOX_PATH as string,
  38. // #ifdef APP-HARMONY
  39. tempPath: uni.env.TEMP_PATH as string,
  40. // #endif
  41. // #ifdef APP-ANDROID
  42. androidInternalSandboxPath: uni.env.ANDROID_INTERNAL_SANDBOX_PATH as string,
  43. // #endif
  44. }
  45. },
  46. methods: {
  47. switchRecursive() {
  48. this.recursive = !this.recursive
  49. },
  50. getDirInfo(dirPath:string) {
  51. const fm = uni.getFileSystemManager()
  52. this.list = [];
  53. fm.stat({
  54. path: dirPath,
  55. recursive: this.recursive,
  56. success: (res: StatSuccessResult) => {
  57. this.result = `获取 "${dirPath}" 成功(success)`
  58. console.log(this.result)
  59. res.stats.forEach((item)=>{
  60. this.list.push({
  61. path: item.path,
  62. size: `${item.stats.size} Bytes`
  63. })
  64. })
  65. },
  66. fail: (err) => {
  67. this.result = `获取 "${dirPath}" 失败(fail)`
  68. console.log(this.result)
  69. this.error = JSON.stringify(err)
  70. console.log(this.error)
  71. }
  72. })
  73. }
  74. }
  75. }
  76. </script>
  77. <style>
  78. .button {
  79. margin-bottom: 4px;
  80. white-space: nowrap;
  81. text-overflow: ellipsis;
  82. }
  83. .result {
  84. font-size: 18px;
  85. font-weight: bold;
  86. text-align: center;
  87. width: 100%;
  88. }
  89. .error {
  90. color: firebrick;
  91. }
  92. .stat {
  93. padding: 8px 16px;
  94. }
  95. .path {
  96. color: darkgray;
  97. }
  98. .size {
  99. color: darkgrey;
  100. }
  101. </style>