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

打印 上一主题 下一主题

主题 1033|帖子 1033|积分 3099

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


  
运行工具:DevEco Studio
一、运行演示

1、登录

2、注册

3、开机动画

4、首页

5、开始学习

6、开始学习-看答案

7、统计分析

8、不背学堂

二、部门代码

  1. import http from '@ohos.net.http';
  2. import fs from '@ohos.file.fs';
  3. import ReadOptions from "@ohos.file.fs"
  4. import common from '@ohos.app.ability.common';
  5. import buffer from '@ohos.buffer';
  6. export namespace YouDaoDict {
  7.   const BUFFER_FILE_PATH = '/you_dao_dict_cache.json';
  8.   export type QueryResult = {
  9.     source: string;
  10.     pronunciation: {
  11.       en?: string;
  12.       us?: string;
  13.     };
  14.     meaning: Array<{
  15.       position?: string;
  16.       translation: string;
  17.     }>;
  18.     sentence: Array<{
  19.       sentence: string;
  20.       translation: string;
  21.     }>;
  22.   };
  23.   let queryCache: Record<string, QueryResult> = undefined;
  24.   async function loadLocalCache() {
  25.     let context = getContext(this) as common.UIAbilityContext;
  26.     let filePath = `${context.cacheDir}${BUFFER_FILE_PATH}`
  27.     let file: fs.File;
  28.     if (!await fs.access(filePath)) {
  29.       file = await fs.open(filePath, fs.OpenMode.CREATE | fs.OpenMode.READ_WRITE);
  30.       await fs.write(file.fd, '{}');
  31.       await fs.fdatasync(file.fd);
  32.       await fs.close(file);
  33.     }
  34.     queryCache = JSON.parse(await fs.readText(filePath, { encoding: 'UTF-8' }));
  35.   }
  36.   async function cacheQuery(word: string): Promise<QueryResult> {
  37.     if (queryCache === undefined) await loadLocalCache();
  38.     let result = queryCache[word];
  39.     if (result !== undefined) return result
  40.     return null;
  41.   }
  42.   async function cacheAdd(word: string, result: QueryResult): Promise<void> {
  43.     queryCache[word] = result;
  44.     let serialized = JSON.stringify(queryCache);
  45.     let context = getContext(this) as common.UIAbilityContext;
  46.     let filePath = `${context.cacheDir}${BUFFER_FILE_PATH}`
  47.     let file = await fs.open(filePath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE);
  48.     await fs.write(file.fd, serialized);
  49.     await fs.fsync(file.fd);
  50.     await fs.close(file);
  51.   }
  52.   export async function query(word: string): Promise<QueryResult> {
  53.     let result: QueryResult | null = await cacheQuery(word);
  54.     if (result) return result;
  55.     let httpReq = http.createHttp();
  56.     let httpResponse = await new Promise<http.HttpResponse>((resolve, reject) => {
  57.       httpReq.request(
  58.         'https://dict.youdao.com/jsonapi_s?doctype=json&jsonversion=4',
  59.         {
  60.           method: http.RequestMethod.POST,
  61.           header:{
  62.             'Content-Type':'application/x-www-form-urlencoded'
  63.           },
  64.           extraData: `q=${word}`
  65.         },
  66.         (err, data) => {
  67.           if (err !== undefined) reject(err);
  68.           resolve(data)
  69.         }
  70.       )
  71.     })
  72.     let res = JSON.parse(httpResponse.result.toString());
  73.     result = {
  74.       source: word,
  75.       pronunciation: {
  76.         en: res['ec']['word']['ukphone'] as string,
  77.         us: res['ec']['word']['usphone'] as string
  78.       },
  79.       meaning: [],
  80.       sentence: []
  81.     };
  82.     (res['ec']['word']['trs'] as Array<{
  83.       pos?: string;
  84.       tran: string
  85.     }>).forEach(elem => {
  86.       result.meaning.push({ position: elem.pos, translation: elem.tran });
  87.     });
  88.     (res['blng_sents_part']['sentence-pair'] as Array<{
  89.       sentence?: string;
  90.       'sentence-translation': string
  91.     }>).forEach(elem => {
  92.       result.sentence.push({ sentence: elem.sentence, translation: elem['sentence-translation'] });
  93.     })
  94.     cacheAdd(word, result);
  95.     return result;
  96.   }
  97. }
复制代码
三、源码

相关鸿蒙项目点此专栏

通过百度网盘分享的文件:…zip 链接:百度网盘 请输入提取码
   文件已经加密,请点击下方手刺获取源码
  或:One_PQ

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

麻花痒

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表