|
|
<template> <div class="customer-css"> <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="390px" class="customer-dialog"> <el-form :inline="true" label-position="top" label-width="80px"> <div class="upload-toolbar"> <el-button type="primary" @click="downloadFile()">下载文件模板</el-button> <el-select v-if="isMasterImport" filterable v-model="selectedSite" placeholder="请选择工厂" style="width: 180px"> <el-option v-for="item in siteList" :key="item.siteID" :label="item.shortSiteName" :value="item.siteID"> </el-option> </el-select> </div> <el-form-item v-if="needSelectBu" label="BU"> <el-select v-model="bu" placeholder="请选择" style="width: 295px"> <el-option v-for="i in userBuList" :key="i.buNo" :label="i.sitename" :value="i.buNo"> <span style="float: left;width: 100px">{{ i.sitename }}</span> <span style="float: right; color: #8492a6;white-space:nowrap;overflow:hidden;text-overflow:ellipsis; font-size: 11px;"> {{ i.buDesc }} </span> </el-option> </el-select> </el-form-item> <el-row> <el-col :span="24"> <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls" :before-upload="beforeUploadHandle" :on-change="onChange" :on-remove="onRemove" :auto-upload="false" style="text-align: left;"> <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> </el-upload> </el-col> </el-row> </el-form> <span slot="footer" class="dialog-footer"> <el-button type="primary" :loading="saveLoading" @click="saveUploadFile()">保存</el-button> <el-button type="primary" @click="closeDialog">关闭</el-button> </span> </el-dialog> </div></template>
<script>import { uploadExcel, // 导入项目文件
uploadTemplateExcel, // 导入模板文件
uploadTemplateItemExcel, // 导入模板检验项目
uploadMethodExcel, // 导入方法文件
uploadPartAttributeExcel, // 导入物料属性文件
uploadPartAttrExcel, uploadPartTemplateExcel, uploadPartStandardExcel, uploadFamilyAttributeExcel, // 导入类别属性文件
downloadQcImportTemplate, queryFileId, // 查询文件ID
getSiteAndBuByUserName} from "@/api/qc/qc.js"import { downLoadObjectFile } from '@/api/eam/eam_object_list.js'import { getSiteData } from '@/api/board.js'export default { data() { return { titleCon: '文件导入', visible: false, fileList: [], bu: '', selectedSite: '', siteList: [], userBuList: [], saveLoading: false, pageData: { flag: '', createBy: '', site: '' }, } }, computed: { isMasterImport() { return ['item', 'method', 'template', 'templateItem', 'partAttr', 'partTemplate', 'partStandard'].indexOf(this.pageData.flag) !== -1 }, needSelectBu() { return !this.isMasterImport }, importSite() { return this.selectedSite || '' } }, created(){ this.getSiteAndBu(); this.loadSiteList() }, methods: { loadSiteList () { return getSiteData({ userID: this.$store.state.user.name }).then(({data}) => { this.siteList = (data && data.rows) ? data.rows : [] if (!this.selectedSite && this.siteList.length) { this.selectedSite = this.siteList[0].siteID } }) }, getDefaultSite () { return (this.siteList && this.siteList.length) ? this.siteList[0].siteID : '' }, getSiteAndBu () { let tempData = { username: this.$store.state.user.name, } getSiteAndBuByUserName(tempData).then(({data}) => { if (data.code === 0) { this.userBuList = data.rows } }) }, init(currentRow) { this.pageData = JSON.parse(JSON.stringify(currentRow)) this.titleCon = this.pageData.title || '文件导入' this.fileList = [] this.bu = '' this.selectedSite = this.getDefaultSite() this.saveLoading = false this.visible = true this.loadSiteList() this.$nextTick(() => { if (this.$refs.uploadFile) { this.$refs.uploadFile.clearFiles() } }) }, beforeUploadHandle(file) { const name = file && file.name ? file.name : '' let extName = name.substring(name.lastIndexOf('.')).toLowerCase() if (!(extName === '.xlsx' || extName === '.xls')) { this.$message.error('数据导入失败,请选择正确的xlsx模板文件') return false } }, onChange(file, fileList) { this.fileList = fileList.slice(-1) }, onRemove() { this.fileList = [] }, closeDialog(){ this.fileList = [] this.bu = '' this.selectedSite = this.getDefaultSite() this.saveLoading = false this.$emit('refreshPageTables'); this.visible = false; }, showImportError(msg) { const text = msg || '导入失败' if (text.indexOf('<br/>') !== -1 || text.indexOf('<br>') !== -1) { this.$alert(text, '导入失败', { confirmButtonText: '确定', dangerouslyUseHTMLString: true }) } else { this.$message.warning(text) } }, handleImportResult(data) { if (data && data.code === 0) { this.$message.success(data.msg || '导入成功') if (this.$refs.uploadFile) { this.$refs.uploadFile.clearFiles() } this.closeDialog() } else { this.showImportError(data && data.msg) } }, saveUploadFile(){ if (this.needSelectBu && (this.bu === '' || this.bu == null)){ this.$message.warning("请选择BU!"); return false; } if (this.isMasterImport && !this.importSite) { this.$message.warning("请选择工厂"); return false; } if (this.fileList == null || this.fileList.length === 0){ this.$message.error("请先上传文件!"); return false; } const formData = new FormData(); formData.append("file", this.fileList[0].raw); formData.append("createBy", this.pageData.createBy); if (this.isMasterImport) { formData.append("site", this.importSite); } else if (this.pageData.flag === 'partAttribute') { formData.append("site", this.bu); } else { formData.append("site", this.bu.split('_')[0]); } let request if (this.pageData.flag === 'item'){ request = uploadExcel(formData) } else if (this.pageData.flag === 'method'){ request = uploadMethodExcel(formData) } else if (this.pageData.flag === 'template'){ request = uploadTemplateExcel(formData) } else if (this.pageData.flag === 'templateItem'){ request = uploadTemplateItemExcel(formData) } else if (this.pageData.flag === 'partAttr'){ request = uploadPartAttrExcel(formData) } else if (this.pageData.flag === 'partTemplate'){ request = uploadPartTemplateExcel(formData) } else if (this.pageData.flag === 'partStandard'){ request = uploadPartStandardExcel(formData) } else if (this.pageData.flag === 'partAttribute'){ request = uploadPartAttributeExcel(formData) } else if (this.pageData.flag === 'familyAttribute'){ request = uploadFamilyAttributeExcel(formData) } else { this.$message.warning('不支持的导入类型') return false } this.saveLoading = true request.then(({data}) => { this.saveLoading = false this.handleImportResult(data) }).catch(() => { this.saveLoading = false this.$message.error('导入失败,请检查文件格式和内容') }) }, bindDownloadBlob(data, fileName) { const blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}) const linkNode = document.createElement('a') linkNode.download = fileName linkNode.style.display = 'none' linkNode.href = URL.createObjectURL(blob) document.body.appendChild(linkNode) linkNode.click() URL.revokeObjectURL(linkNode.href) document.body.removeChild(linkNode) }, async downloadFile(){ const dynamicFlags = ['item', 'template', 'templateItem', 'method', 'partAttr', 'partTemplate', 'partStandard'] if (dynamicFlags.indexOf(this.pageData.flag) !== -1) { const fileNameMap = { item: '检验项目导入模板.xlsx', template: '检验模板导入模板.xlsx', templateItem: '模板检验项目导入模板.xlsx', method: '检验方法导入模板.xlsx', partAttr: '物料属性导入模板.xlsx', partTemplate: '物料检验模板导入模板.xlsx', partStandard: '物料检验标准导入模板.xlsx' } if (!this.importSite) { this.$message.warning("请选择工厂") return } await downloadQcImportTemplate(this.pageData.flag, this.importSite).then(({data}) => { this.bindDownloadBlob(data, fileNameMap[this.pageData.flag]) }) return } let file = { id: 0, fileName: '' } let tempData = { orderRef1: '', orderRef2: '' } if (this.pageData.flag === 'partAttribute'){ tempData.orderRef1 = 'qc'; tempData.orderRef2 = 'partAttributeFormat'; } else if (this.pageData.flag === 'familyAttribute'){ tempData.orderRef1 = 'qc'; tempData.orderRef2 = 'familyAttributeFormat'; } else { this.$message.warning('未找到对应的导入模板') return } await queryFileId(tempData).then(({data}) => { if (data && data.code === 0) { file.id = data.data.id file.fileName = data.data.fileName } else { this.$alert(data.msg, '错误', { confirmButtonText: '确定' }) } }) if (!file.id) { return } await downLoadObjectFile(file).then(({data}) => { this.bindDownloadBlob(data, file.fileName) }) }, }}</script><style scoped lang="scss">.upload-toolbar { display: flex; align-items: center; margin-bottom: 10px; .el-button { margin-right: 8px; }}</style>
|