فهرست منبع

设置APP不可横屏/新增清除缓存模块

fanghuisheng 2 سال پیش
والد
کامیت
3585bde325
4فایلهای تغییر یافته به همراه102 افزوده شده و 5 حذف شده
  1. 3 0
      src/App.vue
  2. 7 5
      src/pages/mine/setting/index.vue
  3. 4 0
      src/plugins/index.js
  4. 88 0
      src/plugins/setting.js

+ 3 - 0
src/App.vue

@@ -38,6 +38,9 @@ onHide(() => {
 
 onLaunch(() => {
   console.log("App Launch");
+  // #ifdef APP-PLUS
+  plus.screen.lockOrientation("portrait-primary");//设置不可横屏
+  // #endif
   initApp();
 });
 </script>

+ 7 - 5
src/pages/mine/setting/index.vue

@@ -51,7 +51,13 @@ function handleToUpgrade() {
   proxy.$modal.showToast("模块建设中~");
 }
 function handleCleanTmp() {
-  proxy.$modal.showToast("模块建设中~");
+  // #ifdef H5
+  proxy.$modal.showToast("H5暂不支持此功能");
+  // #endif
+
+  // #ifdef APP-PLUS
+  proxy.$setting.clearCache();
+  // #endif
 }
 function handleLogout() {
   proxy.$modal.confirm("确定注销并退出系统吗?").then(() => {
@@ -63,10 +69,6 @@ function handleLogout() {
 </script>
 
 <style lang="scss" scoped>
-.page {
-  background-color: #f8f8f8;
-}
-
 .item-box {
   background-color: #ffffff;
   margin: 30rpx;

+ 4 - 0
src/plugins/index.js

@@ -2,6 +2,7 @@ import tab from "./tab";
 import auth from "./auth";
 import modal from "./modal";
 import common from "./common";
+import setting from "./setting";
 
 export default {
   install(app) {
@@ -17,5 +18,8 @@ export default {
     // 数据处理
     app.provide("$common", common);
     app.config.globalProperties.$common = common;
+    // 公共设置方法
+    app.provide("$setting", setting);
+    app.config.globalProperties.$setting = setting;
   },
 };

+ 88 - 0
src/plugins/setting.js

@@ -0,0 +1,88 @@
+/**
+ * @获取缓存
+ */
+const formatSize = () => {
+  let currentSize = "";
+  plus.cache.calculate(function (size) {
+    let sizeCache = parseInt(size);
+    if (sizeCache == 0) {
+      currentSize = "0B";
+    } else if (sizeCache < 1024) {
+      currentSize = sizeCache + "B";
+    } else if (sizeCache < 1048576) {
+      currentSize = (sizeCache / 1024).toFixed(2) + "KB";
+    } else if (sizeCache < 1073741824) {
+      currentSize = (sizeCache / 1048576).toFixed(2) + "MB";
+    } else {
+      v = (sizeCache / 1073741824).toFixed(2) + "GB";
+    }
+  });
+  return currentSize;
+};
+
+/**
+ * @清理缓存
+ */
+const clearCache = () => {
+  let os = plus.os.name;
+  if (os == "Android") {
+    let main = plus.android.runtimeMainActivity();
+    let sdRoot = main.getCacheDir();
+    let files = plus.android.invoke(sdRoot, "listFiles");
+    let len = files.length;
+
+    if (len <= 0) {
+      uni.showToast({
+        title: "暂无缓存可清理",
+        icon: "none",
+        duration: 2000,
+        mask: true,
+      });
+    }
+
+    for (let i = 0; i < len; i++) {
+      let filePath = "" + files[i]; // 没有找到合适的方法获取路径,这样写可以转成文件路径
+      plus.io.resolveLocalFileSystemURL(
+        filePath,
+        function (entry) {
+          if (entry.isDirectory) {
+            entry.removeRecursively(
+              function (entry) {
+                //递归删除其下的所有文件及子目录
+                uni.showToast({
+                  title: "缓存清理完成",
+                  duration: 2000,
+                  mask: true,
+                });
+                formatSize(); // 重新计算缓存
+              },
+              function (e) {
+                console.log(e.message);
+              }
+            );
+          } else {
+            entry.remove();
+          }
+        },
+        function (e) {
+          console.log("文件路径读取失败");
+        }
+      );
+    }
+  } else {
+    // ios
+    plus.cache.clear(function () {
+      uni.showToast({
+        title: "缓存清理完成",
+        duration: 2000,
+        mask: true,
+      });
+      formatSize();
+    });
+  }
+};
+
+export default {
+  formatSize,
+  clearCache,
+};