-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathfavoriteStore.js
More file actions
216 lines (188 loc) · 5.56 KB
/
favoriteStore.js
File metadata and controls
216 lines (188 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
export const useFavoriteStore = defineStore('favorite', () => {
// 收藏列表
const favorites = ref([])
// 从localStorage加载收藏数据
const loadFavorites = () => {
try {
const stored = localStorage.getItem('drplayer-favorites')
if (stored) {
favorites.value = JSON.parse(stored)
}
} catch (error) {
console.error('加载收藏数据失败:', error)
favorites.value = []
}
}
// 保存收藏数据到localStorage
const saveFavorites = () => {
try {
localStorage.setItem('drplayer-favorites', JSON.stringify(favorites.value))
} catch (error) {
console.error('保存收藏数据失败:', error)
}
}
// 添加收藏
const addFavorite = (videoData) => {
const favoriteItem = {
id: videoData.vod_id,
name: videoData.vod_name,
pic: videoData.vod_pic,
year: videoData.vod_year,
area: videoData.vod_area,
type_name: videoData.type_name,
remarks: videoData.vod_remarks,
director: videoData.vod_director,
actor: videoData.vod_actor,
// 保存API调用信息,用于从收藏进入详情页
api_info: {
module: videoData.module || '',
api_url: videoData.api_url || '',
site_name: videoData.site_name || '',
ext: videoData.ext || null // 添加站源扩展配置
},
created_at: new Date().toISOString(),
updated_at: new Date().toISOString()
}
// 检查是否已存在
const existingIndex = favorites.value.findIndex(item =>
item.id === favoriteItem.id &&
item.api_info.api_url === favoriteItem.api_info.api_url
)
if (existingIndex === -1) {
favorites.value.unshift(favoriteItem)
saveFavorites()
return true
}
return false
}
// 移除收藏
const removeFavorite = (videoId, apiUrl) => {
const index = favorites.value.findIndex(item =>
item.id === videoId && item.api_info.api_url === apiUrl
)
if (index !== -1) {
favorites.value.splice(index, 1)
saveFavorites()
return true
}
return false
}
// 检查是否已收藏
const isFavorited = (videoId, apiUrl) => {
return favorites.value.some(item =>
item.id === videoId && item.api_info.api_url === apiUrl
)
}
// 获取收藏项
const getFavorite = (videoId, apiUrl) => {
return favorites.value.find(item =>
item.id === videoId && item.api_info.api_url === apiUrl
)
}
// 清空收藏
const clearFavorites = () => {
favorites.value = []
saveFavorites()
}
// 导出收藏数据
const exportFavorites = () => {
const exportData = {
version: '1.0',
export_time: new Date().toISOString(),
favorites: favorites.value
}
const blob = new Blob([JSON.stringify(exportData, null, 2)], {
type: 'application/json'
})
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `drplayer-favorites-${new Date().toISOString().split('T')[0]}.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
// 导入收藏数据
const importFavorites = (file) => {
return new Promise((resolve, reject) => {
const reader = new FileReader()
reader.onload = (e) => {
try {
const importData = JSON.parse(e.target.result)
// 验证数据格式
if (!importData.favorites || !Array.isArray(importData.favorites)) {
throw new Error('无效的收藏数据格式')
}
// 合并数据,避免重复
let importCount = 0
importData.favorites.forEach(item => {
const exists = favorites.value.some(existing =>
existing.id === item.id &&
existing.api_info.api_url === item.api_info.api_url
)
if (!exists) {
favorites.value.push({
...item,
updated_at: new Date().toISOString()
})
importCount++
}
})
saveFavorites()
resolve(importCount)
} catch (error) {
reject(error)
}
}
reader.onerror = () => {
reject(new Error('文件读取失败'))
}
reader.readAsText(file)
})
}
// 计算属性
const favoriteCount = computed(() => favorites.value.length)
const favoritesByType = computed(() => {
const grouped = {}
favorites.value.forEach(item => {
// 根据站源名称中的标识进行分类
const siteName = item.api_info?.site_name || ''
let type = '影视' // 默认分类
if (siteName.includes('[书]')) {
type = '小说'
} else if (siteName.includes('[画]')) {
type = '漫画'
} else if (siteName.includes('[密]')) {
type = '密'
} else if (siteName.includes('[听]')) {
type = '音频'
} else if (siteName.includes('[儿]')) {
type = '少儿'
}
if (!grouped[type]) {
grouped[type] = []
}
grouped[type].push(item)
})
return grouped
})
// 初始化时加载数据
loadFavorites()
return {
favorites,
favoriteCount,
favoritesByType,
addFavorite,
removeFavorite,
isFavorited,
getFavorite,
clearFavorites,
exportFavorites,
importFavorites,
loadFavorites,
saveFavorites
}
})