商品管理的图片编辑和用户管理的图片编辑功能优化,配置文件的优化
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
<a-image
|
||||
:width="60"
|
||||
:height="60"
|
||||
:src="record.imageFileIds ? record.imageFileIds.split(',')[0] : DEFAULT_PRODUCT_IMAGE"
|
||||
:src="imageUrls[record.id] || DEFAULT_PRODUCT_IMAGE"
|
||||
style="object-fit: cover; border-radius: 4px;"
|
||||
/>
|
||||
</template>
|
||||
@@ -65,7 +65,7 @@
|
||||
<a-modal
|
||||
v-model:open="modalVisible"
|
||||
:title="isEdit ? '编辑商品' : '新增商品'"
|
||||
width="600px"
|
||||
width="700px"
|
||||
@ok="handleModalOk"
|
||||
@cancel="handleModalCancel"
|
||||
:confirmLoading="modalLoading"
|
||||
@@ -87,8 +87,24 @@
|
||||
:rows="3"
|
||||
/>
|
||||
</a-form-item>
|
||||
<a-form-item label="图片ID" name="imageFileIds">
|
||||
<a-input v-model:value="formState.imageFileIds" placeholder="请输入图片文件ID,多个用逗号分隔" />
|
||||
<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
|
||||
@@ -105,6 +121,12 @@
|
||||
<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"
|
||||
@@ -136,10 +158,10 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, reactive, onMounted } from 'vue'
|
||||
import { message } from 'ant-design-vue'
|
||||
import type { FormInstance } 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 } from '@/api/admin'
|
||||
import { adminProductApi, type Product, adminShopApi, type Shop, adminFileApi, type ObjectStorageFileVO } from '@/api/admin'
|
||||
|
||||
const loading = ref(false)
|
||||
const searchText = ref('')
|
||||
@@ -151,6 +173,12 @@ const formRef = ref<FormInstance>()
|
||||
const currentProduct = ref<Product | null>(null)
|
||||
const shops = ref<Shop[]>([])
|
||||
const loadingShops = 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 pagination = reactive({
|
||||
current: 1,
|
||||
@@ -239,6 +267,9 @@ const fetchProductList = async () => {
|
||||
const res = await adminProductApi.listProducts(params)
|
||||
productList.value = res.list || []
|
||||
pagination.total = res.total || 0
|
||||
|
||||
// 预加载图片签名链接
|
||||
await loadImageUrls(res.list || [])
|
||||
} catch (error) {
|
||||
message.error('获取商品列表失败')
|
||||
} finally {
|
||||
@@ -246,6 +277,31 @@ const fetchProductList = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
const loadImageUrls = async (products: Product[]) => {
|
||||
for (const product of products) {
|
||||
if (product.imageFileIds) {
|
||||
const firstId = product.imageFileIds.split(',')[0]
|
||||
if (firstId) {
|
||||
try {
|
||||
// 检查缓存
|
||||
if (!imageIdCache.value[firstId]) {
|
||||
const signedUrl = await adminFileApi.getSignedUrl(Number(firstId))
|
||||
imageIdCache.value[firstId] = signedUrl
|
||||
}
|
||||
imageUrls.value[product.id] = imageIdCache.value[firstId]
|
||||
} catch (error) {
|
||||
console.error('获取签名链接失败:', error)
|
||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
||||
}
|
||||
} else {
|
||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
||||
}
|
||||
} else {
|
||||
imageUrls.value[product.id] = DEFAULT_PRODUCT_IMAGE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearch = () => {
|
||||
pagination.current = 1
|
||||
fetchProductList()
|
||||
@@ -271,10 +327,12 @@ const showAddModal = () => {
|
||||
bookableArtists: '',
|
||||
availableCoupons: ''
|
||||
})
|
||||
imageFileList.value = []
|
||||
uploadedFileIds.value = []
|
||||
modalVisible.value = true
|
||||
}
|
||||
|
||||
const showEditModal = (record: Product) => {
|
||||
const showEditModal = async (record: Product) => {
|
||||
isEdit.value = true
|
||||
currentProduct.value = record
|
||||
Object.assign(formState, {
|
||||
@@ -288,9 +346,94 @@ const showEditModal = (record: Product) => {
|
||||
bookableArtists: record.bookableArtists,
|
||||
availableCoupons: record.availableCoupons
|
||||
})
|
||||
|
||||
if (record.imageFileIds) {
|
||||
const ids = record.imageFileIds.split(',')
|
||||
uploadedFileIds.value = ids
|
||||
|
||||
// 加载图片签名链接
|
||||
const fileList = []
|
||||
for (const [index, id] of ids.entries()) {
|
||||
try {
|
||||
// 检查缓存
|
||||
if (!imageIdCache.value[id]) {
|
||||
const signedUrl = await adminFileApi.getSignedUrl(Number(id))
|
||||
imageIdCache.value[id] = signedUrl
|
||||
}
|
||||
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 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 {
|
||||
const res = await adminFileApi.uploadFile(file)
|
||||
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 signedUrl = await adminFileApi.getSignedUrl(Number(file.uid))
|
||||
imageIdCache.value[file.uid] = signedUrl
|
||||
}
|
||||
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 handleModalOk = async () => {
|
||||
try {
|
||||
await formRef.value?.validate()
|
||||
@@ -323,6 +466,8 @@ const handleDelete = async (record: Product) => {
|
||||
try {
|
||||
await adminProductApi.deleteProduct(record.id)
|
||||
message.success('删除成功')
|
||||
// 从缓存中移除
|
||||
delete imageUrls.value[record.id]
|
||||
fetchProductList()
|
||||
} catch (error: any) {
|
||||
console.error('删除失败:', error)
|
||||
@@ -340,4 +485,4 @@ onMounted(() => {
|
||||
.products {
|
||||
padding: 0;
|
||||
}
|
||||
</style>
|
||||
</style>
|
||||
Reference in New Issue
Block a user