-
Notifications
You must be signed in to change notification settings - Fork 3
/
youdao.js
76 lines (73 loc) · 1.79 KB
/
youdao.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const axios = require("axios")
async function getDefinition(word) {
const le = "en"
const res = await axios({
url: "https://dict.youdao.com/jsonapi",
method: "post",
params: {
le: le,
q: word,
keyfrom: "dataserver",
doctype: "json",
jsonversion: "2",
},
})
let definition = res.data
// 基本释义
let s = definition
? definition.ec ||
definition.ce ||
definition.fc ||
definition.cf ||
definition.jc ||
definition.cj ||
definition.kc ||
definition.ck ||
definition.multle
: ""
if (s && s === definition.ce) {
// 汉英翻译
}
// 日汉翻译
if (s && s === definition.jc) {
this.handleJCData(s)
}
// 发音
const speech = definition.simple ? definition.simple.word[0] : ""
definition = {
...definition,
base: s !== definition.jc ? s.word[0].trs : [],
baseJc: s && s == definition.jc ? s.word[0].trs[0].basejc : [],
fanyi: definition.fanyi ? definition.fanyi : [],
speech,
}
// 基本的界面显示
const baseString =
definition.base && definition.base.length > 0
? definition.base[0].tr[0].l.i[0]
: definition.fanyi
? definition.fanyi.tran
: ""
definition.baseString = baseString
const { base } = definition
// 带有超链接的显示
definition.hyperExplain =
base &&
base.length > 0 &&
base[0].tr &&
base[0].tr.length > 0 &&
base[0].tr[0].l.i.find((ff) => {
return ff["@action"] === "link"
})
? true
: false
definition.baseHyperExplain = definition.hyperExplain
? this.getBaseHyperExplain(base)
: []
// 处理 base 层数过深
definition.base = definition.base.map((v) => v.tr[0].l.i[0])
return definition
}
exports.youdao = {
getDefinition: getDefinition,
}