当前位置: 首页 > java >正文

线上项目https看不了http的图片解决

解决方式:由后端转发,前端将图片url接口传给后端, 后端返回blob数据流展示,前端对图片url相同的进行缓存,不会重复发请求。

封装方法utils/transitionImage.ts


import { previewImage } from '@/api/common'// 跟踪正在加载的URL
export const loadingUrls = new Set<string>()
// 跟踪pending状态的请求(用于取消请求)
const pendingRequests = new Map<string, AbortController>()
// 记录每个URL的最新请求序列号(确保响应顺序正确)
const requestSequence = new Map<string, number>()export const getImageUrl = async (url: string,pictureUrlsRef: { value: Record<string, string> },forceUpdate = false
): Promise<string | undefined> => {if (!url) return// 生成当前请求的序列号const currentSeq = (requestSequence.get(url) || 0) + 1requestSequence.set(url, currentSeq)// 强制刷新时:取消旧请求(如果存在)if (forceUpdate) {// 清除旧缓存if (pictureUrlsRef.value[url]) {URL.revokeObjectURL(pictureUrlsRef.value[url])delete pictureUrlsRef.value[url]}// 取消pending的旧请求const existingController = pendingRequests.get(url)if (existingController) {existingController.abort() // 中断旧请求pendingRequests.delete(url)loadingUrls.delete(url) // 移除加载中标记}} else {// 非强制刷新:如果已有缓存,直接返回(核心逻辑)if (pictureUrlsRef.value[url]) {return pictureUrlsRef.value[url]}// 非强制刷新:如果正在加载,直接返回(避免重复请求)if (loadingUrls.has(url)) {return}}// 发起新请求(仅当无缓存、非加载中、或强制刷新时)const controller = new AbortController()pendingRequests.set(url, controller)loadingUrls.add(url)try {const res = await previewImage(encodeURIComponent(url), {signal: controller.signal})// 校验序列号,确保只处理最新请求的响应if (currentSeq !== requestSequence.get(url)) {console.log(`忽略过期响应: ${url} (当前序列号: ${currentSeq}, 最新序列号: ${requestSequence.get(url)})`)return}if (res?.data instanceof Blob) {const blobUrl = URL.createObjectURL(res.data)pictureUrlsRef.value[url] = blobUrl // 缓存结果return blobUrl}} catch (error) {// 忽略主动取消的错误if ((error as Error).name !== 'AbortError') {console.error('图片加载失败:', error)}} finally {// 清理状态(仅当前请求是最新的才执行)if (currentSeq === requestSequence.get(url)) {loadingUrls.delete(url)pendingRequests.delete(url)}}
}export const clearImageCache = (pictureUrlsRef: { value: Record<string, string> }) => {Object.values(pictureUrlsRef.value).forEach((url) => {if (url.startsWith('blob:')) {URL.revokeObjectURL(url)}})pictureUrlsRef.value = {}requestSequence.clear() // 重置序列号
}

vue页面内使用

import { getImageUrl, loadingUrls, clearImageCache } from '@/utils/transitionImage'const pictureUrls = ref<Record<string, string>>({})const schema2 = reactive<DescriptionsSchema[]>([
{field: 'vehiclePicUri',label: '通行抓拍图',width: 100,slots: {default: (data: any) => {const imageUrl = data.vehiclePicUriconst imgUrl = pictureUrls.value[imageUrl]const isLoading = Array.from(loadingUrls).includes(imageUrl)if (imageUrl && !imgUrl && !isLoading) {getImageUrl(imageUrl, pictureUrls)}return imgUrl ? (<ElImagestyle="width: 100px; height: 100px"src={imgUrl}zoom-rate={1.2}max-scale={7}min-scale={0.2}preview-src-list={[imgUrl]}initial-index={0}fit="cover"/>) : isLoading ? (<div class="flex items-center justify-center h-full"><i class="el-icon-loading"></i></div>) : (<div>(未知)</div>)}}}
])// 监听弹窗打开,触发图片加载
watch(() => dialogVisible2.value,(newVal) => {if (newVal && data1) {const imageUrl = data1.vehiclePicUriif (imageUrl) {getImageUrl(imageUrl, pictureUrls)}}}
)// 组件卸载时清理缓存,防止内存泄漏
onUnmounted(() => {clearImageCache(pictureUrls)
})

http://www.xdnf.cn/news/15594.html

相关文章:

  • 在分布式系统中,如何保证缓存与数据库的数据一致性?
  • docker 容器无法使用dns解析域名异常问题排查
  • springboot 整合spring-kafka客户端:SASL_SSL+PLAINTEXT方式
  • LeetCode20
  • 边界路由器
  • Baumer工业相机堡盟工业相机如何通过YoloV8模型实现人物识别(C#)
  • 如何做好DNA-SIP?
  • Redis完全指南:从基础到实战(含缓存问题、布隆过滤器、持久化及Spring Boot集成)
  • 数据结构 栈(2)--栈的实现
  • 4.PCL点云的数据结构
  • 「Chrome 开发环境快速屏蔽 CORS 跨域限制详细教程」*
  • springboot跨域问题 和 401
  • 人工智能基础知识笔记十四:文本转换成向量
  • Android 实现:当后台数据限制开启时,仅限制互联网APN。
  • 什么是“数据闭环”
  • Docker-Beta?ollama的完美替代品
  • MySQL高可用集群架构:主从复制、MGR与读写分离实战
  • TDengine 的可视化数据库操作工具 taosExplorer(安装包自带)
  • VMware Workstation Pro 17下载安装
  • VR全景园区:开启智慧园区新时代
  • 基于C#+SQlite开发(WinForm)个人日程管理系统
  • 【leetcode】852. 山脉数组的封顶索引
  • 树莓派Qt 安装
  • CDSS系统升级“可视化解释-智能反馈-临床语言“三位一体设计架构设计分析
  • nginx代理websocket请求
  • 【华为】交换机vlan互访实验
  • 语雀编辑器内双击回车插入当前时间js脚本
  • 取消office word中的段落箭头标记
  • Java零基础快速入门
  • Vue3入门-组件及组件化