140 lines
4.4 KiB
JavaScript
140 lines
4.4 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 changeTbAddress(data = {}) {
|
|
const {
|
|
transitAddress = ''
|
|
} = data;
|
|
|
|
console.log('📝 开始自动填写地址...');
|
|
|
|
// 1. 点击"修改"按钮,弹出修改地址框
|
|
var btnType = findElement('(//a[contains(text(), "修改")])[1]');
|
|
for (i = 0; i < 10; i++) {
|
|
if (!btnType) {
|
|
console.warn('⚠️ 找不到"修改"按钮,等待重试' + i);
|
|
await sleep(500);
|
|
btnType = findElement('(//a[contains(text(), "修改")])[1]');
|
|
}
|
|
}
|
|
if (!btnType) {
|
|
console.warn('⚠️ 找不到"修改"按钮');
|
|
return false;
|
|
}
|
|
btnType.click();
|
|
console.log('✅ 已点击"修改"按钮');
|
|
await sleep(500);
|
|
|
|
return true;
|
|
|
|
// // 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 changeTbAddress({
|
|
transitAddress: '{中转仓地址}'
|
|
});
|
|
console.log('执行结果:', result);
|
|
// 批量开票时,C# 端轮询该变量获取执行结果
|
|
window.__invoiceApplyResult = result;
|
|
} catch (ex) {
|
|
console.error('执行异常:', ex);
|
|
window.__invoiceApplyResult = false;
|
|
}
|
|
})(); |