feat 打印storage
This commit is contained in:
10
src/App.vue
10
src/App.vue
@@ -1,9 +1,19 @@
|
||||
<script setup lang="ts">
|
||||
import { useDeviceAuth } from '@/composables/useDeviceAuth'
|
||||
import { initTables } from '@/service'
|
||||
import storage from '@/utils/storage'
|
||||
|
||||
const { initAuth } = useDeviceAuth()
|
||||
|
||||
// 开发环境把 storage 调试方法挂到全局,控制台可直接调用
|
||||
// 用法:storagePrint() / storageAll()
|
||||
if (import.meta.env.DEV) {
|
||||
;(globalThis as any).storagePrint = () => storage.print()
|
||||
;(globalThis as any).storageAll = () => storage.all()
|
||||
// 启动时自动打印一次,不想看就把这行注释掉
|
||||
storage.print()
|
||||
}
|
||||
|
||||
onLaunch(async () => {
|
||||
try {
|
||||
await initTables()
|
||||
|
||||
@@ -142,4 +142,4 @@
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -23,6 +23,12 @@
|
||||
*
|
||||
* // 获取所有key
|
||||
* storage.keys()
|
||||
*
|
||||
* // 打印全部数据(key: value 形式)
|
||||
* storage.print()
|
||||
*
|
||||
* // 打印全部数据并返回对象
|
||||
* const all = storage.all()
|
||||
*/
|
||||
|
||||
interface StorageItem<T = any> {
|
||||
@@ -120,7 +126,86 @@ const storage = {
|
||||
* @returns key数组
|
||||
*/
|
||||
keys(): string[] {
|
||||
return Object.keys(uni.getStorageInfoSync().keys)
|
||||
return uni.getStorageInfoSync().keys || []
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取全部数据(key: value 形式的普通对象)
|
||||
* 会自动过滤已过期的 key
|
||||
* @returns 键值对对象
|
||||
*/
|
||||
all(): Record<string, any> {
|
||||
const result: Record<string, any> = {}
|
||||
for (const key of this.keys()) {
|
||||
const raw = uni.getStorageSync(key)
|
||||
if (!raw)
|
||||
continue
|
||||
|
||||
try {
|
||||
const item: StorageItem = JSON.parse(raw)
|
||||
// 已过期则顺手清掉,不放进结果
|
||||
if (item.expire && item.expire < Date.now()) {
|
||||
this.remove(key)
|
||||
continue
|
||||
}
|
||||
result[key] = item.value
|
||||
}
|
||||
catch {
|
||||
// 非法 JSON(早期版本或其它代码直接写的裸数据)原样返回
|
||||
result[key] = raw
|
||||
}
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
/**
|
||||
* 打印全部数据到控制台(key: value 形式)
|
||||
* H5 直接看浏览器控制台,App / 小程序看 HBuilderX 的控制台
|
||||
*
|
||||
* 注意:App 端真机的 console 会过滤多参数、合并日志,
|
||||
* 所以这里用「一条完整字符串」输出,保证一定能看到。
|
||||
*
|
||||
* @returns 键值对对象,便于链式调试
|
||||
*/
|
||||
print(): Record<string, any> {
|
||||
const data = this.all()
|
||||
const keys = Object.keys(data)
|
||||
|
||||
if (!keys.length) {
|
||||
console.log('[storage] 空')
|
||||
return data
|
||||
}
|
||||
|
||||
// 拼成单条字符串,避免 App 端多参数日志被截断或吞掉
|
||||
const lines = [`[storage] 共 ${keys.length} 项`]
|
||||
keys.forEach((key) => {
|
||||
lines.push(` ${key}: ${this.format(data[key])}`)
|
||||
})
|
||||
lines.push(`[storage] 完整对象: ${JSON.stringify(data)}`)
|
||||
|
||||
console.log(lines.join('\n'))
|
||||
return data
|
||||
},
|
||||
|
||||
/**
|
||||
* 把值格式化成单行字符串,避免对象打印成 [object Object]
|
||||
*/
|
||||
format(value: any): string {
|
||||
if (value === null)
|
||||
return 'null'
|
||||
if (value === undefined)
|
||||
return 'undefined'
|
||||
if (typeof value === 'string')
|
||||
return value
|
||||
if (typeof value === 'object') {
|
||||
try {
|
||||
return JSON.stringify(value)
|
||||
}
|
||||
catch {
|
||||
return String(value)
|
||||
}
|
||||
}
|
||||
return String(value)
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user