后台管理页面初版
This commit is contained in:
@@ -0,0 +1,295 @@
|
||||
<template>
|
||||
<div class="products">
|
||||
<a-page-header title="商品管理" sub-title="管理美甲商品" />
|
||||
|
||||
<a-card :bordered="false">
|
||||
<template #extra>
|
||||
<a-space>
|
||||
<a-input-search
|
||||
v-model:value="searchText"
|
||||
placeholder="搜索商品名称"
|
||||
style="width: 250px"
|
||||
@search="handleSearch"
|
||||
/>
|
||||
<a-button type="primary" @click="showAddModal">
|
||||
<template #icon>
|
||||
<PlusOutlined />
|
||||
</template>
|
||||
新增商品
|
||||
</a-button>
|
||||
</a-space>
|
||||
</template>
|
||||
|
||||
<a-table
|
||||
:columns="columns"
|
||||
:data-source="productList"
|
||||
:loading="loading"
|
||||
:pagination="pagination"
|
||||
@change="handleTableChange"
|
||||
row-key="id"
|
||||
>
|
||||
<template #bodyCell="{ column, record }">
|
||||
<template v-if="column.key === 'image'">
|
||||
<a-image
|
||||
:width="60"
|
||||
:height="60"
|
||||
:src="record.image || DEFAULT_PRODUCT_IMAGE"
|
||||
style="object-fit: cover; border-radius: 4px;"
|
||||
/>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'price'">
|
||||
<span style="color: #cf1322; font-weight: 600;">
|
||||
¥{{ record.price }}
|
||||
</span>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'status'">
|
||||
<a-tag :color="record.status === 1 ? 'green' : 'default'">
|
||||
{{ record.status === 1 ? '上架' : '下架' }}
|
||||
</a-tag>
|
||||
</template>
|
||||
<template v-else-if="column.key === 'actions'">
|
||||
<a-space>
|
||||
<a-button type="link" size="small" @click="showEditModal(record)">
|
||||
编辑
|
||||
</a-button>
|
||||
<a-popconfirm
|
||||
title="确定要删除这个商品吗?"
|
||||
@confirm="handleDelete(record)"
|
||||
>
|
||||
<a-button type="link" size="small" danger>
|
||||
删除
|
||||
</a-button>
|
||||
</a-popconfirm>
|
||||
</a-space>
|
||||
</template>
|
||||
</template>
|
||||
</a-table>
|
||||
</a-card>
|
||||
|
||||
<a-modal
|
||||
v-model:open="modalVisible"
|
||||
:title="isEdit ? '编辑商品' : '新增商品'"
|
||||
width="600px"
|
||||
@ok="handleModalOk"
|
||||
@cancel="handleModalCancel"
|
||||
:confirmLoading="modalLoading"
|
||||
>
|
||||
<a-form
|
||||
ref="formRef"
|
||||
:model="formState"
|
||||
:rules="formRules"
|
||||
:label-col="{ span: 5 }"
|
||||
:wrapper-col="{ span: 18 }"
|
||||
>
|
||||
<a-form-item label="商品名称" name="name">
|
||||
<a-input v-model:value="formState.name" placeholder="请输入商品名称" />
|
||||
</a-form-item>
|
||||
<a-form-item label="商品描述" name="description">
|
||||
<a-textarea
|
||||
v-model:value="formState.description"
|
||||
placeholder="请输入商品描述"
|
||||
:rows="3"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="商品图片" name="image">
|
||||
<a-input v-model:value="formState.image" placeholder="请输入图片URL" />
|
||||
</a-form-item>
|
||||
<a-form-item label="价格" name="price">
|
||||
<a-input-number
|
||||
v-model:value="formState.price"
|
||||
:min="0"
|
||||
:precision="2"
|
||||
style="width: 100%"
|
||||
placeholder="请输入价格"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="预计时长(分钟)" name="estimatedTime">
|
||||
<a-input-number
|
||||
v-model:value="formState.estimatedTime"
|
||||
:min="0"
|
||||
style="width: 100%"
|
||||
placeholder="请输入预计时长"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="分类" name="category">
|
||||
<a-select v-model:value="formState.category" placeholder="请选择分类">
|
||||
<a-select-option value="美甲">美甲</a-select-option>
|
||||
<a-select-option value="美睫">美睫</a-select-option>
|
||||
<a-select-option value="手足护理">手足护理</a-select-option>
|
||||
</a-select>
|
||||
</a-form-item>
|
||||
<a-form-item label="状态" name="status">
|
||||
<a-switch v-model:checked="formState.status" checked-children="上架" un-checked-children="下架" />
|
||||
</a-form-item>
|
||||
</a-form>
|
||||
</a-modal>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import type { FormInstance } from 'ant-design-vue'
|
||||
import { PlusOutlined } from '@ant-design/icons-vue'
|
||||
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
|
||||
import { productApi, type ProductVO } from '@/api/product'
|
||||
|
||||
const loading = ref(false)
|
||||
const searchText = ref('')
|
||||
const productList = ref<ProductVO[]>([])
|
||||
const modalVisible = ref(false)
|
||||
const modalLoading = ref(false)
|
||||
const isEdit = ref(false)
|
||||
const formRef = ref<FormInstance>()
|
||||
const currentProduct = ref<ProductVO | null>(null)
|
||||
|
||||
const pagination = reactive({
|
||||
current: 1,
|
||||
pageSize: 10,
|
||||
total: 0,
|
||||
showSizeChanger: true,
|
||||
showQuickJumper: true,
|
||||
showTotal: (total: number) => `共 ${total} 条`
|
||||
})
|
||||
|
||||
const formState = reactive({
|
||||
name: '',
|
||||
description: '',
|
||||
image: '',
|
||||
price: 0,
|
||||
estimatedTime: 30,
|
||||
category: '美甲',
|
||||
status: true,
|
||||
shopId: null as number | null
|
||||
})
|
||||
|
||||
const formRules = {
|
||||
name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
|
||||
description: [{ required: true, message: '请输入商品描述', trigger: 'blur' }],
|
||||
price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
|
||||
estimatedTime: [{ required: true, message: '请输入预计时长', trigger: 'blur' }],
|
||||
category: [{ required: true, message: '请选择分类', trigger: 'change' }]
|
||||
}
|
||||
|
||||
const columns = [
|
||||
{ title: '图片', key: 'image', width: 100 },
|
||||
{ title: '商品名称', dataIndex: 'name', key: 'name' },
|
||||
{ title: '分类', dataIndex: 'category', key: 'category', width: 100 },
|
||||
{ title: '价格', key: 'price', width: 100 },
|
||||
{ title: '预计时长', dataIndex: 'estimatedTime', key: 'estimatedTime', width: 120, customRender: ({ text }: { text: number }) => `${text}分钟` },
|
||||
{ title: '状态', key: 'status', width: 80 },
|
||||
{ title: '创建时间', dataIndex: 'createTime', key: 'createTime' },
|
||||
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
|
||||
]
|
||||
|
||||
const fetchProductList = async () => {
|
||||
loading.value = true
|
||||
try {
|
||||
const res = await productApi.listProducts({
|
||||
page: pagination.current,
|
||||
size: pagination.pageSize,
|
||||
keyword: searchText.value
|
||||
})
|
||||
productList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
} catch (error) {
|
||||
message.error('获取商品列表失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearch = () => {
|
||||
pagination.current = 1
|
||||
fetchProductList()
|
||||
}
|
||||
|
||||
const handleTableChange = (pag: any) => {
|
||||
pagination.current = pag.current
|
||||
pagination.pageSize = pag.pageSize
|
||||
fetchProductList()
|
||||
}
|
||||
|
||||
const showAddModal = () => {
|
||||
isEdit.value = false
|
||||
Object.assign(formState, {
|
||||
name: '',
|
||||
description: '',
|
||||
image: '',
|
||||
price: 0,
|
||||
estimatedTime: 30,
|
||||
category: '美甲',
|
||||
status: true,
|
||||
shopId: null
|
||||
})
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
const showEditModal = (record: ProductVO) => {
|
||||
isEdit.value = true
|
||||
currentProduct.value = record
|
||||
Object.assign(formState, {
|
||||
name: record.name,
|
||||
description: record.description,
|
||||
image: record.image,
|
||||
price: record.price,
|
||||
estimatedTime: record.estimatedTime,
|
||||
category: record.category,
|
||||
status: record.status === 1,
|
||||
shopId: record.shopId
|
||||
})
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
const handleModalOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
modalLoading.value = true
|
||||
|
||||
const data = {
|
||||
...formState,
|
||||
status: formState.status ? 1 : 0
|
||||
}
|
||||
|
||||
if (isEdit.value && currentProduct.value) {
|
||||
await productApi.updateProduct(currentProduct.value.id, data)
|
||||
message.success('更新成功')
|
||||
} else {
|
||||
await productApi.createProduct(data)
|
||||
message.success('创建成功')
|
||||
}
|
||||
|
||||
modalVisible.value = false
|
||||
fetchProductList()
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
} finally {
|
||||
modalLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const handleModalCancel = () => {
|
||||
modalVisible.value = false
|
||||
formRef.value?.resetFields()
|
||||
}
|
||||
|
||||
const handleDelete = async (record: ProductVO) => {
|
||||
try {
|
||||
await productApi.deleteProduct(record.id)
|
||||
message.success('删除成功')
|
||||
fetchProductList()
|
||||
} catch (error) {
|
||||
message.error('删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
fetchProductList()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.products {
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user