Browse Source

花束新增页面实现删了图片时,立即请求删除接口

shizhongqi 10 months ago
parent
commit
2be7d80b5e
2 changed files with 38 additions and 1 deletions
  1. 37 0
      hdApp/src/admin/goods/add.vue
  2. 1 1
      hdApp/src/admin/goods/detail.vue

+ 37 - 0
hdApp/src/admin/goods/add.vue

@@ -151,6 +151,7 @@ import AppUploader from "@/components/app-uploader";
 const form = require("@/utils/formValidation.js");
 import { getClass } from "@/api/category";
 import { addGoodsData } from "@/api/goods"
+import { delImage } from "@/api/pic-text";
 import htzImageUpload from '@/components/htz-image-upload/htz-image-upload.vue'
 //import productMins from "@/mixins/product"; //复制页面代码时,没去掉
 import inputRestore from "@/mixins/inputRestore";
@@ -230,6 +231,42 @@ export default {
           this.form.shortCover = this.form.shopImg[0];
         }
       }
+
+      const shortDelPath = this.normalizeImgPath(res.del).replace(/^\//, '') // 因为 oss 接收的短路径不带 /
+      console.log('shortDelPath', shortDelPath);
+      if (shortDelPath != '') {
+        delImage({ filePath: shortDelPath }).then(() => {
+          console.log('图片删除成功');
+        }).catch(err => {
+          console.error('图片删除失败:', err);
+        });
+      } else {
+        console.error('图片删除失败:', '短路径为空');
+      }
+    },
+    // 规范化图片路径:去除域名与查询参数,保留以/开头的相对路径
+    normalizeImgPath(url){
+      if (!url || typeof url !== 'string') return ''
+      try {
+        // 去掉查询参数
+        const qIndex = url.indexOf('?')
+        const noQuery = qIndex >= 0 ? url.substring(0, qIndex) : url
+        // 提取以/开头的路径
+        const match = noQuery.match(/:\/\/[\w.-]+(\:\d+)?(\/[^?#]+)$/)
+        if (match && match[2]) {
+          return match[2]
+        }
+        // 若本身就是相对路径则直接返回
+        if (noQuery.startsWith('/')) return noQuery
+        // 若是无协议域名形式 //domain/path
+        if (noQuery.startsWith('//')) {
+          const idx = noQuery.indexOf('/', 2)
+          return idx > 0 ? noQuery.substring(idx+1) : '' // 去掉/
+        }
+        return noQuery
+      } catch (e) {
+        return ''
+      }
     },
     imgUploadSuccess(res) {
       var _res = JSON.parse(res.data)

+ 1 - 1
hdApp/src/admin/goods/detail.vue

@@ -246,7 +246,7 @@ export default {
       // 先记录对应的短路径(无域名、无参数)
       const shortDelPath = this.form.shopImg && this.form.shopImg[index]
         ? this.form.shopImg[index]
-        : this.normalizeImgPath(res && res.del ? res.del : '')
+        : this.normalizeImgPath(res && res.del ? res.del : '').replace(/^\//, '') // 因为 oss 接收的短路径不带 /
       // 从数组中移除
       this.form.shopImg.splice(index,1)
       // 收集删除的小图短路径,去重