const app = new Vue({ el: '#app', data() { return { jsString: '', result: { column: [], filter: [], data: [], }, excelRawList: [], activeName: '历年分数', isIndeterminate1: false, isIndeterminate2: false, checkAll1: true, checkAll2: false, fileTemp: null, form: { column: [], filter: [], }, columns: [], historyColumns: [ { label: '年份', value: '年份' }, { label: '省份', value: '省份' }, { label: '层次', value: '层次' }, { label: '招生类型', value: '招生类型' }, { label: '科类', value: '科类' }, { label: '批次', value: '批次' }, { label: '专业名称', value: '专业名称' }, { label: '最低分', value: '最低分' }, { label: '最低分位次', value: '最低分位次' }, { label: '最高分', value: '最高分' }, { label: '最高分位次', value: '最高分位次' }, { label: '平均分', value: '平均分' }, { label: '省控线', value: '省控线' }, { label: '人数', value: '人数' }, { label: '专业组', value: '专业组' }, ], enrollmentColumns: [ { label: '年份', value: '年份' }, { label: '省份', value: '省份' }, { label: '层次', value: '层次' }, { label: '科类', value: '科类' }, { label: '批次', value: '批次' }, { label: '专业名称', value: '专业名称' }, { label: '专业类别', value: '专业类别' }, { label: '计划性质', value: '计划性质' }, { label: '计划类别', value: '计划类别' }, { label: '计划数', value: '计划数' }, { label: '专业代码', value: '专业代码' }, { label: '专业代号', value: '专业代号' }, { label: '招考方向', value: '招考方向' }, { label: '学制代码', value: '学制代码' }, { label: '收费标准', value: '收费标准' }, { label: '院系部门名称', value: '院系部门名称' }, { label: '办学地点', value: '办学地点' }, { label: '专业备注', value: '专业备注' }, { label: '招考类型名称', value: '招考类型名称' }, { label: '专业组', value: '专业组' }, { label: '备注', value: '备注' }, ], filters: [], historyFilters: [ { label: '年份', value: '年份' }, { label: '省份', value: '省份' }, { label: '层次', value: '层次' }, { label: '招生类型', value: '招生类型' }, { label: '科类', value: '科类' }, { label: '批次', value: '批次' }, ], enrollmentFilters: [ { label: '年份', value: '年份' }, { label: '省份', value: '省份' }, { label: '层次', value: '层次' }, { label: '科类', value: '科类' }, { label: '批次', value: '批次' }, { label: '专业类别', value: '专业类别' }, { label: '计划性质', value: '计划性质' }, { label: '计划类别', value: '计划类别' }, ], // 考生分数列 candidateScoreColumns: [ { label: '年份', value: '年份' }, { label: '姓名', value: '姓名' }, { label: '考生号', value: '考生号' }, { label: '录取批次', value: '录取批次' }, { label: '录取专业', value: '录取专业' }, { label: '快递单号', value: '快递单号' }, ], candidateScoreFilters: [], } }, computed: { excelRawString() { return JSON.stringify(this.excelRawList) }, }, watch: { 'form.column': { handler(newVal) { if (this.activeName === '历年分数') { if (!newVal.length) { this.isIndeterminate1 = false this.checkAll1 = false } else if (newVal.length === this.historyColumns.length) { this.isIndeterminate1 = false this.checkAll1 = true } else { this.isIndeterminate1 = true this.checkAll1 = false } } else if (this.activeName === '招生计划'){ // 招生计划 if (!newVal.length) { this.isIndeterminate1 = false this.checkAll1 = false } else if (newVal.length === this.enrollmentColumns.length) { this.isIndeterminate1 = false this.checkAll1 = true } else { this.isIndeterminate1 = true this.checkAll1 = false } }else{ if (!newVal.length) { this.isIndeterminate1 = false this.checkAll1 = false } else if (newVal.length === this.candidateScoreColumns.length) { this.isIndeterminate1 = false this.checkAll1 = true } else { this.isIndeterminate1 = true this.checkAll1 = false } } this.updateAll() }, deep: true, immediate: true, }, 'form.filter': { handler(newVal) { if (this.activeName === '历年分数') { if (!newVal.length) { this.isIndeterminate2 = false this.checkAll2 = false } else if (newVal.length === this.historyFilters.length) { this.isIndeterminate2 = false this.checkAll2 = true } else { this.isIndeterminate2 = true this.checkAll2 = false } } else if (this.activeName === '招生计划'){ // 招生计划 if (!newVal.length) { this.isIndeterminate2 = false this.checkAll2 = false } else if (newVal.length === this.enrollmentFilters.length) { this.isIndeterminate2 = false this.checkAll2 = true } else { this.isIndeterminate2 = true this.checkAll2 = false } } this.updateAll() }, deep: true, immediate: true, }, }, mounted() { if (this.activeName === '历年分数') { this.columns = [...this.historyColumns] this.filters = [...this.historyFilters] setTimeout(() => { // this.handleCheckAllChange1(true) this.handleTab({name:'历年分数'}) }, 300) } }, methods: { clearAll() { this.form.filter = [] this.checkAll1 = false this.form.column = [] this.checkAll2 = false }, updateAll() { // form.column console.log('this.form.column: ', this.form.column); this.result.column = [...this.form.column] this.form.filter.forEach((item) => { if (!this.form.column.includes(item)) { this.form.filter = this.form.filter.filter((i) => item !== i) } }) // form.filter let data = [...this.result.data] let data1 = this.form.filter.map((item) => { const newItem = {} newItem[item] = [] return newItem }) for (const iterator of data1) { let key = Object.keys(iterator)[0] for (const dataItem of data) { iterator[key].push(dataItem[key]) } iterator[key] = [...new Set(iterator[key])] iterator[key].sort((a, b) => a - b) } this.result.filter = data1 // updateData this.updateData() // createJsString() this.createJsString() }, createJsString() { this.jsString = `var columnList = ${JSON.stringify(this.result.column)}; var filterList = ${JSON.stringify(this.result.filter)}; var dataList = ${JSON.stringify(this.result.data)};` }, updateData() { // console.log(this.form,'====', this.excelRawList); let temp = this.excelRawList.map((item) => { const newItem = {} for (const iterator of this.form.column) { newItem[iterator] = item[iterator] } return newItem }) if (temp[0] && Object.keys(temp[0]).length) this.result.data = [...temp] else this.result.data = [] }, download() { console.log(this.activeName) let blob = new Blob([this.jsString], { type: 'text/javascript' }) let objectURL = window.URL.createObjectURL(blob) const link = document.createElement('a') link.href = objectURL if (this.activeName == '招生计划') { link.download = 'shaixuanandcolumn-jihua.js' } else if (this.activeName == '历年分数') { link.download = 'shaixuanandcolumn-fenshu.js' } else { link.download = 'shaixuanandcolumn-luqu.js' } // link.click() let fileName = ''; if (this.activeName === '招生计划') { fileName = 'shaixuanandcolumn-jihua.js'; } else if (this.activeName === '历年分数') { fileName = 'shaixuanandcolumn-fenshu.js'; } else { fileName = 'shaixuanandcolumn-luqu.js'; } const payload = { fileName, content: this.jsString, }; // 使用 fetch 或 axios 发送请求 fetch('/api/files/save', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }) .then((response) => response.text()) .then((result) => { console.log(result); // 显示服务器返回的结果 this.$message({ type: 'success', message: '保存成功!', }) }) .catch((error) => { this.$message({ type: 'error', message: '保存失败!', }) }); const payload2 = { fileName:`json-${fileName}`, content: JSON.stringify({ columnList:this.form.column, filterList:this.form.filter, dataList:this.result.data, }), }; // 使用 fetch 或 axios 发送请求 fetch('/api/files/save', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload2), }) .then((response) => response.text()) .then((result) => { console.log(result); // 显示服务器返回的结果 }) .catch((error) => { }); }, downloadTemplate() { const link = document.createElement('a') if (this.activeName == '招生计划') { link.href = '/resource/tools/file/enrollment-plan.xlsx' link.download = '招生计划.xlsx' } else if (this.activeName == '历年分数') { link.href = '/resource/tools/file/score-history.xlsx' link.download = '历年分数.xlsx' }else{ link.href = '/resource/tools/file/candidate-score.xlsx' link.download = '考生分数.xlsx' } link.click() }, copy() { navigator.clipboard.writeText(this.jsString) this.$message({ type: 'success', message: '复制成功!', }) }, //上传文件时处理方法 handleChange(file, fileList) { this.fileTemp = file.raw if (this.fileTemp) { if (this.fileTemp.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' || this.fileTemp.type == 'application/vnd.ms-excel') { this.importfxx(this.fileTemp) } else { this.$message({ type: 'warning', message: '附件格式错误,请删除后重新上传!', }) } } else { this.$message({ type: 'warning', message: '请上传附件!', }) } }, //超出最大上传文件数量时的处理方法 handleExceed() { this.$message({ type: 'warning', message: '超出最大上传文件数量的限制!', }) return }, //移除文件的操作方法 handleRemove(file, fileList) { this.fileTemp = null }, handleTemplate() { window.open('/documents/历年分数导入模板.xlsx') }, importfxx(obj) { let _this = this let inputDOM = this.$refs.inputer // 通过DOM取文件数据 this.file = event.currentTarget.files[0] var rABS = false //是否将文件读取为二进制字符串 var f = this.file var reader = new FileReader() //if (!FileReader.prototype.readAsBinaryString) { FileReader.prototype.readAsBinaryString = function (f) { var binary = '' var rABS = false //是否将文件读取为二进制字符串 var pt = this var wb //读取完成的数据 var outdata var reader = new FileReader() reader.onload = function (e) { var bytes = new Uint8Array(reader.result) var length = bytes.byteLength for (var i = 0; i < length; i++) { binary += String.fromCharCode(bytes[i]) } //此处引入,用于解析excel // var XLSX = require("xlsx"); if (rABS) { wb = XLSX.read(btoa(fixdata(binary)), { //手动转化 type: 'base64', }) } else { wb = XLSX.read(binary, { type: 'binary', }) } outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]) console.log('outdata: ', outdata) // let outdata1 = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[1]]) // let outdata2 = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[2]]) // console.log(outdata, outdata1, outdata2); // console.log(outdata, JSON.stringify(outdata)); _this.excelRawList = JSON.parse(JSON.stringify(outdata)) _this.result.data = [...outdata] console.log('_this.result.data: ', _this.result.data) _this.updateAll() _this.$message({ type: 'success', message: '解析成功!', }) } reader.readAsArrayBuffer(f) } if (rABS) { reader.readAsArrayBuffer(f) } else { reader.readAsBinaryString(f) } }, handleCheckAllChange1(value) { console.log('value: ', value,this.activeName); this.isIndeterminate1 = false const result = [] if (value) { if (this.activeName === '历年分数') { this.historyColumns.forEach((item) => { result.push(item.value) }) } else if (this.activeName === '招生计划') { this.enrollmentColumns.forEach((item) => { result.push(item.value) }) } else { this.candidateScoreColumns.forEach((item) => { result.push(item.value) }) } } console.log('result: ', JSON.stringify(result)); this.form.column = result }, handleCheckAllChange2(value) { this.isIndeterminate1 = false const result = [] if (value) { if (this.activeName === '历年分数') { this.historyFilters.forEach((item) => { if (this.form.column.includes(item.value)) result.push(item.value) }) } else if (this.activeName === '招生计划') { this.enrollmentFilters.forEach((item) => { if (this.form.column.includes(item.value)) result.push(item.value) }) } else { this.candidateScoreFilters.forEach((item) => { if (this.form.column.includes(item.value)) result.push(item.value) }) } } this.form.filter = result }, change1(value) { // console.log(this.form); }, loadScript(src) { return new Promise((resolve, reject) => { fetch(src) .then((response) => { if (!response.ok) { return reject(new Error(`Failed to load script: ${src}`)); } return response.text(); // 获取脚本内容 }) .then((scriptContent) => resolve(scriptContent)) .catch((err) => reject(err)); }); }, exportExcel(){ const ws = XLSX.utils.json_to_sheet(this.result.data); // 将数据转换为工作表 const wb = XLSX.utils.book_new(); // 创建新的工作簿 XLSX.utils.book_append_sheet(wb, ws, 'Sheet1'); // 将工作表添加到工作簿 XLSX.writeFile(wb, `${this.activeName}.xlsx`); // 导出为 Excel 文件 }, handleTab({ name }) { this.clearAll() window.columnList=[]; window.filterList=[] if (name === '历年分数') { this.columns = [...this.historyColumns] this.filters = [...this.historyFilters] // } else if (name === '招生计划') { this.columns = [...this.enrollmentColumns] this.filters = [...this.enrollmentFilters] } else { this.columns = [...this.candidateScoreColumns] this.filters = [...this.candidateScoreFilters] } let origin = location.origin // 定义加载文件路径 let scriptPath = ''; if (name === '历年分数') { scriptPath = origin+'/resources/templates/default_v2/web/json-shaixuanandcolumn-fenshu.js'; } else if (name === '招生计划') { scriptPath = origin+'/resources/templates/default_v2/web/json-shaixuanandcolumn-jihua.js'; } else { scriptPath = origin+'/resources/templates/default_v2/web/json-shaixuanandcolumn-luqu.js'; } this.result.data = [] this.excelRawList= [] this.form.column = [] this.form.filter = [] // 动态加载新脚本 this.loadScript(scriptPath) .then((scriptContent) => { console.log(scriptContent) try { let res = JSON.parse(scriptContent) // 使用动态加载的变量更新 columns 和 filters this.result.data = [...(res.dataList || [])]; this.excelRawList= [...(res.dataList || [])]; this.form.column = [...(res.columnList || [])]; this.form.filter = [...(res.filterList || [])]; } catch (error) { } }) .catch((err) => { console.error(`加载脚本失败: ${err}`); setTimeout(() => { this.handleCheckAllChange1(true); }, 300); }); // setTimeout(() => { // this.handleCheckAllChange1(true) // }, 300) }, }, })