Files
yu-zhi-ran/platform/frontend/assets.html
T

252 lines
15 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>宇之然内容创作平台 - 素材库</title>
<link rel="stylesheet" href="element-plus.css">
<link rel="stylesheet" href="theme-modern.css">
<style>
.asset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 16px; }
.asset-item { border: 1px solid #ebeef5; border-radius: 8px; padding: 12px; transition: all 0.3s; cursor: pointer; }
.asset-item:hover { border-color: #409eff; box-shadow: 0 2px 12px rgba(64,158,255,0.2); }
.asset-thumb { width: 100%; height: 120px; border-radius: 4px; object-fit: cover; background: #f5f7fa; display: flex; align-items: center; justify-content: center; font-size: 32px; margin-bottom: 8px; }
.asset-name { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.asset-meta { font-size: 12px; color: #909399; margin-top: 4px; }
.asset-tags { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px; }
.upload-area { border: 2px dashed #dcdfe6; border-radius: 12px; padding: 40px; text-align: center; cursor: pointer; transition: all 0.3s; }
.upload-area:hover { border-color: #409eff; background: #f0f9eb; }
.upload-icon { font-size: 48px; color: #c0c4cc; }
@media (max-width: 768px) {
.asset-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
}
</style>
<script src="uni-nav.js"></script>
</head>
<body>
<div id="app">
<uni-nav title="素材库" :username="currentUser.username" :is-admin="isAdmin" current-page="assets" @navigate="redirectToPage" @logout="handleLogout">
</uni-nav>
<div class="main-content">
<main class="content-area">
<h2 style="font-size: 24px; font-weight: 700; margin-bottom: 24px; color: #303133;"><el-icon style="vertical-align:-2px;"><IconPicture /></el-icon> 素材库</h2>
<div class="card page-fade">
<div class="filter-bar">
<el-input v-model="searchKeyword" placeholder="搜索素材..." clearable @clear="loadAssets" @keyup.enter="loadAssets" style="flex:1;min-width:140px;">
<template #prefix><el-icon style="vertical-align:-2px;"><IconSearch /></el-icon></template>
</el-input>
<el-select v-model="filterType" placeholder="文件类型" clearable @change="loadAssets" style="width:120px;">
<el-option label="全部" value=""></el-option>
<el-option label="图片" value="image"></el-option>
<el-option label="视频" value="video"></el-option>
<el-option label="文档" value="document"></el-option>
</el-select>
<el-select v-model="filterTag" placeholder="标签筛选" clearable @change="loadAssets" style="width:130px;">
<el-option v-for="tag in allTags" :key="tag" :label="tag" :value="tag"></el-option>
</el-select>
<el-button @click="loadAssets"><el-icon style="vertical-align:-2px;"><IconRefresh /></el-icon> 刷新</el-button>
<el-button type="primary" @click="showUploadDialog = true"><el-icon style="vertical-align:-2px;"><IconUpload /></el-icon> 上传素材</el-button>
</div>
<div style="margin-bottom: 16px; display: flex; gap: 20px; font-size: 14px; color: #606266;">
<span>总计: {{ assetStats.total }} 个</span>
<span v-for="(count, type) in assetStats.by_type" :key="type">{{ getTypeName(type) }}: {{ count }}</span>
</div>
<div v-if="loading" style="text-align: center; padding: 40px;">加载中...</div>
<div v-else-if="assets.length === 0" style="text-align: center; padding: 40px; color: #909399;">
<el-icon style="font-size:48px;margin-bottom:16px;color:#c0c4cc;"><IconPicture /></el-icon>
<div>暂无素材,点击上方按钮上传</div>
</div>
<div v-else class="asset-grid">
<div v-for="asset in assets" :key="asset.id" class="asset-item" @click="previewAsset(asset)">
<div class="asset-thumb">
<template v-if="asset.file_type === 'image'"><el-icon style="font-size:32px;"><IconPicture /></el-icon></template>
<template v-else-if="asset.file_type === 'video'">🎬</template>
<template v-else><el-icon style="font-size:32px;"><IconDocument /></el-icon></template>
</div>
<div class="asset-name">{{ asset.filename }}</div>
<div class="asset-meta">{{ formatSize(asset.size) }} | {{ formatDate(asset.created_at) }}</div>
<div class="asset-tags">
<el-tag v-for="tag in (asset.tags || [])" :key="tag" size="small" type="info">{{ tag }}</el-tag>
</div>
<div style="margin-top: 8px; display: flex; gap: 4px; justify-content: flex-end;">
<el-button size="small" type="primary" @click.stop="copyUrl(asset)">复制</el-button>
<el-button size="small" type="danger" @click.stop="deleteAsset(asset.id)">删除</el-button>
</div>
</div>
</div>
</div>
</main>
</div>
<el-dialog v-model="showUploadDialog" title="上传素材" width="500px">
<el-upload ref="uploadRef" drag :auto-upload="false" :limit="10" :on-change="handleFileChange" multiple accept="image/*,.pdf,.doc,.docx,.ppt,.pptx,.mp4,.mov,.avi">
<div class="upload-area">
<el-icon style="font-size:48px;color:#c0c4cc;"><IconUpload /></el-icon>
<div style="margin-top: 12px; color: #606266;">将文件拖到此处,或<span style="color: #409eff;">点击上传</span></div>
<div style="font-size: 12px; color: #909399; margin-top: 8px;">支持: JPG, PNG, GIF, WebP, PDF, Word, PPT, MP4</div>
</div>
</el-upload>
<div style="margin-top: 16px;">
<el-input v-model="uploadTags" placeholder="标签(逗号分隔)" style="margin-bottom: 12px;"></el-input>
<el-input v-model="uploadAltText" placeholder="描述文字(可选)"></el-input>
</div>
<template #footer>
<el-button @click="showUploadDialog = false">取消</el-button>
<el-button type="primary" @click="uploadFiles" :loading="uploading">上传</el-button>
</template>
</el-dialog>
<el-dialog v-model="showPreviewDialog" title="素材预览" width="800px">
<div v-if="previewAssetData" style="text-align: center;">
<div style="font-size: 64px; margin-bottom: 16px;">
<template v-if="previewAssetData.file_type === 'image'"><el-icon style="font-size:64px;"><IconPicture /></el-icon></template>
<template v-else-if="previewAssetData.file_type === 'video'">🎬</template>
<template v-else><el-icon style="font-size:64px;"><IconDocument /></el-icon></template>
</div>
<div style="font-size: 18px; font-weight: 600; margin-bottom: 8px;">{{ previewAssetData.filename }}</div>
<div style="color: #909399; font-size: 14px;">{{ formatSize(previewAssetData.size) }} | {{ formatDate(previewAssetData.created_at) }}</div>
<div v-if="previewAssetData.alt_text" style="margin-top: 12px; padding: 12px; background: #f5f7fa; border-radius: 8px;">{{ previewAssetData.alt_text }}</div>
<div class="asset-tags" style="justify-content: center; margin-top: 12px;">
<el-tag v-for="tag in (previewAssetData.tags || [])" :key="tag" size="small">{{ tag }}</el-tag>
</div>
<div style="margin-top: 16px; font-size: 13px; color: #606266;">使用次数: {{ previewAssetData.usage_count || 0 }}</div>
</div>
</el-dialog>
</div>
<script src="vue.global.prod.js"></script>
<script src="icon-components.js"></script>
<script src="element-plus.full.js"></script>
<script>
const AssetsApp = {
data() {
return {
currentUser: { username: '' },
isAdmin: false,
isLoggedIn: false,
assets: [],
allTags: [],
assetStats: { total: 0, by_type: {} },
loading: false,
searchKeyword: '',
filterType: '',
filterTag: '',
showUploadDialog: false,
showPreviewDialog: false,
previewAssetData: null,
uploadFiles: [],
uploadTags: '',
uploadAltText: '',
uploading: false
}
},
methods: {
handleLogout() { localStorage.removeItem('authToken'); localStorage.removeItem('userRole'); localStorage.removeItem('currentUser'); window.location.href = '/login.html'; },
redirectToPage(page) { window.location.href = page.startsWith('/') ? page : '/' + page; },
checkAuth() {
const token = localStorage.getItem('authToken');
if (!token) { window.location.href = '/login.html'; return; }
fetch('/api/auth/me', { headers: { 'Authorization': 'Bearer ' + token } })
.then(r => r.ok ? r.json() : Promise.reject())
.then(data => {
this.currentUser = data.user;
this.isAdmin = data.user.role === 'admin';
this.isLoggedIn = true;
this.loadAssets();
this.loadTags();
this.loadStats();
})
.catch(() => { localStorage.removeItem('authToken'); localStorage.removeItem('userRole'); localStorage.removeItem('currentUser'); window.location.href = '/login.html'; });
},
formatSize(bytes) {
if (!bytes) return '0 B';
const units = ['B', 'KB', 'MB', 'GB'];
let i = 0;
while (bytes >= 1024 && i < units.length - 1) { bytes /= 1024; i++; }
return bytes.toFixed(1) + ' ' + units[i];
},
formatDate(dateStr) {
if (!dateStr) return '-';
return new Date(dateStr).toLocaleDateString('zh-CN');
},
getTypeName(type) {
const map = { 'image': '图片', 'video': '视频', 'document': '文档' };
return map[type] || type;
},
async loadAssets() {
this.loading = true;
try {
const token = localStorage.getItem('authToken');
let url = '/api/assets?limit=100';
if (this.filterType) url += '&file_type=' + this.filterType;
if (this.filterTag) url += '&tag=' + this.filterTag;
if (this.searchKeyword) url += '&search=' + encodeURIComponent(this.searchKeyword);
const res = await fetch(url, { headers: { 'Authorization': 'Bearer ' + token } });
if (res.ok) this.assets = await res.json();
else { const d = await res.json().catch(() => ({})); throw new Error(d.detail || '加载失败'); }
} catch (e) { console.error(e); this.$message.error('加载素材失败: ' + e.message); }
finally { this.loading = false; }
},
async loadTags() {
try {
const token = localStorage.getItem('authToken');
const res = await fetch('/api/assets/tags', { headers: { 'Authorization': 'Bearer ' + token } });
if (res.ok) this.allTags = await res.json();
} catch (e) { console.error(e); this.$message.error('加载标签失败: ' + e.message); }
},
async loadStats() {
try {
const token = localStorage.getItem('authToken');
const res = await fetch('/api/assets/counts', { headers: { 'Authorization': 'Bearer ' + token } });
if (res.ok) this.assetStats = await res.json();
} catch (e) { console.error(e); this.$message.error('加载统计失败: ' + e.message); }
},
handleFileChange(file, fileList) { this.uploadFiles = fileList; },
async uploadFiles() {
if (this.uploadFiles.length === 0) { this.$message.warning('请选择文件'); return; }
this.uploading = true;
try {
const token = localStorage.getItem('authToken');
for (const fileItem of this.uploadFiles) {
const formData = new FormData();
formData.append('file', fileItem.raw);
if (this.uploadTags) formData.append('tags', this.uploadTags);
if (this.uploadAltText) formData.append('alt_text', this.uploadAltText);
const res = await fetch('/api/assets/upload', { method: 'POST', headers: { 'Authorization': 'Bearer ' + token }, body: formData });
if (!res.ok) throw new Error('上传失败');
}
this.$message.success('上传成功');
this.showUploadDialog = false;
this.uploadFiles = [];
this.uploadTags = '';
this.uploadAltText = '';
this.loadAssets();
this.loadStats();
} catch (e) { this.$message.error(e.message); }
finally { this.uploading = false; }
},
previewAsset(asset) { this.previewAssetData = asset; this.showPreviewDialog = true; },
async copyUrl(asset) {
const url = '/content/images/' + asset.filename.split('/').pop();
navigator.clipboard.writeText(window.location.origin + url).then(() => this.$message.success('链接已复制'));
},
async deleteAsset(id) {
try {
await this.$confirm('确定删除该素材?', '提示', { type: 'warning' });
const token = localStorage.getItem('authToken');
const res = await fetch('/api/assets/' + id, { method: 'DELETE', headers: { 'Authorization': 'Bearer ' + token } });
if (res.ok) { this.$message.success('删除成功'); this.loadAssets(); this.loadStats(); }
} catch (e) { if (e !== 'cancel') this.$message.error(e.message || '删除失败'); }
}
},
mounted() {
this.checkAuth();
}
};
const app = Vue.createApp(AssetsApp);
app.use(ElementPlus);
if (window.installIcons) { window.installIcons(app); }
if (window.installUniNav) { window.installUniNav(app); }
app.mount('#app');
</script>
<script src="ai-assistant.js"></script>
</body>
</html>