/** * 精选筛选模块 * 从本地服务器获取加密的精选列表并解密 */ class FeaturedFilter { constructor() { this.cache = new BrowserCache(); this.decryptedList = null; this.decryptionKey = 'shanhai-pfp-2025'; // 解密密钥(需与后端保持一致) } /** * 从本地服务器获取并解密精选列表 * @returns {Promise} 精选编号数组 */ async loadFeaturedList() { // 1. 检查缓存 const cached = this.cache.getCache('featured_list'); if (cached) { this.decryptedList = cached; console.log('✓ 使用缓存的精选列表'); return cached; } // 2. 从本地服务器API获取加密数据 try { console.log('→ 从服务器获取精选列表...'); const response = await fetch('/api/v1/pfp/featured'); if (!response.ok) { throw new Error(`HTTP错误! 状态: ${response.status}`); } const result = await response.json(); if (!result.success) { throw new Error(result.error || '获取精选列表失败'); } // 3. 解密 const decrypted = this.decrypt(result.encrypted); // 4. 缓存解密后的数据 this.cache.setCache('featured_list', decrypted); this.decryptedList = decrypted; console.log(`✓ 精选列表已解密,共 ${decrypted.length} 个编号`); return decrypted; } catch (error) { console.error('✗ 获取精选列表失败:', error); throw error; } } /** * 解密数据(双重Base64解码) * @param {string} encryptedStr - 加密的Base64字符串 * @returns {Array} 解密后的数组 */ decrypt(encryptedStr) { try { // 第一次Base64解码 const decodedOnce = atob(encryptedStr); // 第二次Base64解码 const decodedTwice = atob(decodedOnce); // 解析JSON const data = JSON.parse(decodedTwice); return data; } catch (error) { console.error('解密失败:', error); throw new Error('无法解密精选列表:' + error.message); } } /** * 检查编号是否在精选列表中 * @param {number} num - PFP编号 * @returns {boolean} 是否精选 */ isFeatured(num) { if (!this.decryptedList) { return false; } return this.decryptedList.includes(num); } /** * 获取精选列表 * @returns {Array|null} 精选编号数组 */ getFeaturedList() { return this.decryptedList; } /** * 清除缓存的精选列表 */ clearCache() { this.cache.removeCache('featured_list'); this.decryptedList = null; } }