<template>
<view>
<!-- 首次显示,第二次打开页面(也就是读缓存)不显示 -->
<view :style="{ 'background-image': 'url(' + smImg(testImg) + ')' }" style="width: 100%;height: 500rpx;" ></view>
<!-- 缓存正常 -->
<image :src="smImg(testImg)" style="margin-top: 200rpx;"></image>
</view>
</template>
<script>
export default {
data() {
return {
'testImg':'https://vkceyugu.cdn.bspapp.com/xxxx.png'
}
},
methods: {
smImg(url){
let real_img=url+'?x-oss-process=image/resize,l_200'
const cached = uni.getStorageSync(real_img)
if (cached) {
console.log(url + "已缓存为:" + cached)
return cached
} else {
console.log("未缓存,延迟下载")
setTimeout(() => {
uni.downloadFile({
url: url
}).then(res => {
if (res[1].statusCode == 200) {
uni.saveFile({
tempFilePath: res[1].tempFilePath,
success: function(res) {
console.log('下载完成:' + res.savedFilePath);
uni.setStorageSync(real_img, res.savedFilePath)
}
});
} else {
return Promise.reject('下载临时文件失败')
}
}).catch(info => {
console.log(info);
})
}, 2000)
return real_img
}
}
}
}
</script>
<style>
</style>
流云决 (作者)
好的,收到
2020-10-17 19:10
流云决 (作者)
带引号的写法微信小程序会报错
2020-11-15 11:48
DCloud_UNI_GSQ
回复 流云决: 先放到js里
2020-12-03 16:39
DCloud_UNI_GSQ
回复 流云决: 先放到js里拼接字符串
2020-12-03 16:40