Skip to content

一分钟搭建影视站,支持Docker等部署方式

refactor: 重构标签管理功能,使用默认标签替代自定义标签,优化加载和保存逻辑

developer1e299655c263
diff --git a/js/douban.js b/js/douban.js
index ff8d09d..d14be3f 100644
--- a/js/douban.js
+++ b/js/douban.js
@@ -1,42 +1,50 @@
// 豆瓣热门电影电视剧推荐功能
-// 豆瓣标签列表
-let movieTags = ['热门', '最新', '经典', '豆瓣高分', '冷门佳片', '华语', '欧美', '韩国', '日本', '动作', '喜剧', '爱情', '科幻', '悬疑', '恐怖', '治愈'];
-let tvTags = ['热门', '美剧', '英剧', '韩剧', '日剧', '国产剧', '港剧', '日本动画', '综艺', '纪录片'];
+// 豆瓣标签列表 - 修改为默认标签
+let defaultMovieTags = ['热门', '最新', '经典', '豆瓣高分', '冷门佳片', '华语', '欧美', '韩国', '日本', '动作', '喜剧', '爱情', '科幻', '悬疑', '恐怖', '治愈'];
+let defaultTvTags = ['热门', '美剧', '英剧', '韩剧', '日剧', '国产剧', '港剧', '日本动画', '综艺', '纪录片'];
-// 自定义标签列表
-let customMovieTags = [];
-let customTvTags = [];
+// 用户标签列表 - 存储用户实际使用的标签(包含保留的系统标签和用户添加的自定义标签)
+let movieTags = [];
+let tvTags = [];
-// 加载自定义标签
-function loadCustomTags() {
+// 加载用户标签
+function loadUserTags() {
try {
- const savedCustomMovieTags = localStorage.getItem('customMovieTags');
- const savedCustomTvTags = localStorage.getItem('customTvTags');
+ // 尝试从本地存储加载用户保存的标签
+ const savedMovieTags = localStorage.getItem('userMovieTags');
+ const savedTvTags = localStorage.getItem('userTvTags');
- if (savedCustomMovieTags) {
- customMovieTags = JSON.parse(savedCustomMovieTags);
+ // 如果本地存储中有标签数据,则使用它
+ if (savedMovieTags) {
+ movieTags = JSON.parse(savedMovieTags);
+ } else {
+ // 否则使用默认标签
+ movieTags = [...defaultMovieTags];
}
- if (savedCustomTvTags) {
- customTvTags = JSON.parse(savedCustomTvTags);
+ if (savedTvTags) {
+ tvTags = JSON.parse(savedTvTags);
+ } else {
+ // 否则使用默认标签
+ tvTags = [...defaultTvTags];
}
} catch (e) {
- console.error('加载自定义标签失败:', e);
- // 初始化为空数组,防止错误
- customMovieTags = [];
- customTvTags = [];
+ console.error('加载标签失败:', e);
+ // 初始化为默认值,防止错误
+ movieTags = [...defaultMovieTags];
+ tvTags = [...defaultTvTags];
}
}
-// 保存自定义标签
-function saveCustomTags() {
+// 保存用户标签
+function saveUserTags() {
try {
- localStorage.setItem('customMovieTags', JSON.stringify(customMovieTags));
- localStorage.setItem('customTvTags', JSON.stringify(customTvTags));
+ localStorage.setItem('userMovieTags', JSON.stringify(movieTags));
+ localStorage.setItem('userTvTags', JSON.stringify(tvTags));
} catch (e) {
- console.error('保存自定义标签失败:', e);
- showToast('保存自定义标签失败', 'error');
+ console.error('保存标签失败:', e);
+ showToast('保存标签失败', 'error');
}
}
@@ -83,11 +91,8 @@ function initDouban() {
updateDoubanVisibility();
}
- // 加载自定义标签
- loadCustomTags();
-
- // 获取豆瓣热门标签
- fetchDoubanTags();
+ // 加载用户标签
+ loadUserTags();
// 渲染电影/电视剧切换
renderDoubanMovieTvSwitch();
@@ -269,17 +274,15 @@ function renderDoubanMovieTvSwitch() {
}
// 渲染豆瓣标签选择器
-function renderDoubanTags(tags = movieTags) {
+function renderDoubanTags(tags) {
const tagContainer = document.getElementById('douban-tags');
if (!tagContainer) return;
- tagContainer.innerHTML = '';
-
- // 确定当前应该使用的标签和自定义标签列表
- const currentCustomTags = doubanMovieTvCurrentSwitch === 'movie' ? customMovieTags : customTvTags;
+ // 确定当前应该使用的标签列表
+ const currentTags = doubanMovieTvCurrentSwitch === 'movie' ? movieTags : tvTags;
- // 合并系统标签和自定义标签
- const allTags = [...tags, ...currentCustomTags];
+ // 清空标签容器
+ tagContainer.innerHTML = '';
// 先添加标签管理按钮
const manageBtn = document.createElement('button');
@@ -290,25 +293,21 @@ function renderDoubanTags(tags = movieTags) {
};
tagContainer.appendChild(manageBtn);
- // 添加系统标签和自定义标签
- allTags.forEach(tag => {
+ // 添加所有标签
+ currentTags.forEach(tag => {
const btn = document.createElement('button');
- // 更新标签样式:统一高度,添加过渡效果,改进颜色对比度
- let btnClass = 'py-1.5 px-3.5 rounded text-sm font-medium transition-all duration-300 ';
- // 自定义标签使用不同的背景颜色来区分
- const isCustomTag = currentCustomTags.includes(tag);
+ // 设置样式
+ let btnClass = 'py-1.5 px-3.5 rounded text-sm font-medium transition-all duration-300 ';
+ // 当前选中的标签使用高亮样式
if (tag === doubanCurrentTag) {
btnClass += 'bg-pink-600 text-white shadow-md';
} else {
- btnClass += isCustomTag ?
- 'bg-[#2a1a2a] text-gray-300 hover:bg-pink-700 hover:text-white' :
- 'bg-[#1a1a1a] text-gray-300 hover:bg-pink-700 hover:text-white';
+ btnClass += 'bg-[#1a1a1a] text-gray-300 hover:bg-pink-700 hover:text-white';
}
btn.className = btnClass;
-
btn.textContent = tag;
btn.onclick = function() {
@@ -316,7 +315,7 @@ function renderDoubanTags(tags = movieTags) {
doubanCurrentTag = tag;
doubanPageStart = 0;
renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
- renderDoubanTags(tags);
+ renderDoubanTags();
}
};
@@ -559,10 +558,10 @@ function showTagManageModal() {
modal.id = 'tagManageModal';
modal.className = 'fixed inset-0 bg-black bg-opacity-75 flex items-center justify-center z-50';
- // 当前使用的标签类型
+ // 当前使用的标签类型和默认标签
const isMovie = doubanMovieTvCurrentSwitch === 'movie';
- const currentCustomTags = isMovie ? customMovieTags : customTvTags;
- const currentSystemTags = isMovie ? movieTags : tvTags;
+ const currentTags = isMovie ? movieTags : tvTags;
+ const defaultTags = isMovie ? defaultMovieTags : defaultTvTags;
// 模态框内容
modal.innerHTML = `
@@ -572,26 +571,28 @@ function showTagManageModal() {
<h3 class="text-xl font-bold text-white mb-4">标签管理 (${isMovie ? '电影' : '电视剧'})</h3>
<div class="mb-4">
- <h4 class="text-lg font-medium text-gray-300 mb-2">系统标签</h4>
- <div class="grid grid-cols-2 sm:grid-cols-3 gap-2 mb-4" id="systemTagsList">
- ${currentSystemTags.map(tag => `
- <div class="bg-[#1a1a1a] text-gray-300 py-1.5 px-3 rounded text-sm font-medium flex justify-between items-center">
- <span>${tag}</span>
- </div>
- `).join('')}
+ <div class="flex justify-between items-center mb-2">
+ <h4 class="text-lg font-medium text-gray-300">标签列表</h4>
+ <button id="resetTagsBtn" class="text-xs px-2 py-1 bg-gray-700 hover:bg-gray-600 text-white rounded">
+ 恢复默认标签
+ </button>
</div>
- </div>
-
- <div class="mb-6">
- <h4 class="text-lg font-medium text-gray-300 mb-2">自定义标签</h4>
- <div class="grid grid-cols-2 sm:grid-cols-3 gap-2 mb-4" id="customTagsList">
- ${currentCustomTags.length ? currentCustomTags.map(tag => `
- <div class="bg-[#2a1a2a] text-gray-300 py-1.5 px-3 rounded text-sm font-medium flex justify-between items-center group">
- <span>${tag}</span>
- <button class="delete-tag-btn text-gray-500 hover:text-red-500 opacity-0 group-hover:opacity-100 transition-opacity"
- data-tag="${tag}">✕</button>
- </div>
- `).join('') : `<div class="col-span-full text-center py-4 text-gray-500">暂无自定义标签</div>`}
+ <div class="grid grid-cols-2 sm:grid-cols-3 gap-2 mb-4" id="tagsGrid">
+ ${currentTags.length ? currentTags.map(tag => {
+ // "热门"标签不能删除
+ const canDelete = tag !== '热门';
+ return `
+ <div class="bg-[#1a1a1a] text-gray-300 py-1.5 px-3 rounded text-sm font-medium flex justify-between items-center group">
+ <span>${tag}</span>
+ ${canDelete ?
+ `<button class="delete-tag-btn text-gray-500 hover:text-red-500 opacity-0 group-hover:opacity-100 transition-opacity"
+ data-tag="${tag}">✕</button>` :
+ `<span class="text-gray-500 text-xs italic opacity-0 group-hover:opacity-100">必需</span>`
+ }
+ </div>
+ `;
+ }).join('') :
+ `<div class="col-span-full text-center py-4 text-gray-500">无标签,请添加或恢复默认</div>`}
</div>
</div>
@@ -627,12 +628,18 @@ function showTagManageModal() {
}
});
+ // 添加事件监听器 - 恢复默认标签按钮
+ document.getElementById('resetTagsBtn').addEventListener('click', function() {
+ resetTagsToDefault();
+ showTagManageModal(); // 重新加载模态框
+ });
+
// 添加事件监听器 - 删除标签按钮
const deleteButtons = document.querySelectorAll('.delete-tag-btn');
deleteButtons.forEach(btn => {
btn.addEventListener('click', function() {
const tagToDelete = this.getAttribute('data-tag');
- deleteCustomTag(tagToDelete);
+ deleteTag(tagToDelete);
showTagManageModal(); // 重新加载模态框
});
});
@@ -644,15 +651,15 @@ function showTagManageModal() {
const newTag = input.value.trim();
if (newTag) {
- addCustomTag(newTag);
+ addTag(newTag);
input.value = '';
showTagManageModal(); // 重新加载模态框
}
});
}
-// 添加自定义标签
-function addCustomTag(tag) {
+// 添加标签
+function addTag(tag) {
// 安全处理标签名,防止XSS
const safeTag = tag
.replace(/</g, '&lt;')
@@ -661,13 +668,10 @@ function addCustomTag(tag) {
// 确定当前使用的是电影还是电视剧标签
const isMovie = doubanMovieTvCurrentSwitch === 'movie';
-
- // 获取当前的系统标签和自定义标签
- const currentSystemTags = isMovie ? movieTags : tvTags;
- const currentCustomTags = isMovie ? customMovieTags : customTvTags;
+ const currentTags = isMovie ? movieTags : tvTags;
// 检查是否已存在(忽略大小写)
- const exists = [...currentSystemTags, ...currentCustomTags].some(
+ const exists = currentTags.some(
existingTag => existingTag.toLowerCase() === safeTag.toLowerCase()
);
@@ -676,39 +680,43 @@ function addCustomTag(tag) {
return;
}
- // 添加到对应的自定义标签数组
+ // 添加到对应的标签数组
if (isMovie) {
- customMovieTags.push(safeTag);
+ movieTags.push(safeTag);
} else {
- customTvTags.push(safeTag);
+ tvTags.push(safeTag);
}
// 保存到本地存储
- saveCustomTags();
+ saveUserTags();
// 重新渲染标签
- renderDoubanTags(isMovie ? movieTags : tvTags);
+ renderDoubanTags();
showToast('标签添加成功', 'success');
}
-// 删除自定义标签
-function deleteCustomTag(tag) {
+// 删除标签
+function deleteTag(tag) {
+ // 热门标签不能删除
+ if (tag === '热门') {
+ showToast('热门标签不能删除', 'warning');
+ return;
+ }
+
// 确定当前使用的是电影还是电视剧标签
const isMovie = doubanMovieTvCurrentSwitch === 'movie';
-
- // 引用当前的自定义标签数组
- const currentCustomTags = isMovie ? customMovieTags : customTvTags;
+ const currentTags = isMovie ? movieTags : tvTags;
// 寻找标签索引
- const index = currentCustomTags.indexOf(tag);
+ const index = currentTags.indexOf(tag);
// 如果找到标签,则删除
if (index !== -1) {
- currentCustomTags.splice(index, 1);
+ currentTags.splice(index, 1);
// 保存到本地存储
- saveCustomTags();
+ saveUserTags();
// 如果当前选中的是被删除的标签,则重置为"热门"
if (doubanCurrentTag === tag) {
@@ -718,8 +726,34 @@ function deleteCustomTag(tag) {
}
// 重新渲染标签
- renderDoubanTags(isMovie ? movieTags : tvTags);
+ renderDoubanTags();
showToast('标签删除成功', 'success');
}
}
+
+// 重置为默认标签
+function resetTagsToDefault() {
+ // 确定当前使用的是电影还是电视剧
+ const isMovie = doubanMovieTvCurrentSwitch === 'movie';
+
+ // 重置为默认标签
+ if (isMovie) {
+ movieTags = [...defaultMovieTags];
+ } else {
+ tvTags = [...defaultTvTags];
+ }
+
+ // 设置当前标签为热门
+ doubanCurrentTag = '热门';
+ doubanPageStart = 0;
+
+ // 保存到本地存储
+ saveUserTags();
+
+ // 重新渲染标签和内容
+ renderDoubanTags();
+ renderRecommend(doubanCurrentTag, doubanPageSize, doubanPageStart);
+
+ showToast('已恢复默认标签', 'success');
+}