-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathvisitedStore.js
More file actions
63 lines (55 loc) · 1.63 KB
/
visitedStore.js
File metadata and controls
63 lines (55 loc) · 1.63 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
import { defineStore } from 'pinia';
export const useVisitedStore = defineStore('visited', {
state: () => ({
lastClickedVideoId: null, // 最后点击的视频ID
lastClickedVideoName: null // 最后点击的视频名称
}),
getters: {
// 检查是否是最后点击的视频
isLastClicked: (state) => (videoId) => {
return state.lastClickedVideoId === videoId;
}
},
actions: {
// 记录最后点击的视频
setLastClicked(videoId, videoName) {
if (!videoId) return;
this.lastClickedVideoId = videoId;
this.lastClickedVideoName = videoName;
// 保存到localStorage
this.saveToStorage();
},
// 清除记录
clear() {
this.lastClickedVideoId = null;
this.lastClickedVideoName = null;
localStorage.removeItem('last-clicked-video');
},
// 保存到localStorage
saveToStorage() {
try {
const data = {
videoId: this.lastClickedVideoId,
videoName: this.lastClickedVideoName
};
localStorage.setItem('last-clicked-video', JSON.stringify(data));
} catch (error) {
console.warn('保存最后点击视频失败:', error);
}
},
// 从localStorage加载
loadFromStorage() {
try {
const stored = localStorage.getItem('last-clicked-video');
if (stored) {
const data = JSON.parse(stored);
this.lastClickedVideoId = data.videoId;
this.lastClickedVideoName = data.videoName;
}
} catch (error) {
console.warn('加载最后点击视频失败:', error);
this.clear();
}
}
}
});