feat(M3.3): 必应词典实现(Mock版) (v0.2.3)

This commit is contained in:
李岩岩 2026-02-11 14:19:10 +08:00
parent e677daa0c0
commit 09f0978025
6 changed files with 151 additions and 7 deletions

View File

@ -7,7 +7,7 @@
## 版本速查
### 当前版本
`0.2.2` → 下一目标 `0.2.3` ([M3.3](./M3.md))
`0.2.3` → 下一目标 `0.2.4` ([M3.4](./M3.md))
### 模块版本范围

View File

@ -65,9 +65,9 @@ M11.10完成 → 1.0.0 (正式发布)
## 当前状态
**当前版本**: `0.2.2`
**当前进度**: 16/97 (16%)
**下一任务**: [M3.3 必应词典实现Mock版](./M3.md#m33-必应词典实现mock版--目标版本-023)
**当前版本**: `0.2.3`
**当前进度**: 17/97 (18%)
**下一任务**: [M3.4 后台查询接口](./M3.md#m34-后台查询接口--目标版本-024)
---

View File

@ -38,7 +38,7 @@
|------|------|------|------|------|
| M3.1 | 0.2.1 | 词典接口基类 | ✅ | 2026-02-11 |
| M3.2 | 0.2.2 | 词典管理器 | ✅ | 2026-02-11 |
| M3.3 | 0.2.3 | 必应词典(Mock) | ⬜ | - |
| M3.3 | 0.2.3 | 必应词典(Mock) | ✅ | 2026-02-11 |
| M3.4 | 0.2.4 | 后台查询接口 | ⬜ | - |
| M3.5 | 0.2.5 | 结果展示组件 | ⬜ | - |
| M3.6 | 0.2.6 | 点击图标查词(Mock) | ⬜ | - |

View File

@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "沙拉查词",
"version": "0.2.2",
"version": "0.2.3",
"description": "聚合词典划词翻译",
"permissions": [
"storage",

View File

@ -1,6 +1,6 @@
{
"name": "salad-dict",
"version": "0.2.2",
"version": "0.2.3",
"description": "聚合词典划词翻译",
"private": true,
"type": "module",

View File

@ -0,0 +1,144 @@
/**
* @file 必应词典实现Mock版
* @description 必应词典的 Mock 实现返回模拟数据
*/
import { DictionaryBase, createResult, createMeaning, createExample } from './base.js';
/**
* 必应词典 Mock 实现
*/
export class BingDictionary extends DictionaryBase {
constructor(config = {}) {
super({
name: '必应词典',
icon: 'icons/bing.png',
languages: ['en', 'zh'],
...config
});
}
/**
* 查询单词Mock 实现
* @param {string} word - 要查询的单词
* @returns {Promise<DictionaryResult>} 模拟查询结果
*/
async search(word) {
if (!word?.trim()) {
throw new Error('Word is empty');
}
const trimmedWord = word.trim();
// 模拟网络延迟
await this._delay(100);
// 返回模拟数据
return createResult({
word: trimmedWord,
phonetic: this._getMockPhonetic(trimmedWord),
meanings: this._getMockMeanings(trimmedWord),
examples: this._getMockExamples(trimmedWord),
url: `https://cn.bing.com/dict/search?q=${encodeURIComponent(trimmedWord)}`
});
}
/**
* 模拟延迟
* @private
* @param {number} ms - 延迟毫秒数
* @returns {Promise<void>}
*/
_delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
/**
* 获取模拟音标
* @private
* @param {string} word - 单词
* @returns {string} 音标
*/
_getMockPhonetic(word) {
const phonetics = {
'hello': '/həˈləʊ/',
'world': '/wɜːld/',
'apple': '/ˈæpl/',
'computer': '/kəmˈpjuːtə(r)/',
'dictionary': '/ˈdɪkʃənri/'
};
return phonetics[word.toLowerCase()] || `/${word.toLowerCase()}/`;
}
/**
* 获取模拟释义
* @private
* @param {string} word - 单词
* @returns {Array<Meaning>} 释义列表
*/
_getMockMeanings(word) {
const meaningsMap = {
'hello': [
createMeaning('int.', ['你好', '喂', '嘿']),
createMeaning('n.', ['问候', '招呼'])
],
'world': [
createMeaning('n.', ['世界', '地球', '天下', '界'])
],
'apple': [
createMeaning('n.', ['苹果', '苹果公司'])
],
'computer': [
createMeaning('n.', ['计算机', '电脑'])
],
'dictionary': [
createMeaning('n.', ['词典', '字典', '辞典'])
]
};
return meaningsMap[word.toLowerCase()] || [
createMeaning('n.', [`${word} 的中文释义`]),
createMeaning('v.', [`${word} 的动词释义`])
];
}
/**
* 获取模拟例句
* @private
* @param {string} word - 单词
* @returns {Array<Example>} 例句列表
*/
_getMockExamples(word) {
const examplesMap = {
'hello': [
createExample('Hello, how are you?', '你好,你好吗?'),
createExample('She said hello to everyone.', '她向每个人问好。')
],
'world': [
createExample('The world is getting smaller.', '世界变得越来越小。'),
createExample('He wants to travel around the world.', '他想环游世界。')
],
'apple': [
createExample('An apple a day keeps the doctor away.', '一天一苹果,医生远离我。'),
createExample('She took a bite of the apple.', '她咬了一口苹果。')
],
'computer': [
createExample('I use a computer for work.', '我用电脑工作。'),
createExample('The computer is running slowly.', '电脑运行很慢。')
],
'dictionary': [
createExample('I looked it up in the dictionary.', '我在词典里查了一下。'),
createExample('This is an English-Chinese dictionary.', '这是一本英汉词典。')
]
};
return examplesMap[word.toLowerCase()] || [
createExample(`This is a sentence with "${word}".`, `这是一个包含"${word}"的句子。`),
createExample(`Can you use "${word}" in a sentence?`, `你能用"${word}"造句吗?`)
];
}
}
// 导出单例实例
export const bingDictionary = new BingDictionary();