index.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <template>
  2. <div>
  3. <template v-if="type === 'office'">
  4. <!-- <iframe :src="`${window.globalVariables.microsoftPreview}${getFullUrl(url)}`" style="width: 100%; height: 600px;" frameborder='0'></iframe> -->
  5. </template>
  6. <template v-else-if="type === 'pdf'">
  7. <object type="application/pdf" :data="getFullUrl(url)" style="width: 100%; height: 600px;"></object>
  8. </template>
  9. <template v-else-if="type === 'video'">
  10. <video class="w-full h-full" :src="getFullUrl(url)" controls></video>
  11. </template>
  12. <template v-else-if="type === 'audio'">
  13. <audio class="w-full h-full" :src="getFullUrl(url)"></audio>
  14. </template>
  15. <template v-else-if="type === 'image'">
  16. <div class=" bg-light-300" style="height: 100%;">
  17. <img class=" object-contain" :src="getFullUrl(url)" style="width: 100%;height: 100%;" />
  18. </div>
  19. </template>
  20. <el-result v-else-if="type === 'other'" icon="error" title="错误信息" sub-title="文件格式不支持">
  21. </el-result>
  22. </div>
  23. </template>
  24. <script>
  25. export default {
  26. data() {
  27. return {
  28. type: ""
  29. }
  30. },
  31. props: ['url'],
  32. watch: {
  33. url: {
  34. immediate: true,
  35. handler(val) {
  36. if (val != "") {
  37. this.type = this.getFileType(val);
  38. }
  39. }
  40. }
  41. },
  42. methods: {
  43. getFullUrl(url) {
  44. // return url;
  45. if (!url) return '';
  46. return url.startsWith('http') ? url : window.GLOBAL_CONFIG.api + '/' + url
  47. },
  48. getFileType(str) {
  49. if (str) {
  50. str = str += ''.toLocaleLowerCase()
  51. const ext = str.split('.').at(-1)
  52. if (['pdf'].includes(ext)) {
  53. return 'pdf'
  54. } else if (['mp4'].includes(ext)) {
  55. return 'video'
  56. } else if (['ppt', 'pptx', 'xls', 'xlsx', 'doc', 'docx'].includes(ext)) {
  57. return 'office'
  58. } else if (['mp3'].includes(ext)) {
  59. return 'audio'
  60. } else if (['png', 'jpg', 'jpeg'].includes(ext)) {
  61. return 'image'
  62. }
  63. return 'other'
  64. }
  65. return 'other'
  66. }
  67. }
  68. }
  69. </script>
  70. <style>
  71. </style>