zhuguifei
2025-04-28 442928123f63ee497d766f9a7a14f0a6ee067e25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<template>
    <div class="report-onlyoffice">
        <div id="placeholder"></div>
    </div>
</template>
 
<script>
// import {
//     createOfficeFile,
//     editOfficeFile,
//     previewOfficeFile
// } from '_r/onlyoffice.js'
import { getAction, postAction } from '@/api/manage'
export default {
    name: 'OnlyOffice',
    data() {
        return {
            docEditor: null, //  文档编辑器
            platform: 'desktop' //  查看平台
        }
    },
    computed: {
        // 文件信息,来自于路由参数query
        fileInfo() {
            return this.$route.query
        }
    },
    created() {
        this.judgePlatform()
    },
    mounted() {
        this.$nextTick(() => {
            switch (this.fileInfo.ot) {
                // 添加
                case 'add':
                    this.initOnlyoffice()
                    break
                // 编辑
                case 'edit':
                    this.editDoc()
                    break
                // 详情
                case 'detail':
                    this.showDocDetail()
                    break
            }
        })
    },
    destroyed() {
        this.docEditor.destroyEditor()
    },
    methods: {
        /**
         * 初始化 onlyoffice
         */
        initOnlyoffice() {
            let data = {
                userFileId: this.fileInfo.userFileId,
                fileId: this.fileInfo.fileId,
                fileName: this.fileInfo.fileName,
                filePath: this.fileInfo.filePath,
                fileCategory: this.fileInfo.fileCategory,
                type: this.fileInfo.type,
                memo: this.fileInfo.memo
            }
            postAction('/office/createofficefile',data).then((res) => {
                if (res.code === 200) {
                    let config = {
                        ...res.result.file,
                        type: this.platform
                    }
                    // config.editorConfig.callbackUrl = config.editorConfig.callbackUrl.replace('/IndexServlet', ONLYOFFICE_BASE_URL + '/IndexServlet')
                    this.initDocEditor(res.result.docserviceApiUrl, config)
                }
            })
        },
        /**
         * 初始化文档编辑器
         * @param {string} docserviceApiUrl 文档服务API url
         * @param {object} config 文件相关配置信息
         */
        initDocEditor(docserviceApiUrl, config) {
            this.loadOnlyOfficeAPI(docserviceApiUrl).then(() => {
                /* global DocsAPI */
                this.docEditor = new DocsAPI.DocEditor('placeholder', {
                    ...config,
                    editorConfig: {
                        ...config.editorConfig,
                        lang: 'zh', //  语言设置为中文
                        customization: {
                            ...config.editorConfig.customization,
                            zoom: 100 //  缩放比例为 100
                        }
                    }
                })
            })
        },
        /**
         * 判断当前平台
         */
        judgePlatform() {
            this.platform = 'desktop' // 浏览平台
            if (
                /AppleWebKit.*Mobile/i.test(navigator.userAgent) ||
                /MIDP|SymbianOS|NOKIA|SAMSUNG|LG|NEC|TCL|Alcatel|BIRD|DBTEL|Dopod|PHILIPS|HAIER|LENOVO|MOT-|Nokia|SonyEricsson|SIE-|Amoi|ZTE/.test(
                    navigator.userAgent
                )
            ) {
                if (window.location.href.indexOf('?mobile') < 0) {
                    try {
                        if (
                            /Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)
                        ) {
                            this.platform = 'mobile'
                        }
                    } catch (e) {
                        console.log(e)
                    }
                }
            }
        },
        /**
         * 展示文档详情
         */
        showDocDetail() {
            let data = {
                userFileId: this.fileInfo.userFileId,
                previewUrl: this.fileInfo.fileUrl
            }
            postAction('/office/previewofficefile',data).then((res) => {
                
                if (res.code === 200) {
                    let config = {
                        ...res.result.file,
                        type: this.platform
                    }
                    config.document.permissions.edit = false //  预览模式下编辑权限为 false
                    this.initDocEditor(res.result.docserviceApiUrl, config)
                }
            })
        },
        /**
         * 编辑文档
         */
        editDoc() {
            let data = {
                userFileId: this.fileInfo.userFileId,
                previewUrl: this.fileInfo.fileUrl
            }
            postAction('/office/editofficefile',data).then((res) => {
                console.log("Office::RES::", res);
                if (res.code === 200) {
                    let config = {
                        ...res.result.file,
                        type: this.platform
                    }
                    this.initDocEditor(res.result.docserviceApiUrl, config)
                }
            })
        },
        /**
         * 加载 onlyoffice api
         * @return {Promise} 返回 api 加载状态
         */
        loadOnlyOfficeAPI(src) {
            return new Promise((resolve, reject) => {
                const script = document.createElement('script')
                script.type = 'text/javascript'
                script.src = src
                document.body.appendChild(script)
                script.onload = () => {
                    resolve()
                }
                script.onerror = () => {
                    reject()
                }
            })
        }
    }
}
</script>
 
<style lang="less" scoped>
.report-onlyoffice {
  height: 100%;
  width: 100%;
}
#app .main-content {
  height: 100%;
  width: 100%;
}
</style>