Files
29WxOrder/29WxOrderWpf/Ctrls/PDDInvoice/Resources/invoice.js
T
2026-08-21 14:15:16 +08:00

134 lines
4.0 KiB
JavaScript

// 填写信息
function changeReactInputValue(inputDom, newText) {
let lastValue = inputDom.value;
inputDom.value = newText;
let event = new Event('input', { bubbles: true });
event.simulated = true;
let tracker = inputDom._valueTracker;
if (tracker) {
tracker.setValue(lastValue);
}
inputDom.dispatchEvent(event);
}
// 异步 sleep
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// 查找元素(支持选择器或XPath)
function findElement(selector, isXPath = true) {
if (isXPath) {
return document.evaluate(
selector,
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
} else {
return document.querySelector(selector);
}
}
// 完整流程
async function fillInvoiceAndApply(data = {}) {
const {
title = '某某科技有限公司',
taxNumber = '91110000123456789X'
} = data;
console.log('📝 开始填充发票信息...');
// 1. 点击"企业"按钮
const invoiceType = findElement('//*[contains(text(), "企业")]');
if (!invoiceType) {
console.warn('⚠️ 找不到"企业"按钮');
return false;
}
invoiceType.click();
console.log('✅ 已点击"企业"按钮');
await sleep(500);
// 2. 查找并填充发票抬头
const invoiceTitle = findElement('//label[contains(text(), "发票抬头")]/following-sibling::textarea[1]') ||
findElement('//*[contains(text(), "发票抬头")]/following-sibling::textarea[1]');
if (invoiceTitle) {
changeReactInputValue(invoiceTitle, title);
console.log('✅ 发票抬头已填充');
} else {
console.warn('⚠️ 未找到发票抬头控件');
}
// 3. 查找并填充税号
const taxNumberInput = findElement('//label[contains(text(), "税号")]/following-sibling::input[1]') ||
findElement('//*[contains(text(), "税号")]/following-sibling::input[1]');
if (taxNumberInput) {
changeReactInputValue(taxNumberInput, taxNumber);
console.log('✅ 税号已填充');
} else {
console.warn('⚠️ 未找到税号控件');
}
await sleep(300);
// 4. 查找并点击"申请发票"按钮
const applyBtn = findElement('//*[contains(text(), "申请发票")]');
if (!applyBtn) {
console.error('❌ 未找到"申请发票"按钮');
return false;
}
applyBtn.scrollIntoView({ behavior: 'smooth', block: 'center' });
await sleep(300);
applyBtn.click();
console.log('✅ 已点击"申请发票"按钮');
// 5. 等待弹窗/状态出现
await sleep(300);
// 6. 判断状态
// 检查是否有"已经申请过"的提示
const alreadyApplied = findElement('//*[contains(text(), "您已经提交过发票申请了")]')
if (alreadyApplied) {
console.log('✅ 已经申请过,无需重复操作');
return true;
}
const successElement = findElement('//*[text()="确认您的发票信息无误后再提交"]');
if (successElement == null) {
return false;
}
// 检查是否有"确认开具"按钮或提示
const confirmBtn = findElement('//*[text()= "确认"]');
if (confirmBtn) {
console.log('✅ 找到"确认"按钮,准备点击...');
await sleep(200);
confirmBtn.click();
console.log('✅ 已点击"确认"按钮');
return true;
}
// 如果都没有找到
console.warn('⚠️ 未找到"已经申请过"或"确认"按钮,请手动检查');
return false;
}
// ========== 调用 ==========
(async function () {
try {
const result = await fillInvoiceAndApply({
title: '{公司}',
taxNumber: '{税号}'
});
console.log('执行结果:', result);
// 批量开票时,C# 端轮询该变量获取执行结果
window.__invoiceApplyResult = result;
} catch (ex) {
console.error('执行异常:', ex);
window.__invoiceApplyResult = false;
}
})();