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.
42 lines
1.4 KiB
42 lines
1.4 KiB
|
1 year ago
|
import store from '@/store'
|
||
|
|
/**
|
||
|
|
* @param{string} printJs 打印模版
|
||
|
|
* @param{boolean} isLog 是否带审批
|
||
|
|
* @param{string} logContent 审批表格html
|
||
|
|
* @return{void}
|
||
|
|
**/
|
||
|
|
export function print(printJs, isLog, logContent) {
|
||
|
|
let printStr = printJs
|
||
|
|
const regexField = /<field[^>]*>(.*?)<\/field>/g;
|
||
|
|
let fieldMaths = []
|
||
|
|
let match;
|
||
|
|
|
||
|
|
while ((match = regexField.exec(printJs)) !== null) {
|
||
|
|
fieldMaths.push(match[0]); // 提取 <field> 之间的内容
|
||
|
|
}
|
||
|
|
fieldMaths.forEach(fieldMath => {
|
||
|
|
const matchName = fieldMath.match(/name="([^"]+)"/);
|
||
|
|
|
||
|
|
if (matchName) {
|
||
|
|
const nameValue = matchName[1];
|
||
|
|
const value = store.getters.device === 'desktop' ? document.querySelector(`[for=${nameValue}]+div`).innerHTML : document.querySelector(`[for=${nameValue}] > div:nth-child(2)`).innerHTML
|
||
|
|
printStr = printStr.replace(fieldMath,`<span>${value}</span>`)
|
||
|
|
} else {
|
||
|
|
console.log('未找到name属性');
|
||
|
|
}
|
||
|
|
})
|
||
|
|
if(isLog) {
|
||
|
|
const logStyle = logContent.match(/<style>(.*?)<\/style>/g)
|
||
|
|
let totalLogStyle= ''
|
||
|
|
logStyle.forEach(item => {
|
||
|
|
totalLogStyle += item
|
||
|
|
})
|
||
|
|
printStr = printStr.replace('</style>',`</style>${totalLogStyle}`)
|
||
|
|
const logBody = logContent.match(/<table(.*?)<\/table>/g)[0]
|
||
|
|
printStr = printStr.replace('</table>',`</talbe>${logBody}`)
|
||
|
|
}
|
||
|
|
let printWindow = window.open('', '_blank');
|
||
|
|
printWindow.document.write(printStr);
|
||
|
|
printWindow.focus();
|
||
|
|
}
|