交通管理局公文项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

136 lines
3.2 KiB

1 month ago
  1. <template>
  2. <el-dialog
  3. :visible.sync="dialogVisible"
  4. :close-on-click-modal="false"
  5. :modal-append-to-body="false"
  6. append-to-body
  7. title="查看打印公文"
  8. width="80%"
  9. :before-close="handleClose"
  10. >
  11. <div class="setting-dialog">
  12. <div
  13. v-if="loading"
  14. v-loading="loading"
  15. element-loading-text="正在加载公文文档,请稍候..."
  16. element-loading-spinner="el-icon-loading"
  17. class="global-loading"
  18. >
  19. <!-- <p>正在加载文档请稍候...</p> -->
  20. </div>
  21. <PdfViewer
  22. v-else-if="pdfSources.length > 0"
  23. ref="pdfViewerRef"
  24. :pdf-sources="pdfSources"
  25. frame-height="70vh"
  26. @pdfPrinted="onPdfPrinted"
  27. />
  28. <div v-else class="empty-state">
  29. <img style="display: block; width: 100px;" src="~@/assets/images/errRecord.png" alt="">
  30. <span>没有可打印的公文文档</span>
  31. </div>
  32. </div>
  33. <div slot="footer" class="dialog-footer">
  34. <el-button @click="dialogVisible = false">关闭</el-button>
  35. <el-button
  36. v-if="pdfSources.length > 0"
  37. type="primary"
  38. style="width: auto; padding: 0 6px;"
  39. @click="printPdf"
  40. >
  41. <i class="iconfont icon-dayin" />打印文档
  42. </el-button>
  43. </div>
  44. </el-dialog>
  45. </template>
  46. <script>
  47. import { FetchHandleDocument } from '@/api/system/documentArchives'
  48. import PdfViewer from './pdfViewer.vue'
  49. export default {
  50. components: { PdfViewer },
  51. data() {
  52. return {
  53. dialogVisible: false,
  54. pdfSources: [],
  55. loading: false,
  56. selectedDocumentId: null
  57. }
  58. },
  59. methods: {
  60. /**
  61. * 打开对话框并加载PDF
  62. */
  63. async openDialog(params) {
  64. this.dialogVisible = true
  65. await this.loadPdfData(params)
  66. },
  67. /**
  68. * 加载PDF数据
  69. */
  70. async loadPdfData(params) {
  71. this.loading = true
  72. this.pdfSources = []
  73. try {
  74. const res = await FetchHandleDocument(params)
  75. // 处理Base64格式的响应
  76. this.pdfSources = res.map(item => {
  77. return item.startsWith('data:application/pdf;base64,')
  78. ? item
  79. : `data:application/pdf;base64,${item}`
  80. })
  81. } catch (error) {
  82. this.$message.error(`加载文档失败: ${error.message}`)
  83. console.error('获取PDF失败:', error)
  84. } finally {
  85. this.loading = false
  86. }
  87. },
  88. /**
  89. * 关闭对话框
  90. */
  91. handleClose() {
  92. this.pdfSources = [] // 清空数据
  93. this.dialogVisible = false
  94. },
  95. /**
  96. * 调用打印功能
  97. */
  98. printPdf() {
  99. this.$refs.pdfViewerRef.handlePrint()
  100. },
  101. /**
  102. * 打印完成回调可选
  103. */
  104. onPdfPrinted() {
  105. console.log('PDF打印已触发')
  106. }
  107. }
  108. }
  109. </script>
  110. <style scoped>
  111. .global-loading {
  112. display: flex;
  113. flex-direction: column;
  114. align-items: center;
  115. justify-content: center;
  116. height: 70vh;
  117. }
  118. .empty-state {
  119. display: flex;
  120. flex-direction: column;
  121. align-items: center;
  122. justify-content: center;
  123. height: 70vh;
  124. color: #000;
  125. }
  126. .dialog-footer{
  127. margin-top: 0 !important;
  128. }
  129. </style>