import { EmojiType as Emoji, EmojiCategoryType as EmojiCategory } from '../types/components/emoji-picker' // 表情数据工具类 export class EmojiData { private static instance: EmojiData private emojiMap: Map = new Map() private categoryMap: Map = new Map() // 常用表情分类 public static readonly CATEGORIES: EmojiCategory[] = [ { id: 'recent', name: '最近', icon: '🕒', order: 0 }, { id: 'smileys', name: '表情', icon: '😀', order: 1 }, { id: 'people', name: '人物', icon: '👨', order: 2 }, { id: 'gestures', name: '手势', icon: '👋', order: 3 }, { id: 'objects', name: '物品', icon: '💡', order: 4 }, { id: 'nature', name: '自然', icon: '🌳', order: 5 }, { id: 'food', name: '食物', icon: '🍎', order: 6 }, { id: 'activities', name: '活动', icon: '⚽', order: 7 }, { id: 'travel', name: '旅行', icon: '🚗', order: 8 }, { id: 'symbols', name: '符号', icon: '💖', order: 9 }, { id: 'flags', name: '旗帜', icon: '🇨🇳', order: 10 } ] // 默认表情数据 public static readonly DEFAULT_EMOJIS: Emoji[] = [ // 笑脸和情感 { key: 'grinning', char: '😀', name: '大笑', category: 'smileys', type: 'unicode', keywords: ['笑', '开心', '大笑'] }, { key: 'smiley', char: '😃', name: '微笑', category: 'smileys', type: 'unicode', keywords: ['微笑', '开心'] }, { key: 'smile', char: '😄', name: '开心', category: 'smileys', type: 'unicode', keywords: ['开心', '笑'] }, { key: 'grin', char: '😁', name: '咧嘴笑', category: 'smileys', type: 'unicode', keywords: ['咧嘴', '笑'] }, { key: 'laughing', char: '😆', name: '大笑', category: 'smileys', type: 'unicode', keywords: ['大笑', '哈哈'] }, { key: 'wink', char: '😉', name: '眨眼', category: 'smileys', type: 'unicode', keywords: ['眨眼', '调皮'] }, { key: 'blush', char: '😊', name: '害羞', category: 'smileys', type: 'unicode', keywords: ['害羞', '脸红'] }, { key: 'heart_eyes', char: '😍', name: '爱心眼', category: 'smileys', type: 'unicode', keywords: ['爱', '喜欢', '爱心'] }, { key: 'kissing_heart', char: '😘', name: '飞吻', category: 'smileys', type: 'unicode', keywords: ['飞吻', '亲'] }, { key: 'kissing', char: '😗', name: '亲吻', category: 'smileys', type: 'unicode', keywords: ['亲吻'] }, // 手势 { key: 'thumbsup', char: '👍', name: '点赞', category: 'gestures', type: 'unicode', keywords: ['赞', '好', '同意'] }, { key: 'thumbsdown', char: '👎', name: '点踩', category: 'gestures', type: 'unicode', keywords: ['踩', '不好', '反对'] }, { key: 'clap', char: '👏', name: '鼓掌', category: 'gestures', type: 'unicode', keywords: ['鼓掌', '表扬', '赞'] }, { key: 'pray', char: '🙏', name: '祈祷', category: 'gestures', type: 'unicode', keywords: ['祈祷', '拜托', '感谢'] }, { key: 'handshake', char: '🤝', name: '握手', category: 'gestures', type: 'unicode', keywords: ['握手', '合作', '协议'] }, { key: 'victory', char: '✌️', name: '胜利', category: 'gestures', type: 'unicode', keywords: ['胜利', '耶', '和平'] }, { key: 'ok_hand', char: '👌', name: 'OK', category: 'gestures', type: 'unicode', keywords: ['OK', '好的', '同意'] }, { key: 'v', char: '✌', name: '剪刀手', category: 'gestures', type: 'unicode', keywords: ['剪刀手', '耶'] }, // 物品 { key: 'heart', char: '❤️', name: '红心', category: 'symbols', type: 'unicode', keywords: ['心', '爱', '喜欢'] }, { key: 'star', char: '⭐', name: '星星', category: 'symbols', type: 'unicode', keywords: ['星星', '收藏'] }, { key: 'fire', char: '🔥', name: '火焰', category: 'symbols', type: 'unicode', keywords: ['火', '热', '厉害'] }, { key: 'rocket', char: '🚀', name: '火箭', category: 'travel', type: 'unicode', keywords: ['火箭', '快', '发射'] }, { key: 'bulb', char: '💡', name: '灯泡', category: 'objects', type: 'unicode', keywords: ['灯泡', '想法', '主意'] }, { key: 'gift', char: '🎁', name: '礼物', category: 'objects', type: 'unicode', keywords: ['礼物', '惊喜'] }, { key: 'bell', char: '🔔', name: '铃铛', category: 'objects', type: 'unicode', keywords: ['铃铛', '通知'] }, { key: 'lock', char: '🔒', name: '锁', category: 'objects', type: 'unicode', keywords: ['锁', '安全', '保密'] }, // 自然 { key: 'sun', char: '☀️', name: '太阳', category: 'nature', type: 'unicode', keywords: ['太阳', '晴天', '热'] }, { key: 'moon', char: '🌙', name: '月亮', category: 'nature', type: 'unicode', keywords: ['月亮', '夜晚'] }, { key: 'star2', char: '🌟', name: '闪星', category: 'nature', type: 'unicode', keywords: ['星星', '闪', '亮'] }, { key: 'cloud', char: '☁️', name: '云', category: 'nature', type: 'unicode', keywords: ['云', '天气'] }, { key: 'rainbow', char: '🌈', name: '彩虹', category: 'nature', type: 'unicode', keywords: ['彩虹', '希望', '美好'] }, { key: 'flower', char: '🌹', name: '玫瑰', category: 'nature', type: 'unicode', keywords: ['玫瑰', '花', '爱情'] }, { key: 'tree', char: '🌳', name: '树', category: 'nature', type: 'unicode', keywords: ['树', '自然'] }, // 食物 { key: 'pizza', char: '🍕', name: '披萨', category: 'food', type: 'unicode', keywords: ['披萨', '美食'] }, { key: 'hamburger', char: '🍔', name: '汉堡', category: 'food', type: 'unicode', keywords: ['汉堡', '快餐'] }, { key: 'coffee', char: '☕', name: '咖啡', category: 'food', type: 'unicode', keywords: ['咖啡', '提神'] }, { key: 'beer', char: '🍺', name: '啤酒', category: 'food', type: 'unicode', keywords: ['啤酒', '喝酒'] }, { key: 'cake', char: '🎂', name: '蛋糕', category: 'food', type: 'unicode', keywords: ['蛋糕', '生日', '庆祝'] }, { key: 'icecream', char: '🍦', name: '冰淇淋', category: 'food', type: 'unicode', keywords: ['冰淇淋', '冷饮'] }, // 活动 { key: 'soccer', char: '⚽', name: '足球', category: 'activities', type: 'unicode', keywords: ['足球', '运动'] }, { key: 'basketball', char: '🏀', name: '篮球', category: 'activities', type: 'unicode', keywords: ['篮球', '运动'] }, { key: 'tennis', char: '🎾', name: '网球', category: 'activities', type: 'unicode', keywords: ['网球', '运动'] }, { key: 'music', char: '🎵', name: '音乐', category: 'activities', type: 'unicode', keywords: ['音乐', '音符'] }, { key: 'movie', char: '🎬', name: '电影', category: 'activities', type: 'unicode', keywords: ['电影', '导演'] }, // 自定义表情(图片类型) { key: 'custom_001', char: '', name: '自定义1', category: 'custom', type: 'image', url: '/static/emoji/custom/001.png' }, { key: 'custom_002', char: '', name: '自定义2', category: 'custom', type: 'image', url: '/static/emoji/custom/002.png' } ] private constructor() { this.initData() } public static getInstance(): EmojiData { if (!EmojiData.instance) { EmojiData.instance = new EmojiData() } return EmojiData.instance } private initData(): void { // 初始化分类 EmojiData.CATEGORIES.forEach(category => { this.categoryMap.set(category.id, category) }) // 初始化表情 EmojiData.DEFAULT_EMOJIS.forEach(emoji => { this.emojiMap.set(emoji.key, emoji) }) } // 获取所有表情 public getAllEmojis(): Emoji[] { return Array.from(this.emojiMap.values()) } // 获取所有分类 public getAllCategories(): EmojiCategory[] { return Array.from(this.categoryMap.values()) } // 根据分类获取表情 public getEmojisByCategory(categoryId: string): Emoji[] { return this.getAllEmojis().filter(emoji => emoji.category === categoryId) } // 搜索表情 public searchEmojis(keyword: string): Emoji[] { const kw = keyword.toLowerCase().trim() if (!kw) return [] return this.getAllEmojis().filter(emoji => { // 搜索名称 if (emoji.name.toLowerCase().includes(kw)) return true // 搜索关键词 if (emoji.keywords?.some(k => k.toLowerCase().includes(kw))) return true // 搜索 key if (emoji.key.toLowerCase().includes(kw)) return true // 搜索字符 if (emoji.char.includes(kw)) return true return false }) } // 根据key获取表情 public getEmojiByKey(key: string): Emoji | undefined { return this.emojiMap.get(key) } // 根据字符获取表情 public getEmojiByChar(char: string): Emoji | undefined { return this.getAllEmojis().find(emoji => emoji.char === char) } // 添加自定义表情 public addCustomEmoji(emoji: Emoji): void { if (!emoji.category) { emoji.category = 'custom' } this.emojiMap.set(emoji.key, emoji) } // 移除表情 public removeEmoji(key: string): boolean { return this.emojiMap.delete(key) } // 获取最近使用的表情(从本地存储) public getRecentEmojis(maxCount: number = 36): Emoji[] { try { const data = uni.getStorageSync('im_emoji_recent') if (data) { const keys = JSON.parse(data) as string[] const emojis = keys .map(key => this.getEmojiByKey(key)) .filter(Boolean) as Emoji[] return emojis.slice(0, maxCount) } } catch (error) { console.error('获取最近表情失败:', error) } return [] } // 添加到最近使用 public addToRecent(key: string): void { try { const data = uni.getStorageSync('im_emoji_recent') let recentKeys: string[] = [] if (data) { recentKeys = JSON.parse(data) as string[] } // 移除重复项 const index = recentKeys.indexOf(key) if (index > -1) { recentKeys.splice(index, 1) } // 添加到开头 recentKeys.unshift(key) // 限制数量 if (recentKeys.length > 50) { recentKeys = recentKeys.slice(0, 50) } uni.setStorageSync('im_emoji_recent', JSON.stringify(recentKeys)) } catch (error) { console.error('保存最近表情失败:', error) } } // 清空最近使用 public clearRecentEmojis(): void { try { uni.removeStorageSync('im_emoji_recent') } catch (error) { console.error('清空最近表情失败:', error) } } } // 导出单例和类型 export const emojiData = EmojiData.getInstance() export type { Emoji, EmojiCategory }