阅行客电子档案
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.

548 lines
17 KiB

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