ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Harmony鸿蒙实战开发-背单词app「仿不背单词」【源码在文末】

Harmony鸿蒙实战开发-背单词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{ position?: string; translation: string; }; sentence: Array{ sentence: string; translation: string; }; }; let queryCache: Recordstring, QueryResult 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): PromiseQueryResult { if (queryCache undefined) await loadLocalCache(); let result queryCache[word]; if (result ! undefined) return result return null; } async function cacheAdd(word: string, result: QueryResult): Promisevoid { 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): PromiseQueryResult { let result: QueryResult | null await cacheQuery(word); if (result) return result; let httpReq http.createHttp(); let httpResponse await new Promisehttp.HttpResponse((resolve, reject) { httpReq.request( https://dict.youdao.com/jsonapi_s?doctypejsonjsonversion4, { 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{ pos?: string; tran: string }).forEach(elem { result.meaning.push({ position: elem.pos, translation: elem.tran }); }); (res[blng_sents_part][sentence-pair] as Array{ sentence?: string; sentence-translation: string }).forEach(elem { result.sentence.push({ sentence: elem.sentence, translation: elem[sentence-translation] }); }) cacheAdd(word, result); return result; } }三、源码相关鸿蒙项目点此专栏通过百度网盘分享的文件…zip 链接百度网盘 请输入提取码文件已经加密请点击下方名片获取源码或Lvnvn0508
返回列表