完善了新增商品功能,将标签、美甲师和优惠券的输入方式改为下拉选择,并支持多选

## 1. 修复选择店铺后报错的问题
- 修改了 fetchArtists 方法,将 res 改为 res?.list ,因为后端返回的是分页数据结构,这样就不会再出现 Cannot read properties of undefined (reading '__asyncLoader') 错误。
## 2. 完善表单验证规则
- 在 formRules 中添加了标签和美甲师的必填验证规则,确保:
- 店铺是必填的
- 标签是必填的
- 美甲师是必填的
- 优惠券是可选的
## 3. 实现标签分店铺的功能
- 修改后端代码 :
- 修改了 TagDO 实体类,添加了 shopId 字段
- 修改了 TagDTO 和 TagVO 类,添加了 shopId 字段
- 修改了 TagService 接口,添加了 listTagsByShopId 方法
- 修改了 TagServiceImpl 实现类,添加了 listTagsByShopId 方法的实现
- 修改了 TagController 类,添加了 /list/by-shop/{shopId} 接口
- 修改前端代码 :
- 在 adminTagApi 中添加了 listTagsByShopId 方法
- 修改了 fetchTags 方法,添加了根据店铺ID获取标签的功能
- 修改了 watch 监听器,当选择店铺时,同时获取该店铺的标签和美甲师
## 4. 实现美甲师分店铺的功能
- 确保美甲师列表是根据选择的店铺动态加载的,只显示当前店铺的美甲师
This commit is contained in:
xiaowang
2026-02-26 15:39:39 +08:00
parent 4275f1840c
commit 9cc74c26e6
12 changed files with 654 additions and 297 deletions
+248 -62
View File
@@ -100,52 +100,13 @@
:rows="3"
/>
</a-form-item>
<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 v-model:value="formState.approximateDuration" placeholder="例如:30分钟" />
</a-form-item>
<a-form-item label="标签ID" name="tags">
<a-input v-model:value="formState.tags" placeholder="请输入标签ID,多个用分号分隔" />
</a-form-item>
<a-form-item label="可预约美甲师ID" name="bookableArtists">
<a-input v-model:value="formState.bookableArtists" placeholder="请输入美甲师ID,多个用逗号分隔" />
</a-form-item>
<a-form-item label="可用优惠券ID" name="availableCoupons">
<a-input v-model:value="formState.availableCoupons" placeholder="请输入优惠券ID,多个用逗号分隔" />
</a-form-item>
<a-form-item label="店铺" name="shopId">
<a-select
v-model:value="formState.shopId"
placeholder="请选择店铺"
style="width: 100%"
:loading="loadingShops"
:disabled="!isAdmin && formState.shopId"
>
<a-select-option
v-if="shops.length === 0"
@@ -163,18 +124,128 @@
</a-select-option>
</a-select>
</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"
>
<a-select-option
v-if="coupons.length === 0"
disabled
value=""
>
暂无优惠券数据
</a-select-option>
<a-select-option
v-for="coupon in coupons"
:key="coupon.id"
:value="coupon.id.toString()"
>
{{ coupon.name }}
</a-select-option>
</a-select>
</a-form-item>
</template>
</a-form>
</a-modal>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ref, reactive, onMounted, watch } from 'vue'
import { message } from 'ant-design-vue'
import type { FormInstance, UploadProps } from 'ant-design-vue'
import { PlusOutlined } from '@ant-design/icons-vue'
import { DEFAULT_PRODUCT_IMAGE } from '@/constants'
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO } from '@/api/admin'
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO, adminTagApi, adminArtistApi, adminCouponApi } from '@/api/admin'
const loading = ref(false)
const searchText = ref('')
@@ -182,16 +253,23 @@ const productList = ref<Product[]>([])
const modalVisible = ref(false)
const modalLoading = ref(false)
const isEdit = ref(false)
const formRef = ref<FormInstance>()
const currentProduct = ref<Product | null>(null)
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[]>([])
const imageUrls = ref<Record<number, string[]>>({})
const imageIdCache = ref<Record<string, string>>({})
const isAdmin = ref(false)
const formRef = ref<any>(null)
const pagination = reactive({
current: 1,
@@ -203,22 +281,24 @@ const pagination = reactive({
})
const formState = reactive({
shopId: undefined as number | undefined,
shopId: undefined,
title: '',
description: '',
imageFileIds: '',
price: 0,
tags: '',
approximateDuration: '',
bookableArtists: '',
availableCoupons: ''
tags: [] as string[],
approximateDuration: undefined,
bookableArtists: [] as string[],
availableCoupons: [] as string[]
})
const formRules = {
title: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
description: [{ required: true, message: '请输入商品描述', trigger: 'blur' }],
price: [{ required: true, message: '请输入价格', trigger: 'blur' }],
shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }]
shopId: [{ required: true, message: '请选择店铺', trigger: 'change' }],
tags: [{ required: true, message: '请选择标签', trigger: 'change' }],
bookableArtists: [{ required: true, message: '请选择美甲师', trigger: 'change' }]
}
const columns = [
@@ -237,7 +317,10 @@ const getCurrentUserInfo = () => {
try {
const userInfoStr = localStorage.getItem('adminUserInfo')
if (userInfoStr) {
return JSON.parse(userInfoStr)
const userInfo = JSON.parse(userInfoStr)
// 判断用户是否为管理员(类型为4)
isAdmin.value = userInfo.type === 4
return userInfo
}
} catch (error) {
console.error('获取当前用户信息失败', error)
@@ -249,11 +332,17 @@ const fetchShops = async () => {
loadingShops.value = true
try {
const res = await adminShopApi.listAllShops()
shops.value = res || []
const currentUser = getCurrentUserInfo()
// 根据用户权限控制店铺列表的显示
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
// 如果是店长,只显示用户所属的店铺
shops.value = res?.filter(shop => shop.id === currentUser.shopId) || []
// 默认选择用户所属的店铺,并且不可修改
formState.shopId = currentUser.shopId
} else {
// 如果是管理员,显示所有店铺
shops.value = res || []
}
} catch (error) {
console.error('获取店铺列表失败', error)
@@ -263,6 +352,50 @@ const fetchShops = async () => {
}
}
const fetchTags = async (shopId?: number) => {
loadingTags.value = true
try {
let res
if (shopId) {
res = await adminTagApi.listTagsByShopId(shopId)
} else {
res = await adminTagApi.listAllTags()
}
tags.value = res || []
} catch (error) {
console.error('获取标签列表失败', error)
message.error('获取标签列表失败')
} finally {
loadingTags.value = false
}
}
const fetchArtists = async (shopId?: number) => {
loadingArtists.value = true
try {
const res = await adminArtistApi.listArtists(shopId)
artists.value = res?.list || []
} catch (error) {
console.error('获取美甲师列表失败', error)
message.error('获取美甲师列表失败')
} finally {
loadingArtists.value = false
}
}
const fetchCoupons = async () => {
loadingCoupons.value = true
try {
const res = await adminCouponApi.listAllCoupons()
coupons.value = res || []
} catch (error) {
console.error('获取优惠券列表失败', error)
message.error('获取优惠券列表失败')
} finally {
loadingCoupons.value = false
}
}
const fetchProductList = async () => {
loading.value = true
try {
@@ -337,35 +470,50 @@ const handleTableChange = (pag: any) => {
const showAddModal = () => {
isEdit.value = false
const currentUser = getCurrentUserInfo()
const shopId = (currentUser && currentUser.type !== 4 && currentUser.shopId) ? currentUser.shopId : undefined
Object.assign(formState, {
shopId: (currentUser && currentUser.type !== 4 && currentUser.shopId) ? currentUser.shopId : undefined,
shopId: shopId,
title: '',
description: '',
imageFileIds: '',
price: 0,
tags: '',
tags: [],
approximateDuration: '',
bookableArtists: '',
availableCoupons: ''
bookableArtists: [],
availableCoupons: []
})
imageFileList.value = []
uploadedFileIds.value = []
// 如果有店铺ID,获取该店铺的美甲师列表
if (shopId) {
fetchArtists(shopId)
}
modalVisible.value = true
}
const showEditModal = async (record: Product) => {
isEdit.value = true
currentProduct.value = record
// 处理标签
const tagsArray = record.tags ? record.tags.split(';').filter(tag => tag) : []
// 处理美甲师
const artistsArray = record.bookableArtists ? record.bookableArtists.split(',').filter(artist => artist) : []
// 处理优惠券
const couponsArray = record.availableCoupons ? record.availableCoupons.split(',').filter(coupon => coupon) : []
// 处理预计时长,将字符串转换为数字
const durationNumber = record.approximateDuration ? parseInt(record.approximateDuration.replace('分钟', '')) : undefined
Object.assign(formState, {
shopId: record.shopId,
title: record.title,
description: record.description,
imageFileIds: record.imageFileIds,
price: record.price,
tags: record.tags,
approximateDuration: record.approximateDuration,
bookableArtists: record.bookableArtists,
availableCoupons: record.availableCoupons
tags: tagsArray,
approximateDuration: durationNumber,
bookableArtists: artistsArray,
availableCoupons: couponsArray
})
if (record.imageFileIds) {
@@ -402,6 +550,12 @@ const showEditModal = async (record: Product) => {
imageFileList.value = []
uploadedFileIds.value = []
}
// 获取该店铺的美甲师列表
if (record.shopId) {
await fetchArtists(record.shopId)
}
modalVisible.value = true
}
@@ -460,11 +614,30 @@ const handleModalOk = async () => {
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,
approximateDuration: durationString
}
if (isEdit.value && currentProduct.value) {
await adminProductApi.updateProduct(currentProduct.value.id, formState)
await adminProductApi.updateProduct(currentProduct.value.id, requestData)
message.success('更新成功')
} else {
await adminProductApi.createProduct(formState)
await adminProductApi.createProduct(requestData)
message.success('创建成功')
}
@@ -498,8 +671,21 @@ const handleDelete = async (record: Product) => {
onMounted(() => {
fetchShops()
fetchTags()
fetchCoupons()
fetchProductList()
})
// 监听店铺选择变化,获取对应的标签和美甲师列表
watch(() => formState.shopId, (newShopId) => {
if (newShopId) {
fetchTags(newShopId)
fetchArtists(newShopId)
} else {
tags.value = []
artists.value = []
}
})
</script>
<style scoped>