Commit aa331854 by 张立顺

fix 上传文件改造

1 parent 5e04e419
<!-- 文件导入弹框通用组件 -->
<template>
<d2-dialog :is-visible="isVisible"
:title="title"
@closeDialog="handleClose"
@confirmDialog="handleConfirm">
<el-form ref="fileFormRef"
label-width="120px"
:model="form"
:rules="rules">
<el-form-item label="导入文件:"
prop="fileName">
<el-col :span="16">
<el-input v-model="form.fileName"
readonly />
</el-col>
<el-col class="m-l-md"
:span="4">
<el-button class="import-btn">
导入文件
<input ref="fileInput"
:accept="accept"
class="import-input"
type="file"
@change="onFileChange"
@click="$event.target.value = null">
<el-link class="import-template"
icon="el-icon-link"
type="primary"
@click="$emit('downloadTemplate')">
{{ templateName }}
</el-link>
</el-button>
</el-col>
</el-form-item>
</el-form>
</d2-dialog>
</template>
<script>
export default {
name: "D2FileImportDialog",
props: {
isVisible: {
type: Boolean,
default: false,
},
title: {
type: String,
default: "批量导入",
},
accept: {
type: String,
default: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel",
},
maxSize: {
type: Number,
default: 9,
},
templateName: {
type: String,
default: "模版下载",
},
},
data() {
return {
form: { fileName: "" },
fileData: null,
rules: {
fileName: [{ required: true, message: "请上传文件", trigger: "change" }],
},
};
},
watch: {
isVisible(val) {
if (!val) {
this.resetForm();
}
},
},
methods: {
resetForm() {
this.$refs.fileFormRef && this.$refs.fileFormRef.resetFields();
this.fileData = null;
if (this.$refs.fileInput) {
this.$refs.fileInput.value = "";
}
},
handleClose() {
this.resetForm();
this.$emit("closeDialog");
},
handleConfirm() {
this.$refs.fileFormRef.validate((valid) => {
if (valid && this.fileData) {
this.$emit("confirmDialog", {
fileName: this.form.fileName,
fileData: this.fileData,
});
}
});
},
onFileChange(e) {
if (!e.target.files || !e.target.files[0])
return;
const file = e.target.files[0];
const size = file.size / 1024 / 1024;
e.target.value = "";
if (size < this.maxSize) {
this.form.fileName = file.name;
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = (event) => {
const imgFile = event.target.result;
const arrBase64 = imgFile.split(",");
const ext = file.name.split(".").pop();
this.fileData = ext + "@" + arrBase64[1];
};
} else {
this.notice.errorTips(`上传文件不可超过${this.maxSize}M`);
}
},
},
};
</script>
<style scoped>
.m-l-md {
margin-left: 20px;
}
.import-btn {
position: relative;
}
.import-input {
opacity: 0;
position: absolute;
right: 0;
top: 0;
width: 98px;
height: 36px;
cursor: pointer;
font-size: 0;
}
.import-template {
position: absolute;
height: 20px;
top: 50px;
left: 0;
}
</style>
......@@ -19,6 +19,7 @@ Vue.component("D2IconSvg", () => import("./d2-icon-svg/index.vue"))
Vue.component("D2TableHeader", () => import("./d2-table-header/index.vue"))
Vue.component("D2Table", () => import("./d2-table/index.vue"))
Vue.component("D2Dialog", () => import("./d2-dialog/index.vue"))
Vue.component("D2FileImportDialog", () => import("./d2-file-import-dialog/index.vue"))
Vue.component("D2Form", () => import("./d2-form/index.vue"))
Vue.component("D2Search", () => import("./d2-search/index.vue"))
Vue.component("RelateStoreDialog", () =>
......
......@@ -92,79 +92,19 @@
:position-type-list="positionTypeList"
@handleDrawerClose="handleSkuDrawerClose" />
<!-- 轻改SKU批量导入 -->
<d2-dialog :is-visible="fileVisible"
title="批量导入轻改SKU"
@closeDialog="handleBatchImport"
@confirmDialog="handleConfirmImport">
<slot>
<el-form ref="fileRuleForm"
label-width="120px"
:model="fileForm"
:rules="fileRules">
<el-form-item label="导入文件:"
prop="fileName">
<el-col :span="16">
<el-input v-model="fileForm.fileName"
readonly />
</el-col>
<el-col class="m-l-md"
:span="4">
<el-button class="import-btn">
导入文件
<input id="fileUploadQinggai"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
class="import-input"
type="file"
@change="importFile($event, 'qinggai')">
<el-link class="import-template"
icon="el-icon-link"
type="primary"
@click="exportTemplate('qinggai')">
轻改模版下载
</el-link>
</el-button>
</el-col>
</el-form-item>
</el-form>
</slot>
</d2-dialog>
<d2-file-import-dialog :is-visible="fileVisible"
template-name="轻改模版下载"
title="批量导入轻改SKU"
@closeDialog="handleBatchImport"
@confirmDialog="handleConfirmImport"
@downloadTemplate="exportTemplate('qinggai')" />
<!-- 延保SKU批量导入 -->
<d2-dialog :is-visible="fileVisibleExtension"
title="批量导入延保SKU"
@closeDialog="handleBatchImportExtension"
@confirmDialog="handleConfirmImportExtension">
<slot>
<el-form ref="fileRuleFormExtension"
label-width="120px"
:model="fileFormExtension"
:rules="fileRules">
<el-form-item label="导入文件:"
prop="fileName">
<el-col :span="16">
<el-input v-model="fileFormExtension.fileName"
readonly />
</el-col>
<el-col class="m-l-md"
:span="4">
<el-button class="import-btn">
导入文件
<input id="fileUploadYanbao"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
class="import-input"
type="file"
@change="importFile($event, 'yanbao')">
<el-link class="import-template"
icon="el-icon-link"
type="primary"
@click="exportTemplate('yanbao')">
延保模版下载
</el-link>
</el-button>
</el-col>
</el-form-item>
</el-form>
</slot>
</d2-dialog>
<d2-file-import-dialog :is-visible="fileVisibleExtension"
template-name="延保模版下载"
title="批量导入延保SKU"
@closeDialog="handleBatchImportExtension"
@confirmDialog="handleConfirmImportExtension"
@downloadTemplate="exportTemplate('yanbao')" />
</d2-container>
</template>
......@@ -446,15 +386,8 @@ export default {
},
selfBuildGoodsType: baseConfig.selfBuildGoodsType || "", // 服务商自建类目值
positionTypeList: [], // 太阳膜类商品部位
fileRules: {
fileName: [{ required: true, message: "请上传文件", trigger: "change" }],
},
fileForm: {},
fileVisible: false,
fileInfo: null,
fileFormExtension: {},
fileVisibleExtension: false,
fileInfoExtension: null,
// 新 SPU 接口不返回 upgradeGoodsList,此时所有行均为 SPU 级
spuOnlyMode: false,
commodityDetailInfoSkuId: "", // 商品详情SKU ID
......@@ -790,72 +723,37 @@ export default {
},
handleBatchImport() {
this.fileVisible = !this.fileVisible;
this.$refs.fileRuleForm && this.$refs.fileRuleForm.resetFields();
this.fileInfo = null;
},
handleBatchImportExtension() {
this.fileVisibleExtension = !this.fileVisibleExtension;
this.$refs.fileRuleFormExtension && this.$refs.fileRuleFormExtension.resetFields();
this.fileInfoExtension = null;
},
// 上传文件,转码为base64;type 为 qinggai 或 yanbao
importFile(e, type) {
if (!e.target.files || !e.target.files[0])
return;
const size = e.target.files[0].size / 1024 / 1024;
const formKey = type === "qinggai" ? "fileForm" : "fileFormExtension";
const fileInfoKey = type === "qinggai" ? "fileInfo" : "fileInfoExtension";
const inputId = type === "qinggai" ? "fileUploadQinggai" : "fileUploadYanbao";
if (size < 9) {
this.$set(this[formKey], "fileName", e.target.files[0].name);
const reader = new FileReader();
reader.readAsDataURL(e.target.files[0]);
reader.onload = (event) => {
const imgFile = event.target.result;
const arrBase64 = imgFile.split(",");
this[fileInfoKey] = e.target.files[0].name.split(".")[e.target.files[0].name.split(".")?.length - 1] + "@" + arrBase64[1];
document.getElementById(inputId).value = "";
};
} else {
this.notice.errorTips("上传文件不可超过9M");
document.getElementById(inputId).value = "";
}
},
handleConfirmImport() {
this.$refs.fileRuleForm.validate((valid) => {
if (valid) {
qinggaiImport({ file: this.fileInfo }).then((res) => {
if (res) {
const list = [...this.updateList, {
name: this.fileForm.fileName,
ifSuccess: false,
uuid: res,
type: "upload",
}];
this.actionSetDownloadList({ list, type: "updateList" });
this.notice.okTips("上传成功");
this.handleBatchImport();
}
});
handleConfirmImport({ fileName, fileData }) {
qinggaiImport({ file: fileData }).then((res) => {
if (res) {
const list = [...this.updateList, {
name: fileName,
ifSuccess: false,
uuid: res,
type: "upload",
}];
this.actionSetDownloadList({ list, type: "updateList" });
this.notice.okTips("上传成功");
this.handleBatchImport();
}
});
},
handleConfirmImportExtension() {
this.$refs.fileRuleFormExtension.validate((valid) => {
if (valid) {
yanbaoImport({ file: this.fileInfoExtension }).then((res) => {
if (res) {
const list = [...this.updateList, {
name: this.fileFormExtension.fileName,
ifSuccess: false,
uuid: res,
type: "upload",
}];
this.actionSetDownloadList({ list, type: "updateList" });
this.notice.okTips("上传成功");
this.handleBatchImportExtension();
}
});
handleConfirmImportExtension({ fileName, fileData }) {
yanbaoImport({ file: fileData }).then((res) => {
if (res) {
const list = [...this.updateList, {
name: fileName,
ifSuccess: false,
uuid: res,
type: "upload",
}];
this.actionSetDownloadList({ list, type: "updateList" });
this.notice.okTips("上传成功");
this.handleBatchImportExtension();
}
});
},
......@@ -996,26 +894,4 @@ export default {
.product-manage p {
margin: 0;
}
.product-manage .m-l-md {
margin-left: 20px;
}
.product-manage .import-btn {
position: relative;
}
.product-manage .import-input {
opacity: 0;
position: absolute;
right: 0;
top: 0;
width: 98px;
height: 36px;
cursor: pointer;
font-size: 0;
}
.product-manage .import-template {
position: absolute;
height: 20px;
top: 50px;
left: 0;
}
</style>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!