HarmonyOS ArkTS 实战:实现一个校园图书借阅与推荐应用

HarmonyOS ArkTS 实战:实现一个校园图书借阅与推荐应用
HarmonyOS ArkTS 实战实现一个校园图书借阅与推荐应用项目效果本文使用 HarmonyOS 和 ArkTS 实现一个校园图书借阅与推荐应用。应用可以检索图书馆藏在线借阅图书查看借阅记录和到期时间收藏喜欢的图书并提供图书分类筛选、借阅统计和热门推荐等功能。项目使用 DevEco Studio 开发适配 API 23 及以上版本。运行效果功能介绍本项目实现了以下功能检索图书馆藏查看图书详情在线借阅图书查看借阅记录续借图书归还图书收藏图书按分类筛选图书按状态筛选借阅统计在借、已归还、逾期数量热门图书推荐搜索图书定义数据结构首先定义图书和借阅记录的数据结构interfaceBorrowRecord{id:number;bookId:number;bookName:string;borrowTime:string;dueTime:string;returnTime:string;status:string;renewCount:number;}interfaceBook{id:number;title:string;author:string;isbn:string;category:string;publisher:string;publishYear:number;location:string;totalCopies:number;availableCopies:number;description:string;coverColor:string;borrowCount:number;isCollected:boolean;}字段说明如下BorrowRecord借阅记录id记录编号bookId图书编号bookName书名borrowTime借阅时间dueTime到期时间returnTime归还时间status状态借阅中/已归还/已逾期renewCount续借次数Book图书信息id图书编号title书名author作者isbnISBNcategory分类publisher出版社publishYear出版年份location馆藏位置totalCopies总册数availableCopies可借册数description简介coverColor封面颜色borrowCount借阅次数isCollected是否收藏初始化页面状态使用State保存输入内容、分类选择、筛选条件和搜索词StateprivatesearchText:string;StateprivateselectedCategory:string全部;StateprivatefilterBorrowStatus:string全部;StateprivatecurrentUserId:string2023001;StateprivatenextBorrowId:number5;准备一些初始图书数据Stateprivatebooks:Book[][{id:1,title:HarmonyOS应用开发实战,author:张荣超,isbn:9787111712345,category:计算机,publisher:机械工业出版社,publishYear:2024,location:三楼A区302架,totalCopies:5,availableCopies:2,description:从零开始学习HarmonyOS应用开发包含大量实战案例,coverColor:#2563EB,borrowCount:128,isCollected:false},{id:2,title:三体,author:刘慈欣,isbn:9787536692930,category:文学小说,publisher:重庆出版社,publishYear:2008,location:二楼B区105架,totalCopies:10,availableCopies:0,description:中国科幻里程碑作品雨果奖获奖作品,coverColor:#1E40AF,borrowCount:356,isCollected:true},{id:3,title:高等数学第七版,author:同济大学数学系,isbn:9787040396638,category:教材教辅,publisher:高等教育出版社,publishYear:2014,location:一楼C区012架,totalCopies:20,availableCopies:8,description:大学高等数学经典教材考研必备,coverColor:#059669,borrowCount:892,isCollected:false},{id:4,title:人类简史,author:尤瓦尔·赫拉利,isbn:9787508647357,category:历史人文,publisher:中信出版社,publishYear:2014,location:二楼B区208架,totalCopies:8,availableCopies:3,description:从认知革命到科技革命讲述人类的进化史,coverColor:#D97706,borrowCount:245,isCollected:false}];准备初始借阅记录StateprivateborrowRecords:BorrowRecord[][{id:1,bookId:1,bookName:HarmonyOS应用开发实战,borrowTime:2026-07-03,dueTime:2026-08-03,returnTime:,status:借阅中,renewCount:0},{id:2,bookId:3,bookName:高等数学第七版,borrowTime:2026-06-15,dueTime:2026-07-15,returnTime:,status:已逾期,renewCount:1},{id:3,bookId:4,bookName:人类简史,borrowTime:2026-06-01,dueTime:2026-07-01,returnTime:2026-06-28,status:已归还,renewCount:0}];借阅图书用户可以借阅可借的图书privateborrowBook(bookId:number):void{constbookthis.books.find((b:Book)b.idbookId);if(!book||book.availableCopies0){return;}constnownewDate();constborrowDatenow.toISOString().split(T)[0];constdueDatenewDate(now.getTime()30*24*60*60*1000).toISOString().split(T)[0];constrecord:BorrowRecord{id:this.nextBorrowId,bookId,bookName:book.title,borrowTime:borrowDate,dueTime:dueDate,returnTime:,status:借阅中,renewCount:0};this.borrowRecords[record,...this.borrowRecords];this.nextBorrowId1;this.booksthis.books.map((b:Book){if(b.idbookId){return{id:b.id,title:b.title,author:b.author,isbn:b.isbn,category:b.category,publisher:b.publisher,publishYear:b.publishYear,location:b.location,totalCopies:b.totalCopies,availableCopies:b.availableCopies-1,description:b.description,coverColor:b.coverColor,borrowCount:b.borrowCount1,isCollected:b.isCollected};}returnb;});}借阅后可借数量减1借阅次数加1借阅期限30天。续借图书借阅中的图书可以续借一次privaterenewBook(recordId:number):void{this.borrowRecordsthis.borrowRecords.map((record:BorrowRecord){if(record.idrecordIdrecord.status借阅中record.renewCount1){constnewDueDatenewDate(newDate(record.dueTime).getTime()30*24*60*60*1000).toISOString().split(T)[0];return{id:record.id,bookId:record.bookId,bookName:record.bookName,borrowTime:record.borrowTime,dueTime:newDueDate,returnTime:record.returnTime,status:借阅中,renewCount:record.renewCount1};}returnrecord;});}每本书最多续借一次续借延长30天。归还图书用户可以归还图书privatereturnBook(recordId:number):void{constrecordthis.borrowRecords.find((r:BorrowRecord)r.idrecordId);if(!record||record.status已归还){return;}constreturnDatenewDate().toISOString().split(T)[0];this.borrowRecordsthis.borrowRecords.map((r:BorrowRecord){if(r.idrecordId){return{id:r.id,bookId:r.bookId,bookName:r.bookName,borrowTime:r.borrowTime,dueTime:r.dueTime,returnTime:returnDate,status:已归还,renewCount:r.renewCount};}returnr;});this.booksthis.books.map((b:Book){if(b.idrecord.bookId){return{id:b.id,title:b.title,author:b.author,isbn:b.isbn,category:b.category,publisher:b.publisher,publishYear:b.publishYear,location:b.location,totalCopies:b.totalCopies,availableCopies:b.availableCopies1,description:b.description,coverColor:b.coverColor,borrowCount:b.borrowCount,isCollected:b.isCollected};}returnb;});}归还后可借数量加1记录归还时间。收藏图书用户可以收藏感兴趣的图书privatetoggleCollect(bookId:number):void{this.booksthis.books.map((book:Book){if(book.idbookId){return{id:book.id,title:book.title,author:book.author,isbn:book.isbn,category:book.category,publisher:book.publisher,publishYear:book.publishYear,location:book.location,totalCopies:book.totalCopies,availableCopies:book.availableCopies,description:book.description,coverColor:book.coverColor,borrowCount:book.borrowCount,isCollected:!book.isCollected};}returnbook;});}点击心形图标切换收藏状态。搜索和筛选图书支持按关键词搜索和分类筛选privategetFilteredBooks():Book[]{letresultthis.books;if(this.selectedCategory!全部){resultresult.filter((b:Book)b.categorythis.selectedCategory);}if(this.searchText.trim().length0){constkeywordthis.searchText.trim().toLowerCase();resultresult.filter((b:Book)b.title.toLowerCase().includes(keyword)||b.author.toLowerCase().includes(keyword));}returnresult;}privategetFilteredRecords():BorrowRecord[]{if(this.filterBorrowStatus全部){returnthis.borrowRecords;}returnthis.borrowRecords.filter((r:BorrowRecord)r.statusthis.filterBorrowStatus);}分类选择按钮封装BuilderCategoryButton(text:string){Button(text).height(32).padding({left:14,right:14}).fontSize(12).fontColor(this.selectedCategorytext?Color.White:#344054).backgroundColor(this.selectedCategorytext?#2563EB:#EFF6FF).borderRadius(16).onClick((){this.selectedCategorytext;});}热门推荐按借阅次数排序推荐热门图书privategetHotBooks():Book[]{return[...this.books].sort((a:Book,b:Book)b.borrowCount-a.borrowCount).slice(0,3);}取借阅次数最多的3本作为热门推荐。统计借阅数据统计在借、已归还、逾期数量privategetBorrowingCount():number{returnthis.borrowRecords.filter((r:BorrowRecord)r.status借阅中).length;}privategetOverdueCount():number{returnthis.borrowRecords.filter((r:BorrowRecord)r.status已逾期).length;}privategetReturnedCount():number{returnthis.borrowRecords.filter((r:BorrowRecord)r.status已归还).length;}顶部统计区域展示在借、已逾期、已归还、收藏数this.StatCard(在借,${this.getBorrowingCount()},#2563EB,#EFF6FF);this.StatCard(已逾期,${this.getOverdueCount()},#DC2626,#FEF2F2);this.StatCard(已归还,${this.getReturnedCount()},#059669,#ECFDF5);this.StatCard(收藏,${this.books.filter((b:Book)b.isCollected).length},#D97706,#FFF7E8);设置状态颜色不同借阅状态使用不同颜色privategetStatusColor(status:string):ResourceColor{if(status借阅中){return#2563EB;}if(status已逾期){return#DC2626;}return#059669;}privategetStatusBgColor(status:string):ResourceColor{if(status借阅中){return#DBEAFE;}if(status已逾期){return#FEE2E2;}return#DCFCE7;}颜色含义如下蓝色借阅中红色已逾期绿色已归还蓝色系页面主题色使用网格展示图书使用Grid两列布局展示图书Grid(){ForEach(this.getFilteredBooks(),(book:Book){GridItem(){this.BookCard(book)}},(book:Book)book.id.toString());}.columnsTemplate(1fr 1fr).rowsGap(12).columnsGap(12).width(100%)图书卡片左侧是彩色封面区域右侧是书名、作者、分类、可借状态和借阅按钮。页面设计说明应用使用蓝色作为主题色体现图书馆专业、知识的氛围。页面主要分为以下区域顶部标题和搜索栏借阅数据统计区域热门推荐横向滚动图书分类筛选图书网格列表我的借阅记录列表页面采用浅灰色背景和白色卡片。图书使用彩色封面模拟书脊效果可借/不可借使用绿色/红色标签逾期使用醒目的红色提醒借阅次数展示图书热度。SDK 配置本项目使用 HarmonyOS API 24满足 API 23 及以上要求{ name: default, compatibleSdkVersion: 6.1.1(24), runtimeOS: HarmonyOS, targetSdkVersion: 6.1.1(24), compileSdkVersion: 6.1.1(24) }entry 模块中的运行系统也要保持一致{ apiType: stageMode, targets: [ { name: default, runtimeOS: HarmonyOS } ] }运行项目使用 DevEco Studio 打开项目然后找到entry/src/main/ets/pages/Index.ets等待项目同步完成点击右侧的Preview按钮即可查看应用效果。项目总结本文使用 HarmonyOS 和 ArkTS 实现了一个校园图书借阅与推荐应用。项目实现了图书检索、在线借阅、续借归还、收藏图书、分类筛选、热门推荐、借阅统计、搜索功能等。通过这个项目可以掌握ArkTS 接口定义和关联数据结构State状态管理借阅流程和库存管理Grid网格布局展示图书List和ForEach列表渲染sort排序实现热门推荐搜索功能实现自定义Builder组件HarmonyOS 图书类应用布局后续还可以加入图书预约、扫码借书、书评评分、阅读时长统计、新书通报、图书到期提醒、电子书在线阅读和图书馆座位预约等功能。