Files
shanxia2.0/web-admin/src/utils/pl-slip-red-seal.ts
T
2026-07-26 21:06:09 +08:00

40 lines
1.1 KiB
TypeScript

/** 运输单据打印弹窗:红章显隐(本次弹窗有效) */
export const SLIP_SEAL_IMG_RE = /<img[^>]*alt=["']印章["'][^>]*>/gi
export function stripSlipSealImages(html: string): string {
return html.replace(SLIP_SEAL_IMG_RE, '')
}
/**
* 将公司章插回单据 HTML。
* 优先落在「印章图片」注释后;否则插入指定 seal-block 容器开头。
*/
export function injectSlipSealImages(
html: string,
sealHtml: string,
sealBlockClass: string
): string {
let result = stripSlipSealImages(html)
if (!sealHtml) return result
if (/<!--\s*印章图片[^>]*-->/.test(result)) {
let sealCommentIndex = 0
return result.replace(/<!--\s*印章图片[^>]*-->/g, (match) => {
sealCommentIndex += 1
if (sealCommentIndex === 1) return `${match}\n ${sealHtml}`
return match
})
}
if (sealBlockClass && result.includes(sealBlockClass)) {
const escaped = sealBlockClass.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
return result.replace(
new RegExp(`(<div class="${escaped}"[^>]*>)`),
`$1\n <!-- 印章图片 -->\n ${sealHtml}\n `
)
}
return result
}