Browse Source

fixed 商品详情视频播放功能的支持

fushengqian 6 months ago
parent
commit
c39a0626b2

+ 33 - 0
fuint-application/src/main/java/com/fuint/common/util/CommonUtil.java

@@ -475,6 +475,39 @@ public class CommonUtil {
                 || ((first >= 0x1F601) && (first <= 0x1F64F));
     }
 
+    /**
+     * 处理html中的视频
+     *
+     * @param html
+     * @return
+     * */
+    public static String fixVideo(String html) {
+        // 正则表达式匹配<iframe>标签,并捕获src属性
+        String iframeRegex = "<iframe[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
+        Pattern pattern = Pattern.compile(iframeRegex);
+        Matcher matcher = pattern.matcher(html);
+
+        // 用于存储替换后的HTML
+        StringBuffer result = new StringBuffer();
+
+        // 遍历所有匹配的<iframe>标签
+        while (matcher.find()) {
+            // 获取src属性的值
+            String src = matcher.group(1);
+
+            // 构建<video>标签
+            String videoTag = "<video controls><source src=\"" + src + "\" type=\"video/mp4\">Your browser does not support the video tag.</video>";
+
+            // 将匹配的<iframe>标签替换为<video>标签
+            matcher.appendReplacement(result, videoTag);
+        }
+
+        // 将剩余的HTML内容追加到结果中
+        matcher.appendTail(result);
+
+        return result.toString();
+    }
+
     /**
      * 过滤特殊字符
      *

+ 4 - 0
fuint-application/src/main/java/com/fuint/module/clientApi/controller/ClientGoodsController.java

@@ -10,6 +10,7 @@ import com.fuint.common.service.CateService;
 import com.fuint.common.service.GoodsService;
 import com.fuint.common.service.MerchantService;
 import com.fuint.common.service.SettingService;
+import com.fuint.common.util.CommonUtil;
 import com.fuint.framework.exception.BusinessCheckException;
 import com.fuint.framework.pagination.PaginationRequest;
 import com.fuint.framework.pagination.PaginationResponse;
@@ -212,6 +213,9 @@ public class ClientGoodsController extends BaseController {
         goodsDetailDto.setStock(goodsDto.getStock());
         goodsDetailDto.setWeight(goodsDto.getWeight());
         goodsDetailDto.setDescription(goodsDto.getDescription());
+        if (StringUtil.isNotEmpty(goodsDetailDto.getDescription())) {
+            goodsDetailDto.setDescription(CommonUtil.fixVideo(goodsDetailDto.getDescription()));
+        }
         goodsDetailDto.setInitSale(goodsDto.getInitSale());
         goodsDetailDto.setStatus(goodsDto.getStatus());