From 44f5886e09171a9f7a29f657cbc47f513fbc589b Mon Sep 17 00:00:00 2001 From: 刘有才luck <14606842+liu-youcai-luck@user.noreply.gitee.com> Date: Mon, 6 Jan 2025 17:35:40 +0800 Subject: [PATCH] wip: 代码提交 --- src/utils/index.js | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------------------------------------------------- 1 file changed, 95 insertions(+), 74 deletions(-) diff --git a/src/utils/index.js b/src/utils/index.js index 7391166..62f4deb 100644 --- a/src/utils/index.js +++ b/src/utils/index.js @@ -276,7 +276,7 @@ export function b64DecodeUnicode(str) { }) .join("") ); - } catch (e) { } + } catch (e) {} return uni; } @@ -623,8 +623,7 @@ export function fetchHTML(url) { } else { return ""; } - } - catch (e) { + } catch (e) { return ""; } } @@ -907,11 +906,17 @@ export function paperPrint(paper) { if (windowParams.navigator.userAgent.indexOf("Trident") > -1) { return "Trident"; // IE } - if (/Chrome/.test(windowParams.navigator.userAgent) && /Google Inc/.test(windowParams.navigator.vendor)) { - return 'Google Chrome'; + if ( + /Chrome/.test(windowParams.navigator.userAgent) && + /Google Inc/.test(windowParams.navigator.vendor) + ) { + return "Google Chrome"; } - if (/360/.test(windowParams.navigator.userAgent) || /QIHU/.test(windowParams.navigator.userAgent)) { - return '360 Browser'; + if ( + /360/.test(windowParams.navigator.userAgent) || + /QIHU/.test(windowParams.navigator.userAgent) + ) { + return "360 Browser"; } return "Blink"; // Assume it's Chrome, Safari, or an alternative Blink-based browser } @@ -920,15 +925,26 @@ export function paperPrint(paper) { windowParams.close(); } function numberToChinese(num) { - const chineseDigits = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; + const chineseDigits = [ + "零", + "一", + "二", + "三", + "四", + "五", + "六", + "七", + "八", + "九", + ]; const chineseUnits = ["", "十", "百", "千", "万", "亿"]; - let result = ''; - let unitPos = 0; // 单位的位置 - let zeroFlag = false; // 是否出现过零 + let result = ""; + let unitPos = 0; // 单位的位置 + let zeroFlag = false; // 是否出现过零 if (num === 0) { - return chineseDigits[0]; // 特殊情况,0 返回 "零" + return chineseDigits[0]; // 特殊情况,0 返回 "零" } // 处理数字,每次取一位并加上单位 @@ -949,34 +965,34 @@ export function paperPrint(paper) { } // 处理 '一十' 和 '一百' 等情况 - if (result.startsWith('一十')) { + if (result.startsWith("一十")) { result = result.substring(1); // 去掉 '一',例如 '一十' -> '十' } return result; } function htmlParseDom(html, getStyleScripts) { - var tempDom = document.createElement('div'); + var tempDom = document.createElement("div"); tempDom.innerHTML = html; - var doms = tempDom.querySelectorAll('p'); + var doms = tempDom.querySelectorAll("p"); if (getStyleScripts == true) { - var styleDoms = tempDom.querySelectorAll('style'); - var scriptDoms = tempDom.querySelectorAll('script'); + var styleDoms = tempDom.querySelectorAll("style"); + var scriptDoms = tempDom.querySelectorAll("script"); return { doms: doms, styles: styleDoms, - scripts: scriptDoms - } + scripts: scriptDoms, + }; } - return { doms: doms } + return { doms: doms }; } function generatePageParams() { const totalHeightPx = Math.max( printWin.document.documentElement.scrollHeight, // 整个文档高度 - printWin.document.body.scrollHeight // Body 的高度 + printWin.document.body.scrollHeight // Body 的高度 ); - const a4Box = printWin.document.createElement('div'); - a4Box.classList.add('altA4Box'); + const a4Box = printWin.document.createElement("div"); + a4Box.classList.add("altA4Box"); printWin.document.body.appendChild(a4Box); const a4BoxHeightPx = a4Box.offsetHeight; if (a4BoxHeightPx > 0) { @@ -984,18 +1000,20 @@ export function paperPrint(paper) { var pages = Math.ceil(totalHeightPx / a4BoxHeightPx); if (pages > 2) pages += 1; for (var page = 0; page < pages; page++) { - var pageFooteDiv = printWin.document.createElement('div'); - pageFooteDiv.classList.add('page-footer'); - pageFooteDiv.innerText = `${paperTitle} ${subjectName} 第${page + 1}页(共${pages}页)`; - pageFooteDiv.style.top = (page + 1) * a4BoxHeightPx + (page * 25) + 'px'; + var pageFooteDiv = printWin.document.createElement("div"); + pageFooteDiv.classList.add("page-footer"); + pageFooteDiv.innerText = `${paperTitle} ${subjectName} 第${ + page + 1 + }页(共${pages}页)`; + pageFooteDiv.style.top = (page + 1) * a4BoxHeightPx + page * 25 + "px"; printWin.document.body.appendChild(pageFooteDiv); } } } - // size: A4 portrait; + // size: A4 portrait; // size: A3 landscape; - const style = printWin.document.createElement('style'); + const style = printWin.document.createElement("style"); style.innerHTML = ` @media print { @@ -1112,56 +1130,58 @@ export function paperPrint(paper) { printWin.document.head.appendChild(style); - const bodyBoxDom = printWin.document.createElement('div'); - bodyBoxDom.classList.add('bodyBox'); + const bodyBoxDom = printWin.document.createElement("div"); + bodyBoxDom.classList.add("bodyBox"); - const titleBoxDom = printWin.document.createElement('div'); - titleBoxDom.classList.add('title-header'); - const titleDom = printWin.document.createElement('h3'); + const titleBoxDom = printWin.document.createElement("div"); + titleBoxDom.classList.add("title-header"); + const titleDom = printWin.document.createElement("h3"); titleDom.innerText = paperTitle; - titleDom.classList.add('title-content'); - const subjectDom = printWin.document.createElement('h2'); + titleDom.classList.add("title-content"); + const subjectDom = printWin.document.createElement("h2"); subjectDom.innerText = subjectName; - subjectDom.classList.add('title-content'); + subjectDom.classList.add("title-content"); titleBoxDom.appendChild(titleDom); titleBoxDom.appendChild(subjectDom); bodyBoxDom.appendChild(titleBoxDom); - const studentInputBoxDom = printWin.document.createElement('div'); - studentInputBoxDom.classList.add('student-input'); - const studentClassInputDom = printWin.document.createElement('span'); - studentClassInputDom.innerHTML = '班级:________________'; - const studentNameInputDom = printWin.document.createElement('span'); - studentNameInputDom.innerHTML = '姓名:________________'; - const studentNoInputDom = printWin.document.createElement('span'); - studentNoInputDom.innerHTML = '学号:________________'; + const studentInputBoxDom = printWin.document.createElement("div"); + studentInputBoxDom.classList.add("student-input"); + const studentClassInputDom = printWin.document.createElement("span"); + studentClassInputDom.innerHTML = "班级:________________"; + const studentNameInputDom = printWin.document.createElement("span"); + studentNameInputDom.innerHTML = "姓名:________________"; + const studentNoInputDom = printWin.document.createElement("span"); + studentNoInputDom.innerHTML = "学号:________________"; studentInputBoxDom.appendChild(studentClassInputDom); studentInputBoxDom.appendChild(studentNameInputDom); studentInputBoxDom.appendChild(studentNoInputDom); bodyBoxDom.appendChild(studentInputBoxDom); - const tableDom = printWin.document.createElement('table'); - const theadDom = printWin.document.createElement('thead'); - const theadTrDom = printWin.document.createElement('tr'); - theadTrDom.appendChild(printWin.document.createElement('th')); - theadTrDom.appendChild(printWin.document.createElement('th')); - theadTrDom.appendChild(printWin.document.createElement('th')); + const tableDom = printWin.document.createElement("table"); + const theadDom = printWin.document.createElement("thead"); + const theadTrDom = printWin.document.createElement("tr"); + theadTrDom.appendChild(printWin.document.createElement("th")); + theadTrDom.appendChild(printWin.document.createElement("th")); + theadTrDom.appendChild(printWin.document.createElement("th")); theadDom.appendChild(theadTrDom); tableDom.appendChild(theadDom); - const tbodyDom = printWin.document.createElement('tbody'); + const tbodyDom = printWin.document.createElement("tbody"); for (var iof = 0; iof < paperQuestions.length; iof++) { var item = paperQuestions[iof]; if (!item || !item.subQuestions) continue; - const tbodyTrDom = printWin.document.createElement('tr'); - const tbodyLeftAltTdDom = printWin.document.createElement('td'); - const tbodyContentTdDom = printWin.document.createElement('td'); - const tbodyRightAltTdDom = printWin.document.createElement('td'); + const tbodyTrDom = printWin.document.createElement("tr"); + const tbodyLeftAltTdDom = printWin.document.createElement("td"); + const tbodyContentTdDom = printWin.document.createElement("td"); + const tbodyRightAltTdDom = printWin.document.createElement("td"); - const questionTitleDom = printWin.document.createElement('div'); - questionTitleDom.innerText = `${numberToChinese(iof + 1)}、${item.questionTitle}(本节共${item.subQuestions.length}小题,共${item.score}分)`; + const questionTitleDom = printWin.document.createElement("div"); + questionTitleDom.innerText = `${numberToChinese(iof + 1)}、${ + item.questionTitle + }(本节共${item.subQuestions.length}小题,共${item.score}分)`; tbodyContentTdDom.appendChild(questionTitleDom); tbodyTrDom.appendChild(tbodyLeftAltTdDom); @@ -1170,12 +1190,12 @@ export function paperPrint(paper) { tbodyDom.appendChild(tbodyTrDom); for (var idof = 0; idof < item.subQuestions.length; idof++) { - const qTbodyTrDom = printWin.document.createElement('tr'); - const qTbodyLeftAltTdDom = printWin.document.createElement('td'); - const qTbodyContentTdDom = printWin.document.createElement('td'); - const qTbodyRightAltTdDom = printWin.document.createElement('td'); + const qTbodyTrDom = printWin.document.createElement("tr"); + const qTbodyLeftAltTdDom = printWin.document.createElement("td"); + const qTbodyContentTdDom = printWin.document.createElement("td"); + const qTbodyRightAltTdDom = printWin.document.createElement("td"); - const questionDom = printWin.document.createElement('div'); + const questionDom = printWin.document.createElement("div"); var subItem = item.subQuestions[idof]; var screenshotHtml = fetchHTML(subItem.screenshot); var getStyleScripts = iof == 0 && idof == 0; @@ -1196,16 +1216,16 @@ export function paperPrint(paper) { tableDom.appendChild(tbodyDom); bodyBoxDom.appendChild(tableDom); - const tfootDom = printWin.document.createElement('tfoot'); - const tfootTrDom = printWin.document.createElement('tr'); - tfootTrDom.appendChild(printWin.document.createElement('td')); - tfootTrDom.appendChild(printWin.document.createElement('td')); - tfootTrDom.appendChild(printWin.document.createElement('td')); + const tfootDom = printWin.document.createElement("tfoot"); + const tfootTrDom = printWin.document.createElement("tr"); + tfootTrDom.appendChild(printWin.document.createElement("td")); + tfootTrDom.appendChild(printWin.document.createElement("td")); + tfootTrDom.appendChild(printWin.document.createElement("td")); tfootDom.appendChild(tfootTrDom); tableDom.appendChild(tfootDom); printWin.document.body.appendChild(bodyBoxDom); // generatePageParams(); - const lastImage = printWin.document.querySelector('img:last-child'); + const lastImage = printWin.document.querySelector("img:last-child"); if (lastImage) { if (lastImage.complete) windowPrint(printWin); lastImage.onload = () => windowPrint(printWin); @@ -1225,9 +1245,7 @@ export function tablePrint(options) { var diffStNumber = options.diffStNumber ?? 0; let divs = document.getElementById(id); let awin = window.open("中天易教", "_blank", ""); - awin.document.getElementsByTagName( - "head" - )[0].innerHTML = `