1、在線文檔引用
由后臺人員搭建服務(wù),引用api
2、封裝組件
<!--
* @Descripttion: 在線編輯插件
* @version: 0.0.1
* @Author: PengShuai
* @Date: 2022-12-23 15:43:07
* @LastEditors: PengShuai
* @LastEditTime: 2023-02-22 10:17:11
-->
<template>
<div class="BaseOffice">
<div id="psOffice"></div>
</div>
</template>
<script>
export default {
name: 'BaseOffice',
data() {
return {
// 文檔類型
doctype: '',
// 編輯狀態(tài)
docEditor: null,
}
},
props: {
// 傳入配置
option: {
type: Object,
default: () => {
return {}
},
},
},
beforeDestroy() {
if (this.docEditor !== null) {
this.docEditor.destroyEditor()
this.docEditor = null
}
},
watch: {
option: {
handler: function (n) {
this.setEditor(n)
this.doctype = this.getFileType(n.fileType)
},
deep: true,
},
},
mounted() {
if (this.option.url) {
this.setEditor(this.option)
}
},
methods: {
setEditor(option) {
if (this.docEditor !== null) {
this.docEditor.destroyEditor()
this.docEditor = null
}
this.doctype = this.getFileType(option.fileType)
let config = {
document: {
fileType: option.fileType,
key: option.key || '',
title: option.title,
permissions: {
edit: option.isEdit,
print: option.isPrint,
modifyContentControl: true,
download: true,
},
url: option.url,
},
documentType: this.doctype,
editorConfig: {
key: option.key || '',
customization: {
autosave: false, //自動(dòng)保存
forcesave: false,
chat: false,
comments: false,
},
callbackUrl: option.editUrl, //回調(diào)的地址
lang: option.lang, //語言設(shè)置
user: {
id: option.user.id,
name: option.user.name,
},
mode: option.model,
},
width: '100%',
height: '100%',
token: option.token,
}
this.docEditor = new DocsAPI.DocEditor('psOffice', config)
},
// 獲取文件類型
getFileType(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
},
},
}
</script>
<style lang="less" scoped>
.BaseOffice {
width: 100%;
height: 100%;
}
</style>
3、引用組件
import BaseOffice from '@/components/BaseOffice'
components: { BaseOffice},
<base-office ref="BaseOffice" :option="officeOption"></base-office>
4、配置
_this.officeOption = {
url: '192.168.**.**/file/demo.doc', // 獲取服務(wù)器文件
isEdit: true, // 是否可編輯
fileType: doc, // 文件類型
title: '測試文檔', // 文件名稱
lang: 'zh-CN', // 國際化
isPrint: true, // 是否打印
model: 'edit', // 模式
key: _this.BaseUuid(), // 唯一鍵
user: { id: userCode, name: userName }, // 用戶信息
editUrl: '192.168.**.**/file/demoSave', // 編輯接口 保存回調(diào)
token: sessionStorage.getItem('token'),
}
5、生成唯一鍵
參考 http://www.rzrgm.cn/psmart/p/16371116.html
6、官網(wǎng)
https://www.onlyoffice.com/zh/
7、api
https://api.onlyoffice.com/
https://api.onlyoffice.com/editors/basic
浙公網(wǎng)安備 33010602011771號