使用OnlyOffice接入office在線編輯


搭建OnlyOffice

使用Docker搭建OnlyOffice服務

# 拉取包涵中文字符的鏡像
docker pull kenlk/onlyoffice
# 啟動容器
docker run -i -t -d -p 80:80 --restart=always kenlk/onlyoffice

需要做https設置及宿主機文件夾映射,請參考官網文檔:https://helpcenter.onlyoffice.com/installation/docs-community-install-docker.aspx

前端Vue接入Demo

  1. index.html引入api.js
<script type="text/javascript" src="http://onlyoffice服務地址/web-apps/apps/api/documents/api.js"></script>
  1. 構建加載參數
<template>
  <div id="monitorOffice"></div>
</template>
<script>
import { handleDocType } from '@/utils/constants'
export default {
  props: {
    option: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      doctype: ''
    }
  },
  mounted() {
    if (this.option.url) {
      this.setEditor(this.option)
    }
  },
  methods: {
    setEditor(option) {
      this.doctype = handleDocType(option.fileType)
      // office配置參數
      let config = {
        document: {
          fileType: option.fileType,
          key: option.key,
          title: option.title,
          permissions: {
            comment: false,
            download: false,
            modifyContentControl: true,
            modifyFilter: true,
            print: false,
            edit: option.isEdit//是否可以編輯: 只能查看,傳false
            // "fillForms": true,//是否可以填寫表格,如果將mode參數設置為edit,則填寫表單僅對文檔編輯器可用。 默認值與edit或review參數的值一致。
            // "review": true //跟蹤變化
          },
          url: option.url
        },

        documentType: this.doctype,
        editorConfig: {
          callbackUrl: option.editUrl,//"編輯word后保存時回調的地址,這個api需要自己寫了,將編輯后的文件通過這個api保存到自己想要的位置
          //語言設置
          lang: 'zh-CN',
          location: 'zh-CN',
          customization: {
            autosave: false,//是否自動保存
            chat: false,
            forcesave: true,// true 表示強制文件保存請求添加到回調處理程序
            feedback: {
              visible: false // 隱藏反饋按鈕
            },
            comments: false,
            help: false,
            hideRightMenu: true,//定義在第一次加載時是顯示還是隱藏右側菜單。 默認值為false
            logo: {
              image: 'https://file.iviewui.com/icon/viewlogo.png',
              imageEmbedded: 'https://file.iviewui.com/icon/viewlogo.png'
            },
            spellcheck: false//拼寫檢查
          }
        },
        width: '100%',
        height: '100%'
      }
      let docEditor = new DocsAPI.DocEditor('monitorOffice', config)
    }
  },
  watch: {
    option: {
      handler: function(n, o) {
        this.setEditor(n)
        this.doctype = handleDocType(n.fileType)
      },
      deep: true
    }
  }
}
</script>

export function handleDocType(fileType) {
  let docType = '';
  let fileTypesDoc = [
    'doc', 'docm', 'docx', 'dot', 'dotm', 'dotx', 'epub', 'fodt', 'htm', 'html', 'mht', 'odt', 'ott', 'pdf', 'rtf', 'txt', 'djvu', 'xps'
  ];
  let fileTypesCsv = [
    'csv', 'fods', 'ods', 'ots', 'xls', 'xlsm', 'xlsx', 'xlt', 'xltm', 'xltx'
  ];
  let fileTypesPPt = [
    'fodp', 'odp', 'otp', 'pot', 'potm', 'potx', 'pps', 'ppsm', 'ppsx', 'ppt', 'pptm', 'pptx'
  ];
  if (fileTypesDoc.includes(fileType)) {
    docType = 'text'
  }
  if (fileTypesCsv.includes(fileType)) {
    docType = 'spreadsheet'
  }
  if (fileTypesPPt.includes(fileType)) {
    docType = 'presentation'
  }
  return docType;
}

詳細參數,參考官網api: https://api.onlyoffice.com/editors/advanced

后端回調接口

  1. 保存回調直接保存阿里OSS中
@Slf4j
@RestController
@RequestMapping("/api")
@Api(tags = "文檔相關接口")
public class DocumentApiController {

    @Autowired
    private OssService aliOssService;

    /**
     * 編輯后回調--保存文件實體
     * <p>onlyoffice在編輯后關閉文件的時候,會回調該接口</p>
     *
     * @param callback
     */
    @PostMapping("/callback")
    public DocumentEditCallbackResponse saveDocumentFile(@RequestBody DocumentEditCallback callback, @RequestParam("fileName") String fileName) throws Exception {
        if (log.isInfoEnabled()) {
            log.info("### 編輯后回調, 回調信息:{}", callback);
        }
        log.info("文檔狀態:{}", callback.getStatus());
        // 需要保存時寫出文件
        if (callback.getStatus() == DocumentStatus.READY_FOR_SAVING.getCode() || callback.getStatus() == DocumentStatus.BEING_EDITED_STATE_SAVED.getCode()) {
            if (log.isDebugEnabled()) {
                log.debug("@@@ 開始保存文件。。。");
            }
            String url = null;
            if (StringUtils.hasText(fileName)) {

                if (fileName.contains("http")) {
                    fileName = URLUtil.getPath(fileName);
                    if (StringUtils.hasText(fileName)) {
                        // 去除"/"
                        fileName = fileName.substring(1);
                    }
                }
                url = aliOssService.uploadFile(callback.getUrl(), fileName);
                log.info("url==={}", url);
            }
            if (log.isDebugEnabled()) {
                log.debug("@@@ 保存文件結束!");
            }
            if (StringUtils.isEmpty(url)) {
                return DocumentEditCallbackResponse.failue();
            }
        }
        return DocumentEditCallbackResponse.success();
    }

}



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM