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

780 lines
24 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="shops">
<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 v-if="hasPermission('shop:create')" type="primary" @click="showAddModal">
<template #icon>
<PlusOutlined />
</template>
新增店铺
</a-button>
</a-space>
</template>
<a-table
:columns="columns"
:data-source="shopList"
:loading="loading"
:pagination="pagination"
@change="handleTableChange"
row-key="id"
>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'image'">
<div class="shop-images">
<a-image
v-for="(url, index) in getShopImages(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="getShopImages(record).length === 0"
:width="50"
:height="50"
:src="DEFAULT_SHOP_IMAGE"
style="object-fit: cover; border-radius: 4px;"
:preview="false"
/>
</div>
</template>
<template v-else-if="column.key === 'banner'">
<div class="shop-banner">
<a-image
v-for="(url, index) in getShopBannerImages(record)"
:key="index"
:width="80"
:height="40"
:src="url"
style="object-fit: cover; border-radius: 4px; margin-right: 4px;"
:preview="false"
/>
<span v-if="getShopBannerImages(record).length === 0" class="no-banner">暂无Banner</span>
</div>
</template>
<template v-else-if="column.key === 'actions'">
<a-space>
<a-button v-if="hasPermission('shop:view')" type="link" size="small" @click="showViewModal(record)">
查看
</a-button>
<a-button v-if="hasPermission('shop:edit')" type="link" size="small" @click="showEditModal(record)">
编辑
</a-button>
<a-popconfirm
v-if="hasPermission('shop:delete')"
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"
@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="shopName">
<a-input v-model:value="formState.shopName" 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="address">
<a-input v-model:value="formState.address" placeholder="请输入店铺地址" />
</a-form-item>
<a-form-item label="营业时间" name="businessHours">
<a-input v-model:value="formState.businessHours" placeholder="例如:09:00-21:00" />
</a-form-item>
<a-form-item label="Banner图片">
<div class="upload-tip">首页轮播图,建议尺寸 750x300 像素</div>
<a-upload
v-model:file-list="bannerFileList"
list-type="picture-card"
:custom-request="handleUploadBanner"
:before-upload="beforeUpload"
@preview="handleBannerPreview"
@remove="handleBannerRemove"
:max-count="5"
>
<div v-if="bannerFileList.length < 5">
<plus-outlined />
<div style="margin-top: 8px">上传Banner</div>
</div>
</a-upload>
<a-modal v-model:open="bannerPreviewVisible" :footer="null" @cancel="bannerPreviewVisible = false">
<img alt="预览" style="width: 100%" :src="bannerPreviewImage" />
</a-modal>
</a-form-item>
<a-form-item label="店铺图片">
<div class="upload-tip">店铺详情展示图</div>
<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>
</a-modal>
<!-- 查看店铺详情模态框 -->
<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.shopName" :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-input v-model:value="viewFormState.address" :disabled="true" />
</a-form-item>
<a-form-item label="营业时间">
<a-input v-model:value="viewFormState.businessHours" :disabled="true" />
</a-form-item>
<a-form-item label="Banner图片">
<div class="shop-images">
<a-image
v-for="(url, index) in viewBannerUrls"
:key="index"
:width="120"
:height="60"
:src="url"
style="object-fit: cover; border-radius: 4px; margin-right: 8px; margin-bottom: 8px;"
:preview="false"
/>
<span v-if="viewBannerUrls.length === 0" class="no-banner">暂无Banner</span>
</div>
</a-form-item>
<a-form-item label="店铺图片">
<div class="shop-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"
/>
<a-image
v-if="viewImageUrls.length === 0"
:width="80"
:height="80"
:src="DEFAULT_SHOP_IMAGE"
style="object-fit: cover; border-radius: 4px;"
:preview="false"
/>
</div>
</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_SHOP_IMAGE } from '@/constants'
import { adminShopApi, adminFileApi, type Shop } from '@/api/admin'
import { usePermission } from '@/utils/permission'
const { hasPermission, loadUserPermissions } = usePermission()
const loading = ref(false)
const searchText = ref('')
const shopList = ref<Shop[]>([])
const modalVisible = ref(false)
const viewModalVisible = ref(false)
const modalLoading = ref(false)
const isEdit = ref(false)
const formRef = ref<FormInstance>()
const currentShop = ref<Shop | null>(null)
const isAdmin = ref(false)
// Banner图片相关
const bannerFileList = ref<any[]>([])
const uploadedBannerIds = ref<string[]>([])
const bannerPreviewVisible = ref(false)
const bannerPreviewImage = ref('')
// 店铺图片相关
const imageFileList = ref<any[]>([])
const uploadedFileIds = ref<string[]>([])
const imageIdCache = ref<Record<string, string>>({})
const shopImageUrls = ref<Record<number, string[]>>({})
const shopBannerUrls = ref<Record<number, string[]>>({})
const viewImageUrls = ref<string[]>([])
const viewBannerUrls = ref<string[]>([])
const previewVisible = ref(false)
const previewImage = ref('')
// 查看店铺相关状态
const viewFormState = reactive({
shopName: '',
description: '',
address: '',
businessHours: '',
coverImageFileId: '',
imageFileIds: ''
})
const pagination = reactive({
current: 1,
pageSize: 10,
total: 0,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total: number) => `共 ${total} 条`
})
const formState = reactive({
shopName: '',
description: '',
address: '',
businessHours: '',
coverImageFileId: '',
imageFileIds: ''
})
const formRules: any = {
shopName: [{ required: true, message: '请输入店铺名称', trigger: 'blur' }],
description: [{ required: true, message: '请输入店铺描述', trigger: 'blur' }],
address: [{ required: true, message: '请输入店铺地址', trigger: 'blur' }],
businessHours: [{ required: true, message: '请输入营业时间', trigger: 'blur' }]
}
const columns = [
{ title: '图片', key: 'image', width: 150 },
{ title: 'Banner', key: 'banner', width: 200 },
{ title: '店铺名称', dataIndex: 'shopName', key: 'shopName' },
{ title: '店铺描述', dataIndex: 'description', key: 'description' },
{ title: '店铺地址', dataIndex: 'address', key: 'address' },
{ title: '营业时间', dataIndex: 'businessHours', key: 'businessHours' },
{ title: '创建时间', dataIndex: 'createTime', key: 'createTime' },
{ title: '操作', key: 'actions', width: 150, fixed: 'right' as const }
]
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 loadImageUrls = async (shops: Shop[]) => {
for (const shop of shops) {
// 加载店铺图片
if (shop.imageFileIds) {
const ids = shop.imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_SHOP_IMAGE)
}
}
}
shopImageUrls.value[shop.id] = urls
} else {
shopImageUrls.value[shop.id] = []
}
// 加载Banner图片
if (shop.coverImageFileId) {
const ids = shop.coverImageFileId.split(',')
const urls: string[] = []
for (const id of ids) {
if (id) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_SHOP_IMAGE)
}
}
}
shopBannerUrls.value[shop.id] = urls
} else {
shopBannerUrls.value[shop.id] = []
}
}
}
const getShopImages = (shop: Shop | Record<string, any>): string[] => {
const shopId = 'id' in shop ? shop.id : (shop as any).id
return shopImageUrls.value[shopId] || []
}
const getShopBannerImages = (shop: Shop | Record<string, any>): string[] => {
const shopId = 'id' in shop ? shop.id : (shop as any).id
return shopBannerUrls.value[shopId] || []
}
const fetchShopList = async () => {
loading.value = true
try {
const currentUser = getCurrentUserInfo()
const params: any = {
page: pagination.current,
size: pagination.pageSize,
keyword: searchText.value
}
if (currentUser && currentUser.type !== 4 && currentUser.shopId) {
params.shopId = currentUser.shopId
}
const response = await adminShopApi.listShops(params)
const res = typeof response === 'object' && 'list' in response ? response : (response as any).data
shopList.value = res.list || []
pagination.total = res.total || 0
// 预加载所有图片签名链接
await loadImageUrls(res.list || [])
} catch (error) {
message.error('获取店铺列表失败')
} finally {
loading.value = false
}
}
const handleSearch = () => {
pagination.current = 1
fetchShopList()
}
const handleTableChange = (pag: any) => {
pagination.current = pag.current
pagination.pageSize = pag.pageSize
fetchShopList()
}
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
}
// Banner图片上传
const handleUploadBanner = async (options: any) => {
const { file, onSuccess, onError } = options
try {
const response = await adminFileApi.uploadFile(file)
const res = typeof response === 'object' && 'id' in response ? response : (response as any).data
uploadedBannerIds.value.push(String(res.id))
formState.coverImageFileId = uploadedBannerIds.value.join(',')
onSuccess({ url: String(res.id) })
message.success('Banner上传成功')
} catch (error) {
console.error('Banner上传失败:', error)
onError(error)
message.error('Banner上传失败')
}
}
const handleBannerPreview = async (file: any) => {
try {
// 检查缓存
if (!imageIdCache.value[file.uid]) {
const response = await adminFileApi.getSignedUrl(Number(file.uid)) as any
imageIdCache.value[file.uid] = response
}
bannerPreviewImage.value = imageIdCache.value[file.uid] || ''
bannerPreviewVisible.value = true
} catch (error) {
console.error('获取预览链接失败:', error)
message.error('获取预览链接失败')
}
}
const handleBannerRemove = (file: any) => {
const index = uploadedBannerIds.value.indexOf(file.uid)
if (index > -1) {
uploadedBannerIds.value.splice(index, 1)
formState.coverImageFileId = uploadedBannerIds.value.join(',')
}
}
// 店铺图片上传
const handleUploadImage = async (options: any) => {
const { file, onSuccess, onError } = options
try {
const response = await adminFileApi.uploadFile(file)
const res = typeof response === 'object' && 'id' in response ? response : (response as any).data
uploadedFileIds.value.push(String(res.id))
formState.imageFileIds = uploadedFileIds.value.join(',')
onSuccess({ url: String(res.id) })
message.success('图片上传成功')
} catch (error) {
console.error('图片上传失败:', error)
onError(error)
message.error('图片上传失败')
}
}
const handlePreview = async (file: any) => {
try {
// 检查缓存
if (!imageIdCache.value[file.uid]) {
const response = await adminFileApi.getSignedUrl(Number(file.uid)) as any
imageIdCache.value[file.uid] = response
}
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(',')
}
}
const showAddModal = () => {
isEdit.value = false
Object.assign(formState, {
shopName: '',
description: '',
address: '',
businessHours: '',
coverImageFileId: '',
imageFileIds: ''
})
bannerFileList.value = []
uploadedBannerIds.value = []
imageFileList.value = []
uploadedFileIds.value = []
modalVisible.value = true
}
const showEditModal = async (record: Shop | Record<string, any>) => {
isEdit.value = true
currentShop.value = record as Shop
Object.assign(formState, {
shopName: 'shopName' in record ? record.shopName : (record as any).shopName,
description: 'description' in record ? record.description : (record as any).description,
address: 'address' in record ? record.address : (record as any).address,
businessHours: 'businessHours' in record ? record.businessHours : (record as any).businessHours,
coverImageFileId: 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId,
imageFileIds: 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
})
// 加载Banner图片
const coverImageFileId = 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId
if (coverImageFileId) {
const ids = coverImageFileId.split(',')
uploadedBannerIds.value = ids
// 加载所有图片签名链接
const fileList = []
for (const [index, id] of ids.entries()) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
fileList.push({
uid: id,
name: `Banner${index + 1}`,
status: 'done',
url: imageIdCache.value[id]
})
} catch (error) {
console.error('获取签名链接失败:', error)
fileList.push({
uid: id,
name: `Banner${index + 1}`,
status: 'done',
url: ''
})
}
}
bannerFileList.value = fileList
} else {
bannerFileList.value = []
uploadedBannerIds.value = []
}
// 加载店铺图片
const imageFileIds = 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
if (imageFileIds) {
const ids = imageFileIds.split(',')
uploadedFileIds.value = ids
// 加载所有图片签名链接
const fileList = []
for (const [index, id] of ids.entries()) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
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 = []
}
modalVisible.value = true
}
const showViewModal = async (record: Shop | Record<string, any>) => {
Object.assign(viewFormState, {
shopName: 'shopName' in record ? record.shopName : (record as any).shopName,
description: 'description' in record ? record.description : (record as any).description,
address: 'address' in record ? record.address : (record as any).address,
businessHours: 'businessHours' in record ? record.businessHours : (record as any).businessHours,
coverImageFileId: 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId,
imageFileIds: 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
})
// 加载Banner图片URL
const coverImageFileId = 'coverImageFileId' in record ? record.coverImageFileId : (record as any).coverImageFileId
if (coverImageFileId) {
const ids = coverImageFileId.split(',')
const urls: string[] = []
for (const id of ids) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_SHOP_IMAGE)
}
}
viewBannerUrls.value = urls
} else {
viewBannerUrls.value = []
}
// 加载店铺图片URL
const imageFileIds = 'imageFileIds' in record ? record.imageFileIds : (record as any).imageFileIds
if (imageFileIds) {
const ids = imageFileIds.split(',')
const urls: string[] = []
for (const id of ids) {
try {
// 检查缓存
if (!imageIdCache.value[id]) {
const response = await adminFileApi.getSignedUrl(Number(id)) as any
imageIdCache.value[id] = response
}
urls.push(imageIdCache.value[id])
} catch (error) {
console.error('获取签名链接失败:', error)
urls.push(DEFAULT_SHOP_IMAGE)
}
}
viewImageUrls.value = urls
} else {
viewImageUrls.value = []
}
viewModalVisible.value = true
}
const handleViewModalCancel = () => {
viewModalVisible.value = false
viewImageUrls.value = []
viewBannerUrls.value = []
}
const handleModalOk = async () => {
try {
await formRef.value?.validate()
modalLoading.value = true
if (isEdit.value && currentShop.value) {
await adminShopApi.updateShop(currentShop.value.id, formState)
message.success('更新成功')
} else {
await adminShopApi.createShop(formState)
message.success('创建成功')
}
modalVisible.value = false
fetchShopList()
} catch (error) {
console.error(error)
} finally {
modalLoading.value = false
}
}
const handleModalCancel = () => {
modalVisible.value = false
formRef.value?.resetFields()
bannerFileList.value = []
uploadedBannerIds.value = []
imageFileList.value = []
uploadedFileIds.value = []
}
const handleDelete = async (record: Shop | Record<string, any>) => {
try {
const shopId = 'id' in record ? record.id : (record as any).id
await adminShopApi.deleteShop(shopId)
message.success('删除成功')
fetchShopList()
} catch (error) {
message.error('删除失败')
}
}
onMounted(async () => {
await loadUserPermissions()
fetchShopList()
})
</script>
<style scoped>
.shops {
padding: 0;
}
.shop-images {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 4px;
}
.shop-banner {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 4px;
}
.no-banner {
color: #999;
font-size: 12px;
}
.upload-tip {
color: #999;
font-size: 12px;
margin-bottom: 8px;
}
</style>