feat: 视频问题

This commit is contained in:
2025-11-28 20:26:47 +08:00
parent 46b98e78e5
commit d9f3103304
26 changed files with 1582 additions and 1021 deletions

View File

@@ -0,0 +1,175 @@
/**
* 封面缓存工具
* 使用localStorage缓存视频封面base64数据降低OSS请求成本
*/
const CACHE_KEY = 'tik_video_covers'
const CACHE_EXPIRE_DAYS = 7 // 缓存过期时间7天
/**
* 获取所有缓存的封面
* @returns {Object} 缓存对象 { fileId: { base64: string, timestamp: number } }
*/
function getAllCaches() {
try {
const cacheJson = localStorage.getItem(CACHE_KEY)
return cacheJson ? JSON.parse(cacheJson) : {}
} catch (error) {
console.warn('[CoverCache] 读取缓存失败:', error)
return {}
}
}
/**
* 保存缓存
* @param {Object} caches 缓存对象
*/
function saveCaches(caches) {
try {
localStorage.setItem(CACHE_KEY, JSON.stringify(caches))
} catch (error) {
console.warn('[CoverCache] 保存缓存失败:', error)
}
}
/**
* 从缓存获取封面
* @param {number} fileId 文件ID
* @returns {string|null} base64数据或null
*/
function getCover(fileId) {
const caches = getAllCaches()
const cache = caches[fileId]
if (!cache) {
return null
}
// 检查是否过期
const now = Date.now()
const expireTime = CACHE_EXPIRE_DAYS * 24 * 60 * 60 * 1000
if (now - cache.timestamp > expireTime) {
// 缓存过期,删除
delete caches[fileId]
saveCaches(caches)
return null
}
return cache.base64
}
/**
* 保存封面到缓存
* @param {number} fileId 文件ID
* @param {string} base64 base64数据
*/
function setCover(fileId, base64) {
if (!fileId || !base64) {
return
}
const caches = getAllCaches()
caches[fileId] = {
base64,
timestamp: Date.now()
}
saveCaches(caches)
}
/**
* 批量保存封面
* @param {Array} coverList 封面列表 [{ fileId, base64 }]
*/
function batchSetCovers(coverList) {
if (!Array.isArray(coverList) || coverList.length === 0) {
return
}
const caches = getAllCaches()
let hasChange = false
coverList.forEach(({ fileId, base64 }) => {
if (fileId && base64 && !caches[fileId]) {
caches[fileId] = {
base64,
timestamp: Date.now()
}
hasChange = true
}
})
if (hasChange) {
saveCaches(caches)
}
}
/**
* 清理所有过期缓存
*/
function cleanExpired() {
const caches = getAllCaches()
const now = Date.now()
const expireTime = CACHE_EXPIRE_DAYS * 24 * 60 * 60 * 1000
let hasChange = false
Object.keys(caches).forEach(fileId => {
if (now - caches[fileId].timestamp > expireTime) {
delete caches[fileId]
hasChange = true
}
})
if (hasChange) {
saveCaches(caches)
}
}
/**
* 清空所有缓存
*/
function clearAll() {
try {
localStorage.removeItem(CACHE_KEY)
} catch (error) {
console.warn('[CoverCache] 清空缓存失败:', error)
}
}
/**
* 获取缓存统计信息
* @returns {Object} 统计信息
*/
function getStats() {
const caches = getAllCaches()
const now = Date.now()
const expireTime = CACHE_EXPIRE_DAYS * 24 * 60 * 60 * 1000
let expiredCount = 0
Object.keys(caches).forEach(fileId => {
if (now - caches[fileId].timestamp > expireTime) {
expiredCount++
}
})
return {
total: Object.keys(caches).length,
expired: expiredCount,
valid: Object.keys(caches).length - expiredCount
}
}
// 页面加载时自动清理过期缓存
if (typeof window !== 'undefined') {
window.addEventListener('load', () => {
cleanExpired()
})
}
export default {
getCover,
setCover,
batchSetCovers,
cleanExpired,
clearAll,
getStats
}