91 lines
3.2 KiB
TypeScript
91 lines
3.2 KiB
TypeScript
/** 运输单据打印:抬头公司(山峡 / 云栋)切换 */
|
|
|
|
import shanxiaDeliverySealImg from '@/assets/images/shanxia-delivery-seal.png'
|
|
import yundongDeliverySealImg from '@/assets/images/yundong-delivery-seal.png'
|
|
|
|
export const PL_SLIP_COMPANY_SHANXIA = '安庆市山峡化工有限公司'
|
|
export const PL_SLIP_COMPANY_YUNDONG = '安徽云栋化工有限公司'
|
|
|
|
export type PlSlipCompanyKey = 'shanxia' | 'yundong'
|
|
|
|
export const PL_SLIP_COMPANY_OPTIONS: { label: string; value: PlSlipCompanyKey }[] = [
|
|
{ label: PL_SLIP_COMPANY_SHANXIA, value: 'shanxia' },
|
|
{ label: PL_SLIP_COMPANY_YUNDONG, value: 'yundong' }
|
|
]
|
|
|
|
/** 山峡红章:送货专用章 */
|
|
export const PL_SLIP_SHANXIA_SEAL_SRC = shanxiaDeliverySealImg as string
|
|
/** 云栋红章:送货专用章 */
|
|
export const PL_SLIP_YUNDONG_SEAL_SRC = yundongDeliverySealImg as string
|
|
|
|
const TITLE_HEADING_SELECTOR =
|
|
'.inbound-slip-title-heading, .delivery-slip-title-heading, .subpack-slip-title-heading'
|
|
|
|
export function getPlSlipCompanyName(key: PlSlipCompanyKey): string {
|
|
return key === 'yundong' ? PL_SLIP_COMPANY_YUNDONG : PL_SLIP_COMPANY_SHANXIA
|
|
}
|
|
|
|
export function getPlSlipSealSrc(key: PlSlipCompanyKey): string {
|
|
return key === 'yundong' ? PL_SLIP_YUNDONG_SEAL_SRC : PL_SLIP_SHANXIA_SEAL_SRC
|
|
}
|
|
|
|
function isSlipLogoImg(img: HTMLImageElement): boolean {
|
|
const alt = (img.getAttribute('alt') || '').trim()
|
|
if (alt === '印章') return false
|
|
if (alt === 'logo') return true
|
|
const src = img.getAttribute('src') || ''
|
|
if (src.includes('watermask')) return false
|
|
return src.includes('logo.gif') || /\/logo(\.|$)/.test(src) || src.includes('/logo')
|
|
}
|
|
|
|
function isCompanyTitleText(text: string): boolean {
|
|
const normalized = text.replace(/\u00a0/g, ' ').replace(/\s+/g, '').trim()
|
|
if (!normalized) return false
|
|
if (normalized.includes('入库') || normalized.includes('货单') || normalized.includes('分装')) {
|
|
return false
|
|
}
|
|
return (
|
|
normalized === PL_SLIP_COMPANY_SHANXIA.replace(/\s+/g, '') ||
|
|
normalized === PL_SLIP_COMPANY_YUNDONG.replace(/\s+/g, '') ||
|
|
normalized.includes('山峡化工') ||
|
|
normalized.includes('云栋化工')
|
|
)
|
|
}
|
|
|
|
/**
|
|
* 按抬头公司更新单据 HTML:标题公司名、山峡 logo 显隐;可选替换红章图。
|
|
* 仅本次弹窗预览有效,不改后端默认模板生成逻辑。
|
|
*/
|
|
export function applyPlSlipCompanyToHtml(
|
|
html: string,
|
|
companyKey: PlSlipCompanyKey,
|
|
options?: { swapSeal?: boolean }
|
|
): string {
|
|
if (!html || typeof document === 'undefined') return html
|
|
|
|
const wrap = document.createElement('div')
|
|
wrap.innerHTML = html
|
|
const companyName = getPlSlipCompanyName(companyKey)
|
|
const isYundong = companyKey === 'yundong'
|
|
|
|
wrap.querySelectorAll('img').forEach((img) => {
|
|
if (!isSlipLogoImg(img as HTMLImageElement)) return
|
|
;(img as HTMLImageElement).style.display = isYundong ? 'none' : ''
|
|
})
|
|
|
|
wrap.querySelectorAll(TITLE_HEADING_SELECTOR).forEach((el) => {
|
|
const text = (el.textContent || '').trim()
|
|
if (!isCompanyTitleText(text)) return
|
|
el.textContent = companyName
|
|
})
|
|
|
|
if (options?.swapSeal) {
|
|
const sealSrc = getPlSlipSealSrc(companyKey)
|
|
wrap.querySelectorAll('img[alt="印章"]').forEach((img) => {
|
|
img.setAttribute('src', sealSrc)
|
|
})
|
|
}
|
|
|
|
return wrap.innerHTML
|
|
}
|