Files
JayuCloud/Frontend/src/views/admin/ProductsView.vue
T

300 lines
8.5 KiB
Vue
Raw Normal View History

2026-02-25 10:37:16 +08:00
<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"
2026-02-25 14:32:34 +08:00
:src="record.imageFileIds ? record.imageFileIds.split(',')[0] : DEFAULT_PRODUCT_IMAGE"
2026-02-25 10:37:16 +08:00
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>
2026-02-25 14:32:34 +08:00
2026-02-25 10:37:16 +08:00
<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 }"
>
2026-02-25 14:32:34 +08:00
<a-form-item label="商品名称" name="title">
<a-input v-model:value="formState.title" placeholder="请输入商品名称" />
2026-02-25 10:37:16 +08:00
</a-form-item>
<a-form-item label="商品描述" name="description">
<a-textarea
v-model:value="formState.description"
placeholder="请输入商品描述"
:rows="3"
/>
</a-form-item>
2026-02-25 14:32:34 +08:00
<a-form-item label="图片ID" name="imageFileIds">
<a-input v-model:value="formState.imageFileIds" placeholder="请输入图片文件ID,多个用逗号分隔" />
2026-02-25 10:37:16 +08:00
</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>
2026-02-25 14:32:34 +08:00
<a-form-item label="预计时长" name="approximateDuration">
<a-input v-model:value="formState.approximateDuration" placeholder="例如:30分钟" />
2026-02-25 10:37:16 +08:00
</a-form-item>
2026-02-25 14:32:34 +08:00
<a-form-item label="标签ID" name="tags">
<a-input v-model:value="formState.tags" placeholder="请输入标签ID,多个用分号分隔" />
2026-02-25 10:37:16 +08:00
</a-form-item>
2026-02-25 14:32:34 +08:00
<a-form-item label="店铺ID" name="shopId">
<a-input-number v-model:value="formState.shopId" :min="1" style="width: 100%" placeholder="请输入店铺ID" />
2026-02-25 10:37:16 +08:00
</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'
2026-02-25 14:32:34 +08:00
import { adminProductApi, type Product } from '@/api/admin'
2026-02-25 10:37:16 +08:00
const loading = ref(false)
const searchText = ref('')
2026-02-25 14:32:34 +08:00
const productList = ref<Product[]>([])
2026-02-25 10:37:16 +08:00
const modalVisible = ref(false)
const modalLoading = ref(false)
const isEdit = ref(false)
const formRef = ref<FormInstance>()
2026-02-25 14:32:34 +08:00
const currentProduct = ref<Product | null>(null)
2026-02-25 10:37:16 +08:00
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total: number) => `共 ${total} 条`
})
const formState = reactive({
2026-02-25 14:32:34 +08:00
shopId: 1,
title: '',
2026-02-25 10:37:16 +08:00
description: '',
2026-02-25 14:32:34 +08:00
imageFileIds: '',
2026-02-25 10:37:16 +08:00
price: 0,
2026-02-25 14:32:34 +08:00
tags: '',
approximateDuration: '',
bookableArtists: '',
availableCoupons: ''
2026-02-25 10:37:16 +08:00
})
const formRules = {
2026-02-25 14:32:34 +08:00
title: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
2026-02-25 10:37:16 +08:00
description: [{ required: true, message: '请输入商品描述', trigger: 'blur' }],
price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
2026-02-25 14:32:34 +08:00
shopId: [{ required: true, message: '请输入店铺ID', trigger: 'blur' }]
2026-02-25 10:37:16 +08:00
}
const columns = [
{ title: '图片', key: 'image', width: 100 },
2026-02-25 14:32:34 +08:00
{ title: '商品名称', dataIndex: 'title', key: 'title' },
{ title: '描述', dataIndex: 'description', key: 'description' },
2026-02-25 10:37:16 +08:00
{ title: '价格', key: 'price', width: 100 },
2026-02-25 14:32:34 +08:00
{ title: '预计时长', dataIndex: 'approximateDuration', key: 'approximateDuration', width: 120 },
{ title: '点赞', dataIndex: 'likeCount', key: 'likeCount', width: 80 },
{ title: '收藏', dataIndex: 'collectCount', key: 'collectCount', width: 80 },
2026-02-25 10:37:16 +08:00
{ title: '创建时间', dataIndex: 'createTime', key: 'createTime' },
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
]
const getCurrentUserInfo = () => {
try {
const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) {
return JSON.parse(userInfoStr)
}
} catch (error) {
console.error('获取当前用户信息失败', error)
}
return null
}
2026-02-25 10:37:16 +08:00
const fetchProductList = async () => {
loading.value = true
try {
const currentUser = getCurrentUserInfo()
const params: any = {
2026-02-25 10:37:16 +08:00
page: pagination.current,
size: pagination.pageSize,
keyword: searchText.value
}
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
params.shopId = currentUser.shopId
}
const res = await adminProductApi.listProducts(params)
2026-02-25 10:37:16 +08:00
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, {
2026-02-25 14:32:34 +08:00
shopId: 1,
title: '',
2026-02-25 10:37:16 +08:00
description: '',
2026-02-25 14:32:34 +08:00
imageFileIds: '',
2026-02-25 10:37:16 +08:00
price: 0,
2026-02-25 14:32:34 +08:00
tags: '',
approximateDuration: '',
bookableArtists: '',
availableCoupons: ''
2026-02-25 10:37:16 +08:00
})
modalVisible.value = true
}
2026-02-25 14:32:34 +08:00
const showEditModal = (record: Product) => {
2026-02-25 10:37:16 +08:00
isEdit.value = true
currentProduct.value = record
Object.assign(formState, {
2026-02-25 14:32:34 +08:00
shopId: record.shopId,
title: record.title,
2026-02-25 10:37:16 +08:00
description: record.description,
2026-02-25 14:32:34 +08:00
imageFileIds: record.imageFileIds,
2026-02-25 10:37:16 +08:00
price: record.price,
2026-02-25 14:32:34 +08:00
tags: record.tags,
approximateDuration: record.approximateDuration,
bookableArtists: record.bookableArtists,
availableCoupons: record.availableCoupons
2026-02-25 10:37:16 +08:00
})
modalVisible.value = true
}
const handleModalOk = async () => {
try {
await formRef.value?.validate()
modalLoading.value = true
if (isEdit.value && currentProduct.value) {
2026-02-25 14:32:34 +08:00
await adminProductApi.updateProduct(currentProduct.value.id, formState)
2026-02-25 10:37:16 +08:00
message.success('更新成功')
} else {
2026-02-25 14:32:34 +08:00
await adminProductApi.createProduct(formState)
2026-02-25 10:37:16 +08:00
message.success('创建成功')
}
modalVisible.value = false
fetchProductList()
} catch (error) {
console.error(error)
} finally {
modalLoading.value = false
}
}
const handleModalCancel = () => {
modalVisible.value = false
formRef.value?.resetFields()
}
2026-02-25 14:32:34 +08:00
const handleDelete = async (record: Product) => {
2026-02-25 10:37:16 +08:00
try {
2026-02-25 14:32:34 +08:00
await adminProductApi.deleteProduct(record.id)
2026-02-25 10:37:16 +08:00
message.success('删除成功')
fetchProductList()
} catch (error) {
message.error('删除失败')
}
}
onMounted(() => {
fetchProductList()
})
</script>
<style scoped>
.products {
padding: 0;
}
</style>