Эх сурвалжийг харах

修复 Word 导入未走 LibreOffice 保格式链路

新增 WordImportService,直接返回包含 htmlContent 的 JSON,不再使用 convert_html 的页面跳转语义;转换前限制文件必须位于系统上传目录且为 doc/docx,转换后返回正文 HTML 和文件路径。

文章编辑器的 Word 转换入口切换为 sys.convert_json,并更新脚本缓存版本;发布脚本补充新服务配置、Java 类和远程验收项。

Validation:
- Java 8 编译通过
- 文章编辑页 token 已实际调用 convert_json;本地因未安装 LibreOffice 返回 JSON 失败并正确回退 Mammoth
- HXCS 已安装 LibreOffice 7.3.7.2,双节点真实转换样本保留字体、字号、颜色、粗体和首行缩进
- bash -n、node --check、Git diff 检查通过
非繁 1 сар өмнө
parent
commit
127601217a

+ 5 - 1
WebRoot/WEB-INF/apps/sys/config/services/libreOffice.service.xml

@@ -6,7 +6,11 @@
 		<service name="convert_html" desc="转换成 html 文件" title="转换成 html 文件" class="space.core.apps.sys.service.LibreOfficeService" method="convertHtml">
 			<verify required="path" />
 		</service>
+
+		<service name="convert_json" desc="Word 转换为富文本 JSON" title="Word 转换为富文本 JSON" class="space.core.apps.sys.service.WordImportService" method="convertJson">
+			<verify required="path" />
+		</service>
 		
 	</service>
 	
-</services>
+</services>

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10104/1002110104_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-editor-format"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>
@@ -58,7 +58,7 @@
 
 							<el-form-item label="正文" prop="body" class="ueditor" style="margin-bottom: 0;">
 								<space-editor v-model="obj.body" :height="editorHeight" placeholder="请输入正文内容" ref="editor"
-								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_html' />"></space-editor>
+								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_json' />"></space-editor>
 							</el-form-item>
 						</el-col>
 

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10105/1002110105_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-editor-format"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>
@@ -59,7 +59,7 @@
 
 							<el-form-item label="正文" prop="body" class="ueditor" style="margin-bottom: 0;">
 								<space-editor v-model="obj.body" :height="editorHeight" placeholder="请输入正文内容" ref="editor"
-								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_html' />"></space-editor>
+								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_json' />"></space-editor>
 								<pre id="body_content" style="display: none;">${editBody}</pre>
 							</el-form-item>
 						</el-col>

+ 2 - 2
WebRoot/web/subassembly/app/pc/10021/10211/1002110211_run.jsp

@@ -18,7 +18,7 @@
  <script src="/apps/space/plug-in/element-resize-detector/element-resize-detector.min.js"></script> 
  <script src="/apps/space/plug-in/vue/vue.min.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/apps/space/plug-in/element-ui@2.13.0/index.js" type="text/javascript" charset="utf-8"></script> 
- <script src="/js/space.vue.pc.subassembly.js?v=20261027-editor-format"></script>
+ <script src="/js/space.vue.pc.subassembly.js?v=20261027-word-format-3"></script>
  <script src="/apps/space/plug-in/layui/dist/layui.all.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.recycle.js" type="text/javascript" charset="utf-8"></script> 
  <script src="/js/space.display.js?v=20261027-image-15m" type="text/javascript" charset="utf-8"></script>
@@ -60,7 +60,7 @@
 
 							<el-form-item label="正文" prop="body" class="ueditor" style="margin-bottom: 0;">
 								<space-editor v-model="obj.body" :height="editorHeight" placeholder="请输入正文内容" ref="editor"
-								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_html' />"></space-editor>
+								 articlepreview="true" :previewdata="obj" previewurl="<app:token name='website.article_preview' />" wordconverturl="<app:token name='sys.convert_json' />"></space-editor>
 								<pre id="body_content" style="display: none;">${editBody}</pre>
 							</el-form-item>
 						</el-col>

+ 1 - 0
local-run/bin/compile-hxcs-deploy-classes.sh

@@ -62,6 +62,7 @@ SOURCES=(
 	"${PROJECT_ROOT}/src/space/apps/moudules/sys/services/RoleService.java"
 	"${PROJECT_ROOT}/src/space/apps/moudules/personnel/services/PersonService.java"
 	"${PROJECT_ROOT}/src/space/apps/moudules/personnel/services/PersonSelectService.java"
+	"${PROJECT_ROOT}/src/space/core/apps/sys/service/WordImportService.java"
 )
 
 for source in "${SOURCES[@]}"; do

+ 6 - 4
local-run/bin/sync-to-hxcs-prod.sh

@@ -456,7 +456,7 @@ sync_static_files() {
 				"${PROJECT_ROOT}/WebRoot/web/subassembly/app/pc/${page_id}/"
 		fi
 	done
-	for svc in unit department post role authorization; do
+	for svc in unit department post role authorization libreOffice; do
 		local src="${PROJECT_ROOT}/WebRoot/WEB-INF/apps/sys/config/services/${svc}.service.xml"
 		[[ -f "${src}" ]] && rsync_to "${host}" "${password}" "${webapp}/WEB-INF/apps/sys/config/services/" "${src}"
 	done
@@ -491,6 +491,7 @@ sync_static_files() {
 		"space/apps/moudules/basic/util/SortOrderUtil.class" \
 		"space/apps/moudules/personnel/services/PersonService.class" \
 		"space/apps/moudules/personnel/services/PersonSelectService.class" \
+		"space/core/apps/sys/service/WordImportService.class" \
 		"space/apps/moudules/website/services/NavigationService.class" \
 		"space/apps/moudules/website/services/NavigationAPIService.class" \
 		"space/apps/moudules/website/services/WebsiteTemplateService.class" \
@@ -1284,9 +1285,10 @@ WHERE zjid=10149
 	verify_remote_file_contains "网站列表排序 JSP 已部署" "web/subassembly/app/pc/10017/10070/1001710070_run.jsp" "排序序号"
 	verify_remote_file_contains "网站录入排序 JSP 已部署" "web/subassembly/app/pc/10017/10071/1001710071_run.jsp" "排序序号"
 	verify_remote_file_contains "网站变动排序 JSP 已部署" "web/subassembly/app/pc/10017/10073/1001710073_run.jsp" "排序序号"
-	verify_remote_file_contains "文章发布编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10104/1002110104_run.jsp" "wordconverturl"
-	verify_remote_file_contains "文章变动编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10105/1002110105_run.jsp" "wordconverturl"
-	verify_remote_file_contains "文章修改编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10211/1002110211_run.jsp" "wordconverturl"
+	verify_remote_file_contains "文章发布编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10104/1002110104_run.jsp" "sys.convert_json"
+	verify_remote_file_contains "文章变动编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10105/1002110105_run.jsp" "sys.convert_json"
+	verify_remote_file_contains "文章修改编辑器 Word 转换入口已部署" "web/subassembly/app/pc/10021/10211/1002110211_run.jsp" "sys.convert_json"
+	verify_remote_file_contains "Word 转换 JSON 服务已部署" "WEB-INF/apps/sys/config/services/libreOffice.service.xml" "convert_json"
 	verify_remote_file_contains "图片上传限制已调整为15M" "apps/space/js/space.display.js" "文件大小不能超过15M,请重新选择"
 
 	verify_http "后台工作台可访问" "https://hxcs.feifan.net/apps/workbench/"

+ 80 - 0
src/space/core/apps/sys/service/WordImportService.java

@@ -0,0 +1,80 @@
+package space.core.apps.sys.service;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.jsoup.Jsoup;
+import org.jsoup.nodes.Document;
+import org.jsoup.select.Elements;
+
+import space.core.apps.common.BaseTransaction;
+import space.core.apps.sys.utils.LibreOfficeUtil;
+import space.core.apps.sys.utils.UploadFileUtil;
+import space.core.common.utils.FileUtil;
+import space.core.common.utils.GsonUtil;
+
+/**
+ * 将后台已上传的 Word 文档转换为可直接插入富文本编辑器的 HTML。
+ */
+public class WordImportService extends BaseTransaction {
+	private static final long serialVersionUID = 1L;
+	private static final String UPLOAD_PREFIX = "/web/upload/modules/";
+
+	public void convertJson() throws IOException {
+		Map<String, Object> result = new HashMap<String, Object>();
+		getResponse().setContentType("application/json;charset=UTF-8");
+
+		try {
+			String path = getParameter("path");
+			String lowerPath = path == null ? "" : path.toLowerCase();
+			if (path == null || !path.startsWith(UPLOAD_PREFIX)
+					|| path.contains("..") || !lowerPath.matches(".*\\.(doc|docx)$")) {
+				throw new IllegalArgumentException("无效的 Word 文件路径");
+			}
+
+			String webRoot = getWebRootPath();
+			File sourceFile = new File(webRoot + path);
+			if (!sourceFile.isFile()) {
+				throw new IllegalArgumentException("Word 文件不存在");
+			}
+
+			String extension = FileUtil.getFileExt(sourceFile.getName());
+			String targetRelativePath = UploadFileUtil.getUploadFilePath(extension);
+			File targetDir = new File(webRoot + targetRelativePath);
+			if (!targetDir.exists() && !targetDir.mkdirs()) {
+				throw new IOException("无法创建 Word 转换目录");
+			}
+
+			String htmlFileName = LibreOfficeUtil.convertHtml(sourceFile.getAbsolutePath(), targetDir.getAbsolutePath());
+			File htmlFile = new File(targetDir, htmlFileName);
+			String htmlContent = normalizeHtml(htmlFile, targetRelativePath);
+			FileUtil.writeFile(htmlFile.getAbsolutePath(), htmlContent);
+
+			result.put("code", "success");
+			result.put("msg", "转换成功");
+			result.put("htmlContent", htmlContent);
+			result.put("filePath", targetRelativePath + htmlFileName);
+		} catch (Exception e) {
+			result.put("code", "fail");
+			result.put("msg", "Word 转换失败,请确认文件未损坏且格式为 .doc 或 .docx");
+			e.printStackTrace();
+		}
+
+		getWriter().write(GsonUtil.toJson(result));
+	}
+
+	private String normalizeHtml(File htmlFile, String targetRelativePath) throws Exception {
+		Document document = Jsoup.parse(htmlFile, "UTF-8", "");
+		Elements images = document.select("img[src]");
+		for (org.jsoup.nodes.Element image : images) {
+			String src = image.attr("src");
+			if (src.length() > 0 && !src.startsWith("/") && !src.startsWith("data:")
+					&& !src.startsWith("http://") && !src.startsWith("https://")) {
+				image.attr("src", targetRelativePath + src);
+			}
+		}
+		return document.select("body").eq(0).html();
+	}
+}