Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
basic-uniapp-v3
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-uniapp-v3
Commits
de2199b1
提交
de2199b1
authored
3月 23, 2023
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 新增 CacheIamge 组件
上级
fa34af88
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
94 行增加
和
0 行删除
+94
-0
index.vue
src/components/CacheImage/index.vue
+93
-0
components.d.ts
types/components.d.ts
+1
-0
没有找到文件。
src/components/CacheImage/index.vue
0 → 100644
浏览文件 @
de2199b1
<
script
setup
lang=
"ts"
>
import
md5
from
'crypto-js/md5'
const
props
=
defineProps
({
width
:
{
type
:
[
String
,
Number
],
default
:
'320'
,
},
height
:
{
type
:
[
String
,
Number
],
default
:
'240'
,
},
radius
:
{
type
:
[
String
,
Number
],
default
:
0
,
},
mode
:
{
type
:
String
,
default
:
'scaleToFill'
,
},
src
:
{
type
:
String
,
required
:
true
,
},
})
const
src
=
ref
()
const
hashCacheKey
=
ref
()
onMounted
(
async
()
=>
{
// 尝试缓存图片,如果失败则使用原始图片
await
tryCache
()
})
async
function
tryCache
()
{
let
url
=
props
.
src
// #ifdef APP-PLUS
const
hash
=
md5
(
url
).
toString
()
hashCacheKey
.
value
=
`G_CACHE_IMAGE_
${
hash
}
`
if
(
uni
.
getStorageSync
(
hashCacheKey
.
value
))
{
console
.
log
(
'CacheImage cache hit'
)
url
=
uni
.
getStorageSync
(
hashCacheKey
.
value
)
}
else
{
console
.
log
(
'CacheImage cache miss'
)
try
{
const
res
=
await
uni
.
downloadFile
({
url
})
if
(
res
.
statusCode
===
200
)
{
const
{
savedFilePath
}
=
await
new
Promise
<
UniApp
.
SaveFileSuccess
>
((
resolve
,
reject
)
=>
{
uni
.
saveFile
({
tempFilePath
:
res
.
tempFilePath
,
success
:
(
res
)
=>
resolve
(
res
),
fail
:
(
err
)
=>
reject
(
err
),
})
})
url
=
`
${
savedFilePath
}
`
// 缓存图片本地地址
uni
.
setStorageSync
(
hashCacheKey
.
value
,
url
)
}
}
catch
(
e
)
{
console
.
error
(
e
)
}
}
console
.
log
(
props
.
src
,
'=>'
,
url
,
hash
,
hashCacheKey
.
value
)
// #endif
src
.
value
=
url
}
const
hasError
=
ref
(
false
)
function
onError
()
{
if
(
hasError
.
value
)
{
return
}
hasError
.
value
=
true
src
.
value
=
props
.
src
// 清除缓存
hashCacheKey
.
value
&&
uni
.
removeStorageSync
(
hashCacheKey
.
value
)
console
.
warn
(
'CacheImage cache error'
)
}
</
script
>
<
template
>
<fui-lazyload
:mode=
"props.mode"
:width=
"props.width"
:height=
"props.height"
:radius=
"props.radius"
:src=
"src"
@
error=
"onError"
/>
</
template
>
types/components.d.ts
浏览文件 @
de2199b1
...
...
@@ -9,6 +9,7 @@ export {}
declare
module
'@vue/runtime-core'
{
export
interface
GlobalComponents
{
CacheImage
:
typeof
import
(
'./../src/components/CacheImage/index.vue'
)[
'default'
]
Empty
:
typeof
import
(
'./../src/components/Empty/index.vue'
)[
'default'
]
FDragItem
:
typeof
import
(
'./../src/components/FirstUI/fui-drag/f-drag-item.vue'
)[
'default'
]
FIndexListItem
:
typeof
import
(
'./../src/components/FirstUI/fui-index-list/f-index-list-item.vue'
)[
'default'
]
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论