Skip to content

Commit 3a9668d

Browse files
committed
product:优化商品列表的样式、实现代码
1 parent 654094b commit 3a9668d

File tree

5 files changed

+68
-96
lines changed

5 files changed

+68
-96
lines changed

src/router/modules/remaining.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,9 +331,8 @@ const remainingRouter: AppRouteRecordRaw[] = [
331331
]
332332
},
333333
{
334-
path: '/product',
334+
path: '/mall/product', // 商品中心
335335
component: Layout,
336-
name: 'Product',
337336
meta: {
338337
hidden: true
339338
},
@@ -348,11 +347,11 @@ const remainingRouter: AppRouteRecordRaw[] = [
348347
canTo: true,
349348
icon: 'ep:edit',
350349
title: '添加商品',
351-
activeMenu: '/product/product-spu'
350+
activeMenu: '/mall/product/spu'
352351
}
353352
},
354353
{
355-
path: 'spu/edit/:spuId(\\d+)',
354+
path: 'spu/edit/:id(\\d+)',
356355
component: () => import('@/views/mall/product/spu/form/index.vue'),
357356
name: 'ProductSpuEdit',
358357
meta: {
@@ -361,11 +360,11 @@ const remainingRouter: AppRouteRecordRaw[] = [
361360
canTo: true,
362361
icon: 'ep:edit',
363362
title: '编辑商品',
364-
activeMenu: '/product/product-spu'
363+
activeMenu: '/mall/product/spu'
365364
}
366365
},
367366
{
368-
path: 'spu/detail/:spuId(\\d+)',
367+
path: 'spu/detail/:id(\\d+)',
369368
component: () => import('@/views/mall/product/spu/form/index.vue'),
370369
name: 'ProductSpuDetail',
371370
meta: {
@@ -374,7 +373,7 @@ const remainingRouter: AppRouteRecordRaw[] = [
374373
canTo: true,
375374
icon: 'ep:view',
376375
title: '商品详情',
377-
activeMenu: '/product/product-spu'
376+
activeMenu: '/mall/product/spu'
378377
}
379378
},
380379
{
@@ -393,9 +392,8 @@ const remainingRouter: AppRouteRecordRaw[] = [
393392
]
394393
},
395394
{
396-
path: '/trade',
395+
path: '/trade', // 交易中心
397396
component: Layout,
398-
name: 'Order',
399397
meta: {
400398
hidden: true
401399
},

src/utils/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ export const yuanToFen = (amount: string | number): number => {
230230
/**
231231
* 分转元
232232
*/
233-
export const fenToYuan = (amount: string | number): number => {
234-
return Number((Number(amount) / 100).toFixed(2))
233+
export const fenToYuan = (price: string | number): number => {
234+
price = Number(price)
235+
return (price / 100.0).toFixed(2)
235236
}

src/utils/tree.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export const defaultProps = {
1313
children: 'children',
1414
label: 'name',
1515
value: 'id',
16-
isLeaf: 'leaf'
16+
isLeaf: 'leaf',
17+
emitPath: false // 用于 cascader 组件:在选中节点改变时,是否返回由该节点所在的各级菜单的值所组成的数组,若设置 false,则只返回该节点的值
1718
}
1819

1920
const getConfig = (config: Partial<TreeHelperConfig>) => Object.assign({}, DEFAULT_CONFIG, config)
@@ -377,10 +378,10 @@ export const treeToString = (tree: any[], nodeId) => {
377378
function performAThoroughValidation(arr) {
378379
for (const item of arr) {
379380
if (item.id === nodeId) {
380-
str += `/${item.name}`
381+
str += ` / ${item.name}`
381382
return true
382383
} else if (typeof item.children !== 'undefined' && item.children.length !== 0) {
383-
str += `/${item.name}`
384+
str += ` / ${item.name}`
384385
if (performAThoroughValidation(item.children)) {
385386
return true
386387
}

src/views/mall/product/spu/form/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ const getDetail = async () => {
102102
if ('ProductSpuDetail' === name) {
103103
isDetail.value = true
104104
}
105-
const id = params.spuId as unknown as number
105+
const id = params.id as unknown as number
106106
if (id) {
107107
formLoading.value = true
108108
try {

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

Lines changed: 53 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
/>
1919
</el-form-item>
2020
<el-form-item label="商品分类" prop="categoryId">
21-
<el-tree-select
21+
<el-cascader
2222
v-model="queryParams.categoryId"
23-
:data="categoryList"
23+
:options="categoryList"
2424
:props="defaultProps"
25-
check-strictly
2625
class="w-1/1"
27-
node-key="id"
26+
clearable
2827
placeholder="请选择商品分类"
29-
@change="nodeClick"
28+
filterable
3029
/>
3130
</el-form-item>
3231
<el-form-item label="创建时间" prop="createTime">
@@ -78,25 +77,25 @@
7877
/>
7978
</el-tabs>
8079
<el-table v-loading="loading" :data="list">
81-
<el-table-column type="expand" width="30">
80+
<el-table-column type="expand">
8281
<template #default="{ row }">
8382
<el-form class="spu-table-expand" label-position="left">
8483
<el-row>
8584
<el-col :span="24">
8685
<el-row>
8786
<el-col :span="8">
8887
<el-form-item label="商品分类:">
89-
<span>{{ categoryString(row.categoryId) }}</span>
88+
<span>{{ formatCategoryName(row.categoryId) }}</span>
9089
</el-form-item>
9190
</el-col>
9291
<el-col :span="8">
9392
<el-form-item label="市场价:">
94-
<span>{{ floatToFixed2(row.marketPrice) }}</span>
93+
<span>{{ fenToYuan(row.marketPrice) }}</span>
9594
</el-form-item>
9695
</el-col>
9796
<el-col :span="8">
9897
<el-form-item label="成本价:">
99-
<span>{{ floatToFixed2(row.costPrice) }}</span>
98+
<span>{{ fenToYuan(row.costPrice) }}</span>
10099
</el-form-item>
101100
</el-col>
102101
</el-row>
@@ -106,9 +105,8 @@
106105
<el-col :span="24">
107106
<el-row>
108107
<el-col :span="8">
109-
<el-form-item label="收藏:">
110-
<!-- TODO 没有这个属性,暂时写死 5 个 -->
111-
<span>5</span>
108+
<el-form-item label="浏览量:">
109+
<span>{{ row.browseCount }}</span>
112110
</el-form-item>
113111
</el-col>
114112
<el-col :span="8">
@@ -122,15 +120,15 @@
122120
</el-form>
123121
</template>
124122
</el-table-column>
125-
<el-table-column key="id" align="center" label="商品编号" prop="id" />
123+
<el-table-column align="center" label="商品编号" min-width="60" prop="id" />
126124
<el-table-column label="商品图" min-width="80">
127125
<template #default="{ row }">
128126
<el-image :src="row.picUrl" class="h-30px w-30px" @click="imagePreview(row.picUrl)" />
129127
</template>
130128
</el-table-column>
131129
<el-table-column :show-overflow-tooltip="true" label="商品名称" min-width="300" prop="name" />
132130
<el-table-column align="center" label="商品售价" min-width="90" prop="price">
133-
<template #default="{ row }"> {{ floatToFixed2(row.price) }}元</template>
131+
<template #default="{ row }"> {{ fenToYuan(row.price) }}元</template>
134132
</el-table-column>
135133
<el-table-column align="center" label="销量" min-width="90" prop="salesCount" />
136134
<el-table-column align="center" label="库存" min-width="90" prop="stock" />
@@ -152,7 +150,7 @@
152150
active-text="上架"
153151
inactive-text="下架"
154152
inline-prompt
155-
@change="changeStatus(row)"
153+
@change="handleStatusChange(row)"
156154
/>
157155
</template>
158156
<template v-else>
@@ -191,7 +189,7 @@
191189
v-hasPermi="['product:spu:update']"
192190
link
193191
type="primary"
194-
@click="changeStatus(row, ProductSpuStatusEnum.DISABLE.status)"
192+
@click="handleStatus02Change(row, ProductSpuStatusEnum.DISABLE.status)"
195193
>
196194
恢复到仓库
197195
</el-button>
@@ -201,7 +199,7 @@
201199
v-hasPermi="['product:spu:update']"
202200
link
203201
type="primary"
204-
@click="changeStatus(row, ProductSpuStatusEnum.RECYCLE.status)"
202+
@click="handleStatus02Change(row, ProductSpuStatusEnum.RECYCLE.status)"
205203
>
206204
加入回收站
207205
</el-button>
@@ -220,12 +218,11 @@
220218
</template>
221219
<script lang="ts" setup>
222220
import { TabsPaneContext } from 'element-plus'
223-
import { cloneDeep } from 'lodash-es'
224221
import { createImageViewer } from '@/components/ImageViewer'
225222
import { dateFormatter } from '@/utils/formatTime'
226-
import { checkSelectedNode, defaultProps, handleTree, treeToString } from '@/utils/tree'
223+
import { defaultProps, handleTree, treeToString } from '@/utils/tree'
227224
import { ProductSpuStatusEnum } from '@/utils/constants'
228-
import { floatToFixed2 } from '@/utils'
225+
import { fenToYuan } from '@/utils'
229226
import download from '@/utils/download'
230227
import * as ProductSpuApi from '@/api/mall/product/spu'
231228
import * as ProductCategoryApi from '@/api/mall/product/category'
@@ -254,7 +251,7 @@ const tabsData = ref([
254251
},
255252
{
256253
count: 0,
257-
name: '已经售空商品',
254+
name: '已售罄商品',
258255
type: 2
259256
},
260257
{
@@ -303,43 +300,37 @@ const getList = async () => {
303300
}
304301
}
305302
306-
/**
307-
* 更改 SPU 状态
308-
*
309-
* @param row
310-
* @param status 更改前的值
311-
*/
312-
const changeStatus = async (row, status?: number) => {
313-
const deepCopyValue = cloneDeep(unref(row))
314-
if (typeof status !== 'undefined') deepCopyValue.status = status
303+
/** 添加到仓库 / 回收站的状态 */
304+
const handleStatus02Change = async (row, newStatus: number) => {
305+
try {
306+
// 二次确认
307+
const text = newStatus === ProductSpuStatusEnum.RECYCLE.status ? '加入到回收站' : '恢复到仓库'
308+
await message.confirm(`确认要"${row.name}"${text}吗?`)
309+
// 发起修改
310+
await ProductSpuApi.updateStatus({ id: row.id, status: newStatus })
311+
message.success(text + '成功')
312+
// 刷新 tabs 数据
313+
await getTabsCount()
314+
// 刷新列表
315+
await getList()
316+
} catch {}
317+
}
318+
319+
/** 更新上架/下架状态 */
320+
const handleStatusChange = async (row) => {
315321
try {
316-
let text = ''
317-
switch (deepCopyValue.status) {
318-
case ProductSpuStatusEnum.DISABLE.status:
319-
text = ProductSpuStatusEnum.DISABLE.name
320-
break
321-
case ProductSpuStatusEnum.ENABLE.status:
322-
text = ProductSpuStatusEnum.ENABLE.name
323-
break
324-
case ProductSpuStatusEnum.RECYCLE.status:
325-
text = `加入${ProductSpuStatusEnum.RECYCLE.name}`
326-
break
327-
}
328-
await message.confirm(
329-
deepCopyValue.status === -1
330-
? `确认要将[${row.name}]${text}吗?`
331-
: row.status === -1 // 再判断一次原对象是否等于-1,例: 把回收站中的商品恢复到仓库中,事件触发时原对象status为-1 深拷贝对象status被赋值为0
332-
? `确认要将[${row.name}]恢复到仓库吗?`
333-
: `确认要${text}[${row.name}]吗?`
334-
)
335-
await ProductSpuApi.updateStatus({ id: deepCopyValue.id, status: deepCopyValue.status })
336-
message.success('更新状态成功')
322+
// 二次确认
323+
const text = row.status ? '上架' : '下架'
324+
await message.confirm(`确认要${text}"${row.name}"吗?`)
325+
// 发起修改
326+
await ProductSpuApi.updateStatus({ id: row.id, status: row.status })
327+
message.success(text + '成功')
337328
// 刷新 tabs 数据
338329
await getTabsCount()
339330
// 刷新列表
340331
await getList()
341332
} catch {
342-
// 取消更改状态时回显数据
333+
// 异常时,需要重置回之前的值
343334
row.status =
344335
row.status === ProductSpuStatusEnum.DISABLE.status
345336
? ProductSpuStatusEnum.ENABLE.status
@@ -380,26 +371,20 @@ const resetQuery = () => {
380371
handleQuery()
381372
}
382373
383-
/**
384-
* 新增或修改
385-
*
386-
* @param id 商品 SPU 编号
387-
*/
374+
/** 新增或修改 */
388375
const openForm = (id?: number) => {
389376
// 修改
390377
if (typeof id === 'number') {
391-
push({ name: 'ProductSpuEdit', params: { spuId: id } })
378+
push({ name: 'ProductSpuEdit', params: { id } })
392379
return
393380
}
394381
// 新增
395382
push({ name: 'ProductSpuAdd' })
396383
}
397384
398-
/**
399-
* 查看商品详情
400-
*/
385+
/** 查看商品详情 */
401386
const openDetail = (id: number) => {
402-
push({ name: 'ProductSpuDetail', params: { spuId: id } })
387+
push({ name: 'ProductSpuDetail', params: { id } })
403388
}
404389
405390
/** 导出按钮操作 */
@@ -417,6 +402,12 @@ const handleExport = async () => {
417402
}
418403
}
419404
405+
const categoryList = ref() // 分类树
406+
/** 获取分类的节点的完整结构 */
407+
const formatCategoryName = (categoryId) => {
408+
return treeToString(categoryList.value, categoryId)
409+
}
410+
420411
// 监听路由变化更新列表,解决商品保存后,列表不刷新的问题。
421412
watch(
422413
() => currentRoute.value,
@@ -425,25 +416,6 @@ watch(
425416
}
426417
)
427418
428-
const categoryList = ref() // 分类树
429-
/**
430-
* 获取分类的节点的完整结构
431-
* @param categoryId 分类id
432-
*/
433-
const categoryString = (categoryId) => {
434-
return treeToString(categoryList.value, categoryId)
435-
}
436-
437-
/**
438-
* 校验所选是否为二级及以下节点
439-
*/
440-
const nodeClick = () => {
441-
if (!checkSelectedNode(categoryList.value, queryParams.value.categoryId)) {
442-
queryParams.value.categoryId = null
443-
message.warning('必须选择二级及以下节点!!')
444-
}
445-
}
446-
447419
/** 初始化 **/
448420
onMounted(async () => {
449421
await getTabsCount()

0 commit comments

Comments
 (0)