完成案例横幅图片编辑功能
This commit is contained in:
@@ -1,7 +1,346 @@
|
||||
<template>
|
||||
<div>
|
||||
<div class="page-cover-management">
|
||||
<!-- 页面头部 -->
|
||||
<el-card class="page-header-card">
|
||||
<h1 class="page-title">基地概况页面封面图管理</h1>
|
||||
</el-card>
|
||||
|
||||
<!-- 封面图管理核心区域 -->
|
||||
<el-card class="cover-management-card mt-4">
|
||||
<div class="cover-uploader-container">
|
||||
<h3 class="section-title">封面图上传</h3>
|
||||
<p class="section-desc">建议上传16:9比例图片,支持JPG/PNG/WEBP格式,大小不超过5MB</p>
|
||||
|
||||
<div class="cover-uploader">
|
||||
<!-- 已上传图片预览 -->
|
||||
<div v-if="formData.pageImageUrl" class="cover-preview">
|
||||
<img :src="formData.pageImageUrl" alt="基地概况页面封面" class="cover-img">
|
||||
<button
|
||||
class="remove-cover-btn"
|
||||
@click="removePageImage"
|
||||
title="删除封面图"
|
||||
:disabled="isSaving"
|
||||
>
|
||||
<el-icon><Close /></el-icon>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 未上传时的上传区域 -->
|
||||
<el-upload
|
||||
v-else
|
||||
class="cover-upload-area"
|
||||
:action="uploadAction"
|
||||
name="image"
|
||||
:show-file-list="false"
|
||||
:on-success="handleCoverSuccess"
|
||||
:before-upload="beforeUpload"
|
||||
:on-error="handlePageImageUploadError"
|
||||
:disabled="isSaving"
|
||||
>
|
||||
<div class="upload-placeholder">
|
||||
<el-icon class="upload-icon"><Upload /></el-icon>
|
||||
<p class="upload-text">点击或拖拽图片至此处上传</p>
|
||||
<p class="upload-subtext">支持JPG/PNG/WEBP,最大5MB,建议16:9比例</p>
|
||||
</div>
|
||||
</el-upload>
|
||||
</div>
|
||||
|
||||
<!-- 操作按钮区域 -->
|
||||
<div class="cover-action-buttons mt-4">
|
||||
<el-button
|
||||
type="primary"
|
||||
@click="saveImage"
|
||||
:loading="isSaving"
|
||||
:disabled="!formData.pageImageUrl || isSaving"
|
||||
>
|
||||
<el-icon><Check /></el-icon>
|
||||
保存封面图
|
||||
</el-button>
|
||||
</div>
|
||||
</div>
|
||||
</el-card>
|
||||
</div>
|
||||
</template>
|
||||
<script setup lang="ts">
|
||||
|
||||
<script lang="ts" setup>
|
||||
import { ref, reactive, onMounted } from 'vue';
|
||||
import { ElMessage } from 'element-plus';
|
||||
import { Close, Upload, Check } from '@element-plus/icons-vue';
|
||||
import type { UploadProps } from 'element-plus';
|
||||
import axios from "axios";
|
||||
|
||||
// 保存状态(防止重复提交)
|
||||
const isSaving = ref(false);
|
||||
// 上传接口地址
|
||||
const uploadAction = import.meta.env.VITE_API_BASE_URL + '/upload/cover';
|
||||
|
||||
// 表单数据
|
||||
const formData = reactive({
|
||||
pageImageUrl: '' // 页面封面图URL
|
||||
});
|
||||
|
||||
// 页面挂载时加载现有封面图
|
||||
onMounted(() => {
|
||||
fetchCoverImage();
|
||||
});
|
||||
|
||||
// 从后端获取已保存的封面图
|
||||
const fetchCoverImage = async () => {
|
||||
try {
|
||||
const response = await axios.post('http://localhost:8080/api/page-image/get', { page: 'CaseResources' });
|
||||
if (response.data.message === '查询成功' && Array.isArray(response.data.images) && response.data.images.length) {
|
||||
formData.pageImageUrl = response.data.images[0].image_url;
|
||||
} else {
|
||||
ElMessage.info('暂无已保存的封面图,可上传新图片');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('封面图加载失败:', error);
|
||||
ElMessage.warning('未获取到现有封面图,可重新上传');
|
||||
}
|
||||
};
|
||||
|
||||
// 上传前校验
|
||||
const beforeUpload: UploadProps['beforeUpload'] = (rawFile) => {
|
||||
const allowTypes = ['image/jpeg', 'image/png', 'image/webp'];
|
||||
if (!allowTypes.includes(rawFile.type)) {
|
||||
ElMessage.error('仅支持JPG/PNG/WEBP格式的图片');
|
||||
return false;
|
||||
}
|
||||
if (rawFile.size / 1024 / 1024 > 5) {
|
||||
ElMessage.error('图片大小不能超过5MB');
|
||||
return false;
|
||||
}
|
||||
console.log("图片校验成功");
|
||||
return true;
|
||||
};
|
||||
|
||||
// 封面上传成功回调
|
||||
const handleCoverSuccess: UploadProps['onSuccess'] = (response) => {
|
||||
const ossUrl = response.data?.url;
|
||||
if (ossUrl) {
|
||||
formData.pageImageUrl = ossUrl;
|
||||
ElMessage.success('封面上传成功');
|
||||
} else {
|
||||
ElMessage.error('封面上传失败:未获取到图片地址');
|
||||
}
|
||||
};
|
||||
|
||||
// 上传失败处理
|
||||
const handlePageImageUploadError = (error: any) => {
|
||||
console.error('页面封面图上传错误:', error);
|
||||
ElMessage.error('页面封面图上传失败,请重试');
|
||||
};
|
||||
|
||||
// 删除封面图
|
||||
const removePageImage = () => {
|
||||
formData.pageImageUrl = '';
|
||||
ElMessage.info('页面封面图已删除');
|
||||
};
|
||||
|
||||
// 保存封面图到后端
|
||||
const saveImage = async () => {
|
||||
try {
|
||||
isSaving.value = true;
|
||||
if (!formData.pageImageUrl) {
|
||||
ElMessage.warning('请先上传封面图再保存');
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await axios.post('http://localhost:8080/api/page-image/save', {
|
||||
id: 8,
|
||||
page: 'CaseResources',
|
||||
image_url: formData.pageImageUrl,
|
||||
});
|
||||
|
||||
if (response.data.success || response.data.message === '保存成功') {
|
||||
ElMessage.success('封面图保存成功');
|
||||
console.log('封面图保存成功,地址:', formData.pageImageUrl);
|
||||
} else {
|
||||
ElMessage.error('封面图保存失败:' + (response.data.message || '未知错误'));
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('封面图保存失败:', error);
|
||||
ElMessage.error('封面图保存失败,请重试');
|
||||
} finally {
|
||||
isSaving.value = false;
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.page-cover-management {
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.page-header-card {
|
||||
padding: 25px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.page-title {
|
||||
font-size: 24px;
|
||||
font-weight: 600;
|
||||
margin: 0;
|
||||
color: #1d2129;
|
||||
}
|
||||
|
||||
.cover-management-card {
|
||||
padding: 25px;
|
||||
}
|
||||
|
||||
.cover-uploader-container {
|
||||
max-width: 800px;
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 16px;
|
||||
font-weight: 500;
|
||||
margin-bottom: 8px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.section-desc {
|
||||
font-size: 14px;
|
||||
color: #666;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.cover-uploader {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
/* 已上传图片预览样式 */
|
||||
.cover-preview {
|
||||
width: 100%;
|
||||
height: 225px; /* 16:9比例适配 */
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
position: relative;
|
||||
background: #f5f5f5;
|
||||
box-shadow: 0 2px 8px rgba(0,0,0,0.08);
|
||||
}
|
||||
|
||||
.cover-img {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
object-fit: cover;
|
||||
}
|
||||
|
||||
.remove-cover-btn {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
right: 10px;
|
||||
width: 36px;
|
||||
height: 36px;
|
||||
border-radius: 50%;
|
||||
background: rgba(0, 0, 0, 0.6);
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: background 0.3s;
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
.remove-cover-btn:hover {
|
||||
background: rgba(255, 0, 0, 0.8);
|
||||
}
|
||||
|
||||
.remove-cover-btn:disabled {
|
||||
background: rgba(0, 0, 0, 0.3);
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
/* 上传区域样式 */
|
||||
.cover-upload-area {
|
||||
width: 100%;
|
||||
height: 225px;
|
||||
}
|
||||
|
||||
.upload-placeholder {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border: 2px dashed #ccc;
|
||||
border-radius: 8px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
background: #fafafa;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s;
|
||||
}
|
||||
|
||||
.upload-placeholder:hover {
|
||||
border-color: #409eff;
|
||||
background: #f0f7ff;
|
||||
}
|
||||
|
||||
.upload-icon {
|
||||
font-size: 36px;
|
||||
color: #999;
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
|
||||
.upload-text {
|
||||
color: #666;
|
||||
font-size: 14px;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
|
||||
.upload-subtext {
|
||||
color: #999;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
/* 操作按钮区域 */
|
||||
.cover-action-buttons {
|
||||
display: flex;
|
||||
gap: 15px;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
/* 响应式适配 */
|
||||
@media (max-width: 768px) {
|
||||
.page-cover-management {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.cover-management-card {
|
||||
padding: 15px;
|
||||
}
|
||||
|
||||
.cover-preview,
|
||||
.cover-upload-area {
|
||||
height: 180px;
|
||||
}
|
||||
|
||||
.cover-action-buttons {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.el-button {
|
||||
width: 100%;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 480px) {
|
||||
.page-title {
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.cover-preview,
|
||||
.cover-upload-area {
|
||||
height: 150px;
|
||||
}
|
||||
|
||||
.upload-icon {
|
||||
font-size: 28px;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user