index.vue
6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<template>
<el-dialog v-bind="$attrs" :close-on-click-modal="false" :modal-append-to-body="false"
v-on="$listeners" @open="onOpen" fullscreen lock-scroll class="NCC-full-dialog"
:show-close="false" :modal="false" append-to-body>
<div class="NCC-full-dialog-header">
<div class="header-title">
<img src="@/assets/images/ncc.png" class="header-logo" />
<p class="header-txt"> · 打印预览</p>
</div>
<div class="options">
<el-button type="primary" size="small" @click="word">Word下载</el-button>
<el-button type="primary" size="small" @click="print">打印</el-button>
<el-button @click="closeDialog()">{{$t('common.cancelButton')}}</el-button>
</div>
</div>
<div class="main" v-loading="loading" :element-loading-text="$t('common.loadingText')">
<div ref="tsPrint" class="print-content" v-html="printTemplate" />
</div>
</el-dialog>
</template>
<script>
import { mapGetters } from "vuex"
import { getData } from '@/api/system/printDev'
export default {
props: ['id', 'formId', 'fullName'],
computed: {
...mapGetters(['userInfo'])
},
data() {
return {
data: {},
printTemplate: '',
loading: false
}
},
methods: {
onOpen() {
if (!this.id) return
this.printTemplate = ''
this.data = {}
this.loading = true
let query = {
id: this.id,
formId: this.formId
}
getData(query).then(res => {
if (!res.data) return
this.printTemplate = res.data.printTemplate
this.data = res.data.printData
this.$nextTick(() => {
const tableList = this.$refs.tsPrint.getElementsByTagName('table')
if (tableList.length) {
for (let j = 0; j < tableList.length; j++) {
const tableObj = tableList[j];
let tds = []
let newTable = []
for (let i = 0; i < tableObj.rows.length; i++) {
tds = tableObj.rows[i]
const dataTag = this.isChildTable(tds.cells)
if (dataTag) {
this.retrieveData(dataTag, tableObj, tds, newTable)
} else {
newTable.push(tds)
}
}
tableObj.getElementsByTagName('tbody')[0].innerHTML = ''
for (let i = 0; i < newTable.length; i++) {
tableObj.getElementsByTagName('tbody')[0].appendChild(newTable[i])
}
}
}
})
this.replaceValue(this.data)
this.replaceSysValue()
this.loading = false
})
},
isChildTable(cells) {
let tableName = ''
outer: for (let j = 0; j < cells.length; j++) {
const cell = cells[j];
let spanList = cells[j].getElementsByTagName('span')
if (!spanList.length) break outer;
let hasChildTable = false
inner: for (let j = 0; j < spanList.length; j++) {
const spanEle = spanList[j];
const dataTag = spanEle.getAttribute('data-tag') ? spanEle.getAttribute('data-tag').split('.')[0] : 'null'
if (dataTag && dataTag !== 'null') {
hasChildTable = true
tableName = dataTag
break inner
}
}
if (hasChildTable) break outer;
}
return tableName
},
closeDialog() {
this.$emit('update:visible', false)
},
shengchengtable(data, tds) {
for (let key in data) {
for (let j = 0; j < tds.cells.length; j++) {
let spanList = tds.cells[j].getElementsByTagName('span')
for (let i = 0; i < spanList.length; i++) {
if (`{${key}}` === spanList[i].innerHTML) {
spanList[i].innerHTML = data[key]
}
}
}
}
return tds
},
retrieveData(dataTag, tableObj, tds, newTable) {
for (let key in this.data) {
if (key == dataTag) {
for (let j = 0; j < this.data[key].length; j++) {
newTable.push(this.shengchengtable(this.data[key][j], tds.cloneNode(true)))
}
}
}
},
replaceSysValue() {
const systemPrinter = this.userInfo.userName + '/' + this.userInfo.userAccount
const systemPrintTime = this.ncc.toDate(new Date())
let systemApprovalContent = ''
this.printTemplate = this.replaceAll(this.printTemplate, '{systemPrinter}', systemPrinter)
this.printTemplate = this.replaceAll(this.printTemplate, '{systemPrintTime}', systemPrintTime)
this.printTemplate = this.replaceAll(this.printTemplate, '{systemApprovalContent}', systemApprovalContent)
},
replaceValue(data) {
for (let key in data) {
this.printTemplate = this.replaceAll(this.printTemplate, `{${key}}`, data[key] || '')
if (Array.isArray(data[key]) && data[key] && data[key].length) {
this.replaceValue(data[key])
}
}
},
replaceAll(data, replace, value) {
const lenr = replace.length
const len = data.length
let newData = ''
let i = 0
for (i; i < len; i++) {
let k = 0
let string = ''
for (k; k < lenr; k++) {
var n = i + k
string += data[n]
}
if (string === replace) {
newData += value
i = i + lenr
}
newData += data[i]
}
return newData
},
word() {
let print = this.$refs.tsPrint.innerHTML
const blob = new Blob([print], {
type: ''
})
const name = this.fullName ? `${this.fullName}.doc` : '下载文档.doc'
this.downloadFile(blob, name)
},
downloadFile(data, name, type) {
let blob = new Blob([data], {
type: type || ''
})
let downloadElement = document.createElement('a')
let href = window.URL.createObjectURL(blob)
downloadElement.href = href
downloadElement.download = name
document.body.appendChild(downloadElement)
downloadElement.click()
document.body.removeChild(downloadElement)
window.URL.revokeObjectURL(href)
},
print() {
let print = this.$refs.tsPrint.innerHTML
let newWindow = window.open('_blank')
newWindow.document.body.innerHTML = print
newWindow.print()
newWindow.close()
},
}
}
</script>
<style lang="scss" scoped>
.print-content {
background: white;
padding: 40px 30px;
margin: 0 auto;
border-radius: 4px;
width: 600px;
height: 100%;
overflow: auto;
}
</style>