Skip to content

Commit 9ee35fc

Browse files
author
puhui999
committed
商品管理: 调整相关组件优化逻辑,完成表单保存和数据回显
1 parent 1116fb2 commit 9ee35fc

File tree

9 files changed

+112
-37
lines changed

9 files changed

+112
-37
lines changed

src/api/mall/product/management/sku.ts

Whitespace-only changes.

src/api/mall/product/management/spu.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@ export const createSpu = (data: SpuType) => {
1313
export const updateSpu = (data: SpuType) => {
1414
return request.put({ url: '/product/spu/update', data })
1515
}
16+
// 获得商品spu
17+
export const getSpu = (id: number) => {
18+
return request.get({ url: `/product/spu/get-detail?id=${id}` })
19+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ export interface Property {
1111
* 关联 {@link ProductPropertyValueDO#getId()}
1212
*/
1313
valueId?: number
14+
/**
15+
* 属性值名称
16+
*/
17+
valueName?: string
1418
}
1519

1620
export interface SkuType {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { SkuType } from './skuType'
22

33
export interface SpuType {
4+
id?: number
45
name?: string // 商品名称
56
categoryId?: number | null // 商品分类
67
keyword?: string // 关键字
@@ -11,7 +12,7 @@ export interface SpuType {
1112
deliveryTemplateId?: number // 运费模版
1213
specType?: boolean // 商品规格
1314
subCommissionType?: boolean // 分销类型
14-
skus?: SkuType[] // sku数组
15+
skus: SkuType[] // sku数组
1516
description?: string // 商品详情
1617
sort?: string // 商品排序
1718
giveIntegral?: number // 赠送积分

src/api/mall/product/property.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ export const getPropertyList = (params: any) => {
7171
}
7272

7373
// 获得属性项列表
74-
export const getPropertyListAndValue = (params: any) => {
75-
return request.get({ url: '/product/property/get-value-list', params })
74+
export const getPropertyListAndValue = (data: any) => {
75+
return request.post({ url: '/product/property/get-value-list', data })
7676
}
7777

7878
// ------------------------ 属性值 -------------------

src/views/mall/product/management/addForm.vue

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { useTagsViewStore } from '@/store/modules/tagsView'
3636
import { BasicInfoForm, DescriptionForm, OtherSettingsForm } from './components'
3737
import type { SpuType } from '@/api/mall/product/management/type/spuType' // 业务api
3838
import * as managementApi from '@/api/mall/product/management/spu'
39+
import * as PropertyApi from '@/api/mall/product/property'
3940
4041
const { t } = useI18n() // 国际化
4142
const message = useMessage() // 消息弹窗
@@ -122,8 +123,20 @@ const formData = ref<SpuType>({
122123
/** 获得详情 */
123124
const getDetail = async () => {
124125
const id = query.id as unknown as number
125-
if (!id) {
126-
return
126+
if (id) {
127+
formLoading.value = true
128+
try {
129+
const res = (await managementApi.getSpu(id)) as SpuType
130+
formData.value = res
131+
// 直接取第一个值就能得到所有属性的id
132+
const propertyIds = res.skus[0]?.properties.map((item) => item.propertyId)
133+
const PropertyS = await PropertyApi.getPropertyListAndValue({ propertyIds })
134+
await nextTick()
135+
// 回显商品属性
136+
BasicInfoRef.value.addAttribute(PropertyS)
137+
} finally {
138+
formLoading.value = false
139+
}
127140
}
128141
}
129142
@@ -145,7 +158,7 @@ const submitForm = async () => {
145158
// 多规格情况移除skus相关属性值value
146159
if (formData.value.specType) {
147160
item.properties.forEach((item2) => {
148-
delete item2.value
161+
delete item2.valueName
149162
})
150163
}
151164
})

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ const ProductManagementBasicInfoRef = ref() // 表单Ref
131131
const attributeList = ref([]) // 商品属性列表
132132
/** 添加商品属性 */
133133
const addAttribute = (property: any) => {
134+
if (Array.isArray(property)) {
135+
attributeList.value = property
136+
return
137+
}
134138
attributeList.value.push(property)
135139
}
136140
const formData = reactive<SpuType>({
@@ -191,7 +195,7 @@ const validate = async () => {
191195
}
192196
})
193197
}
194-
defineExpose({ validate })
198+
defineExpose({ validate, addAttribute })
195199
196200
// 分销类型
197201
const changeSubCommissionType = () => {
@@ -203,7 +207,6 @@ const changeSubCommissionType = () => {
203207
}
204208
// 选择规格
205209
const onChangeSpec = () => {
206-
console.log(111)
207210
// 重置商品属性列表
208211
attributeList.value = []
209212
// 重置sku列表

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

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
<template>
2-
<el-table :data="isBatch ? SkuData : formData.skus" border class="tabNumWidth" size="small">
2+
<el-table
3+
:data="isBatch ? SkuData : formData.skus"
4+
border
5+
class="tabNumWidth"
6+
max-height="500"
7+
size="small"
8+
>
39
<el-table-column align="center" fixed="left" label="图片" min-width="100">
410
<template #default="{ row }">
511
<UploadImg v-model="row.picUrl" height="80px" width="100%" />
@@ -15,7 +21,7 @@
1521
min-width="120"
1622
>
1723
<template #default="{ row }">
18-
{{ row.properties[index].value }}
24+
{{ row.properties[index]?.valueName }}
1925
</template>
2026
</el-table-column>
2127
</template>
@@ -190,15 +196,28 @@ const generateTableData = (data: any[]) => {
190196
for (const item of data) {
191197
const objList = []
192198
for (const v of item.values) {
193-
const obj = { propertyId: 0, valueId: 0, value: '' }
199+
const obj = { propertyId: 0, valueId: 0, valueName: '' }
194200
obj.propertyId = item.id
195201
obj.valueId = v.id
196-
obj.value = v.name
202+
obj.valueName = v.name
197203
objList.push(obj)
198204
}
199205
propertiesItemList.push(objList)
200206
}
201-
build(propertiesItemList).forEach((item) => {
207+
const buildList = build(propertiesItemList)
208+
// 如果构建后的组合数跟sku数量一样的话则不用处理,添加新属性没有属性值也不做处理 (解决编辑表单时或查看详情时数据回显问题)
209+
console.log(
210+
buildList.length === formData.value.skus.length || data.some((item) => item.values.length === 0)
211+
)
212+
if (
213+
buildList.length === formData.value.skus.length ||
214+
data.some((item) => item.values.length === 0)
215+
) {
216+
return
217+
}
218+
// 重置表数据
219+
formData.value!.skus = []
220+
buildList.forEach((item) => {
202221
const row = {
203222
properties: [],
204223
price: 0,
@@ -212,6 +231,7 @@ const generateTableData = (data: any[]) => {
212231
subCommissionFirstPrice: 0,
213232
subCommissionSecondPrice: 0
214233
}
234+
// 判断是否是单一属性的情况
215235
if (Array.isArray(item)) {
216236
row.properties = item
217237
} else {
@@ -269,8 +289,6 @@ watch(
269289
if (JSON.stringify(data) === '[]') return
270290
// 重置表头
271291
tableHeaderList.value = []
272-
// 重置表数据
273-
formData.value!.skus = []
274292
// 生成表头
275293
data.forEach((item, index) => {
276294
// name加属性项index区分属性值

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

Lines changed: 54 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@
8787
<el-image
8888
:src="row.picUrl"
8989
style="width: 36px; height: 36px"
90-
@click="imgViewVisible = true"
90+
@click="imagePreview(row.picUrl)"
9191
/>
9292
</div>
9393
</template>
@@ -106,11 +106,38 @@
106106
/>
107107
<el-table-column fixed="right" label="状态" min-width="80">
108108
<template #default="{ row }">
109-
<!--TODO 暂时用COMMON_STATUS占位一下使其不报错 -->
110-
<dict-tag :type="DICT_TYPE.PRODUCT_SPU_STATUS" :value="row.status" />
109+
<el-switch
110+
v-model="row.status"
111+
:active-value="0"
112+
:disabled="Number(row.status) < 0"
113+
:inactive-value="1"
114+
active-text="上架"
115+
inactive-text="下架"
116+
inline-prompt
117+
@change="changeStatus(row)"
118+
/>
119+
</template>
120+
</el-table-column>
121+
<el-table-column align="center" fixed="right" label="操作" min-width="150">
122+
<template #default="{ row }">
123+
<el-button
124+
v-hasPermi="['product:spu:query']"
125+
link
126+
type="primary"
127+
@click="openForm(row.id)"
128+
>
129+
修改
130+
</el-button>
131+
<el-button
132+
v-hasPermi="['product:spu:update']"
133+
link
134+
type="primary"
135+
@click="changeStatus(row)"
136+
>
137+
加入回收站
138+
</el-button>
111139
</template>
112140
</el-table-column>
113-
<el-table-column align="center" fixed="right" label="操作" min-width="150" />
114141
</el-table>
115142
<!-- 分页 -->
116143
<Pagination
@@ -123,9 +150,7 @@
123150
<!-- 必须在表格外面展示。不然单元格会遮挡图层 -->
124151
<el-image-viewer
125152
v-if="imgViewVisible"
126-
:url-list="[
127-
'http://127.0.0.1:48080/admin-api/infra/file/4/get/6ffdf8f5dfc03f7ceec1ff1ebc138adb8b721a057d505ccfb0e61a8783af1371.png'
128-
]"
153+
:url-list="imageViewerList"
129154
@close="imgViewVisible = false"
130155
/>
131156
</template>
@@ -166,7 +191,8 @@ const headerNum = ref([
166191
type: 5
167192
}
168193
])
169-
const imgViewVisible = ref(false)
194+
const imgViewVisible = ref(false) // 商品图预览
195+
const imageViewerList = ref<string[]>([]) // 商品图预览列表
170196
const queryParams = reactive({
171197
pageNo: 1,
172198
pageSize: 10
@@ -184,7 +210,21 @@ const getList = async () => {
184210
loading.value = false
185211
}
186212
}
187-
213+
/**
214+
* 更改SPU状态
215+
* @param row
216+
*/
217+
const changeStatus = (row) => {
218+
console.log(row)
219+
}
220+
/**
221+
* 商品图预览
222+
* @param imgUrl
223+
*/
224+
const imagePreview = (imgUrl: string) => {
225+
imageViewerList.value = [imgUrl]
226+
imgViewVisible.value = true
227+
}
188228
/** 搜索按钮操作 */
189229
const handleQuery = () => {
190230
getList()
@@ -196,26 +236,18 @@ const resetQuery = () => {
196236
handleQuery()
197237
}
198238
239+
/**
240+
* 新增或修改
241+
* @param id
242+
*/
199243
const openForm = (id?: number) => {
200244
if (typeof id === 'number') {
201245
push('/product/productManagementAdd?id=' + id)
246+
return
202247
}
203248
push('/product/productManagementAdd')
204249
}
205250
206-
/** 删除按钮操作 */
207-
// const handleDelete = async (id: number) => {
208-
// try {
209-
// // 删除的二次确认
210-
// await message.delConfirm()
211-
// // 发起删除
212-
// await ProductBrandApi.deleteBrand(id)
213-
// message.success(t('common.delSuccess'))
214-
// // 刷新列表
215-
// await getList()
216-
// } catch {}
217-
// }
218-
219251
/** 初始化 **/
220252
onMounted(() => {
221253
getList()

0 commit comments

Comments
 (0)