From a82d726906f0264b96a5b1a82d932d0c3c38714e Mon Sep 17 00:00:00 2001
From: king <18310653075@163.com>
Date: 星期二, 05 一月 2021 18:30:21 +0800
Subject: [PATCH] 2021-01-05

---
 src/tabviews/custom/components/group/normal-group/index.jsx |   78 ++++++++++++++++++++++++++++++++++++++-
 1 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/src/tabviews/custom/components/group/normal-group/index.jsx b/src/tabviews/custom/components/group/normal-group/index.jsx
index ac7155e..f38e4a2 100644
--- a/src/tabviews/custom/components/group/normal-group/index.jsx
+++ b/src/tabviews/custom/components/group/normal-group/index.jsx
@@ -2,7 +2,7 @@
 import PropTypes from 'prop-types'
 import { connect } from 'react-redux'
 import { is, fromJS } from 'immutable'
-import { Col, Empty, notification } from 'antd'
+import { Col, Empty, notification, Button } from 'antd'
 import moment from 'moment'
 import md5 from 'md5'
 
@@ -30,6 +30,7 @@
 
   state = {
     mainSearch: [],
+    printing: false,
     data: null
   }
 
@@ -272,11 +273,84 @@
     })
   }
 
+  print = () => {
+    const { config } = this.props
+    const { printing } = this.state
+
+    if (printing) return
+    this.setState({printing: true})
+
+    let pageSize = ['A4', 'A3', 'A5'].includes(config.setting.pageSize) ? config.setting.pageSize : 'A4'
+    let pageLayout = config.setting.pageLayout !== 'horizontal' ? 'vertical' : 'horizontal'
+
+    let pageParam = {
+      A4: {
+        vertical: 980,
+        horizontal: 1200,
+      },
+      A3: {
+        vertical: 1200,
+        horizontal: 1600,
+      },
+      A5: {
+        vertical: 700,
+        horizontal: 1000,
+      }
+    }
+
+    let width = pageParam[pageSize][pageLayout]
+
+    try {
+      let jubuData =  document.getElementById(config.uuid).innerHTML
+
+      let iframe = document.createElement('IFRAME')
+      let linkList = document.getElementsByTagName('link')     // 鑾峰彇鐖剁獥鍙ink鏍囩瀵硅薄鍒楄〃
+      let styleList = document.getElementsByTagName('style')   // 鑾峰彇鐖剁獥鍙tyle鏍囩瀵硅薄鍒楄〃
+
+      document.body.appendChild(iframe)
+      let doc = iframe.contentWindow.document
+      
+      doc.open()
+      doc.write(`<!DOCTYPE html><html lang="en"><head>`)
+      for (let i = 0;i < linkList.length;i++) {
+        if (linkList[i].type === 'text/css') {
+          doc.write(`<LINK rel="stylesheet" type="text/css" href="${linkList[i].href}">`)
+        }
+      }
+      doc.write(`<style>body{width: ${width}px!important;}*{border-style: solid;border-width: 0;}.print-button, .top-search{display: none!important;}</style>`)
+      for (let i = 0;i < styleList.length;i++) {
+        doc.write('<style>' + styleList[i].innerHTML + '</style>')
+      }
+      doc.write(`</head><body>`)
+      doc.write(jubuData)
+      doc.write(`</body></html>`)
+      doc.close()
+
+      setTimeout(() => {
+        iframe.contentWindow.focus()
+        iframe.contentWindow.print()
+
+        document.body.removeChild(iframe)
+
+        this.setState({printing: false})
+      }, 500)
+    } catch (e) {
+      this.setState({printing: false})
+      notification.warning({
+        top: 92,
+        message: '鎵撳嵃寮傚父锛�',
+        duration: 5
+      })
+    }
+  }
+
   render() {
     const { config } = this.props
+    const { printing } = this.state
 
     return (
-      <div className="normal-group-wrap" id={config.uuid}>
+      <div className="normal-group-wrap" id={config.uuid} style={config.style}>
+        {config.setting && config.setting.print === 'true' ? <Button className="print-button" icon="printer" loading={printing} onClick={this.print}></Button> : null}
         {this.getComponents()}
       </div>
     )

--
Gitblit v1.8.0