fix:正确访问已用内存大小bug

This commit is contained in:
zheng020 2026-07-30 16:39:13 +08:00
parent 7667aadf3a
commit 853b9f30d8
3 changed files with 129 additions and 46 deletions

View File

@ -414,7 +414,10 @@
"path": "pages/profile/cache-cleanup",
"style": {
"navigationBarTitleText": "存储空间",
"enablePullDownRefresh": true
"enablePullDownRefresh": true,
"app-plus": {
"bounce": "none"
}
}
},
{

View File

@ -12,37 +12,37 @@
<text class="hero-label">Topfans 已用空间</text>
<text class="hero-value">{{ formatSize(info.appUsedBytes) }}</text>
<!-- 嵌套进度条Topfans 已用空间 内存缓存空间叠在蓝内 总内存空间剩余绿 -->
<!-- 嵌套进度条Topfans 已用空间 其他 app 使用空间 剩余可用空间绿 -->
<view class="progress-bar">
<!-- 绿段剩余配额贴在右侧 -->
<!-- 绿段剩余可用空间贴在右侧 -->
<view
class="seg-available"
:style="{ width: availablePct + '%' }"
></view>
<!-- 蓝段完整 Topfans 已用从左 -->
<!-- 蓝段Topfans 已用空间从左 -->
<view
class="seg-app"
:style="{ width: appPct + '%' }"
class="seg-topfans"
:style="{ width: topfansPct + '%' }"
></view>
<!-- 紫段缓存从左叠在蓝段内不超过蓝段右边界 -->
<!-- 橙段其他 app 使用空间从蓝段右边界开始紧接蓝段 -->
<view
class="seg-cache"
:style="{ width: cachePct + '%' }"
class="seg-other"
:style="{ left: topfansPct + '%', width: otherPct + '%' }"
></view>
</view>
<view class="chips">
<view class="chip">
<view class="chip-dot dot-app"></view>
<view class="chip-dot dot-topfans"></view>
<text>Topfans 已用空间</text>
</view>
<view class="chip">
<view class="chip-dot dot-cache"></view>
<text>内存缓存空间</text>
<view class="chip-dot dot-other"></view>
<text>其他 app 使用空间</text>
</view>
<view class="chip">
<view class="chip-dot dot-available"></view>
<text>总内存空间</text>
<text>剩余可用空间</text>
</view>
</view>
@ -144,25 +144,29 @@ const info = ref({
});
const loadFailed = ref(false);
// quotaTotalBytes = 100%
// deviceTotalBytes = 100%
// 100% 0
// = Topfans = 绿 =
const appPct = computed(() => {
const total = info.value.quotaTotalBytes;
// = Topfans = + app+绿 =
const topfansPct = computed(() => {
const total = info.value.deviceTotalBytes;
if (!total) return 0;
return Math.min(100, Math.max(0, (info.value.appUsedBytes / total) * 100));
});
const cachePct = computed(() => {
const total = info.value.quotaTotalBytes;
const otherPct = computed(() => {
const total = info.value.deviceTotalBytes;
if (!total) return 0;
// Topfans cache used
const cache = Math.min(info.value.totalBytes, info.value.appUsedBytes);
return Math.min(appPct.value, Math.max(0, (cache / total) * 100));
// app + = deviceTotal - Topfans -
// appUsedBytes + deviceFreeBytes deviceTotalBytes
const otherBytes = Math.max(
0,
info.value.deviceTotalBytes - info.value.appUsedBytes - info.value.deviceFreeBytes
);
return Math.min(100 - topfansPct.value, Math.max(0, (otherBytes / total) * 100));
});
const availablePct = computed(() => {
const total = info.value.quotaTotalBytes;
const total = info.value.deviceTotalBytes;
if (!total) return 0;
return Math.max(0, 100 - appPct.value);
return Math.max(0, 100 - topfansPct.value - otherPct.value);
});
// APP-PLUS
@ -182,6 +186,8 @@ async function load() {
const result = await Promise.race([
getCacheInfo(),
new Promise((_, rej) =>
// getCacheInfo < 3s4 + Native.js + handlers
// 2s ~2.1s5s 2x
setTimeout(() => rej(new Error("timeout")), 5000),
),
]);
@ -217,6 +223,8 @@ onPullDownRefresh(async () => {
background: #f5f7fa;
min-height: 100vh;
box-sizing: border-box;
/* 关闭 WebView 层回弹iOS 顶部/底部橡皮筋 + Android 边缘光晕) */
overscroll-behavior: none;
}
/* ── Hero 卡片 ── */
@ -303,10 +311,9 @@ onPullDownRefresh(async () => {
border-radius: 8rpx;
overflow: hidden;
}
/* absolute 绿
紫叠在蓝之上 视觉上"缓存占用 Topfans 已用的一部分" */
.seg-app,
.seg-cache,
/* 三段都用 absolute 定位(蓝、橙从左贴齐→依次拼接;绿从右贴齐) */
.seg-topfans,
.seg-other,
.seg-available {
position: absolute;
top: 0;
@ -314,13 +321,16 @@ onPullDownRefresh(async () => {
min-width: 0;
box-shadow: inset 0 0 0 1rpx rgba(255, 255, 255, 0.2);
}
.seg-app {
.seg-topfans {
left: 0;
background: #1890ff;
/* Topfans 通常 < 0.1% 设备总空间,强制最小可见宽度 2%(保持视觉存在感) */
min-width: 2%;
}
.seg-cache {
.seg-other {
/* 防御left 默认 0inline style 会用 topfansPct% 覆盖 */
left: 0;
background: #722ed1;
background: #fa8c16;
}
.seg-available {
right: 0;
@ -349,16 +359,20 @@ onPullDownRefresh(async () => {
flex-shrink: 0;
}
.chip-dot {
width: 8rpx;
height: 8rpx;
/* 12rpx 8rpx
display: inline-block 防御 uniapp <view> 不渲染的极端 case */
display: inline-block;
width: 12rpx;
height: 12rpx;
border-radius: 50%;
flex-shrink: 0;
vertical-align: middle;
}
.dot-app {
.dot-topfans {
background: #1890ff;
}
.dot-cache {
background: #722ed1;
.dot-other {
background: #fa8c16;
}
.dot-available {
background: #52c41a;
@ -486,3 +500,16 @@ onPullDownRefresh(async () => {
cursor: pointer;
}
</style>
<style>
/* scopedscoped [data-v-xxx]
::-webkit-scrollbar 是伪元素没有 DOM 属性scoped 匹配不上
这里隐藏整页滚动条不影响下拉刷新 */
::-webkit-scrollbar {
display: none;
width: 0 !important;
height: 0 !important;
-webkit-appearance: none;
appearance: none;
}
</style>

View File

@ -368,22 +368,50 @@ async function getAndroidApkSize() {
}
/**
* 计算"app 已用空间" = Android APK 安装包大小 + sandbox 运行时数据大小
* iOS / H5 上只算 sandboxiOS bundle 不易从 js 取大小 sandbox 估算
* 计算"app 已用空间" = Android APK 安装包大小 + 4 sandbox 根目录下所有文件总字节
* iOS / H5 上只算 sandboxiOS .app bundle 沙盒外不可访问 sandbox 估算
* 用于 cacheManager.getCacheInfo() 聚合 appUsedBytes
*
* 4 sandbox
* PRIVATE_DOC (2) _doc 私有文档用户数据/缓存 主用绝大多数占用在此
* PRIVATE_WWW (1) _www 私有资源 manifest.json runmode=liberate 时才有内容
* PUBLIC_DOCUMENTS (3) _documents 公共文档 5+ App 共享
* PUBLIC_DOWNLOADS (4) _downloads 公共下载 5+ App 共享
*
* PRIVATE_WWW 行为说明
* - liberate 模式沙盒根不存在 requestFileSystem 两个回调都不触发 2s 超时降级为 0
* - liberate 模式首次启动资源从 APK 解压到 _www 可正常遍历求和
* liberate 模式下 _www 内容是 APK 资源的解压副本理论上是双倍计数但用户明确要求保留 4 个根遍历
*
* 单个根失败/超时 0不影响其他根
*/
export async function getSandboxTotalSize() {
// #ifdef APP-PLUS
try {
// Android: APK 大小 + sandbox doc 目录运行时数据
// Android: APK 大小 + 4 个沙盒根目录运行时数据
const apkSize = await getAndroidApkSize()
let sandboxBytes = 0
try {
const root = await getSandboxRootDir()
sandboxBytes = await _sumDirSize(root)
} catch (e) {
console.warn('[ioPath] _sumDirSize failed:', e?.message)
}
const rootTypes = [
plus.io.PRIVATE_DOC,
plus.io.PRIVATE_WWW,
plus.io.PUBLIC_DOCUMENTS,
plus.io.PUBLIC_DOWNLOADS,
]
// 并行遍历 4 个沙盒根(互不依赖),单根失败/超时 → 0
// 2s 超时防御性PRIVATE_WWW 在非 liberate 模式下挂死靠它兜底
const sizes = await Promise.all(
rootTypes.map(async (rootType) =>
Promise.race([
_sumDirSizeByRootType(rootType),
new Promise((_, rej) =>
setTimeout(() => rej(new Error('rootType timeout')), 2000)
),
]).catch((e) => {
console.warn(`[ioPath] _sumDirSize failed for rootType=${rootType}:`, e?.message)
return 0
})
)
)
const sandboxBytes = sizes.reduce((sum, s) => sum + s, 0)
return apkSize + sandboxBytes
} catch (e) {
console.warn('[ioPath] getSandboxTotalSize failed:', e?.message)
@ -395,6 +423,31 @@ export async function getSandboxTotalSize() {
// #endif
}
/**
* 取指定 sandbox 根类型对应的 DirectoryEntry递归求总字节
* memoize每次按需取避免 4 个根互相干扰只在 getSandboxTotalSize 单次调用
*/
function _sumDirSizeByRootType(rootType) {
// #ifdef APP-PLUS
return new Promise((resolve, reject) => {
plus.io.requestFileSystem(
rootType,
async (fs) => {
try {
resolve(await _sumDirSize(fs.root))
} catch (e) {
reject(e)
}
},
(e) => reject(e)
)
})
// #endif
// #ifndef APP-PLUS
return Promise.resolve(0)
// #endif
}
/**
* 扫描 sandbox doc "所有业务子目录的 tmp/ 文件总字节数"不包含白名单 preload/share/image
* 用于 sandboxTmpHandler.computeSize