|
|
@@ -0,0 +1,208 @@
|
|
|
+package com.aijiuyi.admin.controller;
|
|
|
+
|
|
|
+import com.aijiuyi.admin.common.annotation.NoAuth;
|
|
|
+import com.aijiuyi.admin.entity.ContentArticle;
|
|
|
+import com.aijiuyi.admin.service.ContentArticleService;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.web.bind.annotation.GetMapping;
|
|
|
+import org.springframework.web.bind.annotation.PathVariable;
|
|
|
+import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 文章公开阅读页 Controller
|
|
|
+ * 提供无需登录即可访问的文章 HTML 阅读页面
|
|
|
+ * 特性:
|
|
|
+ * - 手机端自适应(viewport + 响应式CSS)
|
|
|
+ * - 支持水印(watermarkEnabled=1时展示)
|
|
|
+ * - 禁止文本复制(CSS + JS)
|
|
|
+ * - 增加浏览量统计
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("/article")
|
|
|
+@NoAuth
|
|
|
+public class ArticleViewController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ContentArticleService articleService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 公开阅读文章页面
|
|
|
+ * 仅已发布状态的文章可被访问
|
|
|
+ *
|
|
|
+ * @param id 文章ID
|
|
|
+ * @return HTML 页面内容
|
|
|
+ */
|
|
|
+ @GetMapping(value = "/view/{id}", produces = MediaType.TEXT_HTML_VALUE + ";charset=UTF-8")
|
|
|
+ public String viewArticle(@PathVariable Long id) {
|
|
|
+ ContentArticle article = articleService.getById(id);
|
|
|
+ if (article == null || !"PUBLISHED".equals(article.getStatus())) {
|
|
|
+ return buildErrorPage("文章不存在或未发布");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 增加浏览量
|
|
|
+ ContentArticle update = new ContentArticle();
|
|
|
+ update.setId(id);
|
|
|
+ update.setViewCount(article.getViewCount() == null ? 1 : article.getViewCount() + 1);
|
|
|
+ articleService.updateById(update);
|
|
|
+
|
|
|
+ return buildArticlePage(article);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建文章阅读页 HTML
|
|
|
+ */
|
|
|
+ private String buildArticlePage(ContentArticle article) {
|
|
|
+ String watermarkText = article.getWatermarkText();
|
|
|
+ boolean hasWatermark = watermarkText != null && !watermarkText.trim().isEmpty();
|
|
|
+ String title = escapeHtml(article.getTitle() != null ? article.getTitle() : "");
|
|
|
+ String author = escapeHtml(article.getAuthor() != null ? article.getAuthor() : "");
|
|
|
+ String publishTime = article.getPublishTime() != null ? article.getPublishTime().toString().replace("T", " ") : "";
|
|
|
+ String content = article.getContent() != null ? article.getContent() : "";
|
|
|
+
|
|
|
+ StringBuilder html = new StringBuilder();
|
|
|
+ html.append("<!DOCTYPE html>\n");
|
|
|
+ html.append("<html lang=\"zh-CN\">\n<head>\n");
|
|
|
+ html.append("<meta charset=\"UTF-8\">\n");
|
|
|
+ html.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no\">\n");
|
|
|
+ html.append("<title>").append(title).append("</title>\n");
|
|
|
+ html.append("<style>\n");
|
|
|
+ // 基础样式 - 移动端适配
|
|
|
+ html.append("* { margin: 0; padding: 0; box-sizing: border-box; }\n");
|
|
|
+ html.append("body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif; ");
|
|
|
+ html.append("line-height: 1.8; color: #333; background: #f5f5f5; }\n");
|
|
|
+ html.append(".article-container { max-width: 800px; margin: 0 auto; padding: 20px 16px; background: #fff; min-height: 100vh; }\n");
|
|
|
+ html.append(".article-title { font-size: 22px; font-weight: 700; color: #1a1a1a; margin-bottom: 12px; word-break: break-word; }\n");
|
|
|
+ html.append(".article-meta { font-size: 13px; color: #999; margin-bottom: 20px; padding-bottom: 16px; border-bottom: 1px solid #eee; }\n");
|
|
|
+ html.append(".article-meta span { margin-right: 16px; }\n");
|
|
|
+ html.append(".article-content { font-size: 16px; line-height: 1.9; word-break: break-word; overflow-wrap: break-word; }\n");
|
|
|
+ html.append(".article-content img { max-width: 100% !important; height: auto !important; display: block; margin: 12px auto; border-radius: 4px; }\n");
|
|
|
+ html.append(".article-content video { max-width: 100% !important; height: auto !important; display: block; margin: 12px auto; }\n");
|
|
|
+ html.append(".article-content p { margin-bottom: 16px; }\n");
|
|
|
+ html.append(".article-content table { width: 100% !important; overflow-x: auto; display: block; border-collapse: collapse; margin: 12px 0; }\n");
|
|
|
+ html.append(".article-content td, .article-content th { border: 1px solid #ddd; padding: 8px; text-align: left; }\n");
|
|
|
+ html.append(".article-content pre { background: #f6f8fa; padding: 12px; border-radius: 6px; overflow-x: auto; margin: 12px 0; }\n");
|
|
|
+ html.append(".article-content code { font-family: 'SFMono-Regular', Consolas, monospace; font-size: 14px; }\n");
|
|
|
+ // 禁止复制样式
|
|
|
+ html.append(".no-copy { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }\n");
|
|
|
+ // 水印样式
|
|
|
+ if (hasWatermark) {
|
|
|
+ html.append(".watermark { position: fixed; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 9999; overflow: hidden; }\n");
|
|
|
+ html.append(".watermark-text { position: absolute; color: rgba(0,0,0,0.06); font-size: 16px; transform: rotate(-30deg); white-space: nowrap; font-weight: 500; }\n");
|
|
|
+ }
|
|
|
+ // 移动端适配
|
|
|
+ html.append("@media (max-width: 768px) {\n");
|
|
|
+ html.append(" .article-container { padding: 16px 12px; }\n");
|
|
|
+ html.append(" .article-title { font-size: 20px; }\n");
|
|
|
+ html.append(" .article-content { font-size: 15px; }\n");
|
|
|
+ html.append("}\n");
|
|
|
+ html.append("</style>\n");
|
|
|
+ html.append("</head>\n<body>\n");
|
|
|
+
|
|
|
+ html.append("<div class=\"article-container no-copy\">\n");
|
|
|
+ html.append(" <h1 class=\"article-title\">").append(title).append("</h1>\n");
|
|
|
+ html.append(" <div class=\"article-meta\">\n");
|
|
|
+ if (!author.isEmpty()) {
|
|
|
+ html.append(" <span>").append(author).append("</span>\n");
|
|
|
+ }
|
|
|
+ if (!publishTime.isEmpty()) {
|
|
|
+ html.append(" <span>").append(publishTime).append("</span>\n");
|
|
|
+ }
|
|
|
+ html.append(" </div>\n");
|
|
|
+ html.append(" <div class=\"article-content\">\n");
|
|
|
+ html.append(content);
|
|
|
+ html.append("\n </div>\n");
|
|
|
+ html.append("</div>\n");
|
|
|
+
|
|
|
+ // 水印层
|
|
|
+ if (hasWatermark) {
|
|
|
+ html.append("<div class=\"watermark\" id=\"watermark\"></div>\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 禁止复制 JS
|
|
|
+ html.append("<script>\n");
|
|
|
+ html.append("// 禁止右键菜单\n");
|
|
|
+ html.append("document.addEventListener('contextmenu', function(e) { e.preventDefault(); });\n");
|
|
|
+ html.append("// 禁止复制\n");
|
|
|
+ html.append("document.addEventListener('copy', function(e) { e.preventDefault(); });\n");
|
|
|
+ html.append("// 禁止剪切\n");
|
|
|
+ html.append("document.addEventListener('cut', function(e) { e.preventDefault(); });\n");
|
|
|
+ html.append("// 禁止选择\n");
|
|
|
+ html.append("document.addEventListener('selectstart', function(e) { e.preventDefault(); });\n");
|
|
|
+ html.append("// 禁止拖拽\n");
|
|
|
+ html.append("document.addEventListener('dragstart', function(e) { e.preventDefault(); });\n");
|
|
|
+
|
|
|
+ // 水印生成 JS
|
|
|
+ if (hasWatermark) {
|
|
|
+ html.append("\n// 生成水印\n");
|
|
|
+ html.append("(function() {\n");
|
|
|
+ html.append(" var container = document.getElementById('watermark');\n");
|
|
|
+ html.append(" var text = '").append(escapeJs(watermarkText.trim())).append("';\n");
|
|
|
+ html.append(" var cols = Math.ceil(window.innerWidth / 200);\n");
|
|
|
+ html.append(" var rows = Math.ceil(window.innerHeight / 150);\n");
|
|
|
+ html.append(" for (var i = 0; i < rows + 5; i++) {\n");
|
|
|
+ html.append(" for (var j = 0; j < cols + 3; j++) {\n");
|
|
|
+ html.append(" var span = document.createElement('span');\n");
|
|
|
+ html.append(" span.className = 'watermark-text';\n");
|
|
|
+ html.append(" span.textContent = text;\n");
|
|
|
+ html.append(" span.style.left = (j * 200 - 50) + 'px';\n");
|
|
|
+ html.append(" span.style.top = (i * 150 - 30) + 'px';\n");
|
|
|
+ html.append(" container.appendChild(span);\n");
|
|
|
+ html.append(" }\n");
|
|
|
+ html.append(" }\n");
|
|
|
+ html.append("})();\n");
|
|
|
+ }
|
|
|
+
|
|
|
+ html.append("</script>\n");
|
|
|
+ html.append("</body>\n</html>");
|
|
|
+
|
|
|
+ return html.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建错误页面 HTML
|
|
|
+ */
|
|
|
+ private String buildErrorPage(String message) {
|
|
|
+ return "<!DOCTYPE html>\n" +
|
|
|
+ "<html lang=\"zh-CN\">\n<head>\n" +
|
|
|
+ "<meta charset=\"UTF-8\">\n" +
|
|
|
+ "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
|
|
|
+ "<title>页面不存在</title>\n" +
|
|
|
+ "<style>\n" +
|
|
|
+ "body { font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Microsoft YaHei', sans-serif; " +
|
|
|
+ "display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #f5f5f5; }\n" +
|
|
|
+ ".error-box { text-align: center; padding: 40px 20px; }\n" +
|
|
|
+ ".error-box h2 { font-size: 20px; color: #666; margin-bottom: 12px; }\n" +
|
|
|
+ ".error-box p { font-size: 14px; color: #999; }\n" +
|
|
|
+ "</style>\n</head>\n<body>\n" +
|
|
|
+ "<div class=\"error-box\">\n" +
|
|
|
+ " <h2>" + escapeHtml(message) + "</h2>\n" +
|
|
|
+ " <p>请确认链接是否正确</p>\n" +
|
|
|
+ "</div>\n</body>\n</html>";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * HTML 转义
|
|
|
+ */
|
|
|
+ private String escapeHtml(String text) {
|
|
|
+ if (text == null) return "";
|
|
|
+ return text.replace("&", "&")
|
|
|
+ .replace("<", "<")
|
|
|
+ .replace(">", ">")
|
|
|
+ .replace("\"", """)
|
|
|
+ .replace("'", "'");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * JS 字符串转义
|
|
|
+ */
|
|
|
+ private String escapeJs(String text) {
|
|
|
+ if (text == null) return "";
|
|
|
+ return text.replace("\\", "\\\\")
|
|
|
+ .replace("'", "\\'")
|
|
|
+ .replace("\"", "\\\"")
|
|
|
+ .replace("\n", "\\n")
|
|
|
+ .replace("\r", "\\r");
|
|
|
+ }
|
|
|
+}
|