sealManagement.vue 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <template>
  2. <ele-modal
  3. :visible.sync="showEditFlag"
  4. :close-on-click-modal="false"
  5. custom-class="ele-dialog-form"
  6. append-to-body
  7. :fullscreen="true"
  8. v-if="showEditFlag"
  9. >
  10. <iframe
  11. :src="fileUrl"
  12. v-if="showEditFlag"
  13. style="height: 100vh; width: 100%; position: absolute; left: -1000000px; top: 0;"
  14. frameborder="0"
  15. allowfullscreen="true"
  16. ref="Iframe"
  17. id="Iframe"
  18. ></iframe>
  19. <div class="seal-wrapper" :class="{ 'is-locking': !allPagesLoaded }">
  20. <seal ref="sealRef" @pdf-generated="pdfGenerated"></seal>
  21. <div v-if="!allPagesLoaded" class="seal-lock-hint">
  22. <i class="el-icon-loading seal-lock-spinner"></i>
  23. <span class="seal-lock-text">
  24. PDF 只加载了前 2 页,点击下方"加载全部页"按钮后可盖印到任意页
  25. </span>
  26. </div>
  27. </div>
  28. <template slot="footer">
  29. <el-button
  30. size="small"
  31. @click="renderAllPages"
  32. :disabled="allPagesLoaded"
  33. >{{ allPagesLoaded ? '已加载全部页' : '加载全部页' }}</el-button
  34. >
  35. <el-button
  36. type="primary"
  37. size="small"
  38. @click="handleSave"
  39. :disabled="!allPagesLoaded"
  40. >保存</el-button
  41. >
  42. <el-button size="small" @click="showEditFlag = false" :disabled="false"
  43. >取消</el-button
  44. >
  45. </template>
  46. </ele-modal>
  47. </template>
  48. <script>
  49. import seal from './seal.vue';
  50. import { uploadFileNew } from './api';
  51. export default {
  52. data() {
  53. return {
  54. fileUrl: '',
  55. showEditFlag: false,
  56. restoreData: null,
  57. allPagesLoaded: false // 是否已加载全部页(控制"加载全部页"按钮 + 盖章交互的启用)
  58. };
  59. },
  60. components: { seal },
  61. methods: {
  62. open(row) {
  63. this.showEditFlag = true;
  64. this.restoreData = row;
  65. this.setFileUrl(row);
  66. },
  67. restore() {
  68. this.setFileUrl(this.restoreData);
  69. },
  70. handleSave() {
  71. this.$refs.sealRef.exportToPDF();
  72. },
  73. pdfGenerated({ pdfFile, droppedImages }) {
  74. this.$emit('save', {
  75. pdfFile: pdfFile,
  76. id: this.restoreData.id,
  77. droppedImages
  78. });
  79. this.showEditFlag = false;
  80. // uploadFileNew({
  81. // module: 'fm',
  82. // multiPartFile: pdfFile
  83. // })
  84. // .then((res) => {
  85. // this.$emit('save', {
  86. // stampStoragePath: res.data ? [res.data] : [],
  87. // id: this.restoreData.id,
  88. // droppedImages
  89. // });
  90. // this.showEditFlag = false;
  91. // })
  92. // .finally(() => {
  93. // this.loading = false;
  94. // });
  95. },
  96. setFileUrl(row) {
  97. let file = row.stampStoragePath[0] || row.storagePath[0];
  98. let fileNames = file.storePath.split('/');
  99. // kkFileView 部署在远程,需用远程地址拼 URL(VUE_APP_API_BASE_HOST 已配置为 http://aiot.zoomwin.com.cn:51001)
  100. const apiBaseHost =
  101. process.env.VUE_APP_API_BASE_HOST || window.location.origin;
  102. let url =
  103. apiBaseHost +
  104. '/api/main/file/getFile?objectName=' +
  105. file.storePath +
  106. '&fullfilename=' +
  107. fileNames[fileNames.length - 1];
  108. this.fileUrl = '/kkfile/onlinePreview?url=' + btoa(url);
  109. this.allPagesLoaded = false;
  110. this.$nextTick(() => {
  111. this.$refs.Iframe.onload = () => {
  112. setTimeout(() => {
  113. // 检查 PDF 总页数(kkFileView 在 .container 内为每页生成一个 .page / img)
  114. var iframeDoc = this.$refs.Iframe.contentWindow.document;
  115. var pages = iframeDoc.querySelectorAll(
  116. '.container .page, .container img'
  117. );
  118. console.log('PDF 检测到总页数:', pages.length);
  119. if (pages.length > 0 && pages.length <= 2) {
  120. // ≤ 2 页:无需加载全部,直接视为已加载
  121. console.log('PDF ≤ 2 页,自动标记为已加载,跳过手动加载步骤');
  122. this.allPagesLoaded = true;
  123. }
  124. this.extractAndInit('初始');
  125. }, 800);
  126. };
  127. });
  128. },
  129. renderAllPages() {
  130. if (this.allPagesLoaded) return;
  131. var iframe = document.getElementById('Iframe');
  132. if (!iframe) return;
  133. var iframeWin = iframe.contentWindow;
  134. var iframeDoc = iframeWin.document;
  135. var scrollHeight = Math.max(
  136. iframeDoc.documentElement.scrollHeight,
  137. iframeDoc.body.scrollHeight
  138. );
  139. console.log('开始反向滚动让所有页面渲染,scrollHeight=', scrollHeight);
  140. // 从底反向滚到顶,让 pdfjs 把所有 page canvas 渲染到 viewport
  141. var scrolled = scrollHeight;
  142. var step = -300;
  143. var delay = 60;
  144. var interval = setInterval(() => {
  145. scrolled += step;
  146. iframeWin.scrollTo(0, Math.max(0, scrolled));
  147. if (scrolled <= 0) {
  148. clearInterval(interval);
  149. iframeWin.scrollTo(0, scrollHeight);
  150. setTimeout(() => {
  151. this.extractAndInit('全渲染完成');
  152. this.allPagesLoaded = true;
  153. }, 800);
  154. }
  155. }, delay);
  156. // 30s 兜底
  157. setTimeout(() => {
  158. clearInterval(interval);
  159. iframeWin.scrollTo(0, scrollHeight);
  160. this.extractAndInit('全渲染超时');
  161. this.allPagesLoaded = true;
  162. }, 30000);
  163. },
  164. extractAndInit(stage) {
  165. var iframe = document.getElementById('Iframe');
  166. if (!iframe) return;
  167. var iframeDoc = iframe.contentWindow.document;
  168. var container = iframeDoc.querySelectorAll('.container');
  169. console.log(
  170. '[' + (stage || '未知阶段') + '] container NodeList 长度:',
  171. container.length
  172. );
  173. if (!container[0]) {
  174. console.warn(
  175. '[' +
  176. (stage || '未知阶段') +
  177. '] 未找到 .container 元素。可能原因:\n' +
  178. '1. kkFileView 服务未部署或 /kkfile 代理路径不对\n' +
  179. '2. URL ' +
  180. this.fileUrl +
  181. ' 返回的不是 kkFileView 的 HTML'
  182. );
  183. console.log(
  184. 'iframe body innerHTML 前 500 字符:',
  185. ((iframeDoc.body && iframeDoc.body.innerHTML) || '').slice(0, 500)
  186. );
  187. return;
  188. }
  189. this.$refs.sealRef.init(
  190. container[0].innerHTML.replace(
  191. // 把 kkFileView 输出的绝对/协议相对 URL 改为相对路径
  192. // 这样浏览器从父页面 localhost:8083 加载,走 /kkfile 代理到 51001(浏览器感知同源,无 CORS)
  193. /src="(?:https?:)?\/\/[^/]+(\/[^"]*)"/g,
  194. `src="$1"`
  195. ),
  196. {
  197. droppedImages:
  198. (this.restoreData && this.restoreData.droppedImages) || ''
  199. }
  200. );
  201. }
  202. }
  203. };
  204. </script>
  205. <style scoped lang="scss">
  206. .seal-wrapper {
  207. position: relative;
  208. width: 100%;
  209. height: 100%;
  210. }
  211. // PDF 未全部加载时,禁用盖章区域所有交互(视觉不变)
  212. .seal-wrapper.is-locking {
  213. pointer-events: none;
  214. user-select: none;
  215. }
  216. .seal-lock-hint {
  217. position: absolute;
  218. top: 12px;
  219. left: 50%;
  220. transform: translateX(-50%);
  221. z-index: 10;
  222. background: rgba(64, 158, 255, 0.92);
  223. color: #fff;
  224. padding: 8px 18px;
  225. border-radius: 20px;
  226. font-size: 13px;
  227. display: flex;
  228. align-items: center;
  229. gap: 8px;
  230. box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  231. }
  232. .seal-lock-spinner {
  233. font-size: 14px;
  234. animation: seal-spin 1s linear infinite;
  235. }
  236. @keyframes seal-spin {
  237. to {
  238. transform: rotate(360deg);
  239. }
  240. }
  241. </style>