|
|
<template> <div class="customer-css"> <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="600px" class="customer-dialog" @close="deleteFile"> <el-form label-position="top" > <el-row :gutter="16"> <el-col :span="6" > <el-form-item label=" "> <el-button type="primary" @click="downloadFile()">下载文件模板</el-button> </el-form-item> </el-col> <el-col :span="6"> <el-form-item label="BU"> <el-select v-model="pageData.buNo" placeholder="请选择" style="width: 100%"> <el-option v-for = "i in buList" :key = "i.buNo" :label = "i.buDesc" :value = "i.buNo"></el-option> </el-select> </el-form-item> </el-col> <el-col :span="6"> <el-form-item :label="'贸易国'" > <el-input v-model="pageData.salesArea"></el-input> </el-form-item> </el-col> <el-col :span="6"> <el-form-item :label="'运抵国'" > <el-input v-model="pageData.cnative"></el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item :label="''" > <template #label> <span slot="label" style="" class="big-label"> <a href="#" @click="customFlag=true">客户</a> </span> </template> <el-input type="textarea" :rows="2" v-model="pageData.customerName"></el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item :label="''" > <template #label> <span slot="label" style="" class="big-label"> <a href="#" @click="overseasShipperFlag=true">收货单位</a> </span> </template> <el-input type="textarea" :rows="2" v-model="pageData.overseasShipper"></el-input> </el-form-item> </el-col>
<el-col :span="12" style="margin-top: 30px"> <el-form-item :label="''" > <template #label> <span slot="label" style="" class="big-label"> <a href="#" @click="localShipAddressFlag=true">客户地址</a> </span> </template> <el-input type="textarea" :rows="3" v-model="pageData.localShipAddress"></el-input> </el-form-item> </el-col>
<el-col :span="12" style="margin-top: 30px"> <el-form-item :label="''" > <template #label> <span slot="label" style="" class="big-label"> <a href="#" @click="overseasAddressFlag=true">收货单位地址</a> </span> </template> <el-input type="textarea" :rows="3" v-model="pageData.overseasAddress"></el-input> </el-form-item> </el-col> <el-col :span="24" style="margin-top: 60px"> <el-upload class="customer-upload" drag action="javascript:void(0);" ref="uploadFile" :limit="1" accept=".xlsx,.xls" :before-upload="beforeUploadHandle" :on-change="onChange" :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" style="margin-top: 10px"> <el-button type="primary" @click="saveUploadFile">保存</el-button> <el-button type="primary" @click="closeDialog">关闭</el-button> </span> </el-dialog>
<el-dialog title="客户" @close="closeCustomDialog" :visible.sync="customFlag" width="500px" v-drag> <el-form inline="inline" label-position="top" :model="customSearchData" style="margin-left: 7px;margin-top: -5px;"> <el-form-item label="客户名称"> <el-input v-model="customSearchData.ccusname" clearable style="width: 150px"></el-input> </el-form-item> <el-form-item label=" "> <el-button type="primary" style="padding: 3px 12px" @click="getCustomerList()">查询</el-button> </el-form-item> </el-form> <el-table :height="400" :data="customerList" stripe highlight-current-row border @row-dblclick="rowDblclick" style="width: 100%;"> <el-table-column prop="ccusname" header-align="center" align="left" width="350" label="客户名称"> </el-table-column> <el-table-column prop="country" header-align="center" align="left" label="贸易国"> </el-table-column> </el-table> <el-footer style="height:40px;margin-top: 10px;text-align:center"> <el-button @click="customFlag = false">关闭</el-button> </el-footer> </el-dialog>
<el-dialog title="客户地址" @close="localShipAddressFlag = false" :visible.sync="localShipAddressFlag" width="500px" v-drag> <el-table :height="400" :data="customerAddrs" stripe highlight-current-row border @row-dblclick="rowDblclick2" style="width: 100%;"> <el-table-column prop="cDeliverAdd" header-align="center" align="left" label="客户地址"> </el-table-column> </el-table> <el-footer style="height:40px;margin-top: 10px;text-align:center"> <el-button @click="localShipAddressFlag = false">关闭</el-button> </el-footer> </el-dialog>
<el-dialog title="收货单位地址" @close="overseasAddressFlag = false" :visible.sync="overseasAddressFlag" width="500px" v-drag> <el-table :height="400" :data="customerAddrs" stripe highlight-current-row border @row-dblclick="rowDblclick3" style="width: 100%;"> <el-table-column prop="cDeliverAdd" header-align="center" align="left" label="收货单位地址"> </el-table-column> </el-table> <el-footer style="height:40px;margin-top: 10px;text-align:center"> <el-button @click="overseasAddressFlag = false">关闭</el-button> </el-footer> </el-dialog>
<el-dialog title="收货单位" @close="overseasShipperFlag = false" :visible.sync="overseasShipperFlag" width="500px" v-drag> <el-table :height="400" :data="customerAddrs" stripe highlight-current-row border @row-dblclick="rowDblclick4" style="width: 100%;"> <el-table-column prop="cDeliverUnit" header-align="left" align="left" width="350" label="收货单位"> </el-table-column> <el-table-column prop="deliverycountry" header-align="center" align="left" label="运抵国"> </el-table-column> </el-table> <el-footer style="height:40px;margin-top: 10px;text-align:center"> <el-button @click="overseasShipperFlag = false">关闭</el-button> </el-footer> </el-dialog> </div></template>
<script> import {queryFileId} from "@/api/qc/qc.js" import {saveEcssCoDelNotifyByExcel,getCustomerList,getCustomers,getCustomerAdd} from '@/api/ecss/ecss.js' import {downLoadObjectFile} from '@/api/eam/eam_object_list.js' import {getBuList}from '@/api/factory/site.js' export default { name: 'bomComponentUpload', data() { return { buList: [], titleCon: '文件导入', visible: false, fileList: [], pageData: { site: '', buNo: '', createBy: this.$store.state.user.name, customerName:'', localShipAddress:'', overseasShipper:'', overseasAddress:'', cnative:'', salesArea:'', }, customSearchData: {}, customerList : [],//所有客户
customerAddrs : [],//地址
customerPersons : [],//联系人
customerMap:new Map, customFlag:false, localShipAddressFlag:false, overseasShipperFlag:false, overseasAddressFlag:false, cacheKey: '', // 缓存键
cacheTimer: null, // 缓存定时器
} }, watch: { // 深度监听pageData的变化,自动保存到缓存
pageData: { handler: function(newVal, oldVal) { // 只有在组件已初始化且对话框可见时才保存缓存
if (this.visible && this.cacheKey) { this.saveToCache(); } }, deep: true } }, beforeDestroy() { // 组件销毁前清理定时器
if (this.cacheTimer) { clearTimeout(this.cacheTimer); } }, methods: { // 生成缓存键
generateCacheKey() { const userId = this.$store.state.user.id || this.$store.state.user.name; return `ecss_del_upload_cache_${userId}`; },
// 保存数据到缓存(防抖处理)
saveToCache() { // 清除之前的定时器
if (this.cacheTimer) { clearTimeout(this.cacheTimer); }
// 设置新的定时器,500ms后执行保存
this.cacheTimer = setTimeout(() => { try { const cacheData = { pageData: { buNo: this.pageData.buNo, customerName: this.pageData.customerName, localShipAddress: this.pageData.localShipAddress, overseasShipper: this.pageData.overseasShipper, overseasAddress: this.pageData.overseasAddress, cnative: this.pageData.cnative, salesArea: this.pageData.salesArea, } }; localStorage.setItem(this.cacheKey, JSON.stringify(cacheData)); } catch (error) { console.warn('保存缓存失败:', error); } }, 500); },
// 从缓存加载数据
loadFromCache() { try { const cachedData = localStorage.getItem(this.cacheKey); if (cachedData) { const parsedData = JSON.parse(cachedData); if (parsedData.pageData) { // 恢复缓存的数据(不包括文件)
Object.assign(this.pageData, parsedData.pageData); return true; } } } catch (error) { console.warn('加载缓存失败:', error); } return false; },
// 清除缓存
clearCache() { try { localStorage.removeItem(this.cacheKey); } catch (error) { console.warn('清除缓存失败:', error); } },
// 初始化组件的参数
init () { this.fileList = [] this.cacheKey = this.generateCacheKey();
let tempData = { username: this.$store.state.user.name, } getBuList(tempData).then(({data}) => { if (data.code === 0) { this.buList = data.row2 if(data.row2.length===1){ this.pageData.buNo=data.row2[0].buNo } } // 在获取BU列表后尝试加载缓存
this.loadFromCache(); })
getCustomerList({}).then(({data}) => { //区分请求成功和失败的状况
if (data && data.code === 0) { this.customerList=data.rows } });
// 如果没有缓存数据,则初始化为空
if (!this.loadFromCache()) { this.pageData.customerName='' this.pageData.cnative='' this.pageData.localShipAddress='', this.pageData.overseasShipper='', this.pageData.overseasAddress='', this.pageData.salesArea='' }
this.customerPersons=[] this.customerAddrs=[] // 打开页面
this.visible = true }, getCustomerList(){ getCustomerList(this.customSearchData).then(({data}) => { //区分请求成功和失败的状况
if (data && data.code === 0) { this.customerList=data.rows } }); }, getCusPersons(){ let cusData = {ccusname: this.pageData.customerName} getCustomers(cusData).then(({data}) => { //区分请求成功和失败的状况
if (data && data.code === 0) { this.customerPersons=data.rows this.customerPersons.forEach(o => { if (!this.customerMap.has(o.ccontactname)) { this.customerMap.set(o.ccontactname, o.cnative); } }); } }); getCustomerAdd(cusData).then(({data}) => { //区分请求成功和失败的状况
if (data && data.code === 0) { this.customerAddrs=data.rows } }); }, setCnative(){ this.pageData.cnative=this.customerMap.get(this.pageData.overseasShipper) }, closeCustomDialog () { this.customFlag = false }, rowDblclick (row) { this.pageData.customerName=row.ccusname this.pageData.cnative='', this.pageData.localShipAddress='', this.pageData.overseasShipper='', this.pageData.overseasAddress='', this.pageData.salesArea = row.country this.getCusPersons() this.customFlag = false }, rowDblclick2 (row) { this.pageData.localShipAddress=row.cDeliverAdd this.localShipAddressFlag = false }, rowDblclick3 (row) { this.pageData.overseasAddress=row.cDeliverAdd this.overseasAddressFlag = false }, rowDblclick4 (row) { this.pageData.overseasShipper=row.cDeliverUnit this.pageData.cnative=row.deliverycountry this.overseasShipperFlag = false },
// 上传之前
beforeUploadHandle (file) { let extName = file[0].name.substring(file[0].name.lastIndexOf('.')).toLowerCase() if (!(extName === '.xlsx' || extName === '.xls')) { this.$message.error('数据导入失败,请选择正确的xlsx模板文件') return false } },
// 选择上传文件时
onChange (file) { this.fileList.push(file) },
// 关闭modal
closeDialog () { this.deleteFile() // 关闭当前的页面
this.visible = false // 注意:这里不清除缓存,让数据保留到下次打开
}, deleteFile(){ this.fileList = [] // 清空文件上传记录
this.$refs.uploadFile.clearFiles() // 刷新报工的页面
this.$emit('refreshTable')
},
// 保修当前的数据
saveUploadFile () { if (null == this.pageData.buNo || this.pageData.buNo=='') { this.$message.error("请先选择BU!") return false } if (null == this.pageData.customerName || this.pageData.customerName=='') { this.$message.error("请先选择客户!") return false } if (null == this.pageData.localShipAddress || this.pageData.localShipAddress=='') { this.$message.error("请先填写客户发货地址!") return false } if (null == this.pageData.overseasShipper || this.pageData.overseasShipper=='') { this.$message.error("请先填写收货单位!") return false } if (null == this.pageData.overseasAddress || this.pageData.overseasAddress=='') { this.$message.error("请先填写收货单位地址!") return false } if (null == this.pageData.cnative || this.pageData.cnative=='') { this.$message.error("请先填写收货单位运抵国!") return false } // 判断文件是否上传
if (null == this.fileList || 0 === this.fileList.length) { this.$message.error("请先上传文件!") return false } const formData = new FormData() formData.append("buNo",this.pageData.buNo) formData.append("username",this.$store.state.user.name) formData.append("file", this.fileList[0].raw) formData.append("customerName", this.pageData.customerName) formData.append("localShipAddress", this.pageData.localShipAddress) formData.append("overseasShipper", this.pageData.overseasShipper) formData.append("overseasAddress", this.pageData.overseasAddress) formData.append("cnative", this.pageData.cnative) formData.append("salesArea", this.pageData.salesArea) saveEcssCoDelNotifyByExcel(formData).then(({ data }) => { if (data.code === 0) { const { resultMap } = data; const successList = resultMap.success || []; const failList = resultMap.fail || [];
// 紧凑样式
let html = `
<div style="max-height:380px;overflow:auto;font-size:12px;line-height:1.4;"> `;
if (successList.length > 0) { html += `<div style="margin-bottom:6px;">
<div style="color:green;font-weight:bold;margin-bottom:3px;"> ✅ 成功(${successList.length}) </div> <table border="1" cellspacing="0" cellpadding="2" style="border-collapse:collapse;width:100%;"> `;
successList.forEach(item => { html += `<tr><td style="color:green;padding:2px 4px;">${item}</td></tr>`; }); html += `</table></div>`; }
if (failList.length > 0) { html += `<div>
<div style="color:red;font-weight:bold;margin-bottom:3px;"> ❌ 失败(${failList.length}) </div> <table border="1" cellspacing="0" cellpadding="2" style="border-collapse:collapse;width:100%;"> `;
failList.forEach(item => { html += `<tr><td style="color:red;padding:2px 4px;">${item}</td></tr>`; }); html += `</table></div>`; }
html += `</div>`;
this.$alert(html, '导入结果', { confirmButtonText: '确定', dangerouslyUseHTMLString: true, callback: () => { this.clearCache(); this.closeDialog(); } }); } else { this.$alert(data.msg, '错误', { confirmButtonText: '确定' }); } });
},
// 下载
async downloadFile () { let file = { id: 0, fileName: '' } let tempData = { orderRef1: 'ecss', orderRef2: 'upLoadDel' } 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: '确定' }) } })
await downLoadObjectFile(file).then(({data}) => { // 不限制文件下载类型
const blob = new Blob([data], {type: "application/octet-stream"}) // 下载文件名称
const fileName = file.fileName // a标签下载
const linkNode = document.createElement('a') // a标签的download属性规定下载文件的名称
linkNode.download = fileName linkNode.style.display = 'none' // 生成一个Blob URL
linkNode.href = URL.createObjectURL(blob) document.body.appendChild(linkNode) // 模拟在按钮上的一次鼠标单击
linkNode.click() // 释放URL 对象
URL.revokeObjectURL(linkNode.href) document.body.removeChild(linkNode) }) }, } }</script><style scoped>/deep/ .customer-upload .el-upload .el-upload-dragger { width: 580px;}</style>
|