阅行客电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
19 KiB

2 years ago
2 years ago
2 years ago
2 weeks ago
2 years ago
2 years ago
2 years ago
7 months ago
2 years ago
7 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
4 months ago
2 years ago
7 months ago
7 months ago
2 years ago
2 years ago
7 months ago
2 years ago
2 years ago
2 years ago
2 years ago
7 months ago
2 years ago
7 months ago
7 months ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. <template>
  2. <div class="preview-wrapper">
  3. <div class="collect-header" style="justify-content: flex-start; border-top: none; ">
  4. <h4 class="is-file" style="flex:none; width: 120px;">原文</h4>
  5. <span style="font-size: 12px; flex: 1; line-height: 42px;">所属文件{{ parentArchiveNo }}</span>
  6. </div>
  7. <!-- <div class="top-container">
  8. <div class="top-left">
  9. <input
  10. class="file-select"
  11. type="file"
  12. @change="handleChange"
  13. >
  14. </div>
  15. <div class="top-middle">
  16. 文件预览
  17. </div>
  18. <div class="top-right">
  19. <el-button @click="printWindow">
  20. <i class="el-icon-printer" />window
  21. </el-button>
  22. <el-button v-print="printObj">vue-print打印</el-button>
  23. <el-button type="primary" @click="toImg">转图片打印</el-button>
  24. </div>
  25. </div> -->
  26. <div class="main-content">
  27. <div class="content-list">
  28. <h4 class="arc-title">原文列表</h4>
  29. <ul v-if="allFileTables.length!==0" class="file-list">
  30. <li v-for="(item,index) in allFileTables" :key="index" :class="{'active-file': item.id == fileCurrent.id}" @click="selectFile(item)">
  31. <span>{{ item.file_name }}</span>
  32. <i class="iconfont icon-attachment" />
  33. </li>
  34. </ul>
  35. <el-empty v-else :image-size="100" />
  36. </div>
  37. <div v-loading="loading" element-loading-text="文件加载中" class="content-right">
  38. <div class="preview-info">
  39. <ul>
  40. <li>创建时间{{ fileCurrent && fileCurrent.create_time }}</li>
  41. <li>大小{{ getFileSize(fileCurrent && fileCurrent.file_size) }}</li>
  42. <li>尺寸{{ (!(fileCurrent && fileCurrent.file_dpi) || (fileCurrent && fileCurrent.file_dpi === 'null')) ? '-' : fileCurrent.file_dpi }}</li>
  43. </ul>
  44. <div class="preview-btn">
  45. <!-- <el-button :loading="downloading" class="iconfont icon-xiazai" @click="downloadFile(fileCurrent)">下载</el-button> -->
  46. <!-- <el-button v-print="printObj" class="iconfont icon-dayin">打印</el-button> -->
  47. </div>
  48. </div>
  49. <div id="printArea" ref="output" class="well-box" :style="{overflowY : !loading ? 'auto' : 'hidden'}" />
  50. <div class="water-mask" />
  51. </div>
  52. </div>
  53. </div>
  54. </template>
  55. <script>
  56. import { FetchMinioReDocumentBase64ByFileId } from '@/api/prearchiveLibrary/prearchiveLibrary'
  57. // import { FetchBase64ByFileId } from '@/api/archivesManage/library'
  58. import { getExtend, readBuffer, render } from '@/components/util'
  59. import { parse } from 'qs'
  60. import { watermark } from '@/utils/waterMark'
  61. import { getInitWatermark } from '@/api/system/waterMask'
  62. import { downloadFile } from '@/utils/index'
  63. import { mapGetters } from 'vuex'
  64. import html2canvas from 'html2canvas' // 转图片打印需要先安装html2Canvas和print-js
  65. import printJS from 'print-js'
  66. import { getToken } from '@/utils/auth'
  67. export default {
  68. name: 'Preview',
  69. components: {
  70. },
  71. props: {
  72. msg: {
  73. type: String,
  74. default: ''
  75. }
  76. },
  77. data() {
  78. return {
  79. downloading: false,
  80. src: '',
  81. logo: require('../../assets/images/logo.png'),
  82. // 加载状态跟踪
  83. loading: false,
  84. // 上个渲染实例
  85. last: null,
  86. // 隐藏头部,当基于消息机制渲染,将隐藏
  87. hidden: false,
  88. printObj: {
  89. id: 'printArea',
  90. preview: true, // 是否启动预览模式,默认是false
  91. previewTitle: '电子原文预览', // 打印预览的标题
  92. previewPrintBtnLabel: '预览结束,开始打印',
  93. zIndex: 20002, // 预览窗口的z-index,默认是20002,最好比默认值更高
  94. extraCss: '', // 打印可引入外部的一个css文件
  95. extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>,<style> #printArea { height: auto !important; } <style>', // 打印头部文字
  96. previewBeforeOpenCallback() { console.log('正在加载预览窗口!') }, // 预览窗口打开之前的callback
  97. previewOpenCallback() { console.log('已经加载完预览窗口,预览打开了!') }, // 预览窗口打开时的callback
  98. beforeOpenCallback() { console.log('开始打印之前!') }, // 开始打印之前的callback
  99. openCallback() { console.log('执行打印了!') }, // 调用打印时的callback
  100. closeCallback() { console.log('关闭了打印工具!') }, // 关闭打印的callback(无法区分确认or取消)
  101. clickMounted() { console.log('点击v-print绑定的按钮了!') },
  102. // url: 'http://192.168.99.67:11100/downloadFile/category/AE8B188F0C0314F9BE31B8/82FBCAE96CBC9F50809838/7f3df862-f36d-4061-9c3e-e81556420742.pdf', // 打印指定的URL,确保同源策略相同
  103. // asyncUrl (reslove) {
  104. // setTimeout(() => {
  105. // reslove('http://localhost:8080/')
  106. // }, 2000)
  107. // },
  108. standard: '',
  109. extarCss: ''
  110. },
  111. watermarkInfo: null,
  112. timer: null,
  113. parentArchiveNo: null,
  114. allFileTables: [],
  115. toFile: null,
  116. fileCurrent: null,
  117. fileParentInfo: null,
  118. pdfsArray: [],
  119. documentId: null
  120. }
  121. },
  122. computed: {
  123. ...mapGetters([
  124. 'baseApi'
  125. ])
  126. },
  127. created() {
  128. // 允许使用预留的消息机制发送二进制数据,必须在url后添加?name=xxx.xxx&from=xxx
  129. const { from, name } = parse(location.search.substr(1))
  130. if (from) {
  131. window.addEventListener('message', (event) => {
  132. const { origin, data: blob } = event
  133. if (origin === from && blob instanceof Blob) {
  134. // 构造响应,自动渲染
  135. const file = new File([blob], name, {})
  136. this.hidden = true
  137. this.handleChange({ target: { files: [file] }})
  138. }
  139. })
  140. }
  141. },
  142. mounted() {
  143. if (this.$route.query.archiveNo) {
  144. this.parentArchiveNo = this.$route.query.archiveNo
  145. }
  146. if (localStorage.getItem('documentId')) {
  147. this.documentId = JSON.parse(localStorage.getItem('documentId'))
  148. }
  149. if (localStorage.getItem('fileParentInfo')) {
  150. this.fileParentInfo = JSON.parse(localStorage.getItem('fileTables'))
  151. }
  152. if (localStorage.getItem('fileTables')) {
  153. this.allFileTables = JSON.parse(localStorage.getItem('fileTables'))
  154. }
  155. if (localStorage.getItem('fileCurrent')) {
  156. this.fileCurrent = JSON.parse(localStorage.getItem('fileCurrent'))
  157. }
  158. this.getBase64ByFileId()
  159. },
  160. methods: {
  161. getFileSize(fileSize) {
  162. // 1. 先将接口返回的KB值转为数字,处理非数字/空值情况
  163. const sizeInKB = Number(fileSize)
  164. if (isNaN(sizeInKB) || sizeInKB < 0) {
  165. return '0 KB' // 异常值默认显示0 KB
  166. }
  167. // 2. 定义单位换算关系(1 MB = 1024 KB,1 GB = 1024 MB)
  168. const KB = 1
  169. const MB = 1024 * KB
  170. const GB = 1024 * MB
  171. // 3. 根据大小自动选择单位并格式化
  172. if (sizeInKB >= GB) {
  173. // 大于等于1GB,显示GB(保留2位小数)
  174. return (sizeInKB / GB).toFixed(2) + ' GB'
  175. } else if (sizeInKB >= MB) {
  176. // 大于等于1MB且小于1GB,显示MB(保留2位小数)
  177. return (sizeInKB / MB).toFixed(2) + ' MB'
  178. } else if (sizeInKB < 1) {
  179. // 不足1KB,统一显示1 KB(保持你之前的需求)
  180. return '1 KB'
  181. } else {
  182. // 1KB到1MB之间,显示KB(保留2位小数)
  183. return sizeInKB + ' KB'
  184. }
  185. },
  186. // 下载附件
  187. downloadFile(row) {
  188. // filePath 保存的文件路径
  189. // bucketType 1预归档 2档案
  190. this.downloading = true
  191. let bucketType = null
  192. if (this.documentId) {
  193. bucketType = 1
  194. } else {
  195. bucketType = 2
  196. }
  197. const url = this.baseApi + '/api/minioUpload/getFile?filePath=' + row.file_path + '&bucketType=' + bucketType
  198. const fetchOptions = {
  199. method: 'GET',
  200. headers: {
  201. 'Authorization': getToken()
  202. }
  203. }
  204. fetch(url, fetchOptions).then(res => res.blob()).then(blob => {
  205. downloadFile(blob, row.file_name.split('.')[0], row.file_type)
  206. this.downloading = false
  207. }).catch(() => {
  208. this.$message({ message: '下载文件失败', type: 'error', offset: 8 })
  209. this.downloading = false
  210. })
  211. },
  212. selectFile(item) {
  213. this.fileCurrent = item
  214. this.getBase64ByFileId()
  215. },
  216. getBase64ByFileId() {
  217. this.loading = true
  218. if (this.documentId) {
  219. const params = {
  220. 'filePath': this.fileCurrent.file_path,
  221. 'bucketType': 1
  222. }
  223. FetchMinioReDocumentBase64ByFileId(params).then(res => {
  224. var base64String = res
  225. var fileName = this.fileCurrent && this.fileCurrent.file_name
  226. var mimeType = this.fileCurrent && this.fileCurrent.file_type
  227. this.toFile = this.base64ToFile(base64String, fileName, mimeType)
  228. if (this.toFile) {
  229. this.handleChange()
  230. }
  231. })
  232. } else {
  233. const params = {
  234. 'filePath': this.fileCurrent.file_path,
  235. 'bucketType': 2
  236. }
  237. FetchMinioReDocumentBase64ByFileId(params).then(res => {
  238. console.log('res', res)
  239. var base64String = res
  240. var fileName = this.fileCurrent && this.fileCurrent.file_name
  241. var mimeType = this.fileCurrent && this.fileCurrent.file_type
  242. this.toFile = this.base64ToFile(base64String, fileName, mimeType)
  243. if (this.toFile) {
  244. this.handleChange()
  245. }
  246. })
  247. }
  248. },
  249. base64ToFile(base64String, fileName, mimeType) {
  250. var byteCharacters = atob(base64String)
  251. var byteArrays = []
  252. for (var offset = 0; offset < byteCharacters.length; offset += 512) {
  253. var slice = byteCharacters.slice(offset, offset + 512)
  254. var byteNumbers = new Array(slice.length)
  255. for (var i = 0; i < slice.length; i++) {
  256. byteNumbers[i] = slice.charCodeAt(i)
  257. }
  258. var byteArray = new Uint8Array(byteNumbers)
  259. byteArrays.push(byteArray)
  260. }
  261. var file = new Blob(byteArrays, { type: mimeType })
  262. file.name = fileName
  263. return file
  264. },
  265. toImg() { // 转图片打印
  266. html2canvas(this.$refs.output, {
  267. backgroundColor: null,
  268. useCORS: true,
  269. windowHeight: document.body.scrollHeight
  270. }).then((canvas) => {
  271. // let url = canvas.toDataURL('image/jpeg', 1.0)
  272. const url = canvas.toDataURL()
  273. this.img = url
  274. printJS({
  275. printable: url,
  276. type: 'image',
  277. documentTitle: '打印图片'
  278. })
  279. })
  280. },
  281. getWatermark() {
  282. getInitWatermark().then((res) => {
  283. this.watermarkInfo = res
  284. // this.isEnable = this.watermarkInfo.isEnable
  285. if (res.watermarkType === '1') {
  286. watermark({
  287. watermark_txt: this.watermarkInfo.context,
  288. watermark_alpha: parseFloat(this.watermarkInfo.transparency) / 100
  289. }, 'water-mask', 'img')
  290. } else if (res.watermarkType === '0') {
  291. // if (parent.length > 0) {
  292. // for (let i = 0; i < parent.length; i++) {
  293. // const childImg = parent[i].getElementsByClassName('weterbox')
  294. // if (childImg.length > 0) {
  295. // for (let i = 0; i < childImg.length; i++) {
  296. // parent[i].removeChild(childImg[i])
  297. // }
  298. // }
  299. // }
  300. // }
  301. watermark({
  302. watermark_txt: this.watermarkInfo.context,
  303. watermark_color: this.watermarkInfo.transparency,
  304. watermark_fontsize: this.watermarkInfo.watermarkFont + 'px'
  305. }, 'water-mask', 'text')
  306. }
  307. if (document.getElementById('error-tip')) {
  308. document.getElementsByClassName('water-mask')[0].innerHTML = ''
  309. }
  310. }).catch(err => {
  311. console.log(err)
  312. })
  313. },
  314. async handleChange(e) {
  315. try {
  316. clearTimeout(this.timer)
  317. // const [file] = e.target.files
  318. // console.log('file', file)
  319. const fileReader = new FileReader()
  320. fileReader.readAsArrayBuffer(this.toFile)
  321. fileReader.onload = () => {
  322. this.src = fileReader.result
  323. }
  324. const arrayBuffer = await readBuffer(this.toFile)
  325. this.last = await this.displayResult(arrayBuffer, this.toFile)
  326. this.$nextTick(() => {
  327. // 取得扩展名
  328. const extend = getExtend(this.toFile.name)
  329. // 加水印
  330. const maskDiv = document.getElementsByClassName('mask_div')
  331. const imgMaskDiv = document.getElementsByClassName('weterbox')
  332. if (imgMaskDiv.length === 1) {
  333. document.getElementsByClassName('water-mask')[0].removeChild(imgMaskDiv[0])
  334. }
  335. // docx-wrapper docx
  336. for (var j = maskDiv.length - 1; j >= 0; j--) {
  337. document.getElementsByClassName('water-mask')[0].removeChild(maskDiv[j])
  338. }
  339. if (!extend.includes('pdf')) {
  340. this.getWatermark()
  341. }
  342. })
  343. } catch (e) {
  344. console.error(e)
  345. } finally {
  346. this.timer = setTimeout(() => {
  347. this.loading = false
  348. }, 1000)
  349. }
  350. },
  351. displayResult(buffer, file) {
  352. // 取得文件名
  353. const { name } = file
  354. // 取得扩展名
  355. const extend = getExtend(name)
  356. // 输出目的地
  357. const { output } = this.$refs
  358. // 生成新的dom
  359. const node = document.createElement('div')
  360. // 添加孩子,防止vue实例替换dom元素
  361. if (this.last) {
  362. output.removeChild(this.last.$el)
  363. this.last.$destroy()
  364. }
  365. const child = output.appendChild(node)
  366. // 调用渲染方法进行渲染
  367. return new Promise((resolve, reject) =>
  368. render(buffer, extend, child).then(resolve).catch(reject)
  369. )
  370. },
  371. // 打印ofd文件
  372. async printWindow() {
  373. /* if (this.showScale !== 0) {
  374. this.showScale = 0;
  375. const divs = renderOfd(this.domWidth, this.ofdObj);
  376. await this.displayOfdDiv(divs);
  377. }*/
  378. const childs = this.$refs.output.children
  379. const list = []
  380. for (const page of childs) {
  381. list.push(page.cloneNode(true))
  382. }
  383. if (list.length > 0) {
  384. const printWindow = window.open('打印窗口', '_blank')
  385. // 给新打开的标签页添加画布内容
  386. const documentBody = printWindow.document.body
  387. // 需要给新打开的标签页添加样式,否则打印出来的内容会很错位
  388. documentBody.style.marginTop = '20px'
  389. for (const page of list) {
  390. // 为了让打印的内容不会太靠上,所以给每一页都添加一个marginBottom
  391. page.style.marginBottom = '20px'
  392. documentBody.appendChild(page)
  393. }
  394. // 焦点移到新打开的标签页
  395. printWindow.focus()
  396. // 执行打印的方法(注意打印方法是打印的当前窗口内的元素,所以前面才新建一个窗口:print()--打印当前窗口的内容。)
  397. printWindow.print()
  398. // 操作完成之后关闭当前标签页(点击确定或者取消都会关闭)
  399. printWindow.close()
  400. }
  401. }
  402. }
  403. }
  404. </script>
  405. <style media="print" lang="scss">
  406. @page {
  407. size: auto;
  408. margin: 3mm;
  409. }
  410. @media print {
  411. html {
  412. background-color: #ffffff;
  413. height: auto;
  414. margin: 0px;
  415. }
  416. body {
  417. border: solid 1px #ffffff;
  418. margin: 10mm 15mm 10mm 15mm;
  419. }
  420. table {
  421. table-layout: auto !important;
  422. }
  423. .el-table__header-wrapper .el-table__header {
  424. width: 100% !important;
  425. border: solid 1px #f2f2f2;
  426. }
  427. .el-table__body-wrapper .el-table__body {
  428. width: 100% !important;
  429. }
  430. #pagetable table {
  431. table-layout: fixed !important;
  432. }
  433. }
  434. </style>
  435. <style lang="scss" scoped>
  436. .preview-wrapper{
  437. background-color: #f6f8fc;
  438. .collect-header{
  439. background-color: #fff;
  440. border-bottom: 1px solid #edeff3;
  441. }
  442. }
  443. .top-container{
  444. display: flex;
  445. justify-content: space-between;
  446. padding: 0 20px;
  447. height: 50px;
  448. line-height: 50px;
  449. overflow: hidden;
  450. text-align: center;
  451. align-items: center;
  452. color: #000;
  453. .top-middle{
  454. flex:1;
  455. text-align: center;
  456. font-size: 20px;
  457. font-weight: bold;
  458. }
  459. .top-right{
  460. text-align: center;
  461. align-items: center;
  462. display:flex;
  463. justify-content: flex-end;
  464. .el-button{
  465. padding: 0 4px;
  466. height: 30px;
  467. line-height: 30px;
  468. }
  469. }
  470. }
  471. .main-content{
  472. display: flex;
  473. justify-content: space-between;
  474. width: calc(100%);
  475. height: calc(100vh - 75px);
  476. background-color: #fff;
  477. .content-list{
  478. width: 300px;
  479. .arc-title{
  480. height: 48px;
  481. line-height: 48px;
  482. text-align: center;
  483. font-size: 16px;
  484. color: #0c0e1e;
  485. background-color: #f3f5f8;
  486. }
  487. .file-list{
  488. height: calc(100% - 48px);
  489. padding: 10px 0;
  490. font-size: 12px;
  491. overflow: hidden;
  492. overflow-y: scroll;
  493. li{
  494. display: flex;
  495. justify-content: space-between;
  496. padding: 0 10px 0 20px;
  497. line-height: 26px;
  498. margin-bottom: 4px;
  499. cursor:default;
  500. &.active-file{
  501. background-color: #e8f2ff;
  502. color: #0c0e1e;
  503. }
  504. & span{
  505. display: block;
  506. margin-right: 4px;
  507. }
  508. &:hover{
  509. background-color: #e8f2ff;
  510. color: #0c0e1e;
  511. }
  512. }
  513. }
  514. }
  515. .content-right{
  516. position: relative;
  517. width: calc(100% - 300px);
  518. border: 1px solid #edeff3;
  519. border-top: none;
  520. border-bottom: none;
  521. // overflow-y: auto;
  522. }
  523. }
  524. .well-box{
  525. position: relative;
  526. border: 1px solid #edeff3;
  527. background-color: #f6f8fc;
  528. height: calc(100vh - 150px);
  529. margin: 0 10px;
  530. overflow: hidden;
  531. }
  532. .water-mask{
  533. position: fixed;
  534. left: 300px;
  535. top: 150px;
  536. bottom: 10px;
  537. right: 10px;
  538. width:100%;
  539. z-index:99;
  540. pointer-events: none;
  541. }
  542. .preview-info{
  543. display: flex;
  544. justify-content: space-between;
  545. padding: 15px;
  546. ul{
  547. display: flex;
  548. justify-content: flex-start;
  549. font-size: 14px;
  550. li{
  551. margin-right: 10px;
  552. height: 30px;
  553. line-height: 30px
  554. }
  555. }
  556. .preview-btn{
  557. .el-button.icon-dayin{
  558. &::before{
  559. font-size: 16px;
  560. }
  561. }
  562. }
  563. }
  564. </style>
  565. <style>
  566. .pptx-wrapper {
  567. max-width: 1000px;
  568. margin: 0 auto;
  569. }
  570. .el-loading-mask{
  571. position: fixed;
  572. left: 300px;
  573. top: 75px;
  574. }
  575. </style>