Commit 4ddfb96b by Jaxon

feat(商品上架): 重构表单为多步骤并提供新的业务场景选项

1 parent eb7c4679
export default {
inject: ["formContext"],
props: {
step: String,
order: Number,
},
data() {
return {
_formId: `f_${Math.random()}`,
_loading: false,
};
},
methods: {
async _submitWrapper() {
this._loading = true;
try {
return await this.submit?.();
} finally {
this._loading = false;
}
},
_getLoading() {
return this._loading;
},
},
mounted() {
this.formContext.register(this._formId, {
submit: this._submitWrapper,
validate: this.validate,
getLoading: this._getLoading,
group: this.step,
order: this.order,
});
},
beforeDestroy() {
this.formContext.unregister(this._formId);
},
};
<template>
<div v-show="isActive">
<slot />
</div>
</template>
<script>
export default {
name: "FormStep",
inject: ["formContext"],
props: {
name: {
type: String,
required: true,
},
},
computed: {
isActive() {
return this.formContext.currentStepName === this.name;
},
},
mounted() {
this.formContext.registerStep({
name: this.name,
});
},
};
</script>
<template>
<div>
<slot />
</div>
</template>
<script>
export default {
name: "FormProvider",
provide() {
return {
formContext: this,
};
},
data() {
return {
// 所有 item
registry: {},
// 步骤列表
// { }
steps: [],
// 当前步骤 index
currentStep: 0,
// 汇总数据
formData: {},
};
},
computed: {
currentStepName() {
return this.steps[this.currentStep]?.name;
},
// 全局 loading
globalLoading() {
return Object.values(this.registry).some(i => i.getLoading?.());
},
},
methods: {
/* ================= 注册系统 ================= */
register(id, config) {
this.$set(this.registry, id, config);
},
unregister(id) {
this.$delete(this.registry, id);
},
registerStep(step) {
this.steps.push(step);
},
/* ================= 查询 ================= */
getItems(group) {
return Object.values(this.registry)
.filter(i => !group || i.group === group)
.sort((a, b) => (a.order || 0) - (b.order || 0));
},
stepLoading(stepName) {
return this.getItems(stepName).some(i => i.getLoading?.());
},
/* ================= 校验 ================= */
async validate(group) {
const items = this.getItems(group);
for (let item of items) {
if (item.validate) {
await item.validate();
}
}
},
/* ================= 提交 ================= */
async submit(group) {
const items = this.getItems(group);
const results = [];
for (let item of items) {
if (item.submit) {
const res = await item.submit();
if (res)
results.push(res);
}
}
return results;
},
/* ================= 数据收集 ================= */
async collect(group) {
const results = await this.submit(group);
return results.reduce((acc, cur) => {
if (typeof cur === "object") {
Object.assign(acc, cur);
}
return acc;
}, {});
},
/* ================= Step 控制 ================= */
async next() {
const step = this.steps[this.currentStep];
await this.validate(step.name);
// 收集当前 step 数据
const data = await this.collect(step.name);
Object.assign(this.formData, data);
this.currentStep++;
},
prev() {
if (this.currentStep > 0) {
this.currentStep--;
}
},
async goTo(index) {
// 防止跳步
for (let i = 0; i < index; i++) {
await this.validate(this.steps[i].name);
}
this.currentStep = index;
},
/* ================= 最终提交 ================= */
async submitAll() {
// 1. 校验所有 step
for (let step of this.steps) {
await this.validate(step.name);
const data = await this.collect(step.name);
Object.assign(this.formData, data);
}
return this.formData;
},
},
};
</script>
......@@ -5,21 +5,25 @@
:form-obj="form.data"
:form-rules="form.rules"
is-block
default-width="100%"
default-width="530px"
size="mini"
label-width="150px">
<template #upload>
<el-upload :http-request="handleUpload"
<el-upload :auto-upload="false"
:limit="1"
action="1"
accept=".xls,.xlsx"
:on-change="handleChange"
:file-list="fileList">
:file-list="form.data.fileList">
<template #trigger>
<el-button size="small">
+ 添加附件
</el-button>
</template>
<el-button size="small" @click="handleDownloadTemplate">
<el-button type="text"
style="margin-left:8px"
size="small"
@click="handleDownloadTemplate">
下载模板
</el-button>
......@@ -35,7 +39,7 @@
</template>
<script>
import { mapActions } from "vuex";
import { mapActions, mapState } from "vuex";
import { shelfProductImportModifyFormSchema } from "../../shelfProductImport.data.js";
export default {
......@@ -48,31 +52,43 @@ export default {
form: {
schema: shelfProductImportModifyFormSchema,
rules: {},
data: {},
data: {
sceneType: 1,
shelfType: 1,
fileList: [],
},
},
};
},
computed: {
...mapState({
dowloadList: state => state.d2admin.user.dowloadList,
updateList: state => state.d2admin.user.updateList,
}),
},
mounted() {
},
methods: {
...mapActions("d2admin/user", ["actionSetDownloadList"]),
handleUpload() {},
handleChange(file, fileList) {
// 只保留一个
this.form.data.fileList = fileList.slice(-1);
// 原始文件
this.form.data.file = file.raw;
},
handleDownloadTemplate() {
const params = {
sceneType: this.infoForm.sceneType,
shelfType: this.infoForm.shelfType,
method: "dj.api.create.business.approve.template.export",
};
const { shelfType, sceneType } = this.form.data;
if (!shelfType || !sceneType) {
this.$message.error("请选择上架类型和业务场景");
return;
}
const params = { sceneType, shelfType, method: "dj.api.create.business.approve.template.export" };
const list = [
...this.dowloadList,
{
name: "产品上架表模板",
name: "商品上架表模板",
exportParam: JSON.stringify(params),
status: "waiting",
timeid: new Date().getTime(),
......@@ -80,11 +96,21 @@ export default {
];
this.actionSetDownloadList({ list, type: "dowloadList" });
},
handleSubmit() {
},
},
};
</script>
<style scoped lang="scss">
.el-upload__tip {
line-height: 20px;
margin-top: 4px;
font-weight: 400;
}
::v-deep .el-upload-list__item {
margin-top: 2px;
}
</style>
......@@ -6,18 +6,19 @@ export const shelfProductImportModifyFormSchema = [
name: "sceneType",
type: "select",
optionList: sceneTypeOptions,
optionId: "confId",
optionLabel: "confValue",
optionsName: "sceneCodeList",
optionId: "value",
optionLabel: "label",
},
{
label: "上架类型",
name: "sceneType",
type: "select",
optionList: sceneTypeOptions,
optionId: "confId",
optionLabel: "confValue",
optionsName: "sceneCodeList",
name: "shelfType",
type: "radio",
optionList: [
{ value: 1, label: "单店模式" },
{ value: 2, label: "项目制批量上架" },
],
optionId: "value",
optionLabel: "label",
},
{
label: "流量方信息",
......@@ -29,7 +30,6 @@ export const shelfProductImportModifyFormSchema = [
useSync: true,
syncField: "merchantName",
},
optionsName: "merchantList",
},
{
label: "商品上架表",
......
......@@ -18,6 +18,9 @@ export const statusOptions = [
];
// 商品上架 业务场景
export const sceneTypeOptions = [
{ confId: 1, confValue: "精品升级" },
{ confId: 0, confValue: "会员权益升级" },
{ value: 1, label: "精品升级售前" },
{ value: 2, label: "售前会员权益升级" },
{ value: 3, label: "售后会员权益升级" },
{ value: 4, label: "售前延保" },
{ value: 5, label: "售后延保" },
];
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!