// ==UserScript== // @name 发票信息提交助手 // @namespace http://tampermonkey.net/ // @version 1.1 // @description 自动上传发票信息并提交到指定接口 // @author 您的名字 // @match https://zcgl1.sumhs.edu.cn:8888/* // @grant GM_addStyle // @grant GM_cookie // @connect lab.ideaopen.cn // @connect zcgl1.sumhs.edu.cn // ==/UserScript== (function() { 'use strict'; // 添加样式 GM_addStyle(` #invoiceUploader { position: fixed; top: 100px; right: 20px; background-color: #f7f7f7; border: 1px solid #ccc; padding: 15px; z-index: 9999; } #invoiceUploader input[type="file"] { display: block; margin-bottom: 10px; } `); // 创建上传窗口 var uploaderDiv = document.createElement('div'); uploaderDiv.id = 'invoiceUploader'; uploaderDiv.innerHTML = `

请上传发票文件

`; document.body.appendChild(uploaderDiv); // 上传按钮事件 document.getElementById('uploadButton').addEventListener('click', function() { var fileInput = document.getElementById('invoiceFile'); if (fileInput.files.length === 0) { alert('请先选择发票文件!'); return; } var formData = new FormData(); formData.append('file', fileInput.files[0]); // 使用 fetch 发送请求 fetch("https://lab.ideaopen.cn/upload", { method: 'POST', body: formData, redirect: 'follow' }) .then(response => response.json()) .then(data => { if (data.Items && data.Items.length > 0) { processInvoiceData(data); } else { alert('未能解析发票信息,请检查文件内容。'); } }) .catch(error => { alert('上传发票文件时发生错误:' + error.message); }); }); // 处理发票数据 function processInvoiceData(responseData) { var items = responseData.Items; var enterlowequs = []; var totalPriceSum = 0; items.forEach(function(item) { var amountWithoutTax = parseFloat(item.AmountWithoutTax) || 0; var taxAmount = parseFloat(item.TaxAmount) || 0; var totalItemPrice = amountWithoutTax + taxAmount; var quantity = parseFloat(item.Quantity) || 1; var price = totalItemPrice / quantity; var enterlowequ = { "ID": null, "LOWID": null, "EQUNAME": item.Name || "", "MODEL": null, "SPEC": item.Spec || "", "UNIT": item.Unit || "", "QUANTITY": item.Quantity || "1", "PRICE": price.toFixed(1), "TOTALPRICE": totalItemPrice.toFixed(1), "MANAGERATTRCODE": "03", "MANAGERATTR": "低值易耗", "CUSTODIANID": null, "CUSTODIAN": null, "APPLYCODE": null, "PURCHASEID": null, "PURCHASECODE": null, "PURCHASEEQUID": null, "PURCHASEENTEREQUID": null, "STATUS": null, "APPLYID": null }; enterlowequs.push(enterlowequ); totalPriceSum += totalItemPrice; }); var enterlow = { "ID": null, "CODE": null, "PORGANID": "22600", "PORGANNAME": "协同科研中心", "ORGANID": "2260001", "ORGANNAME": "协同科研中心\t", "ENTERERID": null, "ENTERER": null, "TOTALPRICE": totalPriceSum.toFixed(1), "CLADATE": null, "ENTERDATE": responseData.date, "ADDUSERID": null, "ADDUSERNAME": null, "BUYERID": null, "BUYER": null, "HEADERID": null, "HEADER": null, "VERSION": null, "PROCINSTID": null, "COMMITTIME": null, "FINISHTIME": null, "SCHOOLID": "jkyxy", "ISDIRECT": null, "SCHOOLNAME": "健康医学院", "STATUS": null, "CUSTOMAA": "2", "ADDTIME": null, "CUSTOMAB": responseData.tradename, "CUSTOMAC": responseData.number, "MODELID": null, "CUSTOMAD": "浦东新区当归路111号 305", "REMARK": null, "CUSTOMAF": "科研", "CUSTOMBW": null, "CUSTOMBX": null, "CUSTOMAK": null, "isdirect": "1" }; var enterlowfunds = [{}]; var fileDataList = { "CUSTOMBX": [], "CUSTOMBW": [] }; // 准备要发送的数据 var postData = new FormData(); postData.append('enterlow', JSON.stringify(enterlow)); postData.append('enterlowequs', JSON.stringify(enterlowequs)); postData.append('enterlowfunds', JSON.stringify(enterlowfunds)); postData.append('fileDataList', JSON.stringify(fileDataList)); const cookies = document.cookie; const authorization = localStorage.getItem('Authorization'); // 设置请求头 const headers = { 'Cookie': cookies, 'Authorization': authorization || '' // 如果Authorization不存在则使用空字符串 }; // 使用 fetch 发送数据到指定接口 fetch('https://zcgl1.sumhs.edu.cn:8888/asset_product/enterlow/add.do', { method: 'POST', headers: headers, body: postData }) .then(response => { if (response.ok) { alert('数据提交成功!'); } else { alert('数据提交失败,状态码:' + response.status); } }) .catch(error => { alert('数据提交时发生错误:' + error.message); }); } })();