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:作者isbn:ISBNcategory:分类publisher:出版社publishYear:出版年份location:馆藏位置totalCopies:总册数availableCopies:可借册数description:简介coverColor:封面颜色borrowCount:借阅次数isCollected:是否收藏
初始化页面状态
使用@State保存输入内容、分类选择、筛选条件和搜索词:
@StateprivatesearchText:string='';@StateprivateselectedCategory:string='全部';@StateprivatefilterBorrowStatus:string='全部';@StateprivatecurrentUserId:string='2023001';@StateprivatenextBorrowId:number=5;准备一些初始图书数据:
@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{constbook=this.books.find((b:Book)=>b.id===bookId);if(!book||book.availableCopies<=0){return;}constnow=newDate();constborrowDate=now.toISOString().split('T')[0];constdueDate=newDate(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.nextBorrowId+=1;this.books=this.books.map((b:Book)=>{if(b.id===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.availableCopies-1,description:b.description,coverColor:b.coverColor,borrowCount:b.borrowCount+1,isCollected:b.isCollected};}returnb;});}借阅后可借数量减1,借阅次数加1,借阅期限30天。
续借图书
借阅中的图书可以续借一次:
privaterenewBook(recordId:number):void{this.borrowRecords=this.borrowRecords.map((record:BorrowRecord)=>{if(record.id===recordId&&record.status==='借阅中'&&record.renewCount<1){constnewDueDate=newDate(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.renewCount+1};}returnrecord;});}每本书最多续借一次,续借延长30天。
归还图书
用户可以归还图书:
privatereturnBook(recordId:number):void{constrecord=this.borrowRecords.find((r:BorrowRecord)=>r.id===recordId);if(!record||record.status==='已归还'){return;}constreturnDate=newDate().toISOString().split('T')[0];this.borrowRecords=this.borrowRecords.map((r:BorrowRecord)=>{if(r.id===recordId){return{id:r.id,bookId:r.bookId,bookName:r.bookName,borrowTime:r.borrowTime,dueTime:r.dueTime,returnTime:returnDate,status:'已归还',renewCount:r.renewCount};}returnr;});this.books=this.books.map((b:Book)=>{if(b.id===record.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.availableCopies+1,description:b.description,coverColor:b.coverColor,borrowCount:b.borrowCount,isCollected:b.isCollected};}returnb;});}归还后可借数量加1,记录归还时间。
收藏图书
用户可以收藏感兴趣的图书:
privatetoggleCollect(bookId:number):void{this.books=this.books.map((book:Book)=>{if(book.id===bookId){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[]{letresult=this.books;if(this.selectedCategory!=='全部'){result=result.filter((b:Book)=>b.category===this.selectedCategory);}if(this.searchText.trim().length>0){constkeyword=this.searchText.trim().toLowerCase();result=result.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.status===this.filterBorrowStatus);}分类选择按钮封装:
@BuilderCategoryButton(text:string){Button(text).height(32).padding({left:14,right:14}).fontSize(12).fontColor(this.selectedCategory===text?Color.White:'#344054').backgroundColor(this.selectedCategory===text?'#2563EB':'#EFF6FF').borderRadius(16).onClick(()=>{this.selectedCategory=text;});}热门推荐
按借阅次数排序推荐热门图书:
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 图书类应用布局
后续还可以加入图书预约、扫码借书、书评评分、阅读时长统计、新书通报、图书到期提醒、电子书在线阅读和图书馆座位预约等功能。