商品管理: 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[] // 商品轮播图 sliderPicUrls?: string[] // 商品轮播图
introduction?: string // 商品简介 introduction?: string // 商品简介
deliveryTemplateId?: number // 运费模版 deliveryTemplateId?: number // 运费模版
selectRule?: string // 选择规格 TODO 暂时定义
specType?: boolean // 商品规格 specType?: boolean // 商品规格
subCommissionType?: boolean // 分销类型 subCommissionType?: boolean // 分销类型
skus?: SkuType[] // sku数组 skus?: SkuType[] // sku数组

View File

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

View File

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

View File

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

View File

@ -1,10 +1,21 @@
<template> <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"> <el-table-column align="center" fixed="left" label="图片" min-width="100">
<template #default="{ row }"> <template #default="{ row }">
<UploadImg v-model="row.picUrl" height="80px" width="100%" /> <UploadImg v-model="row.picUrl" height="80px" width="100%" />
</template> </template>
</el-table-column> </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"> <el-table-column align="center" label="商品条码" min-width="120">
<template #default="{ row }"> <template #default="{ row }">
<el-input v-model="row.barCode" :min="0" class="w-100%" /> <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" /> <el-input v-model="row.volume" :min="0" class="w-100%" type="number" />
</template> </template>
</el-table-column> </el-table-column>
<template v-if="subCommissionType"> <template v-if="formData.subCommissionType">
<el-table-column align="center" label="一级返佣(分)" min-width="120"> <el-table-column align="center" label="一级返佣(分)" min-width="120">
<template #default="{ row }"> <template #default="{ row }">
<el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" /> <el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" />
@ -52,35 +63,133 @@
</template> </template>
</el-table-column> </el-table-column>
</template> </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> </el-table>
</template> </template>
<script lang="ts" name="index" setup> <script lang="ts" name="index" setup>
import { propTypes } from '@/utils/propTypes'
import { UploadImg } from '@/components/UploadFile' import { UploadImg } from '@/components/UploadFile'
import { PropType } from 'vue' 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({ const props = defineProps({
skuData: { propFormData: {
type: Array as PropType<SkuType>, type: Object as PropType<SpuType>,
default: () => {}
},
attributeList: {
type: Array,
default: () => [] 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 * 将传进来的值赋值给SkuData
*/ */
watch( watch(
() => props.skuData, () => props.propFormData,
(data) => { (data) => {
if (!data) return if (!data) return
SkuData.value = data formData.value = data
}, },
{ {
deep: true, deep: true,
immediate: 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> </script>