1
jhzh
2026-01-14 fa821ce7a7755dd0e13897cefc57071d4596431b
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
193
194
195
196
197
<template>
  <div class="pdf-viewer-modern">
    <div v-if="loading" class="loading-wrapper">
      <div class="loading-content">
        <div class="spinner"></div>
        <p class="loading-text">正在加载PDF...</p>
      </div>
    </div>
    <div v-else-if="error" class="error-wrapper">
      <div class="error-content">
        <div class="error-icon">⚠️</div>
        <p class="error-text">{{ error }}</p>
        <button @click="loadPdf" class="retry-button">重新加载</button>
      </div>
    </div>
    <div v-else class="viewer-container">
      <iframe
        :src="pdfSrc"
        class="pdf-iframe"
        frameborder="0"
        @load="onPdfLoad"
        @error="onPdfError"
      ></iframe>
    </div>
  </div>
</template>
 
<script>
export default {
  name: 'PdfViewerModern',
  props: {
    pdfUrl: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      loading: true,
      error: null,
      pdfSrc: ''
    }
  },
  mounted() {
    this.loadPdf()
  },
  watch: {
    pdfUrl() {
      this.loadPdf()
    }
  },
  computed: {
    finalPdfUrl() {
      if (!this.pdfUrl) return ''
 
      let url = this.pdfUrl
 
      // 如果URL不包含http,需要添加基础URL
      if (!url.startsWith('http')) {
        const APIUrl = require('@/axios/api.url').default
        url = APIUrl.baseURL + (url.startsWith('/') ? url : '/' + url)
      }
 
      // 添加token
      const token = typeof window !== 'undefined' && window.localStorage ? window.localStorage.getItem('USERTOKEN') || '' : ''
      url = url + (url.indexOf('?') > -1 ? '&' : '?') + 'token=' + encodeURIComponent(token)
      
      // 保留工具栏(包含缩放工具),但隐藏导航面板
      url = url + '#toolbar=1&navpanes=0'
 
      return url
    }
  },
  methods: {
    loadPdf() {
      if (!this.pdfUrl) {
        this.error = 'PDF地址为空'
        this.loading = false
        return
      }
 
      this.loading = true
      this.error = null
 
      // 设置iframe的src,浏览器会自动加载
      this.pdfSrc = this.finalPdfUrl
 
      // 延迟隐藏loading,给iframe一些加载时间
      setTimeout(() => {
        this.loading = false
      }, 500)
    },
    onPdfLoad() {
      this.loading = false
      this.error = null
    },
    onPdfError() {
      this.loading = false
      this.error = 'PDF加载失败,请检查文件是否存在'
    }
  }
}
</script>
 
<style scoped>
.pdf-viewer-modern {
  width: 100%;
  height: 100%;
  max-height: calc(100vh - 400px);
  background: #f5f5f5;
  display: flex;
  flex-direction: column;
}
 
.loading-wrapper,
.error-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f8f9fa;
}
 
.loading-content,
.error-content {
  text-align: center;
  padding: 40px;
}
 
.spinner {
  width: 50px;
  height: 50px;
  margin: 0 auto 20px;
  border: 4px solid rgba(102, 126, 234, 0.1);
  border-top-color: #667eea;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
 
@keyframes spin {
  to { transform: rotate(360deg); }
}
 
.loading-text {
  color: #667eea;
  font-size: 16px;
  font-weight: 500;
}
 
.error-icon {
  font-size: 48px;
  margin-bottom: 16px;
}
 
.error-text {
  color: #e74c3c;
  font-size: 16px;
  margin-bottom: 20px;
}
 
.retry-button {
  padding: 12px 24px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: #fff;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 4px 12px rgba(102, 126, 234, 0.3);
}
 
.retry-button:active {
  transform: scale(0.95);
}
 
.viewer-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #f5f5f5;
  overflow: hidden;
  position: relative;
  max-height: calc(100vh - 200px);
    height: calc(100vh - 200px);
}
 
.pdf-iframe {
  width: 100%;
  height: 100%;
  border: none;
  background: #fff;
  min-height: 0;
}
</style>