onlyoffice历史版本功能实现,版本恢复功能,编辑器功能实现 springboot+vue2

文章目录

  • oonlyoffice历史版本功能实现 (编辑器功能实现)springboot+vue2
  • 前提 需要注意把这个 (改成自己服务器的ip或者域名) 改成 自己服务器的域名或者地址
  • 1. onloyoffice 服务器部署 搜索其他文章
  • 2. 前段代码 vue 2
    • 2.1 需要注意把这个 (改成自己服务器的ip或者域名) 改成 自己服务器的域名或者地址
    • 2.2. openedit getHistoryData 这两个 是调用后端的 接口 改成自己项目的写法 都是 post 请求
    • 2.3 下面是整个页面代码 需要 别的页面进入下面这个页面 入参是 文件id
    • 举例
    • 进入editer.vue 页面的 页面代码 row.id 是文件id 也就是 onloyoffice 文件id
      • 文件名字 editer.vue
  • 3. 后端java 代码
    • 3.1 controller 代码
    • 3.2 service 代码
    • 3.3 serviceImpl 代码
    • 3.3公共方法代码
  • 4.效果图
  • 5.可以参考此文章优化代码

oonlyoffice历史版本功能实现 (编辑器功能实现)springboot+vue2

前提 需要注意把这个 (改成自己服务器的ip或者域名) 改成 自己服务器的域名或者地址

1. onloyoffice 服务器部署 搜索其他文章

2. 前段代码 vue 2

2.1 需要注意把这个 (改成自己服务器的ip或者域名) 改成 自己服务器的域名或者地址

2.2. openedit getHistoryData 这两个 是调用后端的 接口 改成自己项目的写法 都是 post 请求

2.3 下面是整个页面代码 需要 别的页面进入下面这个页面 入参是 文件id

举例

进入editer.vue 页面的 页面代码 row.id 是文件id 也就是 onloyoffice 文件id

//row.id 是文件id 也就是 onloyoffice 文件id
//  /only/editer/   这是页面路由的地址需要在路由里面配置
//页面
openFile(row) {window.open('#/only/editer/' + row.id,'_blank');},

文件名字 editer.vue

<template><div><div id="onlyoffice-container" ref="editorContainer"></div></div>
</template><script>export default {name: 'OnlyOfficeEditor',props: {isEdit: {type: Boolean,default: true}},data() {return {docEditor: null,currentVersion: null,fileToken: null,historyData: null,historys: [],historyList: []};},mounted() {this.loadScript().then(() => {this.initEditor();});},methods: {async loadScript() {return new Promise((resolve, reject) => {const scriptId = "onlyoffice-api-script";if (!document.getElementById(scriptId)) {const script = document.createElement('script');script.id = scriptId;script.src = "https://(改成自己服务器的ip或者域名)/ds-vpath/web-apps/apps/api/documents/api.js"; // 替换为你的 ONLYOFFICE 服务器地址script.type = "text/javascript";script.onload = resolve;script.onerror = reject;document.head.appendChild(script);} else {resolve();}});},initEditor() {if (this.docEditor) {this.docEditor.destroyEditor();this.docEditor = null;}openedit({"fileId": this.$route.params.id}).then(res  => {if (res.code  === 200) {//这是主编辑器获取的数据  config let config = res.data.openedit;//处理后端返回的历史版本数据const historyList = res.data.historyList;const fileToken = res.data.fileToken;const currentVersion  = historyList[0].version; // 假设最新版本为第一个元素// 添加版本历史事件处理config.events  = {//这里拿到的是 左侧版本的 数据 比如 版本1 版本2onRequestHistory: () => {this.docEditor.refreshHistory({currentVersion: currentVersion,token: fileToken,history: historyList});},//这里是 左侧关闭历史记录 按钮调用这个方法onRequestHistoryClose: () => {document.location.reload();},//这是左侧 点击版本  对应的版本内容onRequestHistoryData: (event) => {const version = event.datagetHistoryData({"fileId": this.$route.params.id,"version": version}).then(res  => {const historyData = res.data.historyData;this.docEditor.setHistoryData(historyData);}).catch(err => {console.log(err);});},//版本恢复功能onRequestRestore: (event) => {//待实现}};this.docEditor  = new window.DocsAPI.DocEditor(this.$refs.editorContainer.id,  config);}}).catch(err => {console.log(err);});}},beforeDestroy() {if (this.docEditor)  {this.docEditor.destroyEditor();this.docEditor  = null;}if (this.DocsAPIInterval) {clearInterval(this.DocsAPIInterval);}},beforeRouteLeave(to, from, next) {if (this.docEditor)  {this.docEditor.destroyEditor();this.docEditor  = null;}if (this.DocsAPIInterval) {clearInterval(this.DocsAPIInterval);}next();}
};
</script><style>
iframe {border: none;width: 100%;height: 100vh;
}
</style>

3. 后端java 代码

3.1 controller 代码

    @PostMapping("/openedit")public RestResultDTO openedit(HttpServletRequest request, @RequestBody OnlyofficeRequestDTO params) {try {if (CommonFunctions.isEmpty(params.getFileId())) {throw new BusinessServiceException("非空校验失败");}Map<String, Object> resultMap  = onlyofficeService.openedit(params);return RestResultDTO.success(resultMap);} catch (Exception e) {LOGGER.error("openedit 方法发生异常: ", e);return RestResultDTO.error(ResponseCodeDTO.INTERNAL_SERVER_ERROR, e.getMessage());}}
@PostMapping("/getHistoryData")public RestResultDTO getHistoryData(HttpServletRequest request, @RequestBody OnlyofficeRequestDTO params) {try {if (CommonFunctions.isEmpty(params.getFileId()) || CommonFunctions.isEmpty(params.getVersion())) {throw new BusinessServiceException("非空校验失败");}Map<String, Object> resultMap  = onlyofficeService.getHistoryData(params);return RestResultDTO.success(resultMap);} catch (Exception e) {LOGGER.error("openedit 方法发生异常: ", e);return RestResultDTO.error(ResponseCodeDTO.INTERNAL_SERVER_ERROR, e.getMessage());}}

3.2 service 代码

    Map<String, Object> openedit(OnlyofficeRequestDTO params);
 //获取文件的初始化配置Map<String, Object> getHistoryData(OnlyofficeRequestDTO params);

3.3 serviceImpl 代码

       @Overridepublic Map<String, Object> openedit(OnlyofficeRequestDTO params) {String fileId = params.getFileId();String permissionType = "";JSONObject onlyOfficeConfig = null;try {String result = HttpUtils.executeRequestOnlyoffice(HttpUtils.O_SHAREFILE_URL + fileId + "/openedit", null, null, "UTF-8", "get");JSONObject resJsonObject = JSONObject.parseObject(result);if (CollectionUtils.isEmpty(resJsonObject)) {throw new BusinessServiceException("获取配置文件异常");} else if (resJsonObject.getInteger("statusCode") == 200) {onlyOfficeConfig = resJsonObject.getJSONObject("response");} else if (resJsonObject.getInteger("statusCode") == 500) {throw new BusinessServiceException(resJsonObject.getJSONObject("error").getString("message"));} else {throw new BusinessServiceException("获取配置文件异常");}//只读if ("4".equals(permissionType)) {onlyOfficeConfig.getJSONObject("editorConfig").put("mode", "view");}} catch (Exception e) {LOGGER.error("解析JSON响应失败", e);throw new BusinessServiceException("", e);}JSONArray responseArray = null;try {String result = HttpUtils.executeRequestOnlyoffice(HttpUtils.O_FILES_URL  + "/edit-history?fileId=" + fileId, null, null, "UTF-8", "get");// 尝试解析为 JSONArrayJSONArray resJsonArray = JSONArray.parseArray(result);if (CollectionUtils.isEmpty(resJsonArray))  {throw new BusinessServiceException("获取配置文件异常");}responseArray = resJsonArray;} catch (Exception e) {LOGGER.error(" 解析JSON响应失败", e);throw new BusinessServiceException("", e);}// 使用 Map 封装多个 JSON 对象Map<String, Object> resultMap = new HashMap<>();resultMap.put("openedit", onlyOfficeConfig);resultMap.put("historyList", responseArray);try {resultMap.put("fileToken",HttpUtils.renovateAuthorizationToken());} catch (Exception e) {LOGGER.error(" 获取token失败", e);throw new BusinessServiceException("", e);}return resultMap;}
    @Overridepublic Map<String, Object> getHistoryData(OnlyofficeRequestDTO params) {String fileId = params.getFileId();String version = params.getVersion();JSONObject responseArray = null;try {String result = HttpUtils.executeRequestOnlyoffice(HttpUtils.O_FILES_URL + "/edit-diff-url?fileId=" + fileId + "&version=" + version, null, null, "UTF-8", "get");responseArray = JSONObject.parseObject(result);if (CollectionUtils.isEmpty(responseArray))  {throw new BusinessServiceException("获取配置文件异常");}} catch (Exception e) {LOGGER.error("解析JSON响应失败", e);throw new BusinessServiceException("", e);}// 使用 Map 封装多个 JSON 对象Map<String, Object> resultMap = new HashMap<>();resultMap.put("historyData", responseArray);return resultMap;}

3.3公共方法代码

  /*** 根据请求类型执行POST或PUT请求*/public static String executeRequestOnlyoffice(String url, Map<String, Object> params, Map<String, String> headers, String encoding, String requestType) throws Exception {Map<String, String> headersCover = new HashMap<>();String authorizationToken = getAuthorizationToken();//覆盖默认请求头headersCover.put("Authorization", authorizationToken);headersCover.put("Accept", "application/json");headersCover.put("Content-Type", "application/json");if (headers != null) {for (Map.Entry<String, String> entry : headers.entrySet()) {headersCover.put(entry.getKey(), entry.getValue());}}switch (requestType.toLowerCase()) {case "get":return executeGetRequestCommon(url, params, headersCover, encoding);case "post":return executePostRequestCommon(url, params, headersCover, encoding);case "put":return executePutRequestCommon(url, params, headersCover, encoding);case "delete":return executeDeleteRequestCommon(url, params, headersCover, encoding);default:throw new IllegalArgumentException("Unsupported request type: " + requestType);}}
 /*** 获取授权Token*/public static synchronized void refreshAuthorizationToken() throws Exception {Map<String, Object> params = new HashMap<>();params.put("userName", "");//输入onloyoffice 登录用户名params.put("password", "");//输入onloyoffice登录密码Map<String, String> headers = new HashMap<>();headers.put("Accept", "application/json");headers.put("Content-Type", "application/json");String result = executePostRequestCommon(HttpUtils.O_AUTH_URL, params, headers, "UTF-8");JSONObject jsonObject = JSONObject.parseObject(result);HttpUtils.authorizationToken = "Bearer " + jsonObject.getJSONObject("response").getString("token");}// 获取Token的方法@PostConstructpublic static String getAuthorizationToken() throws Exception {if (CommonFunctions.isEmpty(HttpUtils.authorizationToken)) {refreshAuthorizationToken();}return HttpUtils.authorizationToken;}
    /*** 执行GET通用请求*/public static String executeGetRequestCommon(String url, Map<String, Object> params, Map<String, String> headers, String encoding) throws Exception {// 创建一个带有默认配置的HttpClient,并设置超时时间RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000) // 连接超时时间.setSocketTimeout(10000) // 读取超时时间.build();try (CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(requestConfig).build()) {// 构建带有查询参数的URLif (params != null && !params.isEmpty()) {StringBuilder queryString = new StringBuilder(url);if (!url.contains("?")) {queryString.append("?");} else {queryString.append("&");}for (String key : params.keySet()) {queryString.append(key).append("=").append(params.get(key)).append("&");}// 去掉最后一个多余的&if (queryString.toString().endsWith("&")) {queryString.setLength(queryString.length() - 1);}url = queryString.toString();}HttpGet httpGet = new HttpGet(url);// 添加自定义头信息if (headers != null) {for (Map.Entry<String, String> entry : headers.entrySet()) {httpGet.addHeader(entry.getKey(), entry.getValue());}}// 执行请求并获取响应try (CloseableHttpResponse response = httpClient.execute(httpGet)) {HttpEntity entity = response.getEntity();return entity != null ? EntityUtils.toString(entity, encoding != null ? encoding : "UTF-8") : null;}} catch (IOException e) {logger.error("Error executing GET request to URL: {}. Exception: {}", url, e.getMessage(), e);throw e;}}
public static String O_FILES_URL = "https://(自己服务器ip或者域名)/Products/Files/Services/WCFService/service.svc"public static String O_SHAREFILE_URL = "https://(自己服务器ip或者域名)/api/2.0/files/file/"
/*** 获取授权Token* @return 授权Token字符串* @throws Exception 如果请求或解析失败*/public static String renovateAuthorizationToken() throws Exception {// 构造请求参数Map<String, Object> params = new HashMap<>();params.put("userName", "");//输入onloyoffice 登录用户名params.put("password", "");//输入onloyoffice登录密码// 构造请求头Map<String, String> headers = new HashMap<>();headers.put("Accept", "application/json");headers.put("Content-Type", "application/json");// 执行POST请求并获取结果String result = executePostRequestCommon(HttpUtils.O_AUTH_URL, params, headers, "UTF-8");// 解析JSON结果JSONObject jsonObject = JSONObject.parseObject(result);return jsonObject.getJSONObject("response").getString("token");}

4.效果图

在这里插入图片描述
在这里插入图片描述

5.可以参考此文章优化代码

https://www.cnblogs.com/gamepen/p/17849005.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/78025.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

解决ubuntu server修改为中文后乱码问题(改回英文)

操作步骤 1.安装英文语言包 sudo apt-get install language-pack-en2.编辑/etc/default/locale文件 sudo vim /etc/default/locale修改为以下内容&#xff1a; LANG"en_US.UTF-8" LANGUAGE"en_US:en" LC_ALL"en_US.UTF-8"3.应用配置 sudo l…

安卓的Launcher 在哪个环节进行启动

安卓Launcher在系统启动过程中的关键环节启动&#xff0c;具体如下&#xff1a; 内核启动&#xff1a;安卓设备开机后&#xff0c;首先由引导加载程序启动Linux内核。内核负责初始化硬件设备、建立内存管理机制、启动系统进程等基础工作&#xff0c;为整个系统的运行提供底层支…

数据通信学习笔记之OSPF其他内容2

OSPF 与 BFD 联动 网络上的链路故障或拓扑变化都会导致设备重新进行路由计算&#xff0c;所以缩短路由协议的收敛时间对于提高网络的性能是非常重要的。 OSPF 与 BFD 联动就是将 BFD 和 OSPF 关联起来&#xff0c;一旦与邻居之间的链路出现故障&#xff0c;BFD 对完品以&…

数据库原理及应用mysql版陈业斌实验四

&#x1f3dd;️专栏&#xff1a;Mysql_猫咪-9527的博客-CSDN博客 &#x1f305;主页&#xff1a;猫咪-9527-CSDN博客 “欲穷千里目&#xff0c;更上一层楼。会当凌绝顶&#xff0c;一览众山小。” 目录 实验四索引与视图 1.实验数据如下 student 表&#xff08;学生表&…

[密码学实战]密评考试训练系统v1.0程序及密评参考题库(获取路径在文末)

[密码学实战]密评考试训练系统v1.0程序及密评参考题库 引言:密评考试的重要性与挑战 商用密码应用安全性评估(简称"密评") 作为我国密码领域的重要认证体系,已成为信息安全从业者的必备技能。根据国家密码管理局最新数据,截至2024年6月,全国仅有3000余人持有…

蓝桥杯练习题2

动态规划 动态规划三大题型&#xff1a;计数问题、最值问题、存在性问题&#xff1b; 【最小权值】-- 最值问题 【题目分析】 import java.util.Arrays; Arrays类中的一个方法&#xff1a;Arrays.fill(int[] m,int n) //给 int 类型(或者char类型/Long类型...)的数组全部空间…

【集群IP管理分配技术_DHCP】二、DHCP核心功能与技术实现

一、智能 IP 地址分配功能与技术实现​ 1.1 功能概述​ 智能 IP 地址分配是 DHCP 中间件的核心功能之一&#xff0c;它打破了传统 DHCP 固定的分配模式&#xff0c;能够根据网络的实时状态、客户端类型、接入位置等多种因素&#xff0c;动态且合理地分配 IP 地址。例如&#…

实现AWS Lambda函数安全地请求企业内部API返回数据

需要编写一个Lambda函数在AWS云上运行,它需要访问企业内部的API获取JSON格式的数据,企业有网关和防火墙,API有公司的okta身份认证,通过公司的域账号来授权访问,现在需要创建一个专用的域账号,让Lambda函数访问Secret Manager,来获取账号密码,然后通过配置访问公司内部A…

子网划分的学习

定长子网划分&#xff08;Fixed-length Subnetting&#xff09; 也叫做固定长度子网划分&#xff0c;是指在一个IP网络中&#xff0c;把网络划分成若干个大小相等的子网&#xff0c;每个子网的子网掩码长度是一样的。 一、定长子网划分的背景 在早期的IP地址分配中&#xff0…

3.QT-信号和槽|自定义槽函数|自定义信号}自定义的语法}带参数的信号和槽(C++)

信号和槽 Linux信号 Signal 系统内部的通知机制. 进程间通信的方式. 信号源&#xff1a;谁发的信号.信号的类型&#xff1a;哪种类别的信号信号的处理方式&#xff1a;注册信号处理函数&#xff0c;在信号被触发的时候自动调用执行. Qt中的信号和Linux中的信号&#xff0c;虽…

如何在 Element UI 中优雅地使用 `this.$loading` 显示和隐藏加载动画

如何在 Element UI 中优雅地使用 this.$loading 显示和隐藏加载动画 在现代 Web 应用开发中&#xff0c;用户体验至关重要。当执行耗时操作&#xff08;如网络请求或数据处理&#xff09;时&#xff0c;显示一个友好的加载动画可以让用户知道系统正在工作&#xff0c;而不是卡…

动态加载内容时selenium如何操作?

当处理动态加载的内容时&#xff0c;Selenium 是一个非常强大的工具&#xff0c;因为它可以模拟真实用户的浏览器行为&#xff0c;等待页面元素加载完成后再进行操作。以下是使用 Selenium 获取动态加载内容的详细步骤和代码示例。 一、安装 Selenium 和 ChromeDriver &#…

力扣第446场周赛

有事没赶上, 赛后模拟了一下, 分享一下我的解题思路和做题感受 1.执行指令后的得分 题目链接如下&#xff1a;力扣 给你两个数组&#xff1a;instructions 和 values&#xff0c;数组的长度均为 n。 你需要根据以下规则模拟一个过程&#xff1a; 从下标 i 0 的第一个指令开…

三维点拟合平面ransac c++

理论 平面的一般定义 在三维空间中&#xff0c;一个平面可以由两个要素唯一确定&#xff1a; 法向量 n(a,b,c)&#xff1a;垂直于平面的方向 平面上一点 平面上任意一点 p(x,y,z) 满足&#xff1a; ( p − p 0 ) ∗ n 0 (p - p0) * n 0 (p−p0)∗n0 即 a ( x − x 0 ) …

基于LSTM-AutoEncoder的心电信号时间序列数据异常检测(PyTorch版)

心电信号&#xff08;ECG&#xff09;的异常检测对心血管疾病早期预警至关重要&#xff0c;但传统方法面临时序依赖建模不足与噪声敏感等问题。本文使用一种基于LSTM-AutoEncoder的深度时序异常检测框架&#xff0c;通过编码器-解码器结构捕捉心电信号的长期时空依赖特征&#…

Docker 部署 PostgreSQL 数据库

Docker 部署 PostgreSQL 数据库 基于 Docker 部署 PostgreSQL 数据库一、拉取 PostgreSQL 镜像二、运行 PostgreSQL 容器三、运行命令参数详解四、查看容器运行状态 基于 Docker 部署 PostgreSQL 数据库 一、拉取 PostgreSQL 镜像 首先&#xff0c;确保你的 Docker 环境已正确…

MySQL性能调优(四):MySQL的执行原理(MYSQL的查询成本)

文章目录 MySQL性能调优数据库设计优化查询优化配置参数调整硬件优化 1.MySQL的执行原理-21.1.MySQL的查询成本1.1.1.什么是成本1.1.2.单表查询的成本1.1.2.1.基于成本的优化步骤实战1. 根据搜索条件&#xff0c;找出所有可能使用的索引2. 计算全表扫描的代价3. 计算使用不同索…

用 Go 优雅地清理 HTML 并抵御 XSS——Bluemonday

1、背景与动机 只要你的服务接收并回显用户生成内容&#xff08;UGC&#xff09;——论坛帖子、评论、富文本邮件正文、Markdown 等——就必须考虑 XSS&#xff08;Cross‑Site Scripting&#xff09;攻击风险。浏览器在解析 HTML 时会执行脚本&#xff1b;如果不做清理&#…

Redis SCAN 命令的详细介绍

Redis SCAN 命令的详细介绍 以下是 Redis SCAN​ 命令的详细介绍&#xff0c;结合其核心特性、使用场景及底层原理进行综合说明&#xff1a; 工作原理图 &#xff1a; ​ 一、核心特性 非阻塞式迭代 通过游标&#xff08;Cursor&#xff09; 分批次遍历键&#xff0c;避免一次…

SpringBoot3集成MyBatis-Plus(解决Boot2升级Boot3)

总结&#xff1a;目前升级仅发现依赖有变更&#xff0c;其他目前未发现&#xff0c;如有发现&#xff0c;后续会继续更新 由于项目架构提升&#xff0c;以前开发的很多公共的组件&#xff0c;以及配置都需要升级&#xff0c;因此记录需要更改的配置&#xff08;记录时间&#…