Kaynağa Gözat

AI诗词课解决图片路径编码问题

liyanbo 4 gün önce
ebeveyn
işleme
2f60b52060
1 değiştirilmiş dosya ile 31 ekleme ve 0 silme
  1. 31 0
      src/views/AiPoetry/AIPoetryCourse.vue

+ 31 - 0
src/views/AiPoetry/AIPoetryCourse.vue

@@ -220,10 +220,41 @@ const menuInitialized = ref(false)
 const activeMenuKey = 'aiPoetryCourseActiveMenu'
 const activeIndexKey = 'aiPoetryCourseActiveIndex'
 
+const encodeImageUrl = (url) => {
+  if (!url) return url
+  try {
+    const decoded = decodeURI(url)
+    const lastSlash = decoded.lastIndexOf('/')
+    if (lastSlash === -1) {
+      return encodeURIComponent(decoded).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
+    }
+    const base = decoded.substring(0, lastSlash + 1)
+    const filename = decoded.substring(lastSlash + 1)
+    const encodedFilename = encodeURIComponent(filename)
+      .replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
+    return base + encodedFilename
+  } catch {
+    try {
+      const lastSlash = url.lastIndexOf('/')
+      if (lastSlash === -1) {
+        return encodeURIComponent(url).replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
+      }
+      const base = url.substring(0, lastSlash + 1)
+      const filename = url.substring(lastSlash + 1)
+      const encodedFilename = encodeURIComponent(filename)
+        .replace(/[!'()*]/g, c => '%' + c.charCodeAt(0).toString(16).toUpperCase())
+      return base + encodedFilename
+    } catch {
+      return url
+    }
+  }
+}
+
 const processCourseData = (data) => {
   return data.map((item, index) => {
     item.ctTypeSort = index + 1
     item.ctTypeSort = item.ctTypeSort > 9 ? item.ctTypeSort : "0" + item.ctTypeSort
+    item.ctTypeImage = encodeImageUrl(item.ctTypeImage)
     return item
   })
 }