news 2026/7/7 16:14:29

Cordova与OpenHarmony修剪记录系统

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Cordova与OpenHarmony修剪记录系统

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

修剪管理系统概述

修剪是植物养护的重要环节,它促进植物的生长和塑形。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的修剪记录系统,包括修剪记录的创建、查询、统计和提醒功能。这个系统需要记录修剪的类型、位置和效果。

修剪记录数据模型

classPruningType{constructor(id,name,description,purpose){this.id=id;this.name=name;this.description=description;this.purpose=purpose;// 修剪目的}}classPruningRecord{constructor(plantId,pruningType,location,notes){this.id='prune_'+Date.now();this.plantId=plantId;this.pruningType=pruningType;this.location=location;// 修剪位置this.date=newDate();this.notes=notes;this.branchesRemoved=0;// 移除的枝条数}}classPruningManager{constructor(){this.records=[];this.pruningTypes=[];this.initDefaultPruningTypes();this.loadFromStorage();}initDefaultPruningTypes(){this.pruningTypes=[newPruningType('prune_1','轻修剪','移除枯死或病弱枝条','清理'),newPruningType('prune_2','中度修剪','塑形和促进分枝','塑形'),newPruningType('prune_3','重度修剪','大幅度修剪以促进新生长','更新'),newPruningType('prune_4','摘心','移除顶端生长点','促进分枝')];}addPruningRecord(plantId,pruningType,location,notes){constrecord=newPruningRecord(plantId,pruningType,location,notes);this.records.push(record);this.saveToStorage();returnrecord;}}

这个修剪记录数据模型定义了PruningType、PruningRecord和PruningManager类。PruningType类定义了修剪类型及其目的,PruningRecord类记录每次修剪的详细信息,PruningManager类管理所有修剪记录。

与OpenHarmony数据库的集成

functionsavePruningRecordToDatabase(record){cordova.exec(function(result){console.log("修剪记录已保存到数据库");},function(error){console.error("保存失败:",error);},"DatabasePlugin","savePruningRecord",[{id:record.id,plantId:record.plantId,pruningType:record.pruningType,location:record.location,date:record.date.toISOString(),notes:record.notes,branchesRemoved:record.branchesRemoved}]);}functionloadPruningRecordsFromDatabase(){cordova.exec(function(result){console.log("修剪记录已从数据库加载");pruningManager.records=result.map(rec=>{constrecord=newPruningRecord(rec.plantId,rec.pruningType,rec.location,rec.notes);record.id=rec.id;record.date=newDate(rec.date);record.branchesRemoved=rec.branchesRemoved;returnrecord;});renderPruningRecords();},function(error){console.error("加载失败:",error);},"DatabasePlugin","loadPruningRecords",[]);}

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

修剪记录列表展示

functionrenderPruningRecords(plantId){constplant=plants.find(p=>p.id===plantId);if(!plant)return;constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));constcontainer=document.getElementById('page-container');container.innerHTML=`<div class="pruning-records-container"> <h2>${plant.name}的修剪记录</h2> <button class="add-record-btn" onclick="showAddPruningRecordDialog('${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=>{constpruningType=pruningManager.pruningTypes.find(p=>p.id===record.pruningType);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="pruning-type">✂️ 修剪类型:${pruningType?.name||'未知'}</p> <p class="pruning-purpose">目的:${pruningType?.purpose||'N/A'}</p> <p class="pruning-location">位置:${record.location}</p> <p class="branches-removed">移除枝条:${record.branchesRemoved}条</p>${record.notes?`<p class="record-notes">备注:${record.notes}</p>`:''}</div> <div class="record-actions"> <button onclick="editPruningRecord('${record.id}')">编辑</button> <button onclick="deletePruningRecord('${record.id}')">删除</button> </div>`;recordsList.appendChild(recordItem);});container.appendChild(recordsList);}

这个函数负责渲染修剪记录列表。它显示了特定植物的所有修剪记录,包括日期、修剪类型、目的、位置和移除的枝条数。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。

添加修剪记录对话框

functionshowAddPruningRecordDialog(plantId){constdialog=document.createElement('div');dialog.className='modal-dialog';letpruningTypeOptions='';pruningManager.pruningTypes.forEach(type=>{pruningTypeOptions+=`<option value="${type.id}">${type.name}-${type.purpose}</option>`;});dialog.innerHTML=`<div class="modal-content"> <h3>添加修剪记录</h3> <form id="add-pruning-form"> <div class="form-group"> <label>修剪类型</label> <select id="pruning-type" required> <option value="">请选择修剪类型</option>${pruningTypeOptions}</select> </div> <div class="form-group"> <label>修剪位置</label> <input type="text" id="pruning-location" placeholder="例如: 顶端、左侧枝条" required> </div> <div class="form-group"> <label>移除枝条数</label> <input type="number" id="branches-removed" min="0" value="0"> </div> <div class="form-group"> <label>修剪日期</label> <input type="datetime-local" id="pruning-date" required> </div> <div class="form-group"> <label>备注</label> <textarea id="pruning-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('pruning-date').value=now.toISOString().slice(0,16);document.getElementById('add-pruning-form').addEventListener('submit',function(e){e.preventDefault();constpruningType=document.getElementById('pruning-type').value;constlocation=document.getElementById('pruning-location').value;constbranchesRemoved=parseInt(document.getElementById('branches-removed').value);constdate=newDate(document.getElementById('pruning-date').value);constnotes=document.getElementById('pruning-notes').value;constrecord=newPruningRecord(plantId,pruningType,location,notes);record.date=date;record.branchesRemoved=branchesRemoved;pruningManager.records.push(record);pruningManager.saveToStorage();savePruningRecordToDatabase(record);closeDialog();renderPruningRecords(plantId);showToast('修剪记录已添加');});}

这个函数创建并显示添加修剪记录的对话框。用户可以选择修剪类型、输入位置、枝条数、日期和备注。提交后,新记录会被添加到pruningManager中,并保存到数据库。

修剪统计功能

classPruningStatistics{constructor(pruningManager){this.pruningManager=pruningManager;}getTotalPruningCount(plantId){returnthis.pruningManager.records.filter(r=>r.plantId===plantId).length;}getTotalBranchesRemoved(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);returnrecords.reduce((sum,r)=>sum+r.branchesRemoved,0);}getMostCommonPruningType(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);consttypeCounts={};records.forEach(record=>{typeCounts[record.pruningType]=(typeCounts[record.pruningType]||0)+1;});constmostCommon=Object.keys(typeCounts).reduce((a,b)=>typeCounts[a]>typeCounts[b]?a:b);returnthis.pruningManager.pruningTypes.find(p=>p.id===mostCommon);}getPruningFrequency(plantId,days=30){constrecords=this.pruningManager.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);// 每周修剪次数}}

这个PruningStatistics类提供了修剪的统计功能。getTotalPruningCount返回修剪总次数,getTotalBranchesRemoved计算移除的总枝条数,getMostCommonPruningType返回最常用的修剪类型,getPruningFrequency计算修剪频率。这些统计信息可以帮助用户了解修剪规律。

修剪提醒功能

functioncheckPruningReminders(){plants.forEach(plant=>{constlastPruningDate=getLastPruningDate(plant.id);constpruningInterval=plant.pruningInterval||30;// 默认30天if(!lastPruningDate){sendPruningReminder(plant.id,plant.name,'从未修剪过');return;}constdaysSincePruning=Math.floor((newDate()-newDate(lastPruningDate))/(24*60*60*1000));if(daysSincePruning>=pruningInterval){sendPruningReminder(plant.id,plant.name,`${daysSincePruning}天未修剪`);}});}functionsendPruningReminder(plantId,plantName,message){cordova.exec(function(result){console.log("修剪提醒已发送");},function(error){console.error("提醒发送失败:",error);},"NotificationPlugin","sendReminder",[{title:`${plantName}需要修剪`,message:message,plantId:plantId,type:'pruning'}]);}functiongetLastPruningDate(plantId){constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));returnrecords.length>0?records[0].date:null;}setInterval(checkPruningReminders,60*60*1000);// 每小时检查一次

这段代码实现了修剪提醒功能。checkPruningReminders函数检查所有植物,如果某个植物超过设定的修剪间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。

修剪效果追踪

classPruningEffectTracking{constructor(){this.effectRecords=newMap();// 修剪ID -> 效果数据}recordPruningEffect(pruningRecordId,newGrowthDays,growthRate){this.effectRecords.set(pruningRecordId,{recordedDate:newDate(),newGrowthDays:newGrowthDays,// 新生长出现的天数growthRate:growthRate// 生长速率});}getPruningEffect(pruningRecordId){returnthis.effectRecords.get(pruningRecordId);}}

这个PruningEffectTracking类用于追踪修剪的效果。通过记录修剪后新生长出现的时间和生长速率,用户可以评估修剪的效果。

总结

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

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

Redis 发布订阅

Redis 发布订阅 概述 Redis 发布订阅(Publish/Subscribe)是 Redis 提供的一种消息发布和订阅的机制。它允许消息的发布者发布消息到频道(Channel),而订阅者可以订阅一个或多个频道,以便接收消息。这种机制常用于构建实时消息系统,如实时新闻推送、社交网络消息推送等。…

作者头像 李华
网站建设 2026/7/7 12:21:46

JQuery支持WebUploader完成百万文件断点续传的原理?

前端大文件上传系统&#xff08;纯原生JS实现&#xff09;—— 专治各种不服IE9的倔强开发者 各位前端老炮儿们&#xff0c;今天给大家带来一个能兼容IE9的20G大文件上传系统&#xff0c;保证让你的客户感动到哭&#xff08;或者吓跑&#xff09;。毕竟在这个Vue3横行的时代&a…

作者头像 李华
网站建设 2026/7/6 20:54:45

Vue3如何结合组件实现大文件分片的并行上传优化?

客户这边啊&#xff0c;是汽车制造行业里的大哥大&#xff0c;是那种数一数二的企业。他们自己有一整套非常棒的业务系统&#xff0c;这套系统就像他们的得力助手&#xff0c;每天帮他们处理各种事情。但呢&#xff0c;随着行业竞争越来越激烈&#xff0c;技术也日新月异&#…

作者头像 李华
网站建设 2026/7/6 7:32:59

类型分布统计-Cordovaopenharmony多维分析实战

一、功能概述 除了时间维度外&#xff0c;“喝水类型”也是一个非常重要的分析维度。例如&#xff0c;用户可能想知道最近一周喝了多少白开水、多少茶水、多少含糖饮料。本篇文章围绕“类型分布统计”页面&#xff0c;介绍如何在 Cordova Web 层 按类型进行聚合统计&#xff0c…

作者头像 李华
网站建设 2026/7/7 8:30:19

四时四名,一山万象:朝鲜金刚山的锦绣风姿

金刚山属太白山脉核心段&#xff0c;横跨金刚郡、通川郡等多地及韩国麟蹄郡&#xff0c;总面积达530平方公里。这座秀丽名山东西绵延40公里&#xff0c;南北纵贯60公里&#xff0c;海拔千米以上山峰逾60座&#xff0c;主峰毗卢峰以1638米的海拔雄踞群峰之上。山名源自佛教“金刚…

作者头像 李华
网站建设 2026/7/3 9:32:19

基于Spring Boot的果蔬销售系统

基于Spring Boot的果蔬销售系统&#xff08;含推荐算法&#xff09;介绍 基于Spring Boot的果蔬销售系统是一款结合智能推荐算法的电商平台&#xff0c;旨在优化果蔬销售流程&#xff0c;提升用户体验和销售效率。系统通过整合现代Web开发技术和个性化推荐算法&#xff0c;解决…

作者头像 李华