news 2026/10/31 18:33:15

宠物统计模块 - Cordova与OpenHarmony混合开发实战

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
宠物统计模块 - Cordova与OpenHarmony混合开发实战

欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。

📌 概述

宠物统计模块用于统计每只宠物的相关数据。这个模块提供了宠物的日记数、健康记录数、疫苗接种情况等统计信息。通过Cordova框架,我们能够在Web层实现灵活的宠物统计展示,同时利用OpenHarmony的数据聚合能力执行宠物相关的统计计算。

宠物统计模块采用了卡片式设计,每只宠物显示为一个统计卡片,包含该宠物的各项统计数据。

🔗 完整流程

数据聚合流程:应用为每只宠物收集相关的统计数据,包括日记数、健康记录数、疫苗接种次数等。

统计展示流程:应用以卡片形式展示每只宠物的统计数据,用户可以点击卡片查看详细统计。

对比分析流程:用户可以选择多只宠物进行对比分析,查看它们之间的差异。

🔧 Web代码实现

// 计算宠物统计asyncfunctioncalculatePetStatistics(){try{constpets=awaitdb.getAllPets();constdiaries=awaitdb.getAllDiaries();consthealthRecords=awaitdb.getAllHealthRecords();constvaccinations=awaitdb.getAllVaccinations();returnpets.map(pet=>({id:pet.id,name:pet.name,breed:pet.breed,avatar:pet.avatar,diaryCount:diaries.filter(d=>d.petId===pet.id).length,healthRecordCount:healthRecords.filter(h=>h.petId===pet.id).length,vaccinationCount:vaccinations.filter(v=>v.petId===pet.id).length,lastDiaryDate:getLastDiaryDate(diaries,pet.id),lastHealthCheckDate:getLastHealthCheckDate(healthRecords,pet.id)}));}catch(error){console.error('计算宠物统计失败:',error);return[];}}// 获取最后一条日记的日期functiongetLastDiaryDate(diaries,petId){constpetDiaries=diaries.filter(d=>d.petId===petId);if(petDiaries.length===0)returnnull;returnpetDiaries.reduce((latest,current)=>newDate(current.date)>newDate(latest.date)?current:latest).date;}// 获取最后一次健康检查的日期functiongetLastHealthCheckDate(records,petId){constpetRecords=records.filter(r=>r.petId===petId);if(petRecords.length===0)returnnull;returnpetRecords.reduce((latest,current)=>newDate(current.date)>newDate(latest.date)?current:latest).date;}

这些函数处理宠物统计数据的计算。

// 渲染宠物统计页面asyncfunctionrenderPetStats(){constpetStats=awaitcalculatePetStatistics();consthtml=`<div class="pet-stats-container"> <div class="stats-header"> <h1>宠物统计</h1> </div> <div class="pet-stats-grid">${petStats.map(stat=>`<div class="pet-stat-card"> <div class="card-header"> <img src="${stat.avatar||'assets/default-pet.png'}" alt="${stat.name}" class="pet-avatar"> <div class="pet-info"> <h3>${stat.name}</h3> <p>${stat.breed}</p> </div> </div> <div class="stat-items"> <div class="stat-item"> <span class="label">日记数</span> <span class="value">${stat.diaryCount}</span> </div> <div class="stat-item"> <span class="label">健康记录</span> <span class="value">${stat.healthRecordCount}</span> </div> <div class="stat-item"> <span class="label">疫苗接种</span> <span class="value">${stat.vaccinationCount}</span> </div> </div> <div class="stat-dates">${stat.lastDiaryDate?`<p class="date-info">最后日记:${formatDate(stat.lastDiaryDate)}</p>`:''}${stat.lastHealthCheckDate?`<p class="date-info">最后检查:${formatDate(stat.lastHealthCheckDate)}</p>`:''}</div> <div class="card-actions"> <button class="btn-small" onclick="app.navigateTo('pet-profile',${stat.id})">详情</button> <button class="btn-small" onclick="viewPetDetailStats(${stat.id})">详细统计</button> </div> </div>`).join('')}</div> </div>`;document.getElementById('page-container').innerHTML=html;}// 查看宠物详细统计asyncfunctionviewPetDetailStats(petId){constpet=awaitdb.getPet(petId);constdiaries=awaitdb.getDiariesByPet(petId);consthealthRecords=awaitdb.getHealthRecords(petId);consthtml=`<div class="pet-detail-stats"> <h2>${pet.name}- 详细统计</h2> <div class="detail-charts"> <div class="chart-container"> <h3>日记分布</h3> <canvas id="diary-distribution"></canvas> </div> <div class="chart-container"> <h3>健康记录趋势</h3> <canvas id="health-trend"></canvas> </div> </div> </div>`;document.getElementById('page-container').innerHTML+=html;}

这个渲染函数生成了宠物统计界面,显示每只宠物的统计卡片。

🔌 原生代码实现

// PetStatsPlugin.ets - 宠物统计原生插件 import { fileIo } from '@kit.BasicServicesKit'; @Entry @Component struct PetStatsPlugin { // 生成宠物统计报告 generatePetReport(petData: string, callback: (path: string) => void): void { try { const pet = JSON.parse(petData); const report = ` 宠物统计报告 ============ 宠物名称: ${pet.name} 品种: ${pet.breed} 日记数: ${pet.diaryCount} 健康记录: ${pet.healthRecordCount} 疫苗接种: ${pet.vaccinationCount} 生成时间: ${new Date().toISOString()} `; const reportPath = `/data/reports/pet_${pet.id}_stats_${Date.now()}.txt`; const file = fileIo.openSync(reportPath, fileIo.OpenMode.CREATE | fileIo.OpenMode.WRITE); fileIo.writeSync(file.fd, report); fileIo.closeSync(file.fd); callback(reportPath); } catch (error) { console.error('[PetStatsPlugin] 生成报告失败:', error); callback(''); } } build() { Column() { Web({ src: 'resource://rawfile/www/index.html', controller: new WebviewController() }) } } }

这个原生插件提供了宠物统计报告生成功能。

Web-Native通信代码

// 生成宠物统计报告functiongenerateNativePetReport(petData){returnnewPromise((resolve,reject)=>{cordova.exec((path)=>{if(path){showSuccess(`报告已生成:${path}`);resolve(path);}else{reject(newError('生成失败'));}},(error)=>{console.error('生成失败:',error);reject(error);},'PetStatsPlugin','generatePetReport',[JSON.stringify(petData)]);});}

这段代码展示了如何通过Cordova调用原生的报告生成功能。

📝 总结

宠物统计模块展示了Cordova与OpenHarmony在宠物数据分析方面的应用。在Web层,我们实现了灵活的宠物统计展示。在原生层,我们提供了报告生成功能。

通过宠物统计,用户可以了解每只宠物的相关数据。通过详细统计,用户可以深入分析宠物的情况。通过Web-Native通信,我们能够充分利用OpenHarmony的文件系统能力,为用户提供完整的宠物统计体验。

在实际开发中,建议实现宠物对比功能,提供更多的统计维度,并支持统计数据的导出。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/10/30 20:12:03

时间轴模块 - Cordova与OpenHarmony混合开发实战

欢迎大家加入开源鸿蒙跨平台开发者社区&#xff0c;一起共建开源鸿蒙跨平台生态。 &#x1f4cc; 概述 时间轴模块用于以时间顺序展示所有日记。这个模块提供了按时间排序的日记视图&#xff0c;用户可以直观地看到日记的时间分布。通过Cordova框架&#xff0c;我们能够在Web层…

作者头像 李华
网站建设 2026/10/30 21:05:46

淘宝购物助手:3大技巧助你提升购物效率,轻松选购心仪好物!

还在为淘宝热门商品总是"库存不足"而烦恼吗&#xff1f;每次看到喜欢的商品很快售罄&#xff0c;那种失落感是不是让你有些无奈&#xff1f;别担心&#xff0c;今天我要分享的淘宝购物助手就是你的好帮手&#xff0c;它能帮你提升购物效率&#xff0c;让你更轻松地选…

作者头像 李华
网站建设 2026/10/30 23:57:14

LangFlow与Redis集成:实现高速数据缓存与共享

LangFlow与Redis集成&#xff1a;实现高速数据缓存与共享 在构建现代AI应用的实践中&#xff0c;一个日益突出的矛盾逐渐显现&#xff1a;开发者渴望快速验证复杂逻辑&#xff0c;而现实却是——每修改一次提示词就得重跑一遍LLM调用&#xff0c;每次测试都伴随着数秒甚至更长的…

作者头像 李华
网站建设 2026/10/30 14:14:03

LangFlow与Excel文件处理:读取、写入与数据分析

LangFlow与Excel文件处理&#xff1a;读取、写入与数据分析 在企业数据仍大量沉淀于Excel表格的今天&#xff0c;如何让大语言模型&#xff08;LLM&#xff09;真正“读懂”这些业务报表&#xff0c;并自动完成分析、摘要甚至决策建议&#xff1f;这不仅是技术挑战&#xff0c;…

作者头像 李华
网站建设 2026/10/31 8:31:08

LangFlow中的绩效评估助手:自动生成评语与建议

LangFlow中的绩效评估助手&#xff1a;自动生成评语与建议 在企业HR的实际工作中&#xff0c;每到季度或年度考核期&#xff0c;撰写员工绩效评语往往成了一项令人头疼的任务。几十甚至上百份评语要逐个打磨&#xff0c;既要体现差异化&#xff0c;又要保持语气一致、结构规范&…

作者头像 李华
网站建设 2026/10/31 10:01:26

LangFlow中的OCR节点:图像文字识别集成方案

LangFlow中的OCR节点&#xff1a;图像文字识别集成方案 在智能应用开发日益复杂的今天&#xff0c;如何快速将现实世界中的非结构化信息——比如一张合同截图、一份扫描版发票或教科书的一页照片——转化为可被大语言模型理解与处理的数据&#xff0c;已成为多模态AI系统构建的…

作者头像 李华