Skip to content

Commit 538d1e0

Browse files
author
puhui999
committed
商品管理: fix:根据商品属性动态生成表格值
1 parent 7a64eb5 commit 538d1e0

File tree

5 files changed

+140
-36
lines changed

5 files changed

+140
-36
lines changed

src/api/mall/product/management/type/spuType.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export interface SpuType {
99
sliderPicUrls?: string[] // 商品轮播图
1010
introduction?: string // 商品简介
1111
deliveryTemplateId?: number // 运费模版
12-
selectRule?: string // 选择规格 TODO 暂时定义
1312
specType?: boolean // 商品规格
1413
subCommissionType?: boolean // 分销类型
1514
skus?: SkuType[] // sku数组

src/views/mall/product/management/components/BasicInfoForm.vue

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
</el-col>
6161
<el-col :span="12">
6262
<el-form-item label="商品规格" props="specType">
63-
<el-radio-group v-model="formData.specType" @change="changeSpecType(formData.specType)">
63+
<el-radio-group v-model="formData.specType">
6464
<el-radio :label="false" class="radio">单规格</el-radio>
6565
<el-radio :label="true">多规格</el-radio>
6666
</el-radio-group>
@@ -76,12 +76,17 @@
7676
</el-col>
7777
<!-- 多规格添加-->
7878
<el-col :span="24">
79-
<el-form-item v-if="formData.specType" label="商品属性" prop="">
80-
<el-button class="mr-15px" @click="AttributesAddFormRef.open()">添加规格</el-button>
79+
<el-form-item v-if="formData.specType" label="商品属性">
80+
<el-button class="mr-15px mb-10px" @click="AttributesAddFormRef.open()"
81+
>添加规格
82+
</el-button>
8183
<ProductAttributes :attribute-data="attributeList" />
8284
</el-form-item>
85+
<el-form-item v-if="formData.specType" label="批量设置">
86+
<SkuList :attributeList="attributeList" :is-batch="true" :prop-form-data="formData" />
87+
</el-form-item>
8388
<el-form-item>
84-
<SkuList :sku-data="formData.skus" :subCommissionType="formData.subCommissionType" />
89+
<SkuList :attributeList="attributeList" :prop-form-data="formData" />
8590
</el-form-item>
8691
</el-col>
8792
</el-row>
@@ -110,14 +115,8 @@ const props = defineProps({
110115
})
111116
const AttributesAddFormRef = ref() // 添加商品属性表单
112117
const ProductManagementBasicInfoRef = ref() // 表单Ref
113-
// 属性列表
114-
const attributeList = ref([
115-
{
116-
id: 1,
117-
name: '颜色',
118-
values: [{ id: 1, name: '白色' }]
119-
}
120-
])
118+
const attributeList = ref([]) // 商品属性列表
119+
/** 添加商品属性 */
121120
const addAttribute = (property: any) => {
122121
attributeList.value.push(property)
123122
}
@@ -176,10 +175,10 @@ const rules = reactive({
176175
unit: [required],
177176
introduction: [required],
178177
picUrl: [required],
179-
sliderPicUrls: [required]
178+
sliderPicUrls: [required],
180179
// deliveryTemplateId: [required],
181-
// specType: [required],
182-
// subCommissionType: [required],
180+
specType: [required],
181+
subCommissionType: [required]
183182
})
184183
/**
185184
* 将传进来的值赋值给formData
@@ -215,10 +214,7 @@ const validate = async () => {
215214
})
216215
}
217216
defineExpose({ validate })
218-
// 选择规格
219-
const changeSpecType = (specType) => {
220-
console.log(specType)
221-
}
217+
222218
// 分销类型
223219
const changeSubCommissionType = () => {
224220
// 默认为零,类型切换后也要重置为零
@@ -227,10 +223,7 @@ const changeSubCommissionType = () => {
227223
item.subCommissionSecondPrice = 0
228224
}
229225
}
230-
// 选择属性确认
231-
// const confirm = () => {}
232-
// 添加规格
233-
// const addRule = () => {}
226+
234227
const categoryList = ref() // 分类树
235228
onMounted(async () => {
236229
// 获得分类树

src/views/mall/product/management/components/ProductAttributes.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ const inputVisible = computed(() => (index) => {
4949
if (attributeIndex.value === index) return true
5050
})
5151
const InputRef = ref() //标签输入框Ref
52-
const attributeList = ref([])
52+
const attributeList = ref([]) // 商品属性列表
5353
const props = defineProps({
5454
attributeData: {
55-
type: Object,
55+
type: Array,
5656
default: () => {}
5757
}
5858
})

src/views/mall/product/management/components/ProductAttributesAddForm.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ const submitForm = async () => {
6262
const propertyId = await PropertyApi.createProperty(data)
6363
emit('success', { id: propertyId, ...formData.value, values: [] })
6464
} else {
65+
if (res[0].values === null) {
66+
res[0].values = []
67+
}
6568
emit('success', res[0]) // 因为只用一个
6669
}
6770
message.success(t('common.createSuccess'))

src/views/mall/product/management/components/SkuList/index.vue

Lines changed: 119 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
<template>
2-
<el-table :data="SkuData" border class="tabNumWidth" size="small">
2+
<el-table :data="isBatch ? SkuData : formData.skus" border class="tabNumWidth" size="small">
33
<el-table-column align="center" fixed="left" label="图片" min-width="100">
44
<template #default="{ row }">
55
<UploadImg v-model="row.picUrl" height="80px" width="100%" />
66
</template>
77
</el-table-column>
8+
<template v-if="formData.specType">
9+
<!-- 根据商品属性动态添加 -->
10+
<el-table-column
11+
v-for="(item, index) in tableHeaderList"
12+
:key="index"
13+
:label="item.label"
14+
:prop="item.prop"
15+
align="center"
16+
min-width="120"
17+
/>
18+
</template>
819
<el-table-column align="center" label="商品条码" min-width="120">
920
<template #default="{ row }">
1021
<el-input v-model="row.barCode" :min="0" class="w-100%" />
@@ -40,7 +51,7 @@
4051
<el-input v-model="row.volume" :min="0" class="w-100%" type="number" />
4152
</template>
4253
</el-table-column>
43-
<template v-if="subCommissionType">
54+
<template v-if="formData.subCommissionType">
4455
<el-table-column align="center" label="一级返佣(分)" min-width="120">
4556
<template #default="{ row }">
4657
<el-input v-model="row.subCommissionFirstPrice" :min="0" class="w-100%" type="number" />
@@ -52,35 +63,133 @@
5263
</template>
5364
</el-table-column>
5465
</template>
66+
<el-table-column v-if="formData.specType" align="center" fixed="right" label="操作" width="80">
67+
<template #default>
68+
<el-button v-if="isBatch" link size="small" type="primary">批量添加</el-button>
69+
<el-button v-else link size="small" type="primary">删除</el-button>
70+
</template>
71+
</el-table-column>
5572
</el-table>
5673
</template>
5774

5875
<script lang="ts" name="index" setup>
59-
import { propTypes } from '@/utils/propTypes'
6076
import { UploadImg } from '@/components/UploadFile'
6177
import { PropType } from 'vue'
62-
import type { SkuType } from '@/api/mall/product/management/type/skuType'
78+
import { SpuType } from '@/api/mall/product/management/type/spuType'
79+
import { propTypes } from '@/utils/propTypes'
80+
import { SkuType } from '@/api/mall/product/management/type/skuType'
6381
6482
const props = defineProps({
65-
skuData: {
66-
type: Array as PropType<SkuType>,
83+
propFormData: {
84+
type: Object as PropType<SpuType>,
85+
default: () => {}
86+
},
87+
attributeList: {
88+
type: Array,
6789
default: () => []
6890
},
69-
subCommissionType: propTypes.bool.def(false) // 分销类型
91+
isBatch: propTypes.bool.def(false) // 是否批量操作
7092
})
71-
const SkuData = ref<SkuType[]>([])
93+
const formData = ref<SpuType>() // 表单数据
94+
// 批量添加时的零时数据
95+
const SkuData = ref<SkuType[]>([
96+
{
97+
/**
98+
* 商品价格,单位:分
99+
*/
100+
price: 0,
101+
/**
102+
* 市场价,单位:分
103+
*/
104+
marketPrice: 0,
105+
/**
106+
* 成本价,单位:分
107+
*/
108+
costPrice: 0,
109+
/**
110+
* 商品条码
111+
*/
112+
barCode: '',
113+
/**
114+
* 图片地址
115+
*/
116+
picUrl: '',
117+
/**
118+
* 库存
119+
*/
120+
stock: 0,
121+
/**
122+
* 商品重量,单位:kg 千克
123+
*/
124+
weight: 0,
125+
/**
126+
* 商品体积,单位:m^3 平米
127+
*/
128+
volume: 0
129+
}
130+
])
131+
const tableHeaderList = ref<{ prop: string; label: string }[]>([])
72132
/**
73133
* 将传进来的值赋值给SkuData
74134
*/
75135
watch(
76-
() => props.skuData,
136+
() => props.propFormData,
77137
(data) => {
78138
if (!data) return
79-
SkuData.value = data
139+
formData.value = data
80140
},
81141
{
82142
deep: true,
83143
immediate: true
84144
}
85145
)
146+
/** 监听属性列表生成相关参数和表头 */
147+
watch(
148+
() => props.attributeList,
149+
(data) => {
150+
// 判断代理对象是否为空
151+
if (JSON.stringify(data) === '[]') return
152+
// 重置表头
153+
tableHeaderList.value = []
154+
// 重置表数据
155+
formData.value!.skus = []
156+
SkuData.value = []
157+
// 生成表头
158+
data.forEach((item, index) => {
159+
// name加属性项index区分属性值
160+
tableHeaderList.value.push({ prop: `name${index}`, label: item.name })
161+
})
162+
generateTableData(data)
163+
},
164+
{
165+
deep: true,
166+
immediate: true
167+
}
168+
)
169+
/** 生成表数据 */
170+
const generateTableData = (data: any[]) => {
171+
// const row = {
172+
// price: 0,
173+
// marketPrice: 0,
174+
// costPrice: 0,
175+
// barCode: '',
176+
// picUrl: '',
177+
// stock: 0,
178+
// weight: 0,
179+
// volume: 0
180+
// }
181+
// 先把所有的属性值取出来
182+
const newDataList: any[] = []
183+
for (const index in data) {
184+
newDataList.push(data[index].values)
185+
}
186+
console.log(newDataList)
187+
}
188+
// const buildRow = (list: any[]) => {
189+
// for (const index in data) {
190+
// for (const index1 of data[index].values) {
191+
// row[`name${index1}`] = data[index].values[index1]
192+
// }
193+
// }
194+
// }
86195
</script>

0 commit comments

Comments
 (0)