feat: ✨ 上传文件时校验是否模板文件,否则提示 #96
This commit is contained in:
@@ -4,20 +4,43 @@ import { addOtherInfo } from '@/utils'
|
||||
interface WorkerMessage {
|
||||
type: 'start' | 'stop' | 'reset'
|
||||
data: any
|
||||
templateData: any
|
||||
}
|
||||
|
||||
let allData: any[] = []
|
||||
|
||||
function headersEqual(template: string[], actual: string[]): boolean {
|
||||
return template.length === actual.length
|
||||
&& template.every((value, index) => value === actual[index])
|
||||
}
|
||||
|
||||
// 接收主线程消息
|
||||
globalThis.onmessage = async (e: MessageEvent<WorkerMessage>) => {
|
||||
switch (e.data.type) {
|
||||
case 'start':
|
||||
{
|
||||
const fileData = e.data.data
|
||||
// const dataBinary = await readFileBinary(((fileEvent.target as HTMLInputElement).files as FileList)[0]!)
|
||||
const templateData = e.data.templateData
|
||||
|
||||
const workBook = XLSX.read(fileData, { type: 'binary', cellDates: true })
|
||||
const workSheet = workBook.Sheets[workBook.SheetNames[0]]
|
||||
const excelData = XLSX.utils.sheet_to_json(workSheet)
|
||||
const excelData: object[] = XLSX.utils.sheet_to_json(workSheet)
|
||||
|
||||
const templateWorkBook = XLSX.read(templateData, { type: 'array', cellDates: true })
|
||||
const templateWorkSheet = templateWorkBook.Sheets[templateWorkBook.SheetNames[0]]
|
||||
const templateExcelData: object[] = XLSX.utils.sheet_to_json(templateWorkSheet)
|
||||
|
||||
const templateHeader = Object.keys(templateExcelData[0])
|
||||
const header = Object.keys(excelData[0])
|
||||
|
||||
if (!headersEqual(templateHeader, header)) {
|
||||
globalThis.postMessage({
|
||||
type: 'error',
|
||||
data: null,
|
||||
message: '表头不一致,请先下载模板然后修改',
|
||||
})
|
||||
return
|
||||
}
|
||||
allData = addOtherInfo(excelData)
|
||||
globalThis.postMessage({
|
||||
type: 'done',
|
||||
|
||||
Reference in New Issue
Block a user