news 2026/2/8 12:24:24

Cordova与OpenHarmony施肥记录管理

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Cordova与OpenHarmony施肥记录管理

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

施肥管理系统概述

施肥记录管理系统用于记录和追踪植物的施肥历史。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的施肥管理系统,包括施肥记录的创建、查询、统计和提醒功能。这个系统需要考虑不同类型肥料的管理和施肥周期的计算。

施肥记录数据模型

classFertilizerType{constructor(id,name,npkRatio,description){this.id=id;this.name=name;this.npkRatio=npkRatio;// 氮磷钾比例this.description=description;}}classFertilizingRecord{constructor(plantId,fertilizerType,amount,notes){this.id='fert_'+Date.now();this.plantId=plantId;this.fertilizerType=fertilizerType;this.amount=amount;// 克this.date=newDate();this.notes=notes;}}classFertilizingManager{constructor(){this.records=[];this.fertilizerTypes=[];this.initDefaultFertilizers();this.loadFromStorage();}initDefaultFertilizers(){this.fertilizerTypes=[newFertilizerType('fert_1','通用肥','10-10-10','适合大多数植物'),newFertilizerType('fert_2','高氮肥','20-10-10','促进叶片生长'),newFertilizerType('fert_3','高磷肥','10-20-10','促进开花结果'),newFertilizerType('fert_4','高钾肥','10-10-20','增强抗性')];}addFertilizingRecord(plantId,fertilizerType,amount,notes){constrecord=newFertilizingRecord(plantId,fertilizerType,amount,notes);this.records.push(record);this.saveToStorage();returnrecord;}}

这个施肥记录数据模型定义了FertilizerType、FertilizingRecord和FertilizingManager类。FertilizerType类定义了肥料类型及其NPK比例,FertilizingRecord类记录每次施肥的详细信息,FertilizingManager类管理所有施肥记录和肥料类型。

与OpenHarmony数据库的集成

functionsaveFertilizingRecordToDatabase(record){cordova.exec(function(result){console.log("施肥记录已保存到数据库");},function(error){console.error("保存失败:",error);},"DatabasePlugin","saveFertilizingRecord",[{id:record.id,plantId:record.plantId,fertilizerType:record.fertilizerType,amount:record.amount,date:record.date.toISOString(),notes:record.notes}]);}functionloadFertilizingRecordsFromDatabase(){cordova.exec(function(result){console.log("施肥记录已从数据库加载");fertilizingManager.records=result.map(rec=>{constrecord=newFertilizingRecord(rec.plantId,rec.fertilizerType,rec.amount,rec.notes);record.id=rec.id;record.date=newDate(rec.date);returnrecord;});renderFertilizingRecords();},function(error){console.error("加载失败:",error);},"DatabasePlugin","loadFertilizingRecords",[]);}

这段代码展示了如何与OpenHarmony的数据库进行交互。saveFertilizingRecordToDatabase函数将施肥记录保存到数据库,loadFertilizingRecordsFromDatabase函数从数据库加载所有施肥记录。通过这种方式,我们确保了施肥数据的持久化存储。

施肥记录列表展示

functionrenderFertilizingRecords(plantId){constplant=plants.find(p=>p.id===plantId);if(!plant)return;constrecords=fertilizingManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));constcontainer=document.getElementById('page-container');container.innerHTML=`<div class="fertilizing-records-container"> <h2>${plant.name}的施肥记录</h2> <button class="add-record-btn" onclick="showAddFertilizingRecordDialog('${plantId}')"> ➕ 添加施肥记录 </button> </div>`;if(records.length===0){container.innerHTML+='<p class="empty-message">还没有施肥记录</p>';return;}constrecordsList=document.createElement('div');recordsList.className='records-list';records.forEach(record=>{constfertilizerType=fertilizingManager.fertilizerTypes.find(f=>f.id===record.fertilizerType);constrecordItem=document.createElement('div');recordItem.className='record-item';recordItem.innerHTML=`<div class="record-info"> <p class="record-date">${record.date.toLocaleString('zh-CN')}</p> <p class="fertilizer-type">🌾 肥料:${fertilizerType?.name||'未知'}</p> <p class="fertilizer-npk">NPK比例:${fertilizerType?.npkRatio||'N/A'}</p> <p class="record-amount">用量:${record.amount}克</p>${record.notes?`<p class="record-notes">备注:${record.notes}</p>`:''}</div> <div class="record-actions"> <button onclick="editFertilizingRecord('${record.id}')">编辑</button> <button onclick="deleteFertilizingRecord('${record.id}')">删除</button> </div>`;recordsList.appendChild(recordItem);});container.appendChild(recordsList);}

这个函数负责渲染施肥记录列表。它显示了特定植物的所有施肥记录,包括日期、肥料类型、NPK比例和用量。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。

添加施肥记录对话框

functionshowAddFertilizingRecordDialog(plantId){constdialog=document.createElement('div');dialog.className='modal-dialog';letfertilizerOptions='';fertilizingManager.fertilizerTypes.forEach(fert=>{fertilizerOptions+=`<option value="${fert.id}">${fert.name}(${fert.npkRatio})</option>`;});dialog.innerHTML=`<div class="modal-content"> <h3>添加施肥记录</h3> <form id="add-fertilizing-form"> <div class="form-group"> <label>肥料类型</label> <select id="fertilizer-type" required> <option value="">请选择肥料类型</option>${fertilizerOptions}</select> </div> <div class="form-group"> <label>用量 (克)</label> <input type="number" id="fertilizer-amount" min="0" required> </div> <div class="form-group"> <label>施肥日期</label> <input type="datetime-local" id="fertilizing-date" required> </div> <div class="form-group"> <label>备注</label> <textarea id="fertilizing-notes"></textarea> </div> <div class="form-actions"> <button type="submit">保存</button> <button type="button" onclick="closeDialog()">取消</button> </div> </form> </div>`;document.getElementById('modal-container').appendChild(dialog);constnow=newDate();document.getElementById('fertilizing-date').value=now.toISOString().slice(0,16);document.getElementById('add-fertilizing-form').addEventListener('submit',function(e){e.preventDefault();constfertilizerType=document.getElementById('fertilizer-type').value;constamount=parseFloat(document.getElementById('fertilizer-amount').value);constdate=newDate(document.getElementById('fertilizing-date').value);constnotes=document.getElementById('fertilizing-notes').value;constrecord=newFertilizingRecord(plantId,fertilizerType,amount,notes);record.date=date;fertilizingManager.records.push(record);fertilizingManager.saveToStorage();saveFertilizingRecordToDatabase(record);closeDialog();renderFertilizingRecords(plantId);showToast('施肥记录已添加');});}

这个函数创建并显示添加施肥记录的对话框。用户可以选择肥料类型、输入用量、日期和备注。提交后,新记录会被添加到fertilizingManager中,并保存到数据库。这种设计提供了灵活的记录输入方式。

施肥统计功能

classFertilizingStatistics{constructor(fertilizingManager){this.fertilizingManager=fertilizingManager;}getTotalFertilizingCount(plantId){returnthis.fertilizingManager.records.filter(r=>r.plantId===plantId).length;}getAverageFertilizerAmount(plantId){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);if(records.length===0)return0;consttotal=records.reduce((sum,r)=>sum+r.amount,0);returntotal/records.length;}getMostUsedFertilizer(plantId){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);constfertilizerCounts={};records.forEach(record=>{fertilizerCounts[record.fertilizerType]=(fertilizerCounts[record.fertilizerType]||0)+1;});constmostUsed=Object.keys(fertilizerCounts).reduce((a,b)=>fertilizerCounts[a]>fertilizerCounts[b]?a:b);returnthis.fertilizingManager.fertilizerTypes.find(f=>f.id===mostUsed);}getFertilizingFrequency(plantId,days=30){constrecords=this.fertilizingManager.records.filter(r=>r.plantId===plantId);constcutoffDate=newDate();cutoffDate.setDate(cutoffDate.getDate()-days);constrecentRecords=records.filter(r=>newDate(r.date)>cutoffDate);return(recentRecords.length/days*7).toFixed(2);// 每周施肥次数}}

这个FertilizingStatistics类提供了施肥的统计功能。getTotalFertilizingCount返回施肥总次数,getAverageFertilizerAmount计算平均用量,getMostUsedFertilizer返回最常用的肥料,getFertilizingFrequency计算施肥频率。这些统计信息可以帮助用户了解施肥规律。

施肥提醒功能

functioncheckFertilizingReminders(){plants.forEach(plant=>{constlastFertilizingDate=getLastFertilizingDate(plant.id);constfertilizingInterval=plant.fertilizingInterval||14;// 默认14天if(!lastFertilizingDate){sendFertilizingReminder(plant.id,plant.name,'从未施过肥');return;}constdaysSinceFertilizing=Math.floor((newDate()-newDate(lastFertilizingDate))/(24*60*60*1000));if(daysSinceFertilizing>=fertilizingInterval){sendFertilizingReminder(plant.id,plant.name,`${daysSinceFertilizing}天未施肥`);}});}functionsendFertilizingReminder(plantId,plantName,message){cordova.exec(function(result){console.log("施肥提醒已发送");},function(error){console.error("提醒发送失败:",error);},"NotificationPlugin","sendReminder",[{title:`${plantName}需要施肥`,message:message,plantId:plantId,type:'fertilizing'}]);}functiongetLastFertilizingDate(plantId){constrecords=fertilizingManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));returnrecords.length>0?records[0].date:null;}setInterval(checkFertilizingReminders,60*60*1000);// 每小时检查一次

这段代码实现了施肥提醒功能。checkFertilizingReminders函数检查所有植物,如果某个植物超过设定的施肥间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。这个功能帮助用户不会忘记给植物施肥。

肥料库存管理

classFertilizerInventory{constructor(){this.inventory=newMap();// 肥料ID -> 库存量}addFertilizerStock(fertilizerTypeId,quantity){constcurrent=this.inventory.get(fertilizerTypeId)||0;this.inventory.set(fertilizerTypeId,current+quantity);}useFertilizer(fertilizerTypeId,quantity){constcurrent=this.inventory.get(fertilizerTypeId)||0;if(current<quantity){returnfalse;// 库存不足}this.inventory.set(fertilizerTypeId,current-quantity);returntrue;}getFertilizerStock(fertilizerTypeId){returnthis.inventory.get(fertilizerTypeId)||0;}}

这个FertilizerInventory类管理肥料的库存。通过addFertilizerStock添加库存,useFertilizer消耗库存,getFertilizerStock查询库存。这个功能帮助用户管理肥料的库存情况。

总结

施肥记录管理系统是植物养护应用的重要功能。通过合理的数据模型设计、与OpenHarmony系统的集成和各种统计分析功能,我们可以创建一个功能完整的施肥管理系统,帮助用户科学地管理植物的施肥。

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

OE 平台是什么?基于多来源数字内容管理需求形成的海外工具型平台

OE 平台通常被归纳为一类海外数字内容管理工具&#xff0c;其形成背景并非单一业务需求&#xff0c;而是源于数字内容在不同平台、不同模块中不断分散后所产生的集中管理需求。从平台属性来看&#xff0c;OE 更接近于信息与内容的管理层工具&#xff0c;而非具体功能或服务平台…

作者头像 李华
网站建设 2026/2/6 1:20:25

LobeChat能否绘制思维导图?结构化思考好伙伴

LobeChat能否绘制思维导图&#xff1f;结构化思考好伙伴 在知识爆炸的时代&#xff0c;我们每天都在处理海量信息——会议纪要、读书笔记、项目规划……但真正能被内化和复用的却少之又少。一个核心问题在于&#xff1a;人类擅长线性表达&#xff0c;却不善结构化组织。于是&a…

作者头像 李华
网站建设 2026/2/6 22:12:29

构建高效测试体系:测试文档编写规范详解

在软件开发的生命周期中&#xff0c;测试文档不仅是质量保证的重要载体&#xff0c;更是团队协作的关键纽带。规范的测试文档能够明确测试范围、统一测试标准、提升缺陷跟踪效率&#xff0c;并为产品迭代提供可靠依据。 一、测试计划文档规范 1.1 文档结构要求 测试计划文档…

作者头像 李华
网站建设 2026/2/8 8:38:05

从工具到思维:构筑持续测试的文化基石

一、引言&#xff1a;为何文化是持续测试的“隐形架构”在当今快速迭代的软件开发环境中&#xff0c;“持续测试”&#xff08;Continuous Testing&#xff09;早已不是陌生词汇。然而&#xff0c;实践中我们常常看到这样的场景&#xff1a;团队引入了最先进的自动化测试框架&a…

作者头像 李华
网站建设 2026/2/7 23:08:53

mac 效率工具那么多,为什么这个启动器能留下来

用顺手&#xff0c;才是真效率&#xff1a;我为什么会长期留下 OrbitRing 这个 macOS 启动器效率问题&#xff0c;往往输在“启动那几秒”很多人一提效率工具&#xff0c;就想到复杂设置、快捷键组合、自动化脚本。 但真正把 mac 用久了你会发现&#xff0c;最拖后腿的&#xf…

作者头像 李华