refactor(project): Reorganize frontend and backend directories
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import type { VideoInfo, PlayRecord } from '@/types'
|
||||
import { videoApi } from '@/api/video.ts'
|
||||
|
||||
export const useVideoStore = defineStore('video', () => {
|
||||
// 当前播放的视频
|
||||
const currentVideo = ref<VideoInfo | null>(null)
|
||||
|
||||
// 播放进度
|
||||
const playProgress = ref<number>(0)
|
||||
|
||||
// 播放历史(本地存储)
|
||||
const playHistory = ref<PlayRecord[]>([])
|
||||
|
||||
// 是否正在播放
|
||||
const isPlaying = ref(false)
|
||||
|
||||
// 弹幕开关
|
||||
const danmakuEnabled = ref(true)
|
||||
|
||||
// 音量
|
||||
const volume = ref(0.8)
|
||||
|
||||
// 播放速度
|
||||
const playbackRate = ref(1)
|
||||
|
||||
// 设置当前视频
|
||||
function setCurrentVideo(video: VideoInfo | null) {
|
||||
currentVideo.value = video
|
||||
}
|
||||
|
||||
// 更新播放进度
|
||||
function updateProgress(progress: number) {
|
||||
playProgress.value = progress
|
||||
}
|
||||
|
||||
// 切换播放状态
|
||||
function togglePlaying(playing: boolean) {
|
||||
isPlaying.value = playing
|
||||
}
|
||||
|
||||
// 切换弹幕
|
||||
function toggleDanmaku(enabled: boolean) {
|
||||
danmakuEnabled.value = enabled
|
||||
}
|
||||
|
||||
// 设置音量
|
||||
function setVolume(vol: number) {
|
||||
volume.value = vol
|
||||
}
|
||||
|
||||
// 设置播放速度
|
||||
function setPlaybackRate(rate: number) {
|
||||
playbackRate.value = rate
|
||||
}
|
||||
|
||||
// 记录播放
|
||||
async function recordPlay(videoId: number, progress: number, duration: number) {
|
||||
try {
|
||||
await videoApi.recordPlay({ videoId, progress, duration })
|
||||
|
||||
// 更新本地播放历史
|
||||
const index = playHistory.value.findIndex(item => item.videoId === videoId)
|
||||
const record: PlayRecord = { videoId, progress, duration }
|
||||
|
||||
if (index >= 0) {
|
||||
playHistory.value[index] = record
|
||||
} else {
|
||||
playHistory.value.unshift(record)
|
||||
// 最多保存100条记录
|
||||
if (playHistory.value.length > 100) {
|
||||
playHistory.value = playHistory.value.slice(0, 100)
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('记录播放失败:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// 获取播放进度
|
||||
function getPlayProgress(videoId: number): number {
|
||||
const record = playHistory.value.find(item => item.videoId === videoId)
|
||||
return record?.progress || 0
|
||||
}
|
||||
|
||||
// 清除当前视频
|
||||
function clearCurrentVideo() {
|
||||
currentVideo.value = null
|
||||
playProgress.value = 0
|
||||
isPlaying.value = false
|
||||
}
|
||||
|
||||
return {
|
||||
// 状态
|
||||
currentVideo,
|
||||
playProgress,
|
||||
playHistory,
|
||||
isPlaying,
|
||||
danmakuEnabled,
|
||||
volume,
|
||||
playbackRate,
|
||||
// 方法
|
||||
setCurrentVideo,
|
||||
updateProgress,
|
||||
togglePlaying,
|
||||
toggleDanmaku,
|
||||
setVolume,
|
||||
setPlaybackRate,
|
||||
recordPlay,
|
||||
getPlayProgress,
|
||||
clearCurrentVideo
|
||||
}
|
||||
}, {
|
||||
persist: {
|
||||
paths: ['playHistory', 'danmakuEnabled', 'volume', 'playbackRate']
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user