Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
6fbfa03d
提交
6fbfa03d
authored
5月 05, 2023
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 优化 CacheImage 组件,增加调试参数和本地文件适配
上级
07c4f5dc
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
46 行增加
和
27 行删除
+46
-27
index.vue
src/components/CacheImage/index.vue
+46
-27
没有找到文件。
src/components/CacheImage/index.vue
浏览文件 @
6fbfa03d
...
@@ -26,53 +26,72 @@
...
@@ -26,53 +26,72 @@
type
:
String
,
type
:
String
,
required
:
true
,
required
:
true
,
},
},
debug
:
{
type
:
Boolean
,
default
:
false
,
},
})
})
const
src
=
ref
()
const
src
=
ref
()
const
hashCacheKey
=
ref
()
const
hashCacheKey
=
ref
()
onMounted
(()
=>
{
onMounted
(
async
()
=>
{
tryCache
()
await
tryCache
()
})
})
// 监听 src 变化
// 监听 src 变化
watch
(
watch
(
()
=>
props
.
src
,
()
=>
props
.
src
,
()
=>
tryCache
(),
async
()
=>
await
tryCache
(),
)
)
function
log
(...
args
:
any
[])
{
if
(
props
.
debug
)
{
console
.
log
(
'[CacheImage]'
,
...
args
)
}
}
// 尝试缓存图片,如果失败则使用原始图片
// 尝试缓存图片,如果失败则使用原始图片
async
function
tryCache
()
{
async
function
tryCache
()
{
let
url
=
props
.
src
let
url
=
props
.
src
if
(
!
url
)
{
return
}
// #ifdef APP-PLUS
// 如果是网络图片,则缓存到本地
const
hash
=
md5
(
url
).
toString
()
if
(
url
.
startsWith
(
'http'
))
{
hashCacheKey
.
value
=
`G_CACHE_IMAGE_
${
hash
}
`
// #ifdef APP-PLUS
if
(
uni
.
getStorageSync
(
hashCacheKey
.
value
))
{
const
hash
=
md5
(
url
).
toString
()
console
.
debug
(
'CacheImage cache hit'
)
hashCacheKey
.
value
=
`G_CACHE_IMAGE_
${
hash
}
`
url
=
uni
.
getStorageSync
(
hashCacheKey
.
value
)
if
(
uni
.
getStorageSync
(
hashCacheKey
.
value
))
{
}
else
{
log
(
'cache hit'
,
url
)
console
.
debug
(
'CacheImage cache miss'
)
url
=
uni
.
getStorageSync
(
hashCacheKey
.
value
)
try
{
}
else
{
const
res
=
await
uni
.
downloadFile
({
url
})
log
(
'cache miss'
,
url
)
if
(
res
.
statusCode
===
200
)
{
try
{
const
{
savedFilePath
}
=
await
new
Promise
<
UniApp
.
SaveFileSuccess
>
((
resolve
,
reject
)
=>
{
const
res
=
await
uni
.
downloadFile
({
url
})
uni
.
saveFile
({
if
(
res
.
statusCode
===
200
)
{
tempFilePath
:
res
.
tempFilePath
,
const
{
savedFilePath
}
=
await
new
Promise
<
UniApp
.
SaveFileSuccess
>
((
resolve
,
reject
)
=>
{
success
:
(
res
)
=>
resolve
(
res
),
uni
.
saveFile
({
fail
:
(
err
)
=>
reject
(
err
),
tempFilePath
:
res
.
tempFilePath
,
success
:
(
res
)
=>
resolve
(
res
),
fail
:
(
err
)
=>
reject
(
err
),
})
})
})
})
url
=
`
${
savedFilePath
}
`
url
=
`
${
savedFilePath
}
`
// 缓存图片本地地址
// 缓存图片本地地址
uni
.
setStorageSync
(
hashCacheKey
.
value
,
url
)
uni
.
setStorageSync
(
hashCacheKey
.
value
,
url
)
}
else
{
log
(
'cache error'
,
url
,
res
)
url
=
props
.
src
}
}
catch
(
e
)
{
console
.
error
(
e
)
}
}
}
catch
(
e
)
{
console
.
error
(
e
)
}
}
log
(
props
.
src
,
'=>'
,
url
,
hash
,
hashCacheKey
.
value
)
// #endif
}
}
console
.
debug
(
props
.
src
,
'=>'
,
url
,
hash
,
hashCacheKey
.
value
)
// #endif
src
.
value
=
url
src
.
value
=
url
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论