pdfs.vue 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <template>
  2. <hc-pdf v-if="isPdfDom" :src="pdfUrl" :download="isDownload" :print="isPrint" :is-https="isHttps" :file-name="fileName" />
  3. </template>
  4. <script setup>
  5. import { nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
  6. import { btnAuth, decode } from '~uti/btn-auth'
  7. import { isNullES } from 'js-fast-way'
  8. import website from '~src/config'
  9. const props = defineProps({
  10. url: {
  11. type: String,
  12. default: '',
  13. },
  14. code: {
  15. type: String,
  16. default: '',
  17. },
  18. fileName: {
  19. type: String,
  20. default: '',
  21. },
  22. })
  23. defineOptions({
  24. name: 'HcPdfs',
  25. })
  26. //监听
  27. const queryUrl = ref(props.url)
  28. const queryCode = ref(props.code)
  29. const fileName = ref(props.fileName)
  30. watch(() => [props.url, props.code, props.fileName], ([url, code, name]) => {
  31. queryUrl.value = url
  32. queryCode.value = code
  33. fileName.value = name
  34. setPdfSrcData()
  35. })
  36. //渲染完成
  37. const isPdfDom = ref(false)
  38. const isDownload = ref(false)
  39. const isPrint = ref(false)
  40. const isHttps = ref(false)
  41. onMounted(() => {
  42. isHttps.value = !website.localModel
  43. isDownload.value = btnAuth('client-pdf-download')
  44. isPrint.value = btnAuth('client-pdf-print')
  45. setPdfSrcData()
  46. nextTick(() => {
  47. isPdfDom.value = true
  48. })
  49. })
  50. //设置PDF地址
  51. const pdfUrl = ref('')
  52. const setPdfSrcData = () => {
  53. const url = queryUrl.value, code = queryCode.value
  54. //如果url和code都为空,不做处理
  55. if (isNullES(url) && isNullES(code)) {
  56. return
  57. }
  58. //如果url不为空,code为空,以url为准
  59. if (!isNullES(url) && isNullES(code)) {
  60. pdfUrl.value = url
  61. return
  62. }
  63. //如果url为空,code不为空,以code为准
  64. if (isNullES(url) && !isNullES(code)) {
  65. pdfUrl.value = decode(code ?? '')
  66. return
  67. }
  68. //两个都有的情况下,以code为准
  69. pdfUrl.value = decode(code ?? '')
  70. }
  71. //页面被卸载
  72. onUnmounted(() => {
  73. isPdfDom.value = false
  74. })
  75. </script>