index.vue 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div>
  3. <image-viewer
  4. v-if="fileType === 'image'"
  5. :z-index="zIndex"
  6. :url-list="[url]"
  7. :on-close="closeDialog"
  8. />
  9. <template v-if="fileType !== 'image' && fileType">
  10. <el-dialog
  11. :visible.sync="dialogVisible"
  12. :title="title"
  13. fullscreen
  14. append-to-body
  15. custom-class="ibps-file-preview-dialog"
  16. @close="closeDialog"
  17. >
  18. <editor :option="option" v-if="dialogVisible" />
  19. <pdf-viewer v-if="fileType === 'pdf'" ref="viewer" />
  20. <txt-viewer
  21. v-else-if="fileType === 'txt'"
  22. class="file-type-txt"
  23. :src="url"
  24. />
  25. <div v-else class="lc-fixed-navbar">不支持预览的类型</div>
  26. </el-dialog>
  27. </template>
  28. </div>
  29. </template>
  30. <script>
  31. /**
  32. * 文件预览
  33. * 1、'doc', 'docx', 'ppt', 'pptx', 'xls', 'xlsx' 类型支持
  34. * 2、图片支持缩放
  35. * 3、音频,语音支持
  36. * ==================
  37. * 下一版本支持
  38. * 1、pdf支持缩放
  39. * 2、音频,语音多格式支持
  40. * 3、压缩包支持
  41. */
  42. import ImageViewer from './image'
  43. // import AudioViewer from './audio' //xianyifan
  44. // import VideoViewer from './video' //xianyifan
  45. import TxtViewer from './txt'
  46. import pdfViewer from './pdf/index'
  47. import { officeType, pdfType, txtType, imageType, audioType, videoType } from './constants'
  48. import PopupManager from '@/utils/popup'
  49. import editor from '@/business/platform/file/attachment/editFile/editor.vue'
  50. export default {
  51. components: {
  52. ImageViewer,
  53. // AudioViewer,
  54. // VideoViewer,
  55. TxtViewer,
  56. editor,
  57. pdfViewer
  58. },
  59. props: {
  60. visible: {
  61. type: Boolean,
  62. default: false
  63. },
  64. title: {
  65. type: String
  66. },
  67. fileExt: {
  68. type: String
  69. },
  70. url: {
  71. type: String,
  72. require: true
  73. },
  74. optionFile: {
  75. type: Object,
  76. default: () => {}
  77. },
  78. file:{
  79. type: String,
  80. require: ''
  81. }
  82. },
  83. data() {
  84. return {
  85. dialogVisible: false,
  86. fileType: '',
  87. zIndex: 2000,
  88. // openedLoaded: false,
  89. fileUrl: '',
  90. option: {
  91. url: '',
  92. isEdit: true,
  93. fileType: '',
  94. title: '',
  95. user: {
  96. id: '',
  97. name: ''
  98. },
  99. mode: 'view',
  100. editUrl: '',
  101. key: ''
  102. }
  103. }
  104. },
  105. watch: {
  106. optionFile: {
  107. handler: function (val, oldVal) {
  108. this.dialogVisible = this.visible
  109. this.loadViewer()
  110. },
  111. immediate: true
  112. }
  113. },
  114. beforeDestroy() {
  115. this.fileType = ''
  116. // this.openedLoaded = false
  117. this.option = {}
  118. },
  119. methods: {
  120. /**
  121. * zxh 修复zindex 不是最高的被遮住
  122. */
  123. fixZIndex() {
  124. return PopupManager.getZIndex()
  125. },
  126. loadViewer() {
  127. console.log(this.optionFile.previewType)
  128. var flag = true
  129. if (/Mobi|Android|iPhone|iPad/i.test(navigator.userAgent)) {
  130. flag = false
  131. }
  132. if (imageType.includes(this.fileExt)) {
  133. this.dialogVisible = false
  134. this.zIndex = this.fixZIndex()
  135. this.fileUrl = this.url
  136. this.fileType = 'image'
  137. } else if(flag && pdfType.includes(this.fileExt) && this.optionFile.previewType === 'PDFH5'){
  138. // 仅特定字段的PDF文件用PDFh5预览,其余PDF文件使用onlyoffice预览
  139. this.fileType = 'pdf'
  140. this.$nextTick(() => {
  141. this.$refs.viewer.load(this.url)
  142. })
  143. } else {
  144. this.openDialog() // 先初始化调用参数
  145. this.openWindow() // 打开外部预览页面
  146. this.closeDialog() // 关闭当前
  147. // this.fileType = 'FILE'
  148. }
  149. // else if (
  150. // officeType.includes(this.fileExt) ||
  151. // pdfType.includes(this.fileExt)
  152. // ) {
  153. // this.fileType = 'pdf'
  154. // } else if (audioType.includes(this.fileExt)) {
  155. // this.fileType = 'audio'
  156. // } else if (videoType.includes(this.fileExt)) {
  157. // this.fileType = 'video'
  158. // } else if (txtType.includes(this.fileExt)) {
  159. // this.fileType = 'txt'
  160. // }
  161. },
  162. openWindow(data) {
  163. let routeData = this.$router.resolve({
  164. path: '/fileView',
  165. query: this.option
  166. })
  167. window.open(routeData.href)
  168. },
  169. openDialog() {
  170. this.option.user.id = this.$store.getters.userId
  171. this.option.user.name = this.$store.getters.name
  172. this.getFile()
  173. },
  174. getFile() {
  175. this.option.url = this.optionFile.url // 下载地址
  176. this.option.editUrl = this.optionFile.editUrl // 回调接口url
  177. this.option.title = this.optionFile.title // 文件名称
  178. this.option.fileType = this.optionFile.fileType // 类型
  179. },
  180. // openDialog() {
  181. // if (this.fileType === 'pdf') {
  182. // this.$nextTick(() => {
  183. // const timer = setInterval(() => {
  184. // if (this.openedLoaded) {
  185. // clearInterval(timer)
  186. // } else {
  187. // if (this.$refs.viewer) {
  188. // this.$refs.viewer.load(this.url)
  189. // this.openedLoaded = true
  190. // }
  191. // }
  192. // }, 50)
  193. // })
  194. // }
  195. // },
  196. closeDialog() {
  197. this.fileType = ''
  198. // this.openedLoaded = false
  199. this.$emit('close', false)
  200. }
  201. }
  202. }
  203. </script>
  204. <style lang="scss">
  205. .ibps-file-preview-dialog {
  206. .el-dialog__body {
  207. padding: 0;
  208. }
  209. .file-type-txt {
  210. height: calc(88vh) !important;
  211. }
  212. }
  213. </style>