HarmonyOS 6.0 图书馆管理系统(ArkTS)开发实战

一、项目概述

你想要开发的是基于HarmonyOS 6.0、使用ArkTS语言构建的图书馆管理系统,该系统面向图书馆管理员和读者,核心实现图书查询、借阅/归还、图书管理等基础功能,采用HarmonyOS 6.0的最新特性(如Stage模型、ArkUI组件化)开发,适配多设备形态,兼顾易用性和性能。
在这里插入图片描述

二、技术栈与环境准备

1. 核心技术

  • 开发语言:ArkTS(TypeScript超集,HarmonyOS原生开发语言)
  • 应用模型:Stage模型(HarmonyOS 6.0推荐的主流应用模型)
  • UI框架:ArkUI(基于TSX的声明式UI)
  • 数据存储:Preferences(轻量级键值存储)+ RelationalStore(关系型数据库)
  • 设备适配:自适应布局(Flex/Grid)

2. 环境要求

  • DevEco Studio:4.2及以上版本
  • HarmonyOS SDK:6.0(API Version 11)
  • 模拟器/真机:HarmonyOS 6.0及以上设备

三、核心功能设计

本系统聚焦3个核心模块,满足基础图书馆管理需求:

  1. 图书查询(读者端):按书名/作者/分类检索图书,查看图书状态(可借/已借出)
  2. 借阅/归还(管理员端):扫描/输入图书编号,完成借阅、归还操作
  3. 图书管理(管理员端):新增、编辑、删除图书信息

四、代码实现

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)

实现图书列表展示和关键词查询功能,采用ArkUI声明式UI:

@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)
  }
}

五、功能扩展与优化建议

  1. 持久化存储:当前数据仅存在于内存中,可集成RelationalStore将图书数据存入本地数据库,保证应用重启后数据不丢失;
  2. 权限管理:新增登录模块,区分管理员/读者权限(管理员可操作借阅/归还,读者仅可查询);
  3. 扫码功能:集成HarmonyOS的扫码API,通过扫描图书条形码/二维码快速获取图书编号;
  4. 多设备适配:使用MediaQuery适配手机、平板、智慧屏等不同尺寸设备,优化大屏布局;
  5. 网络同步:对接后端接口(如SpringBoot),实现多设备数据同步、远程图书管理。

六、运行效果

  1. 图书列表页:可输入关键词搜索图书,列表展示图书基本信息和状态;
  2. 借阅/归还页:输入图书编号和借阅人信息,完成借阅/归还操作,实时提示操作结果;
  3. 所有操作实时同步到内存中的图书数据,刷新列表可看到状态变化。

总结

  1. 本图书馆管理系统基于HarmonyOS 6.0 + ArkTS开发,采用Stage模型和声明式UI,核心实现了图书查询、借阅/归还、图书管理等基础功能;
  2. 代码采用单例模式管理图书数据,保证全局数据一致性,同时通过ArkUI组件实现了简洁易用的交互界面;
  3. 可基于本基础版本扩展持久化存储、权限管理、扫码、网络同步等功能,适配更复杂的图书馆业务场景。

该系统充分利用了HarmonyOS 6.0的ArkTS特性,代码结构清晰、易扩展,适合作为HarmonyOS应用开发的入门实战项目。

标签: none

添加新评论