選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Protocol.vue 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <template>
  2. <view class="view-container">
  3. <scroll-view :scroll-y="true" style="height: 100%" v-if='isUrl!== 1'>
  4. <view ref="container" class="docx-content"></view>
  5. </scroll-view>
  6. <view v-else class='empty-view'>{{errTxt}}</view>
  7. </view>
  8. </template>
  9. <script setup>
  10. import {
  11. ref,
  12. onMounted,
  13. nextTick
  14. } from 'vue';
  15. import {
  16. renderAsync
  17. } from 'docx-preview'; // 引入异步渲染方法
  18. const props = defineProps({
  19. url: {
  20. type: String,
  21. default: ''
  22. }
  23. });
  24. onMounted(() => {
  25. getFile(props.url);
  26. });
  27. let container = ref(null);
  28. const isUrl = ref(0)
  29. let errTxt = ref('请求文件错误,文件不存在')
  30. function getFile(url) {
  31. uni.showLoading({
  32. title: '加载中',
  33. mask: true
  34. });
  35. console.log(url, '地址');
  36. let x = new XMLHttpRequest();
  37. x.open('GET', url, true);
  38. x.responseType = 'blob';
  39. x.onload = function(e) {
  40. //self.exportLoading = false;
  41. console.log(e, x, 'e');
  42. if (x.status === 400) {
  43. isUrl.value = 1
  44. uni.hideLoading()
  45. } else {
  46. const blob = new Blob([x.response]);
  47. filechange(blob);
  48. }
  49. };
  50. x.send();
  51. }
  52. function filechange(blob) {
  53. console.log(blob, container.value);
  54. renderAsync(blob, container.value.$el, null, {
  55. className: 'docx', // 默认和文档样式类的类名/前缀
  56. inWrapper: true, // 启用围绕文档内容渲染包装器
  57. ignoreWidth: false, // 禁止页面渲染宽度
  58. ignoreHeight: false, // 禁止页面渲染高度
  59. ignoreFonts: false, // 禁止字体渲染
  60. breakPages: true, // 在分页符上启用分页
  61. ignoreLastRenderedPageBreak: true, //禁用lastRenderedPageBreak元素的分页
  62. experimental: false, //启用实验性功能(制表符停止计算)
  63. trimXmlDeclaration: true, //如果为真,xml声明将在解析之前从xml文档中删除
  64. breakPages: true, // 在分页符上启用分页
  65. debug: false // 启用额外的日志记录
  66. }).then((res) => {
  67. let timer = setTimeout(() => {
  68. const $slides = document.querySelectorAll('section.docx');
  69. uni.hideLoading();
  70. // if ($slides.children.length) {
  71. // const slidesWidth = Math.max(
  72. // ...Array.from($slides.children).map((s) => s.offsetWidth)
  73. // );
  74. const $wrapper = document.querySelector('.docx-wrapper');
  75. const wrapperWidth = $wrapper.offsetWidth;
  76. let wrapperHeight = 0;
  77. $slides.forEach((item) => {
  78. wrapperHeight = handleScale(item, wrapperWidth, wrapperHeight);
  79. });
  80. $wrapper.style.height = wrapperHeight + 5 + 'px';
  81. console.log(wrapperHeight + 'px');
  82. isUrl.value = 2
  83. clearInterval(timer);
  84. // }
  85. }, 100);
  86. }).catch(err => {
  87. isUrl.value = 1
  88. errTxt.value = '请求文件错误,只支持docx文件,当前文件地址:' + props.url
  89. uni.hideLoading();
  90. console.log(errTxt, isUrl, '错误');
  91. })
  92. }
  93. const handleScale = (ele, wrapperWidth, wrapperHeight) => {
  94. const slidesWidth = ele.offsetWidth;
  95. const scales = wrapperWidth / slidesWidth;
  96. ele.style.transform = `scale(${scales})`;
  97. ele.style.transformOrigin = 'top center';
  98. wrapperHeight = ele.offsetHeight * scales + wrapperHeight + 5;
  99. return wrapperHeight;
  100. };
  101. </script>
  102. <style>
  103. .docx-content {
  104. width: 100%;
  105. height: 100%;
  106. }
  107. .view-container {
  108. width: 100%;
  109. height: 100%;
  110. overflow: auto;
  111. }
  112. /deep/.docx-wrapper {
  113. padding: 0 !important;
  114. width: 100%;
  115. }
  116. /deep/.docx-wrapper>section.docx {
  117. width: 100% !important;
  118. display: table !important;
  119. padding: 2em 1em 0 !important;
  120. margin-bottom: 5px;
  121. }
  122. .empty-view {
  123. text-align: center;
  124. padding: 40rpx 0;
  125. }
  126. </style>