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

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


运行工具:DevEco Studio

一、运行演示

1、登录

image-20240720233729892

2、注册

image-20240720233754596

3、开机动画

image-20240720233847059

4、首页

image-20240720233929387

5、开始学习

image-20240720233953687

6、开始学习-看答案

image-20240720233929387

7、统计分析

image-20240720234104765

8、不背学堂

image-20240720234136542

二、部分代码

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: Record<string, 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): Promise<QueryResult> {
    if (queryCache === undefined) await loadLocalCache();
    let result = queryCache[word];
    if (result !== undefined) return result
    return null;
  }

  async function cacheAdd(word: string, result: QueryResult): Promise<void> {
    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<QueryResult> {
    let result: QueryResult | null = await cacheQuery(word);
    if (result) return result;
    let httpReq = http.createHttp();
    let httpResponse = await new Promise<http.HttpResponse>((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<{
      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;
  }
}

三、源码

相关鸿蒙项目点此专栏

image-20241020235757199

通过百度网盘分享的文件:…zip 链接:百度网盘 请输入提取码

文件已经加密,请点击下方名片获取源码

或:One_PQ

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值