feat: 物料、单位导入数据、导出模板功能

This commit is contained in:
2025-07-29 17:32:34 +08:00
parent a76c7657f5
commit 2de0b7149b
15 changed files with 447 additions and 18 deletions

View File

@@ -28,7 +28,10 @@
将文件拖到此处
<em>点击上传</em>
</div>
<div slot="tip" class="el-upload__tip">只能上传Excel文件且不超过10MB</div>
<div slot="tip" class="el-upload__tip">
只能上传Excel文件且不超过10MB
<el-button type="text" @click="downloadImportTemplate">下载模板</el-button>
</div>
</el-upload>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false"> </el-button>
@@ -96,6 +99,35 @@ export default {
uploadByJsqd(file) {
this.file1 = file
},
downloadImportTemplate() {
crudMaterial.downloadImportMaterialTemplate().then(res => {
// 1. 创建Blob对象
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// 2. 创建临时下载链接
const downloadUrl = window.URL.createObjectURL(blob);
// 3. 创建隐藏的<a>标签
const link = document.createElement('a');
link.href = downloadUrl;
// 设置下载文件名(根据后端返回或固定名称)
link.download = '物料导入模板.xlsx';
// 4. 添加到DOM并触发点击
document.body.appendChild(link);
link.click();
// 5. 清理资源
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
}).catch(error => {
console.error('下载失败:', error);
// 这里可以添加错误提示如ElementUI的Message
this.$message.error('模板下载失败,请重试');
})
},
submit() {
if (this.beforeAvatarUpload(this.file1)) {
this.fileList.name = this.file1.name

View File

@@ -62,4 +62,12 @@ export function excelImport(data) {
data
})
}
export default { add, edit, del, excelImport, getMaterOptType, isAlongMaterType, getProductSeries, materialSync }
export function downloadImportMaterialTemplate(data) {
return request({
url: 'api/Materia/downloadImportMaterialTemplate',
method: 'get',
responseType: 'blob'
})
}
export default { add, edit, del, excelImport, getMaterOptType, isAlongMaterType, getProductSeries, materialSync, downloadImportMaterialTemplate }

View File

@@ -0,0 +1,157 @@
<template>
<el-dialog
title="导入Excel文件"
append-to-body
:visible.sync="dialogVisible"
destroy-on-close
width="400px"
:show-close="true"
@close="close"
@open="open"
>
<el-upload
ref="upload"
class="upload-demo"
action=""
drag
:on-exceed="is_one"
:limit="1"
:auto-upload="false"
:multiple="false"
:show-file-list="true"
:on-change="uploadByJsqd"
:file-list="fileList"
accept=".xlsx,.xls"
>
<i class="el-icon-upload" />
<div class="el-upload__text">
将文件拖到此处
<em>点击上传</em>
</div>
<div slot="tip" class="el-upload__tip">
只能上传Excel文件且不超过10MB
<el-button type="text" @click="downloadImportTemplate">下载模板</el-button>
</div>
</el-upload>
<span slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false"> </el-button>
<el-button type="primary" @click="submit"> </el-button>
</span>
</el-dialog>
</template>
<script>
import crudmeasure from './mdPbMeasureunit'
import CRUD, { crud } from '@crud/crud'
import { download2 } from '@/api/data'
import { downloadFile } from '@/utils'
export default {
name: 'UploadDialog',
mixins: [crud()],
components: {},
props: {
dialogShow: {
type: Boolean,
default: false
},
openParam: {
type: String
}
},
data() {
return {
dialogVisible: false,
fileList: [],
file1: ''
}
},
watch: {
dialogShow: {
handler(newValue, oldValue) {
this.dialogVisible = newValue
}
},
openParam: {
handler(newValue, oldValue) {
this.opendtlParam = newValue
}
}
},
methods: {
open() {
},
close() {
this.$emit('update:dialogShow', false)
},
is_one() {
this.crud.notify('只能上传一个excel文件', CRUD.NOTIFICATION_TYPE.WARNING)
},
// 文件校验方法
beforeAvatarUpload(file) {
// 不能导入大小超过2Mb的文件
if (file.size > 10 * 1024 * 1024) {
return false
}
return true
},
// 文件发生改变就会触发的事件
uploadByJsqd(file) {
this.file1 = file
},
downloadImportTemplate() {
crudmeasure.downloadImportUnitTemplate().then(res => {
// 1. 创建Blob对象
const blob = new Blob([res], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
// 2. 创建临时下载链接
const downloadUrl = window.URL.createObjectURL(blob);
// 3. 创建隐藏的<a>标签
const link = document.createElement('a');
link.href = downloadUrl;
// 设置下载文件名(根据后端返回或固定名称)
link.download = '单位导入模板.xlsx';
// 4. 添加到DOM并触发点击
document.body.appendChild(link);
link.click();
// 5. 清理资源
document.body.removeChild(link);
window.URL.revokeObjectURL(downloadUrl);
}).catch(error => {
console.error('下载失败:', error);
// 这里可以添加错误提示如ElementUI的Message
this.$message.error('模板下载失败,请重试');
})
},
submit() {
if (this.beforeAvatarUpload(this.file1)) {
this.fileList.name = this.file1.name
this.fileList.url = ''
var formdata = new FormData()
formdata.append('file', this.file1.raw)
// excelImport请求接口 formdata传递参数
crudmeasure.excelImport(formdata).then((res) => {
this.crud.notify('导入成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
this.$emit('tableChanged3', '')
this.$emit('update:dialogShow', false)
}).catch(err => {
const list = err.response.data.message
download2('/api/produceWorkorder/download', list).then(result => {
downloadFile(result, '错误信息汇总', 'xlsx')
crud.downloadLoading = false
})
})
} else {
this.crud.notify('文件过大请上传小于10MB的文件〜', CRUD.NOTIFICATION_TYPE.WARNING)
}
}
}
}
</script>

View File

@@ -13,7 +13,18 @@
/>
<rrOperation />
<!--如果想在工具栏加入更多按钮可以使用插槽方式 slot = 'left' or 'right'-->
<crudOperation :permission="permission" />
<crudOperation :permission="permission">
<el-button
slot="right"
class="filter-item"
type="warning"
icon="el-icon-upload2"
size="mini"
@click="uploadShow = true"
>
导入
</el-button>
</crudOperation>
<!--表单组件-->
<el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
<el-form ref="form" :model="form" :rules="rules" size="mini" label-width="80px">
@@ -70,6 +81,7 @@
<!--分页组件-->
<pagination />
</div>
<upload-dialog :dialog-show.sync="uploadShow"/>
</div>
</template>
@@ -80,11 +92,12 @@ import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import UploadDialog from './UploadDialog.vue'
const defaultForm = { measure_unit_id: null, unit_code: null, unit_name: null, qty_precision: null, is_used: '1', create_id: null, create_name: null, create_time: null, update_optid: null, update_optname: null, update_time: null, is_delete: null, ext_id: null }
export default {
dicts: ['is_used'],
name: 'MdPbMeasureunit',
components: { pagination, crudOperation, rrOperation, udOperation },
components: { UploadDialog, pagination, crudOperation, rrOperation, udOperation },
mixins: [presenter(), header(), form(defaultForm), crud()],
cruds() {
return CRUD({ title: '计量单位',
@@ -101,6 +114,7 @@ export default {
return {
permission: {
},
uploadShow: false,
rules: {
measure_unit_id: [
{ required: true, message: '不能为空', trigger: 'blur' }

View File

@@ -32,4 +32,20 @@ export function getUnit(params) {
})
}
export default { add, edit, del, getUnit }
export function excelImport(data) {
return request({
url: 'api/mdPbMeasureunit/importExcel',
method: 'post',
data
})
}
export function downloadImportUnitTemplate(data) {
return request({
url: 'api/mdPbMeasureunit/downloadImportUnitTemplate',
method: 'get',
responseType: 'blob'
})
}
export default { add, edit, del, getUnit, excelImport, downloadImportUnitTemplate }