setting.plugins.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { settingStores } from "@/store/modules/index";
  2. /**
  3. * @获取缓存
  4. */
  5. const formatSize = () => {
  6. let currentSize = "";
  7. plus.cache.calculate(function (size) {
  8. let sizeCache = parseInt(size);
  9. if (sizeCache == 0) {
  10. currentSize = "0B";
  11. } else if (sizeCache < 1024) {
  12. currentSize = sizeCache + "B";
  13. } else if (sizeCache < 1048576) {
  14. currentSize = (sizeCache / 1024).toFixed(2) + "KB";
  15. } else if (sizeCache < 1073741824) {
  16. currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
  17. } else {
  18. v = (sizeCache / 1073741824).toFixed(2) + "GB";
  19. }
  20. settingStores().currentSize = currentSize
  21. });
  22. return currentSize;
  23. };
  24. /**
  25. * @清理缓存
  26. */
  27. const clearCache = () => {
  28. let os = plus.os.name;
  29. if (os == "Android") {
  30. let main = plus.android.runtimeMainActivity();
  31. let sdRoot = main.getCacheDir();
  32. let files = plus.android.invoke(sdRoot, "listFiles");
  33. let len = files.length;
  34. if (len <= 0) {
  35. uni.showToast({
  36. title: "暂无缓存可清理",
  37. icon: "none",
  38. duration: 2000,
  39. mask: true,
  40. });
  41. }
  42. for (let i = 0; i < len; i++) {
  43. let filePath = "" + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径
  44. plus.io.resolveLocalFileSystemURL(
  45. filePath,
  46. function (entry) {
  47. if (entry.isDirectory) {
  48. entry.removeRecursively(
  49. function (entry) {
  50. //递归删除其下的所有文件及子目录
  51. uni.showToast({
  52. title: "缓存清理完成",
  53. duration: 2000,
  54. mask: true,
  55. });
  56. formatSize(); // 重新计算缓存
  57. },
  58. function (e) {
  59. console.log(e.message);
  60. }
  61. );
  62. } else {
  63. entry.remove();
  64. }
  65. },
  66. function (e) {
  67. console.log("文件路径读取失败");
  68. }
  69. );
  70. }
  71. } else {
  72. // ios
  73. plus.cache.clear(function () {
  74. uni.showToast({
  75. title: "缓存清理完成",
  76. duration: 2000,
  77. mask: true,
  78. });
  79. formatSize();
  80. });
  81. }
  82. };
  83. export default {
  84. // 设置角标
  85. setBadge(value) {
  86. //#ifdef APP-PLUS
  87. plus.runtime.setBadgeNumber(value);
  88. //#endif
  89. },
  90. formatSize,
  91. clearCache,
  92. };