HarmonyOS 6.0 图书馆管理系统(ArkTS)开发实战
你想要开发的是基于HarmonyOS 6.0、使用ArkTS语言构建的图书馆管理系统,该系统面向图书馆管理员和读者,核心实现图书查询、借阅/归还、图书管理等基础功能,采用HarmonyOS 6.0的最新特性(如Stage模型、ArkUI组件化)开发,适配多设备形态,兼顾易用性和性能。 本系统聚焦3个核心模块,满足基础图书馆管理需求: 定义图书和借阅记录的数据结构,作为全局数据模型: 实现图书列表展示和关键词查询功能,采用ArkUI声明式UI: 实现图书借阅和归还的核心操作: 该系统充分利用了HarmonyOS 6.0的ArkTS特性,代码结构清晰、易扩展,适合作为HarmonyOS应用开发的入门实战项目。HarmonyOS 6.0 图书馆管理系统(ArkTS)开发实战
一、项目概述

二、技术栈与环境准备
1. 核心技术
2. 环境要求
三、核心功能设计
四、代码实现
1. 项目结构(Stage模型)
library-system/
├── entry/
│ ├── src/main/ets/
│ │ ├── entryability/ # 应用入口
│ │ ├── pages/ # 页面(图书列表、借阅页、管理页)
│ │ ├── model/ # 数据模型
│ │ ├── util/ # 工具类(数据库、存储)
│ │ └── resources/ # 资源(字符串、样式)
2. 数据模型定义(model/BookModel.ets)
/**
* 图书数据模型
*/
export interface Book {
id: string; // 图书编号(唯一标识)
name: string; // 书名
author: string; // 作者
category: string; // 分类(如计算机、文学)
status: boolean; // 状态:true-可借,false-已借出
borrowTime?: string;// 借阅时间(可选)
borrower?: string; // 借阅人(可选)
}
/**
* 全局状态管理(简化版)
*/
export class BookManager {
private static instance: BookManager;
private books: Book[] = [];
private constructor() {
// 初始化测试数据
this.books = [
{ id: "001", name: "ArkTS开发实战", author: "鸿蒙开发者", category: "计算机", status: true },
{ id: "002", name: "HarmonyOS 6.0进阶", author: "华为技术团队", category: "计算机", status: false, borrowTime: "2026-02-01", borrower: "张三" },
{ id: "003", name: "百年孤独", author: "加西亚·马尔克斯", category: "文学", status: true }
];
}
// 单例模式,保证全局唯一实例
public static getInstance(): BookManager {
if (!BookManager.instance) {
BookManager.instance = new BookManager();
}
return BookManager.instance;
}
// 获取所有图书
getBooks(): Book[] {
return this.books;
}
// 按关键词查询图书
searchBooks(keyword: string): Book[] {
return this.books.filter(book =>
book.name.includes(keyword) ||
book.author.includes(keyword) ||
book.category.includes(keyword)
);
}
// 借阅图书
borrowBook(bookId: string, borrower: string): boolean {
const book = this.books.find(b => b.id === bookId);
if (book && book.status) {
book.status = false;
book.borrowTime = new Date().toLocaleDateString();
book.borrower = borrower;
return true;
}
return false;
}
// 归还图书
returnBook(bookId: string): boolean {
const book = this.books.find(b => b.id === bookId);
if (book && !book.status) {
book.status = true;
book.borrowTime = undefined;
book.borrower = undefined;
return true;
}
return false;
}
// 新增图书
addBook(book: Book): void {
this.books.push(book);
}
// 删除图书
deleteBook(bookId: string): boolean {
const index = this.books.findIndex(b => b.id === bookId);
if (index !== -1) {
this.books.splice(index, 1);
return true;
}
return false;
}
}3. 图书列表/查询页面(pages/BookListPage.ets)
@Entry
@Component
struct BookListPage {
// 状态变量:搜索关键词、图书列表
@State searchKeyword: string = "";
@State bookList: Book[] = [];
private bookManager = BookManager.getInstance();
// 页面初始化时加载数据
aboutToAppear() {
this.bookList = this.bookManager.getBooks();
}
// 搜索图书
onSearch() {
this.bookList = this.bookManager.searchBooks(this.searchKeyword);
}
build() {
Column() {
// 搜索栏
Row({ space: 10 }) {
TextField({ placeholder: "输入书名/作者/分类查询" })
.width("70%")
.height(40)
.border({ width: 1, radius: 8 })
.padding(8)
.onChange((value) => {
this.searchKeyword = value;
})
Button("搜索")
.width("20%")
.height(40)
.backgroundColor("#007DFF")
.onClick(() => this.onSearch())
}
.padding(10)
.width("100%")
// 图书列表
List() {
ForEach(this.bookList, (book: Book) => {
ListItem() {
Column() {
Row({ space: 15 }) {
Text(`编号:${book.id}`)
.fontSize(14)
.fontColor("#666")
Text(`书名:${book.name}`)
.fontSize(16)
.fontWeight(FontWeight.Bold)
Text(book.status ? "可借" : "已借出")
.fontSize(14)
.fontColor(book.status ? "#00C800" : "#FF4D4F")
}
.width("100%")
.padding(5)
Row({ space: 15 }) {
Text(`作者:${book.author}`)
.fontSize(14)
Text(`分类:${book.category}`)
.fontSize(14)
}
.width("100%")
.padding(5)
// 已借出图书显示借阅信息
if (!book.status) {
Row() {
Text(`借阅人:${book.borrower}`)
.fontSize(12)
.fontColor("#999")
Text(`借阅时间:${book.borrowTime}`)
.fontSize(12)
.fontColor("#999")
}
.width("100%")
.padding(5)
}
}
.width("100%")
.padding(10)
.borderBottom({ width: 0.5, color: "#EEEEEE" })
}
})
}
.width("100%")
.flexGrow(1)
}
.width("100%")
.height("100%")
.padding(5)
}
}4. 借阅/归还页面(pages/BorrowReturnPage.ets)
@Entry
@Component
struct BorrowReturnPage {
@State bookId: string = "";
@State borrower: string = "";
@State tipText: string = "";
@State tipColor: string = "#333";
private bookManager = BookManager.getInstance();
// 借阅操作
borrowBook() {
if (!this.bookId || !this.borrower) {
this.tipText = "图书编号和借阅人不能为空!";
this.tipColor = "#FF4D4F";
return;
}
const result = this.bookManager.borrowBook(this.bookId, this.borrower);
if (result) {
this.tipText = `借阅成功!图书${this.bookId}已借出`;
this.tipColor = "#00C800";
} else {
this.tipText = "借阅失败!图书不存在或已借出";
this.tipColor = "#FF4D4F";
}
// 清空输入框
this.bookId = "";
this.borrower = "";
}
// 归还操作
returnBook() {
if (!this.bookId) {
this.tipText = "图书编号不能为空!";
this.tipColor = "#FF4D4F";
return;
}
const result = this.bookManager.returnBook(this.bookId);
if (result) {
this.tipText = `归还成功!图书${this.bookId}已入库`;
this.tipColor = "#00C800";
} else {
this.tipText = "归还失败!图书不存在或未借出";
this.tipColor = "#FF4D4F";
}
// 清空输入框
this.bookId = "";
}
build() {
Column({ space: 20 }) {
// 借阅模块
Column({ space: 10 }) {
Text("图书借阅")
.fontSize(18)
.fontWeight(FontWeight.Bold)
.alignSelf(ItemAlign.Start)
TextField({ placeholder: "输入图书编号" })
.width("100%")
.height(40)
.border({ width: 1, radius: 8 })
.padding(8)
.onChange((value) => this.bookId = value)
TextField({ placeholder: "输入借阅人姓名" })
.width("100%")
.height(40)
.border({ width: 1, radius: 8 })
.padding(8)
.onChange((value) => this.borrower = value)
Button("确认借阅")
.width("100%")
.height(40)
.backgroundColor("#007DFF")
.onClick(() => this.borrowBook())
}
.width("90%")
.padding(15)
.backgroundColor("#F5F7FA")
.borderRadius(10)
// 归还模块
Column({ space: 10 }) {
Text("图书归还")
.fontSize(18)
.fontWeight(FontWeight.Bold)
.alignSelf(ItemAlign.Start)
TextField({ placeholder: "输入图书编号" })
.width("100%")
.height(40)
.border({ width: 1, radius: 8 })
.padding(8)
.onChange((value) => this.bookId = value)
Button("确认归还")
.width("100%")
.height(40)
.backgroundColor("#00C800")
.onClick(() => this.returnBook())
}
.width("90%")
.padding(15)
.backgroundColor("#F5F7FA")
.borderRadius(10)
// 提示信息
Text(this.tipText)
.fontSize(14)
.fontColor(this.tipColor)
}
.width("100%")
.height("100%")
.padding(20)
.justifyContent(FlexAlign.Center)
}
}五、功能扩展与优化建议
六、运行效果
总结