// 全局公共js方法库 2024-12-08 export default { //获取考试背景对应的颜色 getExamBackgroundColor(type) { let result = "#F56C6C"; const examTypesColor = [ { type: 1, title: "期末", color: "#F56C6C", background: "#F56C6C", }, //期末 { type: 2, title: "期中", color: "#3ba272", background: "#3ba272", }, //期中 { type: 3, title: "模拟", color: "#ea7acb", background: "#ea7acb", }, //模拟 { type: 4, title: "月考", color: "#fac858", background: "#fac858", }, //月考 { type: 5, title: "周测", color: "#995fb3", background: "#995fb3", }, //周测 { type: 6, title: "平时", color: "#72C0DD", background: "#72C0DD", }, //平时 ]; for (let i = 0; i < examTypesColor.length; i++) { if (examTypesColor[i].type == type || examTypesColor[i].title == type) { result = examTypesColor[i].background; break; } } // console.log("打印颜色值",type,result); return result; }, //获取五率对应的颜色值 getFiveRateColor(type) { let list=[ { name: "top", color: "#5470C6", }, { name: "excellent", color: "#3BA272", }, { name: "good", color: "#FAC858", }, { name: "pass", color: "#995FB3", }, { name: "low", color: "#EE6666", } ];//五率颜色配置 return list.find((item) => item.name === type)?.color || "#5470C6";//默认5470C6颜色 }, //获取五率对应的颜色值 getSchoolFiveRateColor(type) { let list=[ { name: "high", color: "#5470C6", }, { name: "good", color: "#3BA272", }, { name: "fine", color: "#FAC858", }, { name: "pass", color: "#995FB3", }, { name: "low", color: "#EE6666", } ];//五率颜色配置 return list.find((item) => item.name === type)?.color || "#5470C6";//默认5470C6颜色 }, // 获取分析报告成绩分析中默认的20个颜色 getScorePerformanceAnalysis() { return [ "#5470C6", "#3BA272", "#EE6666", "#FAC858", "#995FB3", "#72C0DD", "#90CB75", "#EA7ACB", "#FC8451", "#65789B", "#178BD3", "#26A616", "#A6129E", "#D3A141", "#234098", "#047640", "#B43535", "#848BDC", "#D6AF83", "#84313D", //20个颜色一组 "#5470C6", "#3BA272", "#EE6666", "#FAC858", "#995FB3", "#72C0DD", "#90CB75", "#EA7ACB", "#FC8451", "#65789B", "#178BD3", "#26A616", "#A6129E", "#D3A141", "#234098", "#047640", "#B43535", "#848BDC", "#D6AF83", "#84313D", // "#5470C6", "#3BA272", "#EE6666", "#FAC858", "#995FB3", "#72C0DD", "#90CB75", "#EA7ACB", "#FC8451", "#65789B", "#178BD3", "#26A616", "#A6129E", "#D3A141", "#234098", "#047640", "#B43535", "#848BDC", "#D6AF83", "#84313D", ]; }, // 获取分析报告成绩分析中默认的20个颜色 getCompareAnalysis() { return [ "#EE6666", "#5470C6", "#3BA272", "#FAC858", "#995FB3", "#72C0DD", "#90CB75", "#EA7ACB", "#FC8451", "#65789B", "#178BD3", "#26A616", "#A6129E", "#D3A141", "#234098", "#047640", "#B43535", "#848BDC", "#D6AF83", "#84313D", //20个颜色一组 "#EE6666", "#5470C6", "#3BA272", "#FAC858", "#995FB3", "#72C0DD", "#90CB75", "#EA7ACB", "#FC8451", "#65789B", "#178BD3", "#26A616", "#A6129E", "#D3A141", "#234098", "#047640", "#B43535", "#848BDC", "#D6AF83", "#84313D", ]; }, //获取G组 G10-G1对应的颜色值 getGGroupColor() { return [ { name: "G1", color: "#5470C6", }, { name: "G2", color: "#90CB75", }, { name: "G3", color: "#D3A141", }, { name: "G4", color: "#EE6666", }, { name: "G5", color: "#72C0DD", }, { name: "G6", color: "#3BA272", }, { name: "G7", color: "#FC8451", }, { name: "G8", color: "#995FB3", }, { name: "G9", color: "#EA7ACB", }, { name: "G10", color: "#FAC858", }, ]; }, //获取G组 g10-g1对应的颜色值 getgGroupColor() { return [ { name: "g1", color: "#5470C6", }, { name: "g2", color: "#90CB75", }, { name: "g3", color: "#D3A141", }, { name: "g4", color: "#EE6666", }, { name: "g5", color: "#72C0DD", }, { name: "g6", color: "#3BA272", }, { name: "g7", color: "#FC8451", }, { name: "g8", color: "#995FB3", }, { name: "g9", color: "#EA7ACB", }, { name: "g10", color: "#FAC858", }, ]; }, //G组颜色 gGroupColor(){ return [ '#FAC858','#EA7ACB','#995FB3','#FC8451','#3BA272','#72C0DD','#EE6666','#D3A141','#90CB75','#5470C6', '#65789B','#178BD3','#26A616','#A6129E','#234098','#047640','#B43535','#848BDC','#D6AF83','#84313D', ] }, // G组柱子颜色 gGroupBarColor(){ return ['#848BDC','#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC','#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC','#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0'] }, //知识点分层柱子颜色 knowledgeBarColor(){ return [ '#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC', '#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC', '#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC', '#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC', '#E999AA','#8CCCB1','#B5DDEE','#F4DBC6','#D2B2E2','#F4DFA0','#848BDC', ]; }, //知识点分层层级颜色 knowledgeLayeredColor(){ return [ '#5470C6','#3BA272','#FAC858','#995FB3','#EE6666','#72C0DD','#90CB75','#EA7ACB','#FC8451','#65789B', '#5470C6','#3BA272','#FAC858','#995FB3','#EE6666','#72C0DD','#90CB75','#EA7ACB','#FC8451','#65789B', '#5470C6','#3BA272','#FAC858','#995FB3','#EE6666','#72C0DD','#90CB75','#EA7ACB','#FC8451','#65789B' ] }, //获取百分比 type progress 表示进度条使用 getPercentage(num1, num2, type) { let result = 0; let percentage = 0; if (num1 && num2) { percentage = (parseFloat(num1) / parseFloat(num2)).toFixed(2); } result = Math.round(percentage * 100); // console.log("num1,num2",num1,num2,percentage,result); if (type == "progress") { //进度条的值不能超100% if (result > 100) { result = 100; } } if (result < 0) { result = 0; } // console.log("打印结果",result); return result; }, //获取当前日期 返回的格式为yyyy-MM-dd hh:mm:ss getNowDate(format = "yyyy-mm-dd") { const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, "0"); const date = String(now.getDate()).padStart(2, "0"); const hours = String(now.getHours()).padStart(2, "0"); const minutes = String(now.getMinutes()).padStart(2, "0"); const seconds = String(now.getSeconds()).padStart(2, "0"); switch (format) { case "yyyy-mm-dd": return `${year}-${month}-${date}`; case "yyyy-mm-dd hh:mm:ss": return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`; case "yyyy-mm-dd hh:mm": return `${year}-${month}-${date} ${hours}:${minutes}`; // 可以根据需要添加更多格式 case "yyyy年mm月dd日 hh:mm:ss": return `${year}年${month}月${date}日 ${hours}:${minutes}:${seconds}`; default: return `${year}-${month}-${date} ${hours}:${minutes}:${seconds}`; } }, // 时间戳转换为时间字符串 getTimestamp(timestamp, format = 'YYYY-MM-DD HH:mm:ss') { // console.log('timestamp', timestamp); if (!timestamp && timestamp !== 0) return ''; // 处理不同格式的时间戳 let timeValue = timestamp; // 如果是字符串,尝试转换为数字 if (typeof timeValue === 'string') { timeValue = Number(timeValue); } // 检查是否为有效数字 if (isNaN(timeValue) || timeValue < 0) return ''; // 处理秒级时间戳(10位数字) if (timeValue.toString().length === 10) { timeValue = timeValue * 1000; } // 处理毫秒级时间戳(13位数字) const date = new Date(timeValue); // 检查日期是否有效 if (isNaN(date.getTime())) return ''; const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); const day = String(date.getDate()).padStart(2, '0'); const hours = String(date.getHours()).padStart(2, '0'); const minutes = String(date.getMinutes()).padStart(2, '0'); const seconds = String(date.getSeconds()).padStart(2, '0'); return format .replace('YYYY', year) .replace('MM', month) .replace('DD', day) .replace('HH', hours) .replace('mm', minutes) .replace('ss', seconds); }, // 保留小数点后几位 str 数值 fixed 小数点后保留位数 floatNum(str, fixed) { // console.log("打印参数",str,fixed); if (str == null || str == undefined || str == "") { return 0; } else { const num = parseFloat(str); if (isNaN(num)) { throw new Error("Invalid number string"); } return parseFloat(num.toFixed(fixed)); } }, // 去掉小数点后面为0的 如果不为0 最多只保留2位小数 getFormatNumber(numberStr) { // 尝试将字符串转为数字 const num = parseFloat(numberStr); // 检查是否为NaN(非数字情况) if (isNaN(num)) { return numberStr; } // 检查是否为整数(小数点后全为0) if (num % 1 === 0) { return num.toString(); } // 保留最多2位小数,并去除末尾的0 let formatted = num.toFixed(2); // 如果小数点后都是0,则返回整数形式 if (formatted.endsWith('.00')) { return formatted.split('.')[0]; } // 去除末尾的0(如1.50 → 1.5) formatted = formatted.replace(/\.?0+$/, ''); return formatted; }, //过滤掉html标签的方法 getStripHtmlTags(str) { // 创建一个新的 div 元素 const tempDiv = document.createElement("div"); // 将 HTML 字符串设置为 div 的 innerHTML tempDiv.innerHTML = str; // 返回 div 的 textContent,这将去除所有 HTML 标签 return tempDiv.textContent || tempDiv.innerText; }, //获取评阅类型 单评还是双评等 getRatingType(type) { let result = "单评"; if (type == 1) { result = "单评"; } else if (type == 2) { result = "2评0补0审"; } else if (type == 3) { result = "2评1补0审"; } else if (type == 4) { result = "2评0补1审"; } else if (type == 5) { result = "2评1补1审"; } return result; }, //根据起始题号,结束题号,题号间隔获取题号列表 getTopicNumberList(startNumber, endNumber, interval) { const topicNumberList = []; for (let i = startNumber; i <= endNumber; i += interval) { topicNumberList.push(i); } // console.log("打印题号列表",topicNumberList); return topicNumberList; }, //获取科目图片名称 根据汉字 getSubjectsName(value,count,type) { let name=value; if(count==0) { } else { switch (count) { case 1: name="其他"; break; case 2: name="二科"; break; case 3: name="三科"; break; case 4: name="四科"; break; case 5: name="五科"; break; case 6: name="六科"; break; case 7: name="七科"; break; case 8: name="八科"; break; case 9: name="九科"; break; } } const subjectList = [ { name: "生物", image: "biology.png", color: "#3BA272", }, { name: "化学", image: "chemistry.png", color: "#73C0DE", }, { name: "语文", image: "chinese.png", color: "#91CC75", }, { name: "道德与法治", image: "daofa.png", color: "#FF869D", }, { name: "八科", image: "eight_subject.png", color: "#3BA272", }, { name: "英语", image: "english.png", color: "#EE6666", }, { name: "五科", image: "five_subject.png", color: "#63A1FF", }, { name: "四科", image: "four_subject.png", color: "#FF869D", }, { name: "地理", image: "geography.png", color: "#9A60B4", }, { name: "历史", image: "history.png", color: "#FC8452", }, { name: "信息", image: "information.png", color: "#63A1FF", }, { name: "数学", image: "math.png", color: "#FAC858", }, { name: "九科", image: "nine_subject.png", color: "#EE6666", }, { name: "其他", image: "other.png", color: "#C495FA", }, { name: "物理", image: "physics.png", color: "#817BE2", }, { name: "政治", image: "politics.png", color: "#63A1FF", }, { name: "七科", image: "seven_subject.png", color: "#FC8452", }, { name: "六科", image: "six_subject.png", color: "#9A60B4", }, { name: "体育", image: "sport.png", color: "#FF8B8B", }, { name: "三科", image: "three_subject.png", color: "#5CC1F6", }, { name: "二科", image: "two_subject.webp", color: "#C495FA", }, { name: "总分", image: "totalScore.png", color: "#325AD4", }, { name: "通用", image: "tongyong.png", color: "#5CC1F6", }, { name: "地理学考", image: "geography_exam.webp", color: "#9A60B4", }, { name: "化学学考", image: "chemistry_exam.webp", color: "#73C0DE", }, { name: "历史学考", image: "history_exam.webp", color: "#FC8452", }, { name: "生物学考", image: "biology_exam.webp", color: "#3BA272", }, { name: "物理学考", image: "physics_exam.webp", color: "#817BE2", }, { name: "政治学考", image: "politics_exam.webp", color: "#63A1FF", } ]; let result = subjectList.find((item) => item.name === name); if(result) { if(type && type == 'color') { return result.color; }else { return result.image; } } else { if(type && type == 'color') { return '#C495FA'; }else { return "other.png" } } }, //包含0的正整数 包含0的正整数但不是以0开头的多位正整数 '00'、'01' 和 '0001' 都不会被匹配 isNumber(str){ const regex = /^(?:[1-9][0-9]*|0)$/; return regex.test(str); }, // 大于等于 0 的数字(含整数、小数都行,但不能有负号) isNotNegativeNumber(val){ // return /^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(val); // 先去除首尾空格和中间所有空格 const trimmedVal = String(val).replace(/\s+/g, ''); return /^(?:0|[1-9]\d*)(?:\.\d+)?$/.test(trimmedVal); }, //根据当前值 获取最大值 echarts 图表使用 getMaxValue(value) { const thresholdMap =[ { maxThreshold:0.3,//最大阈值0-0.6 bound:0.3,//边界值 }, { maxThreshold:0.6,//最大阈值0-0.6 bound:0.6,//边界值 }, { maxThreshold:1,//最大阈值 0.6 - 1 bound:1,//边界值 }, { maxThreshold:1.5,//最大阈值 0.6 - 1 bound:1.5,//边界值 }, { maxThreshold:2,//最大阈值 bound:2,//边界值 }, { maxThreshold:4,//最大阈值 bound:4,//边界值 }, { maxThreshold:8,//最大阈值 bound:8,//边界值 }, { maxThreshold:10,//最大阈值 bound:10,//边界值 }, { maxThreshold:20,//最大阈值 bound:20,//边界值 }, { maxThreshold:30,//最大阈值 bound:30,//边界值 }, { maxThreshold:40,//最大阈值 bound:40,//边界值 }, { maxThreshold:50,//最大阈值 bound:50,//边界值 }, { maxThreshold:60,//最大阈值 bound:60,//边界值 }, { maxThreshold:70,//最大阈值 bound:70,//边界值 }, { maxThreshold:80,//最大阈值 bound:80,//边界值 }, { maxThreshold:90,//最大阈值 bound:90,//边界值 }, { maxThreshold:100,//最大阈值 bound:100,//边界值 }, { maxThreshold:120,//最大阈值 bound:120,//边界值 }, { maxThreshold:150,//最大阈值 bound:150,//边界值 }, { maxThreshold:180,//最大阈值 bound:180,//边界值 }, { maxThreshold:200,//最大阈值 bound:200,//边界值 }, { maxThreshold:250,//最大阈值 bound:250,//边界值 }, { maxThreshold:200,//最大阈值 bound:200,//边界值 }, { maxThreshold:300,//最大阈值 bound:300,//边界值 }, { maxThreshold:350,//最大阈值 bound:350,//边界值 }, { maxThreshold:400,//最大阈值 bound:400,//边界值 }, { maxThreshold:450,//最大阈值 bound:450,//边界值 }, { maxThreshold:500,//最大阈值 bound:500,//边界值 }, { maxThreshold:600,//最大阈值 bound:600,//边界值 }, { maxThreshold:700,//最大阈值 bound:700,//边界值 }, ];//配置区间规则 //查找匹配的边界值 const matchedRule = thresholdMap.find(({ maxThreshold }) => value < maxThreshold); // 处理超过阈值的情况(≥5) const bound = matchedRule ? matchedRule.bound : Math.ceil((value) / 10) * 10; // console.log("打印",value); // console.log("打印边界值",bound); return bound; }, //中间截断显示文本,保留前后各n位字符 truncateMiddleText(text,num,startStr,endStr) { //num:不超过多少字符,全部展示 startStr:开头保留多少字符 endStr:结尾保留多少字符 if (text.length <= num) { return text; } // 保留开头和结尾各5位字符 const start = text.substring(0, startStr); const end = text.substring(text.length - endStr); return `${start}...${end}`; }, //根据题库的试卷题目信息 获取答题卡所有小题的数据 getAllQuestionByKnowTopic(list) { console.log("公共方法库处理list:", list); for (let i = 0; i < list.length; i++) { const item = list[i]; if(item.type==1) { //选择题 处理 } } }, //手阅卡 根据分值 生成对应的数组列表 getNumberListByScore(score) { const TOTAL_SLOTS = 24;//格子数量 固定24个 后期可调整 const result = []; // 初始化24个格子 for (let i = 0; i < TOTAL_SLOTS; i++) { result.push({ index: i, value: '', type: -1 }); // type为-1表示空 } if (score < 10) { // 规则1: 分数小于10,从右开始显示 0.5, 0, 1, 2, ..., 到score const values = ['0.5', '0']; // 添加1到score的所有整数 for (let i = 1; i <= score; i++) { values.push(i.toString()); } // 从右向左填充(从最后一个格子开始) for (let i = 0; i < values.length; i++) { const slotIndex = TOTAL_SLOTS - 1 - i; // 从索引23开始 if (slotIndex >= 0) { result[slotIndex].value = values[i]; // 设置type:0.5为小数(0),0-9为个位数(1) result[slotIndex].type = values[i] === '0.5' ? 0 : 1; } } } else if (score >= 10 && score <= 22) { // 规则2: 分数在10到22之间,从右开始显示 0.5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12...直到score const values = ['0.5', '0']; // 从0.5和0开始 // 添加1到score的所有整数 for (let i = 1; i <= score; i++) { values.push(i.toString()); } // 从右向左填充 for (let i = 0; i < values.length; i++) { const slotIndex = TOTAL_SLOTS - 1 - i; if (slotIndex >= 0) { result[slotIndex].value = values[i]; // 规则2中,所有数值都是连续显示的,包括10-22,所以都视为个位数(1) // 0.5为小数(0) result[slotIndex].type = values[i] === '0.5' ? 0 : 1; } } } else if (score > 22) { // 规则3: 分数大于22,从右开始显示 0.5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,中间空一个格子,然后10,20,30... const values = []; // 第一部分:0.5, 0-9 values.push('0.5'); // 最右边 values.push('0'); for (let i = 1; i <= 9; i++) { values.push(i.toString()); } // 中间空一个格子 values.push(''); // 计算十位数的最大值(不超过分数) const maxTens = Math.floor(score / 10) * 10; // 添加十位数:10, 20, 30... for (let tens = 10; tens <= maxTens; tens += 10) { values.push(tens.toString()); } // 从右向左填充 for (let i = 0; i < values.length; i++) { const slotIndex = TOTAL_SLOTS - 1 - i; if (slotIndex >= 0) { result[slotIndex].value = values[i]; // 设置type if (values[i] === '') { result[slotIndex].type = -1; // 空 } else if (values[i] === '0.5') { result[slotIndex].type = 0; // 小数 } else { const num = parseInt(values[i], 10); // 0-9为个位数,10,20,30...为十位数 result[slotIndex].type = num >= 10 ? 2 : 1; } } } } return result; }, };