Explorar el Código

优化文章详情页视频展示

为文章正文中的视频统一添加 article-inline-video 处理,归一化 /web/upload/ 上传视频地址,避免详情页仍访问旧域名导致黑屏。

补充正文视频样式,使 CKEditor 媒体块中的 video/iframe 按正文宽度铺满并保持 16:9 比例;详情模板将 articleThumbnail 作为视频 poster 传入,缩略图不再单独出现在正文中。

同步主站、学院站、蓝色学院站详情模板的 CSS/JS 版本号,并更新红色首页脚本版本。

Validation:

- node --check WebRoot/main/ffkj-portal/portal-zq.js

- git diff --check -- WebRoot/main/ffkj-portal/portal-zq.js WebRoot/main/ffkj-portal/portal-source.css WebRoot/web/upload/ffkj-template/portal-source.css WebRoot/web/upload/ffkj-template/school-details.jsp WebRoot/web/upload/ffkj-template/college-details.jsp WebRoot/web/upload/ffkj-template/college-blue-details.jsp WebRoot/web/upload/ffkj-template/screen-index.jsp

- 浏览器刷新 http://ffkj-screen-red.localhost:4188/duocaihuaxia/260627/m8HjAfx6lwfv.htm,确认正文 video 宽高为 821x462、src 归一到当前站点 /web/upload/ 路径、poster 使用上传缩略图并可见。
非繁 hace 5 meses
padre
commit
14c53e072a

+ 27 - 0
WebRoot/main/ffkj-portal/portal-source.css

@@ -6304,6 +6304,33 @@ body.screen-red-theme .screen-qr-image {
   margin: 18px auto;
 }
 
+.article-body figure.media,
+.article-body .media {
+  width: 100%;
+  margin: 24px 0;
+}
+
+.article-body figure.media > div,
+.article-body .media > div {
+  width: 100%;
+}
+
+.article-body video,
+.article-body iframe {
+  display: block;
+  width: 100% !important;
+  max-width: 100% !important;
+}
+
+.article-body video,
+.article-body .article-inline-video {
+  height: auto !important;
+  aspect-ratio: 16 / 9;
+  border-radius: 4px;
+  background: #111;
+  object-fit: contain;
+}
+
 .article-body table {
   width: 100%;
   max-width: 100%;

+ 133 - 0
WebRoot/main/ffkj-portal/portal-zq.js

@@ -7,6 +7,35 @@
     }
   }
 
+  var articleVideoPattern = /\.(mp4|webm|ogg|m4v|mov|m3u8)(\?|#|$)/i;
+
+  function normalizePortalUrl(url, baseUrl) {
+    if (!url) return "";
+    try {
+      return new URL(url, baseUrl || window.location.href).href;
+    } catch (error) {
+      return url;
+    }
+  }
+
+  function normalizePortalUploadUrl(url, baseUrl) {
+    var normalized = normalizePortalUrl(url, baseUrl);
+    if (!normalized) return "";
+    try {
+      var parsed = new URL(normalized, window.location.href);
+      var uploadIndex = parsed.pathname.indexOf("/web/upload/");
+      if (uploadIndex >= 0) {
+        var uploadPath = parsed.pathname.slice(uploadIndex).replace(/\/{2,}/g, "/");
+        return window.location.origin + uploadPath + parsed.search + parsed.hash;
+      }
+    } catch (error) {
+      normalized = String(normalized).replace(/^(?:https?:)?\/\/[^/]*(\/web\/upload\/.*)$/i, "$1");
+      normalized = normalized.replace(/^(https?:)\/+(\/web\/upload\/.*)$/i, "$2");
+      normalized = normalized.replace(/(\/web\/upload\/modules)\/+/i, "$1/");
+    }
+    return normalized;
+  }
+
   function setDrawer(open, trigger) {
     var drawerLayer = document.querySelector(".drawer-layer");
     var buttons = Array.prototype.slice.call(document.querySelectorAll(".mobile-menu"));
@@ -163,6 +192,109 @@
     });
   }
 
+  function setupArticleBodyVideos() {
+    var videos = Array.prototype.slice.call(document.querySelectorAll(".article-body video"));
+    if (!videos.length) return;
+
+    function sourceOf(video) {
+      var source = video.querySelector("source[src]");
+      return {
+        node: source,
+        value: video.getAttribute("src") || (source && source.getAttribute("src")) || ""
+      };
+    }
+
+    function closestArticleBody(node) {
+      while (node && node !== document) {
+        if (node.classList && node.classList.contains("article-body")) return node;
+        node = node.parentNode;
+      }
+      return null;
+    }
+
+    function thumbnailPoster(video) {
+      var body = closestArticleBody(video);
+      var poster = body ? body.getAttribute("data-article-thumbnail") : "";
+      return poster ? normalizePortalUploadUrl(poster) : "";
+    }
+
+    function syncSource(video) {
+      var current = sourceOf(video);
+      if (!current.value || !articleVideoPattern.test(current.value)) return;
+      var normalized = normalizePortalUploadUrl(current.value);
+      if (!normalized || normalized === current.value) return;
+      if (video.getAttribute("src")) video.setAttribute("src", normalized);
+      if (current.node) current.node.setAttribute("src", normalized);
+      if (video.load) video.load();
+    }
+
+    function capturePoster(video) {
+      if (video.poster || video.getAttribute("data-article-poster-ready") === "true") return;
+      if (!video.videoWidth || !video.videoHeight) return;
+      try {
+        var canvas = document.createElement("canvas");
+        canvas.width = video.videoWidth;
+        canvas.height = video.videoHeight;
+        canvas.getContext("2d").drawImage(video, 0, 0, canvas.width, canvas.height);
+        video.poster = canvas.toDataURL("image/jpeg", 0.86);
+        video.setAttribute("data-article-poster-ready", "true");
+        if (video.paused && video.currentTime > 0.02) {
+          try {
+            video.currentTime = 0;
+          } catch (error) {}
+        }
+      } catch (error) {}
+    }
+
+    function primePoster(video) {
+      if (video.poster) return;
+      var seeked = false;
+
+      function seekFirstFrame() {
+        if (seeked) return;
+        seeked = true;
+        if (video.readyState >= 2) {
+          capturePoster(video);
+          return;
+        }
+        if (!isFinite(video.duration) || video.duration <= 0) return;
+        try {
+          video.currentTime = Math.min(0.12, Math.max(0, video.duration - 0.05));
+        } catch (error) {}
+      }
+
+      video.addEventListener("loadeddata", function () {
+        capturePoster(video);
+      }, { once: true });
+      video.addEventListener("seeked", function () {
+        capturePoster(video);
+      }, { once: true });
+      video.addEventListener("loadedmetadata", seekFirstFrame, { once: true });
+
+      if (video.readyState >= 1) {
+        seekFirstFrame();
+      } else if (video.load) {
+        video.load();
+      }
+    }
+
+    videos.forEach(function (video) {
+      video.classList.add("article-inline-video");
+      video.controls = true;
+      video.playsInline = true;
+      video.setAttribute("playsinline", "");
+      video.setAttribute("preload", "metadata");
+      video.removeAttribute("width");
+      video.removeAttribute("height");
+      syncSource(video);
+      if (!video.poster) {
+        var poster = thumbnailPoster(video);
+        if (poster) video.poster = poster;
+      }
+      if (!video.poster) primePoster(video);
+    });
+  }
+
   function setupColorfulVideoPlayer() {
     var cards = Array.prototype.slice.call(document.querySelectorAll(".colorful-slide.is-video"));
     if (!cards.length || !window.DOMParser) return;
@@ -1037,6 +1169,7 @@
     setupDrawer();
     setupSearch();
     setupEmptyArticleNotice();
+    setupArticleBodyVideos();
     setupScreenHeaderScroll();
     setupDepartmentNavStickyState();
     setupScreenNavPanelBounds();

+ 3 - 3
WebRoot/web/upload/ffkj-template/college-blue-details.jsp

@@ -10,7 +10,7 @@
 	<meta name="viewport" content="width=device-width, initial-scale=1">
 	<meta name="description" content="${articleDescription}">
 	<title>${articleTitle}_${navigation.name}_${website.seoTitle}</title>
-	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260623-building-lines">
+	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260627-article-video-fit">
 	<c:if test="${not empty portalThemeStyle}"><style>${portalThemeStyle}</style></c:if>
 </head>
 <body class="${portalThemeClass}">
@@ -39,7 +39,7 @@
 							<h1>${articleTitle}</h1>
 							<p><time>${articleReleaseTime}</time><em>来源:${articleSource}</em></p>
 						</header>
-						<div class="article-body">${articleContent}</div>
+						<div class="article-body" data-article-thumbnail="${articleThumbnail}">${articleContent}</div>
 						<div class="article-tags"><span>${navigation.name}</span><span>${website.seoTitle}</span></div>
 					</article>
 
@@ -57,6 +57,6 @@
 
 		<%@ include file="college-blue-footer.jsp"%>
 	</div>
-	<script src="/main/ffkj-portal/portal-zq.js?v=20260623-college-blue-nju-title"></script>
+	<script src="/main/ffkj-portal/portal-zq.js?v=20260627-article-video-fit"></script>
 </body>
 </html>

+ 3 - 3
WebRoot/web/upload/ffkj-template/college-details.jsp

@@ -8,7 +8,7 @@
 	<meta name="viewport" content="width=device-width, initial-scale=1">
 	<meta name="description" content="${articleDescription}">
 	<title>${articleTitle}_${navigation.name}_${website.seoTitle}</title>
-	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260601-drawer-touch-toggle">
+	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260627-article-video-fit">
 </head>
 <body>
 	<div class="college-page college-department-page department-subpage">
@@ -36,7 +36,7 @@
 							<h1>${articleTitle}</h1>
 							<p><time>${articleReleaseTime}</time><em>来源:${articleSource}</em></p>
 						</header>
-						<div class="article-body">${articleContent}</div>
+						<div class="article-body" data-article-thumbnail="${articleThumbnail}">${articleContent}</div>
 						<div class="article-tags"><span>${navigation.name}</span><span>${website.seoTitle}</span></div>
 					</article>
 
@@ -54,6 +54,6 @@
 
 		<footer class="college-footer department-footer"><div class="wrap college-footer-inner"><p>${website.name}</p><nav aria-label="${website.name}页脚链接"><a href="http://127.0.0.1:4188/">学校首页</a><a href="/">${website.seoTitle}首页</a></nav></div></footer>
 	</div>
-	<script src="/main/ffkj-portal/portal-zq.js?v=20260601-drawer-touch-toggle"></script>
+	<script src="/main/ffkj-portal/portal-zq.js?v=20260627-article-video-fit"></script>
 </body>
 </html>

+ 27 - 0
WebRoot/web/upload/ffkj-template/portal-source.css

@@ -6304,6 +6304,33 @@ body.screen-red-theme .screen-qr-image {
   margin: 18px auto;
 }
 
+.article-body figure.media,
+.article-body .media {
+  width: 100%;
+  margin: 24px 0;
+}
+
+.article-body figure.media > div,
+.article-body .media > div {
+  width: 100%;
+}
+
+.article-body video,
+.article-body iframe {
+  display: block;
+  width: 100% !important;
+  max-width: 100% !important;
+}
+
+.article-body video,
+.article-body .article-inline-video {
+  height: auto !important;
+  aspect-ratio: 16 / 9;
+  border-radius: 4px;
+  background: #111;
+  object-fit: contain;
+}
+
 .article-body table {
   width: 100%;
   max-width: 100%;

+ 3 - 3
WebRoot/web/upload/ffkj-template/school-details.jsp

@@ -10,7 +10,7 @@
 	<meta name="viewport" content="width=device-width, initial-scale=1">
 	<meta name="description" content="${articleDescription}">
 	<title>${articleTitle}_${navigation.name}_${website.seoTitle}</title>
-	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260626-screen-red-inner-title-edge">
+	<link rel="stylesheet" href="/main/ffkj-portal/portal-source.css?v=20260627-article-video-fit">
 </head>
 <body class="${portalThemeClass}">
 	<div class="portal-shell inner-shell ${portalThemeClass} ${screenHeaderMode ? 'screen-shell screen-inner-shell' : ''}">
@@ -94,7 +94,7 @@
 							<h1>${articleTitle}</h1>
 							<p><time>${articleReleaseTime}</time><em>来源:${articleSource}</em></p>
 						</header>
-						<div class="article-body">${articleContent}</div>
+						<div class="article-body" data-article-thumbnail="${articleThumbnail}">${articleContent}</div>
 						<div class="article-tags"><span>${navigation.name}</span><span>${website.name}</span></div>
 					</article>
 
@@ -168,6 +168,6 @@
 			</div>
 		</footer>
 	</div>
-	<script src="/main/ffkj-portal/portal-zq.js?v=20260626-screen-red-nav-panel-fade"></script>
+	<script src="/main/ffkj-portal/portal-zq.js?v=20260627-article-video-fit"></script>
 </body>
 </html>

+ 1 - 1
WebRoot/web/upload/ffkj-template/screen-index.jsp

@@ -10,7 +10,7 @@
 <c:set var="screenPortalJsVersion" value="20260623-hero-wheel-return"/>
 <c:if test="${portalThemeClass eq 'screen-red-theme'}">
 	<c:set var="screenPortalCssVersion" value="20260627-screen-red-video-buttons"/>
-	<c:set var="screenPortalJsVersion" value="20260627-screen-red-video-xhr-normalize"/>
+	<c:set var="screenPortalJsVersion" value="20260627-article-video-fit"/>
 </c:if>
 <!doctype html>
 <html lang="zh-CN">