fix:总内存显示bug修复
This commit is contained in:
parent
2f672920d9
commit
7667aadf3a
@ -161,9 +161,11 @@ const info = await cacheManager.getCacheInfo()
|
||||
// quotaTotalBytes, // 配额总量 = uni.getStorageInfoSync().limitSize*1024
|
||||
// quotaAvailableBytes, // 配额可用 = quotaTotalBytes - appUsedBytes
|
||||
// usagePercent, // 百分比(0-100,1 位小数)= appUsedBytes / quotaTotalBytes * 100
|
||||
// deviceTotalBytes, // 设备总存储 = plus.io.getStorageInfo().totalSize * 1024(HTML5+ API,MVP 已支持)
|
||||
// deviceFreeBytes, // 设备可用存储 = plus.io.getStorageInfo().availableSize * 1024
|
||||
// deviceTotalBytes, // 设备总存储字节:Android 走 StatFs(Environment.getDataDirectory()),iOS 走 NSFileManager.attributesOfFileSystem
|
||||
// deviceFreeBytes, // 设备可用字节,同上
|
||||
// deviceUsagePercent, // 设备占比(0-100,2 位小数)= appUsedBytes / deviceTotalBytes * 100
|
||||
// // 注:deviceTotalBytes/Free 通过 Native.js 在 ioPath.getDeviceStorageInfo() 取得
|
||||
// // 与 getAndroidApkSize / useShare.js 同模式;不算 native plugin
|
||||
// othersBytes, // "其他" section 大小 = 所有黑名单 keys + 不可清理文件大小
|
||||
// categories: [{id, label, description, sizeBytes, keyCount, warning}, ...]
|
||||
// }
|
||||
@ -323,8 +325,12 @@ await invalidateAll() // 清空 memoryMap + inFlightMap
|
||||
|
||||
**关于"磁盘" vs "配额"**:
|
||||
- **进度条**仍以 `uni.getStorageInfoSync().limitSize`(app 自己的 SQLite 配额)为 100%,表达"app 缓存压力"(用户的清理决策依据)
|
||||
- **百分比分母** = `plus.io.getStorageInfo().totalSize`(设备总存储),即"占设备 X% 存储空间"——HTML5+ 标准 API,Android/iOS 均原生支持,**不需要 native plugin**
|
||||
- **设备信息行** = `plus.io.getStorageInfo()` 直接拿 `{ totalSize, availableSize }`,无权限要求;非 APP-PLUS 平台或调用失败时显示 `—`
|
||||
- **百分比分母** = 设备总存储(`appUsedBytes / deviceTotalBytes * 100`),即"占设备 X% 存储空间"
|
||||
- **设备信息行**(设备总空间 / 设备可用)通过 **Native.js** 在 `utils/ioPath.js#getDeviceStorageInfo()` 取得:
|
||||
- Android:`plus.android.importClass('android.os.StatFs')` + `Environment.getDataDirectory()`,无需权限
|
||||
- iOS:`plus.ios.invoke('NSFileManager', 'attributesOfFileSystemForPath:error:', '/')` 读 `NSFileSystemSize` / `NSFileSystemFreeSize`,public API
|
||||
- 非 APP-PLUS 平台或失败时返回 `{ totalBytes: 0, freeBytes: 0 }`,前端 `formatSize(0)` 显示 `—`
|
||||
- Native.js **不是** native plugin(不需要写 Java/ObjC、不需要 `nativeplugins/` 目录、不需要 `manifest.json` 改动),与项目已有 `getAndroidApkSize` / `useShare.js` 同模式
|
||||
|
||||
**草稿/引导的二级提示**:行右侧副文字"按账号分组清理",警示用户详情页会有分组。
|
||||
|
||||
@ -426,7 +432,7 @@ cacheManager.getCacheInfo()
|
||||
├─ [并行 2] 读取存储配额
|
||||
│ ├─ uni.getStorageInfoSync() → { currentSize (KB), limitSize (KB) }
|
||||
│ ├─ ioPath.getSandboxTotalSize() → 沙盒 doc 目录下所有文件总字节数(新增 ioPath 只读 API;与 scanSandboxTmpFiles 不同,前者含白名单 preload/share/image 所有文件,后者仅统计 tmp/)
|
||||
│ └─ ioPath.getDeviceStorageInfo() → 设备级 { totalSize (KB), availableSize (KB) },HTML5+ API,非 APP-PLUS 返回 {0,0}
|
||||
│ └─ ioPath.getDeviceStorageInfo() → 设备级 { totalBytes, freeBytes }:Android 走 StatFs,iOS 走 NSFileManager(Native.js,非 native plugin);非 APP-PLUS 返回 {0,0}
|
||||
↓
|
||||
聚合:
|
||||
totalBytes = sum(categories.sizeBytes) // 可清理总量(=内存缓存空间 chip)
|
||||
|
||||
@ -105,13 +105,12 @@ export async function getCacheInfo() {
|
||||
} catch (e) {
|
||||
console.warn('[cacheManager] getSandboxTotalSize failed:', e.message)
|
||||
}
|
||||
let deviceTotalKB = 0, deviceFreeKB = 0
|
||||
let deviceTotalBytes = 0, deviceFreeBytes = 0
|
||||
try {
|
||||
const dev = await getDeviceStorageInfo()
|
||||
deviceTotalKB = dev.totalBytes || 0
|
||||
deviceFreeKB = dev.freeBytes || 0
|
||||
// 调试日志:把 getDeviceStorageInfo 的原始返回和 cacheManager 解析后的值都打出来
|
||||
console.log('[cacheManager][debug] getDeviceStorageInfo raw =', JSON.stringify(dev), 'parsed totalKB =', deviceTotalKB, 'freeKB =', deviceFreeKB)
|
||||
// ioPath.getDeviceStorageInfo 返回的 totalBytes/freeBytes 已是字节(Android: blockSize*blocks, iOS: NSFileSystemSize),不要再 * 1024
|
||||
deviceTotalBytes = dev.totalBytes || 0
|
||||
deviceFreeBytes = dev.freeBytes || 0
|
||||
} catch (e) {
|
||||
console.warn('[cacheManager] getDeviceStorageInfo failed:', e.message)
|
||||
}
|
||||
@ -123,9 +122,7 @@ export async function getCacheInfo() {
|
||||
const quotaAvailableBytes = Math.max(0, raw)
|
||||
const quotaExceeded = raw < 0
|
||||
const usagePercent = quotaTotalBytes > 0 ? (appUsedBytes / quotaTotalBytes) * 100 : 0
|
||||
// 设备级数据:plus.io.getStorageInfo 返回 KB,转字节
|
||||
const deviceTotalBytes = deviceTotalKB * 1024
|
||||
const deviceFreeBytes = deviceFreeKB * 1024
|
||||
// 设备级数据已在 try 块里取好(ioPath 返回的就是字节,不再 * 1024)
|
||||
const deviceUsagePercent = deviceTotalBytes > 0
|
||||
? (appUsedBytes / deviceTotalBytes) * 100
|
||||
: 0
|
||||
|
||||
@ -524,31 +524,66 @@ async function _listDir(dirEntry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备级存储信息(HTML5+ 标准 API,Android/iOS 均支持)
|
||||
* 获取设备级存储信息(Native.js:Android 走 StatFs,iOS 走 NSFileManager)
|
||||
* 不需要任何 native plugin;非 APP-PLUS 平台或失败时返回安全零值
|
||||
* 注意:单位为 KB(plus.io 原始返回),调用方按需 * 1024 转字节
|
||||
* 注:与项目已有 getAndroidApkSize / useShare.js 的 Native.js 模式 1:1 对齐
|
||||
* @returns {Promise<{totalBytes:number, freeBytes:number}>}
|
||||
*/
|
||||
export function getDeviceStorageInfo() {
|
||||
return new Promise((resolve) => {
|
||||
// #ifdef APP-PLUS
|
||||
try {
|
||||
plus.io.getStorageInfo({
|
||||
success: (res) => {
|
||||
// 调试日志:打印 plus.io.getStorageInfo 的真实返回 shape
|
||||
console.log('[ioPath][debug] plus.io.getStorageInfo res =', JSON.stringify(res), 'keys =', res ? Object.keys(res) : null)
|
||||
if (typeof plus === 'undefined') {
|
||||
return resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
}
|
||||
if (plus.os.name === 'Android') {
|
||||
// Android:用 StatFs 查 /data 分区(即系统"内部存储")
|
||||
// 不需要任何权限;Android 10+ 分区存储不影响 StatFs(StatFs 查的是卷级元数据)
|
||||
const Environment = plus.android.importClass('android.os.Environment')
|
||||
const StatFs = plus.android.importClass('android.os.StatFs')
|
||||
const dataDir = Environment.getDataDirectory()
|
||||
const stat = new StatFs(dataDir.getPath())
|
||||
// getBlockSizeLong 是 API 18+,project minSdkVersion=21 保证可用
|
||||
// plus.android 对 *Long 变体代理有时不稳定 → parseFloat 兜底
|
||||
const blockSize = parseFloat(plus.android.invoke(stat, 'getBlockSizeLong'))
|
||||
const totalBlocks = parseFloat(plus.android.invoke(stat, 'getBlockCountLong'))
|
||||
const freeBlocks = parseFloat(plus.android.invoke(stat, 'getAvailableBlocksLong'))
|
||||
if (blockSize > 0 && totalBlocks > 0) {
|
||||
resolve({
|
||||
totalBytes: Number(res?.totalSize) || 0,
|
||||
freeBytes: Number(res?.availableSize) || 0,
|
||||
totalBytes: blockSize * totalBlocks,
|
||||
freeBytes: blockSize * freeBlocks,
|
||||
})
|
||||
},
|
||||
fail: (e) => {
|
||||
console.warn('[ioPath] getStorageInfo failed:', e?.message)
|
||||
} else {
|
||||
console.warn('[ioPath] getDeviceStorageInfo (Android) got non-positive values')
|
||||
resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
},
|
||||
})
|
||||
}
|
||||
} else if (plus.os.name === 'iOS') {
|
||||
// iOS:用 NSFileManager.attributesOfFileSystem 查系统卷
|
||||
// NSFileSystemSize / NSFileSystemFreeSize 是 public API(参照 useShare.js 调用风格)
|
||||
const fm = plus.ios.invoke('NSFileManager', 'defaultManager')
|
||||
if (!fm) {
|
||||
console.warn('[ioPath] getDeviceStorageInfo (iOS) fm is null')
|
||||
return resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
}
|
||||
const attrs = plus.ios.invoke(fm, 'attributesOfFileSystemForPath:error:', '/')
|
||||
if (attrs) {
|
||||
const total = parseFloat(attrs.plusGetAttribute('NSFileSystemSize')) || 0
|
||||
const free = parseFloat(attrs.plusGetAttribute('NSFileSystemFreeSize')) || 0
|
||||
// 必须 deleteObject,否则 NSObject proxy 会泄露(参照 useShare.js 清理模式)
|
||||
plus.ios.deleteObject(attrs)
|
||||
plus.ios.deleteObject(fm)
|
||||
resolve({ totalBytes: total, freeBytes: free })
|
||||
} else {
|
||||
plus.ios.deleteObject(fm)
|
||||
console.warn('[ioPath] getDeviceStorageInfo (iOS) attrs is null')
|
||||
resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
}
|
||||
} else {
|
||||
// Harmony / 其它平台:暂不支持,返回 0
|
||||
resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[ioPath] getStorageInfo threw:', e?.message)
|
||||
console.warn('[ioPath] getDeviceStorageInfo failed:', e?.message)
|
||||
resolve({ totalBytes: 0, freeBytes: 0 })
|
||||
}
|
||||
// #endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user