Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
706cab6c
提交
706cab6c
authored
9月 05, 2023
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 优化 CacheImage 组件,记录文件大小及时间戳,以及计算缓存大小和清理功能
上级
a1a5336f
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
121 行增加
和
12 行删除
+121
-12
index.ts
src/components/CacheImage/index.ts
+67
-0
index.vue
src/components/CacheImage/index.vue
+40
-12
index.ts
src/utils/file/index.ts
+14
-0
没有找到文件。
src/components/CacheImage/index.ts
0 → 100644
浏览文件 @
706cab6c
import
{
convertKB
}
from
'@/utils/file'
/**
* 图片 Hash 缓存标识前缀
*/
export
const
CACHE_PREFIX
=
'G_CACHE_IMAGE__'
/**
* 计算缓存大小
* @returns 字节
*/
export
function
calculateCacheSize
()
{
const
storage
=
uni
.
getStorageInfoSync
()
return
storage
.
keys
.
filter
((
key
)
=>
key
.
startsWith
(
CACHE_PREFIX
))
.
reduce
((
total
,
key
)
=>
{
try
{
const
cache
=
uni
.
getStorageSync
(
key
)
const
result
=
JSON
.
parse
(
cache
)
total
+=
result
.
size
}
catch
(
_
)
{}
return
total
},
0
)
}
/**
* 计算缓存大小格式化
* @returns 格式化后的缓存大小
* @example 1.2MB
* @see {@link calculateCacheSize}
* @see {@link convertKB}
* @see {@link CACHE_PREFIX}
*/
export
function
calculateCacheSizeFormat
()
{
const
cacheSize
=
calculateCacheSize
()
const
{
size
,
unit
}
=
convertKB
(
cacheSize
)
return
`
${
size
}${
unit
}
`
}
/**
* 清理缓存
*/
export
function
cleanCache
()
{
const
storage
=
uni
.
getStorageInfoSync
()
const
promises
=
storage
.
keys
.
filter
((
key
)
=>
key
.
startsWith
(
CACHE_PREFIX
))
.
map
((
key
)
=>
{
return
new
Promise
((
resolve
,
reject
)
=>
{
try
{
const
cache
=
uni
.
getStorageSync
(
key
)
const
result
=
JSON
.
parse
(
cache
)
uni
.
removeSavedFile
({
filePath
:
result
.
url
,
complete
:
resolve
,
})
}
catch
(
e
)
{
console
.
error
(
e
)
reject
(
e
)
}
finally
{
uni
.
removeStorageSync
(
key
)
}
})
})
return
Promise
.
allSettled
(
promises
)
}
src/components/CacheImage/index.vue
浏览文件 @
706cab6c
<
script
setup
lang=
"ts"
>
<
script
setup
lang=
"ts"
>
import
md5
from
'crypto-js/md5'
import
md5
from
'crypto-js/md5'
import
{
CACHE_PREFIX
}
from
'./index'
const
props
=
defineProps
({
const
props
=
defineProps
({
width
:
{
width
:
{
...
@@ -60,27 +61,53 @@
...
@@ -60,27 +61,53 @@
// 如果是网络图片,则缓存到本地
// 如果是网络图片,则缓存到本地
if
(
url
.
startsWith
(
'http'
))
{
if
(
url
.
startsWith
(
'http'
))
{
// #ifdef APP-PLUS
// #ifdef APP-PLUS
const
hash
=
md5
(
url
).
toString
()
const
hash
=
md5
(
url
).
toString
().
toUpperCase
()
hashCacheKey
.
value
=
`G_CACHE_IMAGE_
${
hash
}
`
hashCacheKey
.
value
=
`
${
CACHE_PREFIX
}${
hash
}
`
if
(
uni
.
getStorageSync
(
hashCacheKey
.
value
))
{
const
cache
=
uni
.
getStorageSync
(
hashCacheKey
.
value
)
if
(
cache
)
{
log
(
'cache hit'
,
url
)
log
(
'cache hit'
,
url
)
url
=
uni
.
getStorageSync
(
hashCacheKey
.
value
)
try
{
const
result
=
JSON
.
parse
(
cache
)
url
=
result
.
url
}
catch
(
_
)
{
url
=
cache
}
}
else
{
}
else
{
log
(
'cache miss'
,
url
)
log
(
'cache miss'
,
url
)
try
{
try
{
const
res
=
await
uni
.
downloadFile
({
url
})
const
res
=
await
uni
.
downloadFile
({
url
})
if
(
res
.
statusCode
===
200
)
{
if
(
res
.
statusCode
===
200
)
{
const
{
savedFilePath
}
=
await
new
Promise
<
UniApp
.
SaveFileSuccess
>
((
resolve
,
reject
)
=>
{
const
{
savedFilePath
,
size
}
=
await
new
Promise
<
{
savedFilePath
:
string
;
size
:
number
}
>
(
uni
.
saveFile
({
(
resolve
,
reject
)
=>
{
tempFilePath
:
res
.
tempFilePath
,
uni
.
saveFile
({
success
:
(
res
)
=>
resolve
(
res
),
tempFilePath
:
res
.
tempFilePath
,
fail
:
(
err
)
=>
reject
(
err
),
success
:
(
res
)
=>
{
})
const
filePath
=
res
.
savedFilePath
})
uni
.
getFileInfo
({
filePath
,
success
:
(
res
)
=>
resolve
({
savedFilePath
:
filePath
,
size
:
res
.
size
,
}),
fail
:
(
err
)
=>
reject
(
err
),
})
},
fail
:
(
err
)
=>
reject
(
err
),
})
},
)
url
=
`
${
savedFilePath
}
`
url
=
`
${
savedFilePath
}
`
// 缓存图片本地地址
// 缓存图片本地地址
uni
.
setStorageSync
(
hashCacheKey
.
value
,
url
)
uni
.
setStorageSync
(
hashCacheKey
.
value
,
JSON
.
stringify
({
url
,
size
,
timestamp
:
Date
.
now
(),
}),
)
}
else
{
}
else
{
log
(
'cache error'
,
url
,
res
)
log
(
'cache error'
,
url
,
res
)
url
=
props
.
src
url
=
props
.
src
...
@@ -97,6 +124,7 @@
...
@@ -97,6 +124,7 @@
}
}
const
hasError
=
ref
(
false
)
const
hasError
=
ref
(
false
)
function
onError
()
{
function
onError
()
{
if
(
hasError
.
value
)
{
if
(
hasError
.
value
)
{
return
return
...
...
src/utils/file/index.ts
0 → 100644
浏览文件 @
706cab6c
// 将 KB 转换为其他单位
export
function
convertKB
(
kb
:
number
)
{
const
bytes
=
kb
*
1024
const
units
=
[
'B'
,
'KB'
,
'MB'
,
'GB'
,
'TB'
]
let
size
=
bytes
let
unitIndex
=
0
while
(
size
>=
1024
&&
unitIndex
<
units
.
length
-
1
)
{
size
/=
1024
unitIndex
++
}
return
{
size
:
size
.
toFixed
(
2
),
unit
:
units
[
unitIndex
]
}
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论