吉安感知网项目-前端
shuishen
5 days ago 6e88705bd5b443a259b24c17c8a299765d059d96
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
<template>
    <div class="document-preview-page" v-loading="loading">
        <DocumentEditor
            v-if="config"
            id="inspectionReportOfficeEditor"
            :documentServerUrl="documentServerUrl"
            :config="config"
            height="100%"
            width="100%"
            :events_onAppReady="onAppReady"
            :events_onDocumentReady="onDocumentReady"
            :events_onDocumentStateChange="onDocumentStateChange"
            :events_onWarning="onWarning"
            :events_onError="onError"
            :onLoadComponentError="onLoadComponentError"
        />
        <el-empty v-else-if="!loading" :description="errorMessage || '缺少文档配置'" />
    </div>
</template>
 
<script setup>
import { computed, ref, watch } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { DocumentEditor } from '@onlyoffice/document-editor-vue'
import { getOnlyOfficeConfigApi } from '@/api/documentPreview'
 
const route = useRoute()
const documentServerUrl = import.meta.env.VITE_APP_ONLYOFFICE_DOCUMENT_SERVER_URL
const attachId = computed(() => String(route.query.attachId || ''))
const mode = computed(() => String(route.query.mode || ''))
 
const config = ref(null)
const loading = ref(false)
const errorMessage = ref('')
 
async function getOnlyOfficeConfig() {
    if (!attachId.value) {
        config.value = null
        errorMessage.value = '缺少附件ID'
        return
    }
 
    loading.value = true
    errorMessage.value = ''
 
    try {
        const res = await getOnlyOfficeConfigApi({
            attachId: attachId.value,
            mode: mode.value,
        })
        const data = res?.data?.data || res?.data
        if (!data?.document || !data?.editorConfig) {
            throw new Error('OnlyOffice配置不完整')
        }
        config.value = data
    } catch (error) {
        console.error('获取OnlyOffice配置失败:', error)
        config.value = null
        errorMessage.value = '获取文档配置失败'
        ElMessage.error(error?.message || '获取文档配置失败')
    } finally {
        loading.value = false
    }
}
 
watch(attachId, getOnlyOfficeConfig, {
    immediate: true,
})
 
function onAppReady(event) {
    console.log('OnlyOffice app ready:', event)
}
 
function onDocumentReady(event) {
    console.log('OnlyOffice document ready:', event)
}
 
function onDocumentStateChange(event) {
    console.log('OnlyOffice document state change:', event)
}
 
function onWarning(event) {
    console.warn('OnlyOffice warning:', event)
}
 
function onError(event) {
    console.error('OnlyOffice error:', event)
}
 
function onLoadComponentError(errorCode, errorDescription) {
    console.error('OnlyOffice load error:', errorCode, errorDescription)
}
</script>
 
<style scoped lang="scss">
.document-preview-page {
    height: 100vh;
    min-height: 0;
    width: 100vw;
    overflow: hidden;
}
</style>