Harmony鸿蒙实战开发-背单词app「仿不背单词」【源码在文末】_背单词app源码
Harmony鸿蒙实战开发-背单词app【源码在文末】
文章目录
- Harmony鸿蒙实战开发-背单词app【源码在文末】
-
- 一、运行演示
-
- 1、登录
- 2、注册
- 3、开机动画
- 4、首页
- 5、开始学习
- 6、开始学习-看答案
- 7、统计分析
- 8、不背学堂
- 二、部分代码
- 三、源码
运行工具:DevEco Studio
一、运行演示
1、登录
2、注册
3、开机动画
4、首页
5、开始学习
6、开始学习-看答案
7、统计分析
8、不背学堂
二、部分代码
import http from \'@ohos.net.http\';import fs from \'@ohos.file.fs\';import ReadOptions from \"@ohos.file.fs\"import common from \'@ohos.app.ability.common\';import buffer from \'@ohos.buffer\';export namespace YouDaoDict { const BUFFER_FILE_PATH = \'/you_dao_dict_cache.json\'; export type QueryResult = { source: string; pronunciation: { en?: string; us?: string; }; meaning: Array; sentence: Array; }; let queryCache: Record = undefined; async function loadLocalCache() { let context = getContext(this) as common.UIAbilityContext; let filePath = `${context.cacheDir}${BUFFER_FILE_PATH}` let file: fs.File; if (!await fs.access(filePath)) { file = await fs.open(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE); await fs.write(file.fd, \'{}\'); await fs.fdatasync(file.fd); await fs.close(file); } queryCache = JSON.parse(await fs.readText(filePath, { encoding: \'UTF-8\' })); } async function cacheQuery(word: string): Promise { if (queryCache === undefined) await loadLocalCache(); let result = queryCache[word]; if (result !== undefined) return result return null; } async function cacheAdd(word: string, result: QueryResult): Promise { queryCache[word] = result; let serialized = JSON.stringify(queryCache); let context = getContext(this) as common.UIAbilityContext; let filePath = `${context.cacheDir}${BUFFER_FILE_PATH}` let file = await fs.open(filePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE); await fs.write(file.fd, serialized); await fs.fsync(file.fd); await fs.close(file); } export async function query(word: string): Promise { let result: QueryResult | null = await cacheQuery(word); if (result) return result; let httpReq = http.createHttp(); let httpResponse = await new Promise((resolve, reject) => { httpReq.request( \'https://dict.youdao.com/jsonapi_s?doctype=json&jsonversion=4\', { method: http.RequestMethod.POST, header:{ \'Content-Type\':\'application/x-www-form-urlencoded\' }, extraData: `q=${word}` }, (err, data) => { if (err !== undefined) reject(err); resolve(data) } ) }) let res = JSON.parse(httpResponse.result.toString()); result = { source: word, pronunciation: { en: res[\'ec\'][\'word\'][\'ukphone\'] as string, us: res[\'ec\'][\'word\'][\'usphone\'] as string }, meaning: [], sentence: [] }; (res[\'ec\'][\'word\'][\'trs\'] as Array).forEach(elem => { result.meaning.push({ position: elem.pos, translation: elem.tran }); }); (res[\'blng_sents_part\'][\'sentence-pair\'] as Array).forEach(elem => { result.sentence.push({ sentence: elem.sentence, translation: elem[\'sentence-translation\'] }); }) cacheAdd(word, result); return result; }}
三、源码
相关鸿蒙项目点此专栏
通过百度网盘分享的文件:…zip 链接:百度网盘 请输入提取码
文件已经加密,请点击下方名片获取源码
或:Lvnvn0508