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

943 lines
30 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"
/>
2026-02-26 22:51:56 +08:00
<a-button v-if="hasPermission('product:create')" type="primary" @click="showAddModal">
2026-02-25 10:37:16 +08:00
<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'">
2026-02-26 11:16:24 +08:00
<div class="product-images">
<a-image
v-for="(url, index) in getProductImages(record)"
:key="index"
:width="50"
:height="50"
:src="url"
style="object-fit: cover; border-radius: 4px; margin-right: 4px;"
:preview="false"
/>
<a-image
v-if="getProductImages(record).length === 0"
:width="50"
:height="50"
:src="DEFAULT_PRODUCT_IMAGE"
style="object-fit: cover; border-radius: 4px;"
:preview="false"
/>
</div>
2026-02-25 10:37:16 +08:00
</template>
<template v-else-if="column.key === 'shop'">
<span>{{ getShopName(record.shopId) }}</span>
</template>
2026-02-25 10:37:16 +08:00
<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>
2026-02-28 14:32:28 +08:00
<a-button type="link" size="small" @click="showViewModal(record)">
查看
</a-button>
2026-02-26 22:51:56 +08:00
<a-button v-if="hasPermission('product:edit')" type="link" size="small" @click="showEditModal(record)">
2026-02-25 10:37:16 +08:00
编辑
</a-button>
<a-popconfirm
2026-02-26 22:51:56 +08:00
v-if="hasPermission('product:delete')"
2026-02-25 10:37:16 +08:00
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="700px"
2026-02-25 10:37:16 +08:00
@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 20:31:00 +08:00
<a-form-item label="店铺" name="shopId">
<a-select
v-model:value="formState.shopId"
placeholder="请选择店铺"
style="width: 100%"
:loading="loadingShops"
:disabled="!isAdmin && !!formState.shopId"
2026-02-25 20:31:00 +08:00
>
<a-select-option
v-if="shops.length === 0"
disabled
value=""
>
暂无店铺数据
</a-select-option>
<a-select-option
v-for="shop in shops"
:key="shop.id"
:value="shop.id"
>
{{ shop.shopName }}
</a-select-option>
</a-select>
2026-02-25 10:37:16 +08:00
</a-form-item>
<template v-if="formState.shopId">
<a-form-item label="商品图片">
<a-upload
v-model:file-list="imageFileList"
list-type="picture-card"
:custom-request="handleUploadImage"
:before-upload="beforeUpload"
@preview="handlePreview"
@remove="handleRemove"
:max-count="5"
>
<div v-if="imageFileList.length < 5">
<plus-outlined />
<div style="margin-top: 8px">上传图片</div>
</div>
</a-upload>
<a-modal v-model:open="previewVisible" :footer="null" @cancel="previewVisible = false">
<img alt="预览" style="width: 100%" :src="previewImage" />
</a-modal>
</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="approximateDuration">
<a-input-number
v-model:value="formState.approximateDuration"
placeholder="请输入分钟数"
:min="1"
style="width: 100%"
/>
</a-form-item>
<a-form-item label="标签" name="tags">
<a-select
v-model:value="formState.tags"
mode="multiple"
placeholder="请选择标签"
style="width: 100%"
:loading="loadingTags"
>
<a-select-option
v-if="tags.length === 0"
disabled
value=""
>
暂无标签数据
</a-select-option>
<a-select-option
v-for="tag in tags"
:key="tag.id"
:value="tag.id.toString()"
>
{{ tag.tagName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="可预约美甲师" name="bookableArtists">
<a-select
v-model:value="formState.bookableArtists"
mode="multiple"
placeholder="请选择美甲师"
style="width: 100%"
:loading="loadingArtists"
>
<a-select-option
v-if="artists.length === 0"
disabled
value=""
>
暂无美甲师数据
</a-select-option>
<a-select-option
v-for="artist in artists"
:key="artist.id"
:value="artist.id.toString()"
>
{{ artist.nickname }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="可用优惠券" name="availableCoupons">
<a-select
v-model:value="formState.availableCoupons"
mode="multiple"
placeholder="请选择优惠券"
style="width: 100%"
:loading="loadingCoupons"
:disabled="!isShopOwner()"
>
<a-select-option
v-if="coupons.length === 0"
disabled
value=""
>
暂无优惠券数据
</a-select-option>
<a-select-option
v-for="coupon in coupons"
2026-02-27 11:05:15 +08:00
:key="coupon?.id"
:value="coupon?.id?.toString()"
>
2026-02-27 11:05:15 +08:00
{{ coupon?.couponName || '' }}
</a-select-option>
</a-select>
</a-form-item>
</template>
2026-02-25 10:37:16 +08:00
</a-form>
</a-modal>
2026-02-28 14:32:28 +08:00
<!-- 查看商品详情模态框 -->
<a-modal
v-model:open="viewModalVisible"
title="查看商品详情"
width="700px"
@cancel="handleViewModalCancel"
>
<a-form
:model="viewFormState"
:label-col="{ span: 5 }"
:wrapper-col="{ span: 18 }"
>
<a-form-item label="商品名称">
<a-input v-model:value="viewFormState.title" :disabled="true" />
</a-form-item>
<a-form-item label="商品描述">
<a-textarea
v-model:value="viewFormState.description"
:rows="3"
:disabled="true"
/>
</a-form-item>
<a-form-item label="店铺">
<a-select v-model:value="viewFormState.shopId" :disabled="true">
<a-select-option
v-for="shop in shops"
:key="shop.id"
:value="shop.id"
>
{{ shop.shopName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="商品图片">
<div class="product-images">
<a-image
v-for="(url, index) in viewImageUrls"
:key="index"
:width="80"
:height="80"
:src="url"
style="object-fit: cover; border-radius: 4px; margin-right: 8px; margin-bottom: 8px;"
:preview="false"
/>
</div>
</a-form-item>
<a-form-item label="价格">
<a-input-number
v-model:value="viewFormState.price"
:disabled="true"
style="width: 100%"
/>
</a-form-item>
<a-form-item label="预计时长">
<a-input-number
v-model:value="viewFormState.approximateDuration"
:disabled="true"
style="width: 100%"
/>
</a-form-item>
<a-form-item label="标签">
<a-select
v-model:value="viewFormState.tags"
mode="multiple"
:disabled="true"
style="width: 100%"
>
<a-select-option
v-for="tag in tags"
:key="tag.id"
:value="tag.id.toString()"
>
{{ tag.tagName }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="可预约美甲师">
<a-select
v-model:value="viewFormState.bookableArtists"
mode="multiple"
:disabled="true"
style="width: 100%"
>
<a-select-option
v-for="artist in artists"
:key="artist.id"
:value="artist.id.toString()"
>
{{ artist.nickname }}
</a-select-option>
</a-select>
</a-form-item>
<a-form-item label="可用优惠券">
<a-select
v-model:value="viewFormState.availableCoupons"
mode="multiple"
:disabled="true"
style="width: 100%"
>
<a-select-option
v-for="coupon in coupons"
:key="coupon?.id"
:value="coupon?.id?.toString()"
>
{{ coupon?.couponName || '' }}
</a-select-option>
</a-select>
</a-form-item>
</a-form>
</a-modal>
2026-02-25 10:37:16 +08:00
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, watch } from 'vue'
2026-02-25 10:37:16 +08:00
import { message } from 'ant-design-vue'
import type { FormInstance, UploadProps } from 'ant-design-vue'
2026-02-25 10:37:16 +08:00
import { PlusOutlined } from '@ant-design/icons-vue'
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
2026-02-26 17:54:18 +08:00
import { usePermission } from '@/utils/permission'
2026-02-25 10:37:16 +08:00
2026-02-26 18:44:47 +08:00
const { hasPermission, loadUserPermissions } = usePermission()
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)
2026-02-28 14:32:28 +08:00
const viewModalVisible = ref(false)
2026-02-25 10:37:16 +08:00
const modalLoading = ref(false)
const isEdit = ref(false)
2026-02-25 14:32:34 +08:00
const currentProduct = ref<Product | null>(null)
2026-02-25 20:31:00 +08:00
const shops = ref<Shop[]>([])
const loadingShops = ref(false)
const tags = ref<any[]>([])
const loadingTags = ref(false)
const artists = ref<any[]>([])
const loadingArtists = ref(false)
const coupons = ref<any[]>([])
const loadingCoupons = ref(false)
const previewVisible = ref(false)
const previewImage = ref('')
const imageFileList = ref<any[]>([])
const uploadedFileIds = ref<string[]>([])
2026-02-26 11:16:24 +08:00
const imageUrls = ref<Record<number, string[]>>({})
2026-02-28 14:32:28 +08:00
const viewImageUrls = ref<string[]>([])
const imageIdCache = ref<Record<string, string>>({})
const isAdmin = ref(false)
const formRef = ref<any>(null)
2026-02-25 10:37:16 +08:00
2026-02-28 14:32:28 +08:00
const viewFormState = reactive({
shopId: undefined,
title: '',
description: '',
imageFileIds: '',
price: 0,
tags: [] as string[],
approximateDuration: undefined,
bookableArtists: [] as string[],
availableCoupons: [] as string[]
})
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({
shopId: undefined,
2026-02-25 14:32:34 +08:00
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,
tags: [] as string[],
approximateDuration: undefined,
bookableArtists: [] as string[],
availableCoupons: [] as string[]
2026-02-25 10:37:16 +08:00
})
2026-03-02 10:13:20 +08:00
const formRules: any = {
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' }],
shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
tags: [{ required: true, message: '请选择标签', trigger: 'change' }],
bookableArtists: [{ required: true, message: '请选择美甲师', trigger: 'change' }]
2026-02-25 10:37:16 +08:00
}
const columns = [
2026-02-26 11:16:24 +08:00
{ title: '图片', key: 'image', width: 150 },
2026-02-25 14:32:34 +08:00
{ title: '商品名称', dataIndex: 'title', key: 'title' },
{ title: '所属店铺', key: 'shop', width: 150 },
2026-02-25 14:32:34 +08:00
{ 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 getShopName = (shopId: number): string => {
const shop = shops.value.find(s => s.id === shopId)
return shop ? shop.shopName : '未知店铺'
}
const getCurrentUserInfo = () => {
try {
const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) {
const userInfo = JSON.parse(userInfoStr)
// 判断用户是否为管理员(类型为4)
isAdmin.value = userInfo.type === 4
return userInfo
}
} catch (error) {
console.error('获取当前用户信息失败', error)
}
return null
}
const isShopOwner = () => {
const userInfo = getCurrentUserInfo()
return userInfo && userInfo.type === 2
}
2026-02-25 20:31:00 +08:00
const fetchShops = async () => {
loadingShops.value = true
try {
const currentUser = getCurrentUserInfo()
2026-02-26 23:28:24 +08:00
// 检查用户类型和权限
if (currentUser && currentUser.type === 3 && currentUser.shopId) {
// 美甲师直接使用自己的店铺ID,不需要获取店铺列表
shops.value = []
2026-02-25 20:31:00 +08:00
formState.shopId = currentUser.shopId
} else {
2026-03-02 10:13:20 +08:00
const response = await adminShopApi.listAllShops() as any
const res = response || []
2026-02-26 23:28:24 +08:00
// 根据用户权限控制店铺列表的显示
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
// 如果是店长,只显示用户所属的店铺
shops.value = res?.filter(shop => shop.id === currentUser.shopId) || []
// 默认选择用户所属的店铺,并且不可修改
formState.shopId = currentUser.shopId
} else {
// 如果是管理员,显示所有店铺
shops.value = res || []
}
2026-02-25 20:31:00 +08:00
}
} catch (error) {
2026-02-26 23:28:24 +08:00
// 权限不足时不显示错误提示
console.error('获取店铺列表失败:', error)
2026-02-25 20:31:00 +08:00
} finally {
loadingShops.value = false
}
}
const fetchTags = async (shopId?: number) => {
loadingTags.value = true
try {
2026-03-02 10:13:20 +08:00
let response
if (shopId) {
2026-03-02 10:13:20 +08:00
response = await adminTagApi.listTagsByShopId(shopId) as any
} else {
2026-03-02 10:13:20 +08:00
response = await adminTagApi.listAllTags() as any
}
2026-03-02 10:13:20 +08:00
tags.value = response || []
} catch (error) {
console.error('获取标签列表失败', error)
message.error('获取标签列表失败')
} finally {
loadingTags.value = false
}
}
const fetchArtists = async (shopId?: number) => {
loadingArtists.value = true
try {
2026-03-02 10:13:20 +08:00
const response = await adminArtistApi.listArtists(shopId) as any
artists.value = response || []
} catch (error) {
console.error('获取美甲师列表失败', error)
message.error('获取美甲师列表失败')
} finally {
loadingArtists.value = false
}
}
const fetchCoupons = async () => {
loadingCoupons.value = true
try {
2026-03-02 10:13:20 +08:00
const response = await adminCouponApi.listAllCoupons()
const res = typeof response === 'object' && 'data' in response ? response.data : response
coupons.value = res || []
} catch (error) {
console.error('获取优惠券列表失败', error)
message.error('获取优惠券列表失败')
} finally {
loadingCoupons.value = false
}
}
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
}
2026-03-02 10:13:20 +08:00
const response = await adminProductApi.listProducts(params) as any
productList.value = response?.list || []
pagination.total = response?.total || 0
2026-03-02 10:13:20 +08:00
await loadImageUrls(response?.list || [])
2026-02-25 10:37:16 +08:00
} catch (error) {
message.error('获取商品列表失败')
} finally {
loading.value = false
}
}
const loadImageUrls = async (products: Product[]) => {
for (const product of products) {
if (product.imageFileIds) {
2026-02-26 11:16:24 +08:00
const ids = product.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
2026-03-02 10:13:20 +08:00
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
2026-02-26 11:16:24 +08:00
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_PRODUCT_IMAGE)
}
}
}
2026-02-26 11:16:24 +08:00
imageUrls.value[product.id] = urls
} else {
2026-02-26 11:16:24 +08:00
imageUrls.value[product.id] = [DEFAULT_PRODUCT_IMAGE]
}
}
}
2026-03-02 10:13:20 +08:00
const getProductImages = (product: Product | Record<string, any>): string[] => {
const productId = 'id' in product ? product.id : (product as any).id
return imageUrls.value[productId] || [DEFAULT_PRODUCT_IMAGE]
2026-02-26 11:16:24 +08:00
}
2026-02-25 10:37:16 +08:00
const handleSearch = () => {
pagination.current = 1
fetchProductList()
}
const handleTableChange = (pag: any) => {
pagination.current = pag.current
pagination.pageSize = pag.pageSize
fetchProductList()
}
const showAddModal = () => {
isEdit.value = false
2026-02-25 20:31:00 +08:00
const currentUser = getCurrentUserInfo()
const shopId = (currentUser && currentUser.type !== 4 && currentUser.shopId) ? currentUser.shopId : undefined
2026-02-25 10:37:16 +08:00
Object.assign(formState, {
shopId: shopId,
2026-02-25 14:32:34 +08:00
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,
tags: [],
2026-02-25 14:32:34 +08:00
approximateDuration: '',
bookableArtists: [],
availableCoupons: []
2026-02-25 10:37:16 +08:00
})
imageFileList.value = []
uploadedFileIds.value = []
// 如果有店铺ID,获取该店铺的美甲师列表
if (shopId) {
fetchArtists(shopId)
}
2026-02-25 10:37:16 +08:00
modalVisible.value = true
}
2026-03-02 10:13:20 +08:00
const showEditModal = async (record: Product | Record<string, any>) => {
2026-02-25 10:37:16 +08:00
isEdit.value = true
2026-03-02 10:13:20 +08:00
currentProduct.value = record as Product
const recordId = 'id' in record ? record.id : (record as any).id
const recordShopId = 'shopId' in record ? record.shopId : (record as any).shopId
const recordTitle = 'title' in record ? record.title : (record as any).title
const recordDescription = 'description' in record ? record.description : (record as any).description
const recordImageFileIds = 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
const recordPrice = 'price' in record ? record.price : (record as any).price
const recordTags = 'tags' in record ? record.tags : (record as any).tags
const recordApproximateDuration = 'approximateDuration' in record ? record.approximateDuration : (record as any).approximateDuration
const recordBookableArtists = 'bookableArtists' in record ? record.bookableArtists : (record as any).bookableArtists
const recordAvailableCoupons = 'availableCoupons' in record ? record.availableCoupons : (record as any).availableCoupons
// 处理标签
2026-03-02 10:13:20 +08:00
const tagsArray = recordTags ? recordTags.split(';').filter((tag: string) => tag) : []
// 处理美甲师
2026-03-02 10:13:20 +08:00
const artistsArray = recordBookableArtists ? recordBookableArtists.split(',').filter((artist: string) => artist) : []
// 处理优惠券
2026-03-02 10:13:20 +08:00
const couponsArray = recordAvailableCoupons ? recordAvailableCoupons.split(',').filter((coupon: string) => coupon) : []
// 处理预计时长,将字符串转换为数字
2026-03-02 10:13:20 +08:00
const durationNumber = recordApproximateDuration ? parseInt(recordApproximateDuration.replace('分钟', '')) : undefined
2026-02-25 10:37:16 +08:00
Object.assign(formState, {
2026-03-02 10:13:20 +08:00
shopId: recordShopId,
title: recordTitle,
description: recordDescription,
imageFileIds: recordImageFileIds,
price: recordPrice,
tags: tagsArray,
approximateDuration: durationNumber,
bookableArtists: artistsArray,
availableCoupons: couponsArray
2026-02-25 10:37:16 +08:00
})
2026-03-02 10:13:20 +08:00
if (recordImageFileIds) {
const ids = recordImageFileIds.split(',')
uploadedFileIds.value = ids
2026-02-26 11:16:24 +08:00
// 加载所有图片签名链接
const fileList = []
for (const [index, id] of ids.entries()) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
2026-03-02 10:13:20 +08:00
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
fileList.push({
uid: id,
name: `图片${index + 1}`,
status: 'done',
url: imageIdCache.value[id]
})
} catch (error) {
console.error('获取签名链接失败:', error)
fileList.push({
uid: id,
name: `图片${index + 1}`,
status: 'done',
url: ''
})
}
}
imageFileList.value = fileList
} else {
imageFileList.value = []
uploadedFileIds.value = []
}
// 获取该店铺的美甲师列表
2026-03-02 10:13:20 +08:00
if (recordShopId) {
await fetchArtists(recordShopId)
}
2026-02-25 10:37:16 +08:00
modalVisible.value = true
}
2026-03-02 10:13:20 +08:00
const showViewModal = async (record: Product | Record<string, any>) => {
const recordShopId = 'shopId' in record ? record.shopId : (record as any).shopId
const recordTitle = 'title' in record ? record.title : (record as any).title
const recordDescription = 'description' in record ? record.description : (record as any).description
const recordImageFileIds = 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
const recordPrice = 'price' in record ? record.price : (record as any).price
const recordTags = 'tags' in record ? record.tags : (record as any).tags
const recordApproximateDuration = 'approximateDuration' in record ? record.approximateDuration : (record as any).approximateDuration
const recordBookableArtists = 'bookableArtists' in record ? record.bookableArtists : (record as any).bookableArtists
const recordAvailableCoupons = 'availableCoupons' in record ? record.availableCoupons : (record as any).availableCoupons
2026-02-28 14:32:28 +08:00
// 处理标签
2026-03-02 10:13:20 +08:00
const tagsArray = recordTags ? recordTags.split(';').filter((tag: string) => tag) : []
2026-02-28 14:32:28 +08:00
// 处理美甲师
2026-03-02 10:13:20 +08:00
const artistsArray = recordBookableArtists ? recordBookableArtists.split(',').filter((artist: string) => artist) : []
2026-02-28 14:32:28 +08:00
// 处理优惠券
2026-03-02 10:13:20 +08:00
const couponsArray = recordAvailableCoupons ? recordAvailableCoupons.split(',').filter((coupon: string) => coupon) : []
2026-02-28 14:32:28 +08:00
// 处理预计时长,将字符串转换为数字
2026-03-02 10:13:20 +08:00
const durationNumber = recordApproximateDuration ? parseInt(recordApproximateDuration.replace('分钟', '')) : undefined
2026-02-28 14:32:28 +08:00
Object.assign(viewFormState, {
2026-03-02 10:13:20 +08:00
shopId: recordShopId,
title: recordTitle,
description: recordDescription,
imageFileIds: recordImageFileIds,
price: recordPrice,
2026-02-28 14:32:28 +08:00
tags: tagsArray,
approximateDuration: durationNumber,
bookableArtists: artistsArray,
availableCoupons: couponsArray
})
// 加载图片URL
2026-03-02 10:13:20 +08:00
if (recordImageFileIds) {
const ids = recordImageFileIds.split(',')
2026-02-28 14:32:28 +08:00
const urls: string[] = []
for (const id of ids) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
2026-03-02 10:13:20 +08:00
const response = await adminFileApi.getSignedUrl(Number(id))
imageIdCache.value[id] = response.data
2026-02-28 14:32:28 +08:00
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_PRODUCT_IMAGE)
}
}
viewImageUrls.value = urls
} else {
viewImageUrls.value = [DEFAULT_PRODUCT_IMAGE]
}
// 获取该店铺的美甲师列表
2026-03-02 10:13:20 +08:00
if (recordShopId) {
await fetchArtists(recordShopId)
2026-02-28 14:32:28 +08:00
}
viewModalVisible.value = true
}
const handleViewModalCancel = () => {
viewModalVisible.value = false
viewImageUrls.value = []
}
const beforeUpload = (file: File) => {
const isImage = file.type.startsWith('image/')
if (!isImage) {
message.error('只能上传图片文件!')
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
message.error('图片大小不能超过 2MB!')
}
return isImage && isLt2M
}
const handleUploadImage = async (options: any) => {
const { file, onSuccess, onError } = options
try {
2026-03-02 10:13:20 +08:00
const response = await adminFileApi.uploadFile(file) as any
uploadedFileIds.value.push(String(response.id))
formState.imageFileIds = uploadedFileIds.value.join(',')
2026-03-02 10:13:20 +08:00
onSuccess({ url: String(response.id) })
message.success('图片上传成功')
} catch (error) {
console.error('图片上传失败:', error)
onError(error)
message.error('图片上传失败')
}
}
const handlePreview = async (file: any) => {
try {
// 检查缓存
if (!imageIdCache.value[file.uid]) {
2026-03-02 10:13:20 +08:00
const response = await adminFileApi.getSignedUrl(Number(file.uid)) as any
imageIdCache.value[file.uid] = response
}
2026-03-02 10:13:20 +08:00
previewImage.value = imageIdCache.value[file.uid] || ''
previewVisible.value = true
} catch (error) {
console.error('获取预览链接失败:', error)
message.error('获取预览链接失败')
}
}
const handleRemove = (file: any) => {
const index = uploadedFileIds.value.indexOf(file.uid)
if (index > -1) {
uploadedFileIds.value.splice(index, 1)
formState.imageFileIds = uploadedFileIds.value.join(',')
}
}
2026-02-25 10:37:16 +08:00
const handleModalOk = async () => {
try {
await formRef.value?.validate()
modalLoading.value = true
// 处理标签,转换为分号分隔的字符串
const tagsString = Array.isArray(formState.tags) ? formState.tags.join(';') : formState.tags
// 处理美甲师,转换为逗号分隔的字符串
const artistsString = Array.isArray(formState.bookableArtists) ? formState.bookableArtists.join(',') : formState.bookableArtists
// 处理优惠券,转换为逗号分隔的字符串
const couponsString = Array.isArray(formState.availableCoupons) ? formState.availableCoupons.join(',') : formState.availableCoupons
// 处理预计时长,转换为字符串并添加"分钟"单位
const durationString = formState.approximateDuration ? `${formState.approximateDuration}分钟` : ''
// 准备请求数据
const requestData = {
...formState,
imageFileIds: uploadedFileIds.value.join(','),
tags: tagsString,
bookableArtists: artistsString,
availableCoupons: couponsString,
2026-03-02 10:13:20 +08:00
approximateDuration: durationString,
shopId: formState.shopId || 0
}
2026-02-25 10:37:16 +08:00
if (isEdit.value && currentProduct.value) {
await adminProductApi.updateProduct(currentProduct.value.id, requestData)
2026-02-25 10:37:16 +08:00
message.success('更新成功')
} else {
await adminProductApi.createProduct(requestData)
2026-02-25 10:37:16 +08:00
message.success('创建成功')
}
modalVisible.value = false
fetchProductList()
2026-02-25 20:31:00 +08:00
} catch (error: any) {
2026-02-25 10:37:16 +08:00
console.error(error)
2026-02-25 20:31:00 +08:00
message.error(error.message || (isEdit.value ? '更新失败' : '创建失败'))
2026-02-25 10:37:16 +08:00
} finally {
modalLoading.value = false
}
}
const handleModalCancel = () => {
modalVisible.value = false
formRef.value?.resetFields()
}
2026-03-02 10:13:20 +08:00
const handleDelete = async (record: Product | Record<string, any>) => {
2026-02-25 10:37:16 +08:00
try {
2026-03-02 10:13:20 +08:00
const recordId = 'id' in record ? record.id : (record as any).id
await adminProductApi.deleteProduct(recordId)
2026-02-25 10:37:16 +08:00
message.success('删除成功')
// 从缓存中移除
2026-03-02 10:13:20 +08:00
delete imageUrls.value[recordId]
2026-02-25 10:37:16 +08:00
fetchProductList()
2026-02-25 20:31:00 +08:00
} catch (error: any) {
console.error('删除失败:', error)
message.error(error.message || '删除失败')
2026-02-25 10:37:16 +08:00
}
}
2026-02-26 18:44:47 +08:00
onMounted(async () => {
await loadUserPermissions()
2026-02-25 20:31:00 +08:00
fetchShops()
fetchTags()
fetchCoupons()
2026-02-25 10:37:16 +08:00
fetchProductList()
})
// 监听店铺选择变化,获取对应的标签和美甲师列表
watch(() => formState.shopId, (newShopId) => {
if (newShopId) {
fetchTags(newShopId)
fetchArtists(newShopId)
} else {
tags.value = []
artists.value = []
}
})
2026-02-25 10:37:16 +08:00
</script>
<style scoped>
.products {
padding: 0;
}
2026-02-26 11:16:24 +08:00
.product-images {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 4px;
}
.product-images :deep(.ant-image) {
border-radius: 4px;
}
</style>