You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
12 KiB

<template>
<el-dialog
title="导入"
element-loading-text="正在导入..."
:visible.sync="show"
:append-to-body="false"
:modal-append-to-body="false"
:fullscreen="true"
v-loading="loading"
:modal="false"
>
<!-- v-loading.fullscreen.lock="loading" -->
<el-button
type="success"
size="mini"
icon="el-icon-download"
@click="postImportDown"
>获取模板</el-button
>
<!-- <el-button
type="warning"
size="mini"
icon="el-icon-upload"
@click="postImportDown"
>导入数据</el-button
> -->
<input ref="excel-upload-input" class="excel-upload-input" type="file" accept=".xlsx, .xls" @change="handleClick">
<el-button
:loading="loading"
style="margin-left:16px;"
size="mini"
type="primary"
@click="handleUpload"
icon="el-icon-download"
>
导入数据
</el-button>
<!-- <el-button
:loading="loading"
style="margin-left:16px;"
size="mini"
type="warning"
@click="handlePrint()"
icon="el-icon-printer"
>
全部打印
</el-button> -->
<!-- <div class="drop" @drop="handleDrop" @dragover="handleDragover" @dragenter="handleDragover">
Drop excel file here or
<el-button :loading="loading" style="margin-left:16px;" size="mini" type="primary" @click="handleUpload">
Browse
</el-button>
</div> -->
<div style="height:calc(100% - 60px)" id="tableOuter">
<el-table
:data="excelData.results"
border
highlight-current-row
style="width: 100%;margin-top:20px;"
:max-height="tableMaxHeight"
>
<!-- @selection-change="handleSelectionPrint" -->
<!-- <el-table-column type="selection" width="55"></el-table-column> -->
<el-table-column
v-if="excelData.header.length > 0"
type="index"
fixed="left"
label="序号"
width="50"
/>
<el-table-column
v-for="(item,index) of excelData.header"
:key="index"
:prop="item"
:label="item"
:fixed="headerFixed(item)"
/>
<!-- <el-table-column v-if="excelData.header.length > 0" label="明细-箱标签" fixed="right" width="100">
<template slot-scope="scope">
{{scope.row['明细-箱标签']}}
</template>
</el-table-column> -->
<!-- <el-table-column label="操作" fixed="right">
<template slot-scope="scope">
<el-button type="text" @click="handlePrint([scope.row])">打印</el-button>
</template>
</el-table-column> -->
</el-table>
</div>
<span slot="footer" class="dialog-footer">
<el-button @click="submitForm(0)"> </el-button>
<el-button type="primary" @click="submitForm(1)"> </el-button>
</span>
</el-dialog>
</template>
<script>
import XLSX from 'xlsx'
import { mixins } from "@/mixins/mixins"
import { postInventoryLabelCode } from "@/api/wms-core"
import { supplierAsnExcel } from "@/filters/excelOrReportsOption";
import {SupplierAsnCtypeStaBack} from "@/filters/status"
export default {
name:"UploadExcel",
mixins:[mixins],
watch: {
show () {
this.reset()
}
},
props: {
//表头浮动项
fixedArr:{
type:Array,
default: () => {
return [ {name:supplierAsnExcel.details.packingCode,value:"right"} ]
}
},
fixedLeftArr:{
type:Array,
default:null
},
show: {
type: Boolean,
default: false
},
beforeUpload: Function, // eslint-disable-line
onSuccess: Function// eslint-disable-line
},
data() {
return {
loading: false,
excelData: {
header: [],
results: []
},
// printSelection:[],//已选打印
tableMaxHeight:null,//表格流体最大高度
}
},
methods: {
// 数据重置
reset(){
this.excelData = {
header:[],
results:[]
}
this.loading = false
// this.printSelection = [];
},
headerFixed(item){
let _fix = false
this.fixedArr.forEach(fix=>{
if(fix.name == item){
_fix = fix.value
}
})
return _fix
},
/**
* 按钮操作
* index 0:取消1:确定
*/
submitForm(index) {
this.$emit("submitForm", index,this.excelData);
},
// 多选
// handleSelectionPrint(val) {
// this.printSelection = val;
// },
/**
* 打印
* arr 有参数为单独打印无参数为批量打印
*/
// handlePrint(arr){
// let rdlx = "xiangbq.rdlx";
// let params = {
// "boxlabel":[]
// }
// // let printArr = arr ? arr : this.printSelection;
// let printArr = arr ? arr : this.excelData.results;
// if(!printArr || printArr.length <= 0){
// this.$message({
// message: '请选择要打印的数据',
// type: 'error'
// });
// return
// }
// 打印操作
// this.getPackingCode(arr)
// },
// 获取箱标签并回显
getPackingCode(header, results){
let createManyParams = [];
results.forEach(item => {
let _item = {
itemCode: item[supplierAsnExcel.details.itemCode] || "",
lot: item[supplierAsnExcel.details.lot] || "",
labelType:1,
qty: Number(item[supplierAsnExcel.details.qty]),
uom: item[supplierAsnExcel.details.uom],
stdPackQty:0,
company:localStorage.getItem("company"),
// poNumber:item[supplierAsnExcel.poNumber],
// number:item[supplierAsnExcel.number],
supplierCode:item[supplierAsnExcel.supplierCode],
planUserCode:item[supplierAsnExcel.planUserCode],
contactUserName:item[supplierAsnExcel.planUserCode],
planArriveDate:item[supplierAsnExcel.planArriveDate],
shipDate:item[supplierAsnExcel.shipDate],
remark:item[supplierAsnExcel.remark],
ctype:SupplierAsnCtypeStaBack(item[supplierAsnExcel.details.ctype]),
projectCode:item[supplierAsnExcel.details.projectCode],
itemCode:item[supplierAsnExcel.details.itemCode],
recommendErpCode:item[supplierAsnExcel.details.recommendErpCode],
qty:item[supplierAsnExcel.details.qty],
uom:item[supplierAsnExcel.details.uom],
lot:item[supplierAsnExcel.details.lot],
produceDate:item[supplierAsnExcel.details.produceDate],
expireDate:item[supplierAsnExcel.details.expireDate],
};
createManyParams.push(_item)
});
postInventoryLabelCode(createManyParams)
.then((res,a) => {
let _header = JSON.parse(JSON.stringify(header))
_header.push(supplierAsnExcel.details.packingCode)
res.forEach((v,k)=>{
this.$set(results[k],supplierAsnExcel.details.packingCode, res[k].code)
})
this.successShowExcel(_header, results)
}).catch(err => {
console.log(err)
this.loading = false
this.$message.error('导入失败,请重新导入');
// this.reset()
})
},
postImportDown () {
this.$emit('postImportDown')
},
// 成功后回显及渲染
successShowExcel(header, results){
this.excelData.header = header
this.excelData.results = results
this.tableMaxHeight = document.getElementById("tableOuter").clientHeight
this.loading = false
},
generateData({ header, results }) {
// this.excelData.header = header
// this.excelData.results = results
// this.tableMaxHeight = document.getElementById("tableOuter").clientHeight
if(this.onSuccess){
this.onSuccess(header, results).then((res_header, res_results)=>{
let _header = res_header ? res_header : header;
let _results = res_results ? res_results : results;
this.successShowExcel(_header, _results)
}).catch(err=>{
this.loading = false
})
}else{
this.successShowExcel(header, results)
// this.getPackingCode(header, results)
}
},
handleDrop(e) {
e.stopPropagation()
e.preventDefault()
if (this.loading) return
const files = e.dataTransfer.files
if (files.length !== 1) {
this.$message.error('Only support uploading one file!')
return
}
const rawFile = files[0] // only use files[0]
if (!this.isExcel(rawFile)) {
this.$message.error('Only supports upload .xlsx, .xls, .csv suffix files')
return false
}
this.upload(rawFile)
e.stopPropagation()
e.preventDefault()
},
handleDragover(e) {
e.stopPropagation()
e.preventDefault()
e.dataTransfer.dropEffect = 'copy'
},
handleUpload() {
this.$refs['excel-upload-input'].click()
},
handleClick(e) {
const files = e.target.files
const rawFile = files[0] // only use files[0]
if (!rawFile) return
this.upload(rawFile)
},
upload(rawFile) {
this.loading = true
this.$refs['excel-upload-input'].value = null // fix can't select the same excel
if (!this.beforeUpload) {
this.readerData(rawFile)
this.loading = false
return
}
const before = this.beforeUpload(rawFile)
if (before) {
this.readerData(rawFile)
}
},
readerData(rawFile) {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = e => {
const data = e.target.result
// const workbook = XLSX.read(data, { type: 'array',cellText:false,cellDates:true })
const workbook = XLSX.read(data, { type: 'array'})
const firstSheetName = workbook.SheetNames[0]
const worksheet = workbook.Sheets[firstSheetName]
const header = this.getHeaderRow(worksheet)
const results = XLSX.utils.sheet_to_json(worksheet,{raw:false})
// const results = XLSX.utils.sheet_to_json(worksheet)
if(results.length <= 0){
this.$message.error('导入表格内无数据,请确认后重新导入');
this.loading = false
return
}
this.generateData({ header, results })
resolve()
this.tab
}
reader.readAsArrayBuffer(rawFile)
})
},
getHeaderRow(sheet) {
const headers = []
const range = XLSX.utils.decode_range(sheet['!ref'])
let C
const R = range.s.r
/* start in the first row */
for (C = range.s.c; C <= range.e.c; ++C) { /* walk every column in the range */
const cell = sheet[XLSX.utils.encode_cell({ c: C, r: R })]
/* find the cell in the first row */
let hdr = 'UNKNOWN ' + C // <-- replace with your desired default
if (cell && cell.t) hdr = XLSX.utils.format_cell(cell)
headers.push(hdr)
}
return headers
},
isExcel(file) {
return /\.(xlsx|xls|csv)$/.test(file.name)
}
}
}
</script>
<style scoped>
.excel-upload-input{
display: none;
z-index: -9999;
}
.drop{
border: 2px dashed #bbb;
width: 600px;
height: 160px;
line-height: 160px;
margin: 0 auto;
font-size: 24px;
border-radius: 5px;
text-align: center;
color: #bbb;
position: relative;
}
</style>