商品管理: fix:根据商品属性动态生成表格值

This commit is contained in:
puhui999 2023-04-30 02:26:35 +08:00
parent 7a64eb5198
commit 538d1e0b6c
5 changed files with 140 additions and 36 deletions

View File

@ -9,7 +9,6 @@ export interface SpuType {
sliderPicUrls?: string[] // 商品轮播图
introduction?: string // 商品简介
deliveryTemplateId?: number // 运费模版
selectRule?: string // 选择规格 TODO 暂时定义
specType?: boolean // 商品规格
subCommissionType?: boolean // 分销类型
skus?: SkuType[] // sku数组

View File

@ -60,7 +60,7 @@
</el-col>
<el-col :span="12">
<el-form-item label="商品规格" props="specType">
<el-radio-group v-model="formData.specType" @change="changeSpecType(formData.specType)">
<el-radio-group v-model="formData.specType">
<el-radio :label="false" class="radio">单规格</el-radio>
<el-radio :label="true">多规格</el-radio>
</el-radio-group>
@ -76,12 +76,17 @@
</el-col>
<!-- 多规格添加-->
<el-col :span="24">
<el-form-item v-if="formData.specType" label="商品属性" prop="">
<el-button class="mr-15px" @click="AttributesAddFormRef.open()">添加规格</el-button>
<el-form-item v-if="formData.specType" label="商品属性">
<el-button class="mr-15px mb-10px" @click="AttributesAddFormRef.open()"
>添加规格
</el-button>
<ProductAttributes :attribute-data="attributeList" />
</el-form-item>
<el-form-item v-if="formData.specType" label="批量设置">
<SkuList :attributeList="attributeList" :is-batch="true" :prop-form-data="formData" />
</el-form-item>
<el-form-item>
<SkuList :sku-data="formData.skus" :subCommissionType="formData.subCommissionType" />
<SkuList :attributeList="attributeList" :prop-form-data="formData" />
</el-form-item>
</el-col>
</el-row>
@ -110,14 +115,8 @@ const props = defineProps({
})
const AttributesAddFormRef = ref() //
const ProductManagementBasicInfoRef = ref() // Ref
//
const attributeList = ref([
{
id: 1,
name: '颜色',
values: [{ id: 1, name: '白色' }]
}
])
const attributeList = ref([]) //
/** 添加商品属性 */
const addAttribute = (property: any) => {
attributeList.value.push(property)
}
@ -176,10 +175,10 @@ const rules = reactive({
unit: [required],
introduction: [required],
picUrl: [required],
sliderPicUrls: [required]
sliderPicUrls: [required],
// deliveryTemplateId: [required],
// specType: [required],
// subCommissionType: [required],
specType: [required],
subCommissionType: [required]
})
/**
* 将传进来的值赋值给formData
@ -215,10 +214,7 @@ const validate = async () => {
})
}
defineExpose({ validate })
//
const changeSpecType = (specType) => {
console.log(specType)
}
//
const changeSubCommissionType = () => {
//
@ -227,10 +223,7 @@ const changeSubCommissionType = () => {
item.subCommissionSecondPrice = 0
}
}
//
// const confirm = () => {}
//
// const addRule = () => {}
const categoryList = ref() //
onMounted(async () => {
//

View File

@ -49,10 +49,10 @@ const inputVisible = computed(() => (index) => {
if (attributeIndex.value === index) return true
})
const InputRef = ref() //Ref
const attributeList = ref([])
const attributeList = ref([]) //
const props = defineProps({
attributeData: {
type: Object,
type: Array,
default: () => {}
}
})

View File

@ -62,6 +62,9 @@ const submitForm = async () => {
const propertyId = await PropertyApi.createProperty(data)
emit('success', { id: propertyId, ...formData.value, values: [] })
} else {
if (res[0].values === null) {
res[0].values = []
}
emit('success', res[0]) //
}
message.success(t('common.createSuccess'))

View File

@ -1,10 +1,21 @@
<template>
<el-table :data="SkuData" border class="tabNumWidth" size="small">
<el-table :data="isBatch ? SkuData : formData.skus" border class="tabNumWidth" size="small">
<el-table-column align="center" fixed="left" label="图片" min-width="100">
<template #default="{ row }">
<UploadImg v-model="row.picUrl" height="80px" width="100%" />
</template>
</el-table-column>
<template v-if="formData.specType">
<!-- 根据商品属性动态添加 -->
<el-table-column
v-for="(item, index) in tableHeaderList"
:key="index"
:label="item.label"
:prop="item.prop"
align="center"
min-width="120"
/>
</template>
<el-table-column align="center" label="商品条码" min-width="120">
<template #default="{ row }">
<el-input v-model="row.barCode" :min="0" class="w-100%" />
@ -40,7 +51,7 @@
<el-input v-model="row.volume" :min="0" class="w-100%" type="number" />
</template>
</el-table-column>
<template v-if="subCommissionType">
<template v-if="formData.subCommissionType">
<el-table-column align="center" label="一级返佣(分)" min-width="120">
<template #default="{ row }">
<el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" />
@ -52,35 +63,133 @@
</template>
</el-table-column>
</template>
<el-table-column v-if="formData.specType" align="center" fixed="right" label="操作" width="80">
<template #default>
<el-button v-if="isBatch" link size="small" type="primary">批量添加</el-button>
<el-button v-else link size="small" type="primary">删除</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script lang="ts" name="index" setup>
import { propTypes } from '@/utils/propTypes'
import { UploadImg } from '@/components/UploadFile'
import { PropType } from 'vue'
import type { SkuType } from '@/api/mall/product/management/type/skuType'
import { SpuType } from '@/api/mall/product/management/type/spuType'
import { propTypes } from '@/utils/propTypes'
import { SkuType } from '@/api/mall/product/management/type/skuType'
const props = defineProps({
skuData: {
type: Array as PropType<SkuType>,
propFormData: {
type: Object as PropType<SpuType>,
default: () => {}
},
attributeList: {
type: Array,
default: () => []
},
subCommissionType: propTypes.bool.def(false) //
isBatch: propTypes.bool.def(false) //
})
const SkuData = ref<SkuType[]>([])
const formData = ref<SpuType>() //
//
const SkuData = ref<SkuType[]>([
{
/**
* 商品价格单位
*/
price: 0,
/**
* 市场价单位
*/
marketPrice: 0,
/**
* 成本价单位
*/
costPrice: 0,
/**
* 商品条码
*/
barCode: '',
/**
* 图片地址
*/
picUrl: '',
/**
* 库存
*/
stock: 0,
/**
* 商品重量单位kg 千克
*/
weight: 0,
/**
* 商品体积单位m^3 平米
*/
volume: 0
}
])
const tableHeaderList = ref<{ prop: string; label: string }[]>([])
/**
* 将传进来的值赋值给SkuData
*/
watch(
() => props.skuData,
() => props.propFormData,
(data) => {
if (!data) return
SkuData.value = data
formData.value = data
},
{
deep: true,
immediate: true
}
)
/** 监听属性列表生成相关参数和表头 */
watch(
() => props.attributeList,
(data) => {
//
if (JSON.stringify(data) === '[]') return
//
tableHeaderList.value = []
//
formData.value!.skus = []
SkuData.value = []
//
data.forEach((item, index) => {
// nameindex
tableHeaderList.value.push({ prop: `name${index}`, label: item.name })
})
generateTableData(data)
},
{
deep: true,
immediate: true
}
)
/** 生成表数据 */
const generateTableData = (data: any[]) => {
// const row = {
// price: 0,
// marketPrice: 0,
// costPrice: 0,
// barCode: '',
// picUrl: '',
// stock: 0,
// weight: 0,
// volume: 0
// }
//
const newDataList: any[] = []
for (const index in data) {
newDataList.push(data[index].values)
}
console.log(newDataList)
}
// const buildRow = (list: any[]) => {
// for (const index in data) {
// for (const index1 of data[index].values) {
// row[`name${index1}`] = data[index].values[index1]
// }
// }
// }
</script>