Skip to content

Commit c15a219

Browse files
committed
word api first version
1 parent 3e81345 commit c15a219

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/api/word.js

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
import cache from '@/api/cache'
2+
import store from '@/global/store'
3+
4+
import list1 from '@/static/words/List1.json'
5+
import list2 from '@/static/words/List2.json'
6+
import list3 from '@/static/words/List3.json'
7+
import list4 from '@/static/words/List4.json'
8+
import list5 from '@/static/words/List5.json'
9+
import list6 from '@/static/words/List6.json'
10+
11+
// 词汇api设计大体上分为学习模式的api和复习模式的api
12+
13+
// 学习模式即学习新词
14+
// - 对应的应用在 /learn 页面下
15+
// - 单词记忆周期为5分钟和30分钟的词(learned.words[word].period <= 2)也会在学习模式中出现(本地数据库的数据结构在cache.js里有介绍)
16+
17+
// 复习模式即复习学习过的词
18+
// - 对应的应用在 /revise 页面下
19+
// - 被添加到一个user的learned表里的词汇就算是这个用户学过的词
20+
// - 根据记忆周期period和不熟练度stage来计算不同单词的权重,然后根据权重排序决定下一个要复习的词
21+
22+
cache.connect()
23+
24+
const lists = {
25+
list1,
26+
list2,
27+
list3,
28+
list4,
29+
list5,
30+
list6
31+
}
32+
33+
const isListExist = (listName) => {
34+
return !!lists[listName]
35+
}
36+
37+
const getWordList = (listName) => {
38+
return isListExist(listName) ? lists[listName] : {}
39+
}
40+
41+
// 得到的是一个根据单词的index属性(来自于json源文件)排序过的单词数组而不是对象
42+
const getSortedWordList = (listName) => {
43+
const wordList = getWordList(listName)
44+
return Object.keys(wordList).sort((a, b) => {
45+
return wordList[a].index - wordList[b].index
46+
})
47+
}
48+
49+
const getUserLearned = () => {
50+
const { user } = store.getters || {}
51+
return new Promise((resolve, reject) => {
52+
if (!user._id) return reject(new Error('user not login'))
53+
cache.getLearnedByUserId(user._id)
54+
.then(res => resolve(res))
55+
.catch(err => reject(err))
56+
})
57+
}
58+
59+
const getUserProgress = () => {
60+
const { user } = store.getters || {}
61+
return new Promise((resolve, reject) => {
62+
if (!user._id) return reject(new Error('user not login'))
63+
cache.getProgressByUserId(user._id)
64+
.then(res => resolve(res))
65+
.catch(err => reject(err))
66+
})
67+
}
68+
69+
// 将一个list里的某一个单词标记为已学,即添加到本地数据库的learned表里
70+
// 因为输入的wordEn只是一个String单词的名称,因此需要对应listName找到单词所在list来获取完整单词对象
71+
// 通常在此之前调用getNextLearnWordFromList()来得到当前学习的list里下一个要学的单词是啥
72+
const learnWordFromList = (wordEn, listName) => {
73+
const { user } = store.getters || {}
74+
const wordZh = ((lists[listName] || {})[wordEn] || {}).value
75+
return new Promise((resolve, reject) => {
76+
if (!user._id) return reject(new Error('user not login'))
77+
if (!wordZh) return reject(new Error('word not found in list'))
78+
const wordObj = { wordEn, wordZh }
79+
cache.editUserLearned(user._id, wordObj, {})
80+
.then((status) => {
81+
if (status === 'add' || status === 'new') cache.editUserProgress(user._id, listName, { change: 1 }).then(res => resolve(res))
82+
else resolve(status)
83+
})
84+
.catch(err => reject(err))
85+
})
86+
}
87+
88+
// 在学习模式( /learn 页面)时,需要检查记忆周期为5分钟和30分钟的单词是否需要在下一个复习
89+
// const checkReviseWhenLearn = () => {
90+
91+
// }
92+
93+
// 获取学习模式下下一个学习的词,返回结果是单词的String
94+
// 调用这个api不会造成该改词的属性的变化,仅获取并返回单词
95+
// 通常在后续调用learnWordFromList()将改词添加到已学单词列表中
96+
const getNextLearnWordFromList = (listName) => {
97+
const { user } = store.getters || {}
98+
return new Promise((resolve, reject) => {
99+
if (!user._id) return reject(new Error('user not login'))
100+
if (!isListExist(listName)) return reject(new Error('word list not exist'))
101+
getUserProgress(user._id).then((dict) => {
102+
const progress = (dict || {})[listName]
103+
const sortedList = getSortedWordList(listName)
104+
if (progress && progress.location >= 0) {
105+
// list progress record found
106+
// checkReviseWhenLearn()
107+
const wordEn = sortedList[progress.location]
108+
resolve(wordEn)
109+
} else {
110+
// new record added to progress
111+
cache.editUserProgress(user._id, listName, { location: 0, change: 0 })
112+
.then(() => { resolve(sortedList[0]) })
113+
}
114+
}).catch(err => reject(err))
115+
})
116+
}
117+
118+
const reviseWordFromList = (wordEn, listName, stageChange) => {
119+
120+
}
121+
122+
const word = {
123+
isListExist,
124+
getWordList,
125+
getUserLearned,
126+
getUserProgress,
127+
learnWordFromList,
128+
getNextLearnWordFromList,
129+
reviseWordFromList
130+
}
131+
132+
export default word

0 commit comments

Comments
 (0)