PC 端完成
@@ -0,0 +1,58 @@
|
||||
# Build directories
|
||||
build/
|
||||
build-*/
|
||||
cmake-build-*/
|
||||
out/
|
||||
|
||||
# CMake generated files
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
compile_commands.json
|
||||
CTestTestfile.cmake
|
||||
DartConfiguration.tcl
|
||||
|
||||
# Qt Creator local settings
|
||||
.qtcreator/
|
||||
*.user
|
||||
*.user.*
|
||||
|
||||
# Qt generated artifacts
|
||||
*_autogen/
|
||||
*.qmlc
|
||||
*.jsc
|
||||
qrc_*.cpp
|
||||
moc_*.cpp
|
||||
moc_*.h
|
||||
ui_*.h
|
||||
|
||||
# Compiled binaries and libraries
|
||||
*.exe
|
||||
*.dll
|
||||
*.so
|
||||
*.dylib
|
||||
*.a
|
||||
*.lib
|
||||
*.o
|
||||
*.obj
|
||||
*.pdb
|
||||
*.ilk
|
||||
|
||||
# Android build outputs
|
||||
android-build/
|
||||
*.apk
|
||||
*.aab
|
||||
|
||||
# Logs and temporary files
|
||||
*.log
|
||||
*.tmp
|
||||
*.temp
|
||||
*.bak
|
||||
*.swp
|
||||
*~
|
||||
|
||||
# OS files
|
||||
.DS_Store
|
||||
Thumbs.db
|
||||
desktop.ini
|
||||
@@ -0,0 +1,124 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(rewrite_qt VERSION 1.0.0 LANGUAGES CXX)
|
||||
|
||||
set(QT_QML_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/qml")
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_AUTOMOC ON)
|
||||
set(CMAKE_AUTOMOC_COMPILER_PREDEFINES OFF)
|
||||
set(CMAKE_AUTORCC ON)
|
||||
|
||||
file(TO_CMAKE_PATH "$ENV{LOCALAPPDATA}" REWRITE_QT_LOCALAPPDATA)
|
||||
if(REWRITE_QT_LOCALAPPDATA)
|
||||
set(REWRITE_QT_AUTOGEN_DIR "${REWRITE_QT_LOCALAPPDATA}/rewrite_qt_autogen")
|
||||
else()
|
||||
set(REWRITE_QT_AUTOGEN_DIR "${CMAKE_BINARY_DIR}/autogen_ascii")
|
||||
endif()
|
||||
file(MAKE_DIRECTORY "${REWRITE_QT_AUTOGEN_DIR}")
|
||||
|
||||
find_package(Qt6 6.2 REQUIRED COMPONENTS Core Gui Qml Quick QuickControls2 Network)
|
||||
|
||||
qt_standard_project_setup()
|
||||
|
||||
set(REWRITE_QT_APP_ICON "${CMAKE_CURRENT_SOURCE_DIR}/assets/favicon.ico")
|
||||
set(REWRITE_QT_ICON_RESOURCES
|
||||
assets/favicon.ico
|
||||
assets/favicon-256x256.png
|
||||
assets/favicon-192x192.png
|
||||
assets/favicon-128x128.png
|
||||
assets/favicon-64x64.png
|
||||
assets/favicon-48x48.png
|
||||
assets/favicon-32x32.png
|
||||
assets/favicon-16x16.png
|
||||
)
|
||||
|
||||
qt_add_executable(apprewrite_qt
|
||||
main.cpp
|
||||
src/AppController.cpp
|
||||
src/ApiClient.cpp
|
||||
src/CryptoUtils.cpp
|
||||
src/RecordListModel.cpp
|
||||
src/StatisticsListModel.cpp
|
||||
src/TimeSlotListModel.cpp
|
||||
src/MappingListModel.cpp
|
||||
src/FaceListModel.cpp
|
||||
)
|
||||
|
||||
if(WIN32 AND EXISTS "${REWRITE_QT_APP_ICON}")
|
||||
set(REWRITE_QT_WIN32_ICON_RC "${CMAKE_CURRENT_BINARY_DIR}/app_icon.rc")
|
||||
configure_file(cmake/app_icon.rc.in "${REWRITE_QT_WIN32_ICON_RC}" @ONLY)
|
||||
target_sources(apprewrite_qt PRIVATE "${REWRITE_QT_WIN32_ICON_RC}")
|
||||
endif()
|
||||
|
||||
if(ANDROID)
|
||||
set_target_properties(apprewrite_qt PROPERTIES
|
||||
QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android"
|
||||
QT_ANDROID_APP_ICON "@mipmap/ic_launcher"
|
||||
)
|
||||
endif()
|
||||
|
||||
target_include_directories(apprewrite_qt PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/src
|
||||
)
|
||||
|
||||
set(REWRITE_QT_RESOURCE_FILES
|
||||
qml/Main.qml
|
||||
)
|
||||
|
||||
foreach(REWRITE_QT_ICON_RESOURCE IN LISTS REWRITE_QT_ICON_RESOURCES)
|
||||
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${REWRITE_QT_ICON_RESOURCE}")
|
||||
list(APPEND REWRITE_QT_RESOURCE_FILES "${REWRITE_QT_ICON_RESOURCE}")
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
qt_add_resources(apprewrite_qt "qml_resources"
|
||||
PREFIX "/"
|
||||
FILES
|
||||
${REWRITE_QT_RESOURCE_FILES}
|
||||
)
|
||||
|
||||
target_link_libraries(apprewrite_qt
|
||||
PRIVATE
|
||||
Qt6::Core
|
||||
Qt6::Gui
|
||||
Qt6::Qml
|
||||
Qt6::Quick
|
||||
Qt6::QuickControls2
|
||||
Qt6::Network
|
||||
)
|
||||
|
||||
set_target_properties(apprewrite_qt PROPERTIES
|
||||
AUTOGEN_BUILD_DIR "${REWRITE_QT_AUTOGEN_DIR}"
|
||||
QT_QML_MODULE_NO_IMPORT_SCAN TRUE
|
||||
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
|
||||
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
|
||||
MACOSX_BUNDLE TRUE
|
||||
WIN32_EXECUTABLE TRUE
|
||||
)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
if(UNIX AND NOT APPLE AND NOT ANDROID)
|
||||
set(REWRITE_QT_DESKTOP_FILE "${CMAKE_CURRENT_BINARY_DIR}/rewrite_qt.desktop")
|
||||
configure_file(cmake/rewrite_qt.desktop.in "${REWRITE_QT_DESKTOP_FILE}" @ONLY)
|
||||
install(FILES "${REWRITE_QT_DESKTOP_FILE}"
|
||||
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/applications"
|
||||
)
|
||||
|
||||
foreach(REWRITE_QT_ICON_SIZE IN ITEMS 16 32 48 64 128 192 256)
|
||||
set(REWRITE_QT_LINUX_ICON "${CMAKE_CURRENT_SOURCE_DIR}/assets/favicon-${REWRITE_QT_ICON_SIZE}x${REWRITE_QT_ICON_SIZE}.png")
|
||||
if(EXISTS "${REWRITE_QT_LINUX_ICON}")
|
||||
install(FILES "${REWRITE_QT_LINUX_ICON}"
|
||||
DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/icons/hicolor/${REWRITE_QT_ICON_SIZE}x${REWRITE_QT_ICON_SIZE}/apps"
|
||||
RENAME rewrite_qt.png
|
||||
)
|
||||
endif()
|
||||
endforeach()
|
||||
endif()
|
||||
|
||||
install(TARGETS apprewrite_qt
|
||||
BUNDLE DESTINATION .
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
)
|
||||
@@ -0,0 +1,422 @@
|
||||
# 人脸识别考勤系统 Qt 客户端说明文档
|
||||
|
||||
## 1. 项目简介
|
||||
|
||||
本项目是基于 Qt 6 Quick/QML 开发的人脸识别考勤系统客户端,用于连接考勤服务端,完成考勤记录查看、筛选导出、统计分析、签到时段配置、学生和班级名称映射、人脸照片映射管理等功能。
|
||||
|
||||
程序入口为 `main.cpp`。启动后创建 `QGuiApplication` 和 `AppController`,并将 `AppController` 通过 QML 上下文属性 `appController` 暴露给 `qml/Main.qml`。界面由 QML 实现,业务逻辑、网络请求、数据模型、配置保存和 CSV 导出由 C++ 实现。
|
||||
|
||||
默认服务端地址:
|
||||
|
||||
```text
|
||||
http://27.106.113.205:8082
|
||||
```
|
||||
|
||||
## 2. 技术栈
|
||||
|
||||
- 语言:C++17、QML
|
||||
- 框架:Qt 6.2 或更高版本
|
||||
- Qt 模块:Core、Gui、Qml、Quick、QuickControls2、Network
|
||||
- 构建系统:CMake 3.16 或更高版本
|
||||
- 加密处理:AES-128-CBC,相关代码位于 `src/CryptoUtils.cpp`
|
||||
|
||||
## 3. 当前目录结构
|
||||
|
||||
```text
|
||||
rewrite_qt/
|
||||
├── CMakeLists.txt
|
||||
├── README.md
|
||||
├── main.cpp
|
||||
├── android/
|
||||
│ └── res/
|
||||
│ ├── mipmap-mdpi/ic_launcher.png
|
||||
│ ├── mipmap-hdpi/ic_launcher.png
|
||||
│ ├── mipmap-xhdpi/ic_launcher.png
|
||||
│ ├── mipmap-xxhdpi/ic_launcher.png
|
||||
│ └── mipmap-xxxhdpi/ic_launcher.png
|
||||
├── assets/
|
||||
│ ├── favicon.ico
|
||||
│ ├── favicon-16x16.png
|
||||
│ ├── favicon-32x32.png
|
||||
│ ├── favicon-48x48.png
|
||||
│ ├── favicon-64x64.png
|
||||
│ ├── favicon-128x128.png
|
||||
│ ├── favicon-192x192.png
|
||||
│ └── favicon-256x256.png
|
||||
├── cmake/
|
||||
│ ├── app_icon.rc.in
|
||||
│ └── rewrite_qt.desktop.in
|
||||
├── qml/
|
||||
│ └── Main.qml
|
||||
└── src/
|
||||
├── ApiClient.h/.cpp
|
||||
├── AppController.h/.cpp
|
||||
├── AttendanceTypes.h
|
||||
├── CryptoUtils.h/.cpp
|
||||
├── FaceListModel.h/.cpp
|
||||
├── MappingListModel.h/.cpp
|
||||
├── RecordListModel.h/.cpp
|
||||
├── StatisticsListModel.h/.cpp
|
||||
├── TimeSlotListModel.h/.cpp
|
||||
└── crypto/tiny_aes.hpp
|
||||
```
|
||||
|
||||
## 4. 构建与运行
|
||||
|
||||
### 4.1 环境要求
|
||||
|
||||
需要安装:
|
||||
|
||||
- Qt 6.2+
|
||||
- CMake 3.16+
|
||||
- 支持 C++17 的编译器,例如 MSVC、MinGW、Clang 或 GCC
|
||||
|
||||
Android 构建还需要配置 Qt for Android、Android SDK、Android NDK 和 JDK。
|
||||
|
||||
### 4.2 使用 Qt Creator
|
||||
|
||||
1. 打开项目根目录下的 `CMakeLists.txt`。
|
||||
2. 选择合适的 Qt Kit,例如 Desktop Qt 6.x 或 Android Qt 6.x。
|
||||
3. 重新配置 CMake。
|
||||
4. 构建并运行目标 `apprewrite_qt`。
|
||||
|
||||
### 4.3 使用命令行
|
||||
|
||||
Windows 示例:
|
||||
|
||||
```powershell
|
||||
cmake -S . -B build -DCMAKE_PREFIX_PATH=D:/Qt/6.10.0/mingw_64
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
Linux 示例:
|
||||
|
||||
```bash
|
||||
cmake -S . -B build -DCMAKE_PREFIX_PATH=/path/to/Qt/6.x/gcc_64
|
||||
cmake --build build
|
||||
```
|
||||
|
||||
安装到系统路径:
|
||||
|
||||
```bash
|
||||
cmake --install build
|
||||
```
|
||||
|
||||
## 5. 多平台图标配置
|
||||
|
||||
项目已经配置为多平台使用同一套应用图标。
|
||||
|
||||
### 5.1 Windows
|
||||
|
||||
Windows exe 图标使用:
|
||||
|
||||
```text
|
||||
assets/favicon.ico
|
||||
```
|
||||
|
||||
CMake 会在 Windows 构建时根据 `cmake/app_icon.rc.in` 生成资源文件,并把图标嵌入到可执行文件中。
|
||||
|
||||
### 5.2 桌面运行时图标
|
||||
|
||||
`main.cpp` 会设置应用窗口图标:
|
||||
|
||||
```cpp
|
||||
QIcon appIcon(QStringLiteral(":/assets/favicon.ico"));
|
||||
```
|
||||
|
||||
同时会加入多尺寸 PNG 作为回退资源:
|
||||
|
||||
```text
|
||||
assets/favicon-256x256.png
|
||||
assets/favicon-192x192.png
|
||||
assets/favicon-128x128.png
|
||||
assets/favicon-64x64.png
|
||||
assets/favicon-48x48.png
|
||||
assets/favicon-32x32.png
|
||||
assets/favicon-16x16.png
|
||||
```
|
||||
|
||||
这些图标文件会被加入 Qt 资源系统,路径前缀为 `:/assets/...`。
|
||||
|
||||
### 5.3 Linux
|
||||
|
||||
Linux 安装时会生成 `.desktop` 文件:
|
||||
|
||||
```text
|
||||
cmake/rewrite_qt.desktop.in
|
||||
```
|
||||
|
||||
并把不同尺寸的 PNG 安装到:
|
||||
|
||||
```text
|
||||
share/icons/hicolor/{size}x{size}/apps/rewrite_qt.png
|
||||
```
|
||||
|
||||
`.desktop` 文件中使用的图标名为:
|
||||
|
||||
```text
|
||||
rewrite_qt
|
||||
```
|
||||
|
||||
### 5.4 Android
|
||||
|
||||
Android 启动器图标位于:
|
||||
|
||||
```text
|
||||
android/res/mipmap-mdpi/ic_launcher.png
|
||||
android/res/mipmap-hdpi/ic_launcher.png
|
||||
android/res/mipmap-xhdpi/ic_launcher.png
|
||||
android/res/mipmap-xxhdpi/ic_launcher.png
|
||||
android/res/mipmap-xxxhdpi/ic_launcher.png
|
||||
```
|
||||
|
||||
对应尺寸:
|
||||
|
||||
| 目录 | 尺寸 |
|
||||
| --- | --- |
|
||||
| `mipmap-mdpi` | 48x48 |
|
||||
| `mipmap-hdpi` | 72x72 |
|
||||
| `mipmap-xhdpi` | 96x96 |
|
||||
| `mipmap-xxhdpi` | 144x144 |
|
||||
| `mipmap-xxxhdpi` | 192x192 |
|
||||
|
||||
CMake 中 Android 相关配置:
|
||||
|
||||
```cmake
|
||||
QT_ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android"
|
||||
QT_ANDROID_APP_ICON "@mipmap/ic_launcher"
|
||||
```
|
||||
|
||||
`QT_ANDROID_APP_ICON` 需要 Qt 6.9 或更高版本。当前 Qt 6.10 可正常使用。
|
||||
|
||||
## 6. 功能说明
|
||||
|
||||
### 6.1 考勤记录
|
||||
|
||||
考勤记录页支持:
|
||||
|
||||
- 查看总记录数、今日记录数、已加载记录数和当前筛选结果数。
|
||||
- 按班级、学号、姓名关键字筛选。
|
||||
- 按今天、最近 7 天、最近 30 天或指定日期范围筛选。
|
||||
- 导出当前筛选结果为 CSV。
|
||||
- 清空服务端全部考勤记录。
|
||||
|
||||
导出的记录 CSV 字段:
|
||||
|
||||
```text
|
||||
签到时间,班级编号,班级名称,学号,姓名,置信度,状态
|
||||
```
|
||||
|
||||
### 6.2 统计分析
|
||||
|
||||
统计页基于已加载记录在本地计算,支持:
|
||||
|
||||
- 今天、最近 7 天、最近 30 天、本月等快捷范围。
|
||||
- 自定义统计日期范围。
|
||||
- 按学生汇总签到次数、今日签到次数、最后签到时间和平均置信度。
|
||||
- 导出统计结果为 CSV。
|
||||
|
||||
导出的统计 CSV 字段:
|
||||
|
||||
```text
|
||||
学号,姓名,总签到次数,今日签到,最后签到时间,平均置信度
|
||||
```
|
||||
|
||||
### 6.3 配置中心
|
||||
|
||||
配置中心包含:
|
||||
|
||||
- 服务端地址配置和连接测试。
|
||||
- 签到时段新增、编辑、删除、启用和禁用。
|
||||
- 学号到姓名的映射。
|
||||
- 班级编号到班级名称的映射。
|
||||
|
||||
默认签到时段:
|
||||
|
||||
| 名称 | 开始时间 | 结束时间 | 默认状态 |
|
||||
| --- | --- | --- | --- |
|
||||
| 上午签到 | 08:00 | 09:30 | 启用 |
|
||||
| 中午签退 | 12:00 | 13:00 | 启用 |
|
||||
| 下午签到 | 14:00 | 15:00 | 启用 |
|
||||
| 晚上签退 | 17:00 | 18:30 | 启用 |
|
||||
|
||||
### 6.4 人脸管理
|
||||
|
||||
人脸管理页支持:
|
||||
|
||||
- 从服务端同步人脸照片 ID。
|
||||
- 从服务端同步所有学生 ID。
|
||||
- 展示人脸照片和绑定状态。
|
||||
- 为人脸 ID 绑定真实学号。
|
||||
- 保存人脸 ID 与学号映射。
|
||||
- 删除指定人脸信息,可选择同时删除该学生的考勤记录。
|
||||
|
||||
照片访问地址格式:
|
||||
|
||||
```text
|
||||
{serverUrl}/api/face_photo/{id}?ts={timestamp}
|
||||
```
|
||||
|
||||
`ts` 参数用于避免缓存导致照片不刷新。
|
||||
|
||||
## 7. 核心模块
|
||||
|
||||
| 文件 | 说明 |
|
||||
| --- | --- |
|
||||
| `main.cpp` | 初始化应用、设置应用信息和图标、加载 QML |
|
||||
| `qml/Main.qml` | 主界面和用户交互 |
|
||||
| `src/AppController.*` | 应用业务中枢,连接 QML、模型和网络客户端 |
|
||||
| `src/ApiClient.*` | HTTP API 客户端 |
|
||||
| `src/*ListModel.*` | 暴露给 QML 的列表模型 |
|
||||
| `src/AttendanceTypes.h` | 业务数据结构 |
|
||||
| `src/CryptoUtils.*` | AES-128-CBC 解密工具 |
|
||||
| `src/crypto/tiny_aes.hpp` | AES 底层实现 |
|
||||
|
||||
## 8. 服务端 API
|
||||
|
||||
客户端会把服务端地址与 `/api` 前缀拼接后访问接口。
|
||||
|
||||
| 方法 | 路径 | 用途 |
|
||||
| --- | --- | --- |
|
||||
| `GET` | `/api/records?limit={limit}&offset={offset}` | 获取考勤记录 |
|
||||
| `GET` | `/api/statistics` | 获取总数、今日数和服务端时间 |
|
||||
| `POST` | `/api/clear` | 清空全部记录 |
|
||||
| `DELETE` | `/api/record?timestamp={timestamp}&student_id={studentId}` | 删除单条记录 |
|
||||
| `GET` | `/api/timeslots` | 获取签到时段 |
|
||||
| `POST` | `/api/timeslots` | 保存签到时段 |
|
||||
| `GET` | `/api/mapping_config` | 获取学生和班级映射 |
|
||||
| `POST` | `/api/mapping_config` | 保存学生和班级映射 |
|
||||
| `GET` | `/api/face_id_mapping` | 获取人脸 ID 到学号映射 |
|
||||
| `POST` | `/api/face_id_mapping` | 保存人脸 ID 到学号映射 |
|
||||
| `GET` | `/api/face_photos` | 获取照片 ID 列表 |
|
||||
| `GET` | `/api/all_student_ids` | 获取所有学生 ID |
|
||||
| `GET` | `/api/face_photo/{id}` | 获取人脸照片 |
|
||||
| `DELETE` | `/api/face/{studentId}` | 删除人脸信息 |
|
||||
| `DELETE` | `/api/face/{studentId}?delete_records=true` | 删除人脸信息并删除相关考勤记录 |
|
||||
| `GET` | `/api/health` | 健康检查 |
|
||||
|
||||
### 8.1 考勤记录格式
|
||||
|
||||
`/api/records` 解密后应返回 JSON 数组。单条记录示例:
|
||||
|
||||
```json
|
||||
{
|
||||
"timestamp": "2026-04-24 08:10:23",
|
||||
"student_id": "20240001",
|
||||
"name": "张三",
|
||||
"confidence": 0.9821,
|
||||
"status": "success",
|
||||
"class_id": "1"
|
||||
}
|
||||
```
|
||||
|
||||
### 8.2 统计数据格式
|
||||
|
||||
```json
|
||||
{
|
||||
"total": 120,
|
||||
"today": 8,
|
||||
"server_time": "2026-04-24 10:30:00"
|
||||
}
|
||||
```
|
||||
|
||||
### 8.3 签到时段格式
|
||||
|
||||
```json
|
||||
{
|
||||
"time_slots": [
|
||||
{
|
||||
"name": "上午签到",
|
||||
"start_time": "08:00",
|
||||
"end_time": "09:30",
|
||||
"enabled": true
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
### 8.4 映射配置格式
|
||||
|
||||
```json
|
||||
{
|
||||
"student_name_map": {
|
||||
"20240001": "张三"
|
||||
},
|
||||
"class_name_map": {
|
||||
"1": "软件工程一班"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 8.5 人脸映射格式
|
||||
|
||||
```json
|
||||
{
|
||||
"mappings": {
|
||||
"face_001": "20240001"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 9. 本地配置
|
||||
|
||||
应用会保存以下本地状态:
|
||||
|
||||
- 服务端地址:`QSettings`,键名为 `server/url`。
|
||||
- 映射配置:`QStandardPaths::AppDataLocation/mapping_config.json`。
|
||||
- 签到时段配置:`QStandardPaths::AppDataLocation/timeslot_config.json`。
|
||||
|
||||
应用信息:
|
||||
|
||||
```text
|
||||
ApplicationName: 人脸识别考勤系统
|
||||
ApplicationVersion: 3.0.0
|
||||
OrganizationName: STM32 Face Recognition
|
||||
```
|
||||
|
||||
## 10. 加密响应
|
||||
|
||||
`/api/records` 和 `/api/statistics` 请求会带上:
|
||||
|
||||
```text
|
||||
X-Encrypted-Response: true
|
||||
```
|
||||
|
||||
客户端期望服务端返回 Base64 编码的 AES-128-CBC 密文。
|
||||
|
||||
解密参数位于 `src/CryptoUtils.cpp`:
|
||||
|
||||
- 算法:AES-128-CBC
|
||||
- Padding:PKCS#7
|
||||
- Key:`2b 7e 15 16 28 ae d2 a6 ab f7 15 88 09 cf 4f 3c`
|
||||
- IV:`00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f`
|
||||
|
||||
## 11. 常见问题
|
||||
|
||||
### 11.1 修改图标后没有变化
|
||||
|
||||
先重新运行 CMake 配置,再重新构建。Windows 资源管理器可能缓存 exe 图标,可以尝试换构建目录、改输出文件名或重启资源管理器。
|
||||
|
||||
### 11.2 启动后没有数据
|
||||
|
||||
检查服务端地址是否正确,并在配置中心点击连接测试。如果服务端不可达,状态栏会显示错误信息。
|
||||
|
||||
### 11.3 姓名或班级显示不正确
|
||||
|
||||
在配置中心维护学生姓名映射和班级名称映射,然后保存。保存后记录列表和统计结果会重新计算。
|
||||
|
||||
### 11.4 人脸照片不显示
|
||||
|
||||
检查 `/api/face_photos` 是否返回照片 ID,并确认 `/api/face_photo/{id}` 可以访问。
|
||||
|
||||
### 11.5 Android 图标不生效
|
||||
|
||||
确认使用 Qt 6.9 或更高版本,并重新配置 Android Kit。图标资源位于 `android/res/mipmap-*`,CMake 使用 `QT_ANDROID_APP_ICON "@mipmap/ic_launcher"` 指定启动图标。
|
||||
|
||||
## 12. 维护建议
|
||||
|
||||
- 构建目录已从项目中清理,后续重新构建时由 Qt Creator 或 CMake 自动生成。
|
||||
- 如果服务端记录量超过 1000 条,需要扩展分页加载逻辑。
|
||||
- 如果服务端加密参数变化,需要同步修改 `src/CryptoUtils.cpp`。
|
||||
- 如果要发布桌面版本,建议使用 Qt 部署工具收集运行时依赖。
|
||||
- 如果要发布 Android 版本,建议补充签名配置和 release 构建流程。
|
||||
|
After Width: | Height: | Size: 8.8 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 27 KiB |
|
After Width: | Height: | Size: 44 KiB |
|
After Width: | Height: | Size: 20 KiB |
|
After Width: | Height: | Size: 627 B |
|
After Width: | Height: | Size: 39 KiB |
|
After Width: | Height: | Size: 67 KiB |
|
After Width: | Height: | Size: 1.8 KiB |
|
After Width: | Height: | Size: 3.7 KiB |
|
After Width: | Height: | Size: 6.0 KiB |
|
After Width: | Height: | Size: 99 KiB |
@@ -0,0 +1 @@
|
||||
IDI_ICON1 ICON "@REWRITE_QT_APP_ICON@"
|
||||
@@ -0,0 +1,7 @@
|
||||
[Desktop Entry]
|
||||
Type=Application
|
||||
Name=Face Attendance
|
||||
Exec=apprewrite_qt
|
||||
Icon=rewrite_qt
|
||||
Terminal=false
|
||||
Categories=Utility;
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <QGuiApplication>
|
||||
#include <QIcon>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <QQmlContext>
|
||||
#include <QQuickStyle>
|
||||
|
||||
#include "src/AppController.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
QIcon appIcon(QStringLiteral(":/assets/favicon.ico"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-256x256.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-192x192.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-128x128.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-64x64.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-48x48.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-32x32.png"));
|
||||
appIcon.addFile(QStringLiteral(":/assets/favicon-16x16.png"));
|
||||
app.setWindowIcon(appIcon);
|
||||
|
||||
QCoreApplication::setApplicationName("人脸识别考勤系统");
|
||||
QCoreApplication::setApplicationVersion("3.0.0");
|
||||
QCoreApplication::setOrganizationName("STM32 Face Recognition");
|
||||
QQuickStyle::setStyle("Basic");
|
||||
|
||||
AppController controller;
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.rootContext()->setContextProperty("appController", &controller);
|
||||
|
||||
const QUrl url(QStringLiteral("qrc:/qml/Main.qml"));
|
||||
QObject::connect(
|
||||
&engine,
|
||||
&QQmlApplicationEngine::objectCreated,
|
||||
&app,
|
||||
[url](QObject *obj, const QUrl &objUrl) {
|
||||
if (!obj && url == objUrl) {
|
||||
QCoreApplication::exit(-1);
|
||||
}
|
||||
},
|
||||
Qt::QueuedConnection);
|
||||
|
||||
engine.load(url);
|
||||
return app.exec();
|
||||
}
|
||||
@@ -0,0 +1,494 @@
|
||||
#include "ApiClient.h"
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QUrlQuery>
|
||||
|
||||
#include "CryptoUtils.h"
|
||||
|
||||
namespace {
|
||||
constexpr int NetworkTimeoutMs = 5000;
|
||||
}
|
||||
|
||||
ApiClient::ApiClient(QObject *parent)
|
||||
: QObject(parent)
|
||||
, m_serverUrl("http://27.106.113.205:8082")
|
||||
{
|
||||
}
|
||||
|
||||
void ApiClient::setServerUrl(const QString &url)
|
||||
{
|
||||
const QString trimmed = url.trimmed();
|
||||
if (!trimmed.isEmpty()) {
|
||||
m_serverUrl = trimmed;
|
||||
}
|
||||
}
|
||||
|
||||
QString ApiClient::serverUrl() const
|
||||
{
|
||||
return m_serverUrl;
|
||||
}
|
||||
|
||||
QString ApiClient::buildUrl(const QString &endpoint, const QMap<QString, QString> ¶ms) const
|
||||
{
|
||||
QString url = m_serverUrl + "/api" + endpoint;
|
||||
if (!params.isEmpty()) {
|
||||
QUrlQuery query;
|
||||
for (auto it = params.begin(); it != params.end(); ++it) {
|
||||
query.addQueryItem(it.key(), it.value());
|
||||
}
|
||||
url += "?" + query.toString(QUrl::FullyEncoded);
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
QNetworkRequest ApiClient::createRequest(const QString &url, bool encryptedResponse) const
|
||||
{
|
||||
QNetworkRequest request{QUrl(url)};
|
||||
if (encryptedResponse) {
|
||||
request.setRawHeader("X-Encrypted-Response", "true");
|
||||
}
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
request.setTransferTimeout(NetworkTimeoutMs);
|
||||
return request;
|
||||
}
|
||||
|
||||
QByteArray ApiClient::maybeDecrypt(const QByteArray &data, bool encrypted) const
|
||||
{
|
||||
if (!encrypted) {
|
||||
return data;
|
||||
}
|
||||
return CryptoUtils::decryptAes128CbcBase64(data);
|
||||
}
|
||||
|
||||
bool ApiClient::isConnectionError(QNetworkReply::NetworkError error) const
|
||||
{
|
||||
switch (error) {
|
||||
case QNetworkReply::ConnectionRefusedError:
|
||||
case QNetworkReply::RemoteHostClosedError:
|
||||
case QNetworkReply::HostNotFoundError:
|
||||
case QNetworkReply::TimeoutError:
|
||||
case QNetworkReply::OperationCanceledError:
|
||||
case QNetworkReply::SslHandshakeFailedError:
|
||||
case QNetworkReply::TemporaryNetworkFailureError:
|
||||
case QNetworkReply::NetworkSessionFailedError:
|
||||
case QNetworkReply::BackgroundRequestNotAllowedError:
|
||||
case QNetworkReply::TooManyRedirectsError:
|
||||
case QNetworkReply::InsecureRedirectError:
|
||||
case QNetworkReply::UnknownNetworkError:
|
||||
case QNetworkReply::UnknownProxyError:
|
||||
case QNetworkReply::UnknownContentError:
|
||||
case QNetworkReply::ProtocolFailure:
|
||||
case QNetworkReply::UnknownServerError:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void ApiClient::emitNetworkError(const QString &prefix, QNetworkReply *reply)
|
||||
{
|
||||
const QString message = QStringLiteral("%1: %2").arg(prefix, reply->errorString());
|
||||
emit networkError(message);
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(message);
|
||||
}
|
||||
}
|
||||
|
||||
QVector<AttendanceRecord> ApiClient::parseRecords(const QByteArray &data) const
|
||||
{
|
||||
QVector<AttendanceRecord> records;
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(data);
|
||||
if (!doc.isArray()) {
|
||||
return records;
|
||||
}
|
||||
|
||||
const QJsonArray array = doc.array();
|
||||
records.reserve(array.size());
|
||||
for (const QJsonValue &value : array) {
|
||||
const QJsonObject object = value.toObject();
|
||||
AttendanceRecord record;
|
||||
record.timestamp = object.value("timestamp").toString().trimmed();
|
||||
record.studentId = object.value("student_id").toString().trimmed();
|
||||
record.originalName = object.value("name").toString().trimmed();
|
||||
record.confidence = object.value("confidence").toDouble();
|
||||
const QJsonValue statusValue = object.value("status");
|
||||
if (statusValue.isBool()) {
|
||||
record.status = statusValue.toBool() ? QStringLiteral("success") : QStringLiteral("failed");
|
||||
} else if (statusValue.isDouble()) {
|
||||
record.status = QString::number(statusValue.toInt());
|
||||
} else {
|
||||
record.status = statusValue.toString().trimmed();
|
||||
}
|
||||
record.classId = object.value("class_id").toString().trimmed();
|
||||
if (record.classId.isEmpty()) {
|
||||
record.classId = "0";
|
||||
}
|
||||
if (!record.timestamp.startsWith("0000-") && !record.timestamp.isEmpty() && !record.studentId.isEmpty()) {
|
||||
records.push_back(record);
|
||||
}
|
||||
}
|
||||
return records;
|
||||
}
|
||||
|
||||
void ApiClient::fetchRecords(int limit, int offset)
|
||||
{
|
||||
const QString url = buildUrl("/records", {
|
||||
{"limit", QString::number(limit)},
|
||||
{"offset", QString::number(offset)}
|
||||
});
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(url, true));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray rawData = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载记录失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QByteArray data = maybeDecrypt(rawData, true);
|
||||
if (data.isEmpty()) {
|
||||
emit networkError("记录数据解密失败");
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
emit recordsFetched(parseRecords(data));
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchStatistics()
|
||||
{
|
||||
const QString url = buildUrl("/statistics");
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(url, true));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray rawData = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载统计失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QByteArray data = maybeDecrypt(rawData, true);
|
||||
if (data.isEmpty()) {
|
||||
emit networkError("统计数据解密失败");
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonDocument doc = QJsonDocument::fromJson(data);
|
||||
const QJsonObject object = doc.object();
|
||||
emit statisticsFetched(object.value("total").toInt(),
|
||||
object.value("today").toInt(),
|
||||
object.value("server_time").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::clearAllRecords()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.post(createRequest(buildUrl("/clear")), QByteArray());
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("清空失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit recordsCleared(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit recordsCleared(object.value("success").toBool(), object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::deleteRecord(const QString ×tamp, const QString &studentId)
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.deleteResource(createRequest(buildUrl("/record", {
|
||||
{"timestamp", timestamp},
|
||||
{"student_id", studentId}
|
||||
})));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("记录删除失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit recordDeleted(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit recordDeleted(object.value("success").toBool(), object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchTimeSlots()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/timeslots")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载时间段失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
QVector<TimeSlot> timeSlots;
|
||||
const QJsonObject root = QJsonDocument::fromJson(data).object();
|
||||
const QJsonArray array = root.value("time_slots").toArray();
|
||||
for (const QJsonValue &value : array) {
|
||||
const QJsonObject object = value.toObject();
|
||||
timeSlots.push_back({
|
||||
object.value("name").toString(),
|
||||
QTime::fromString(object.value("start_time").toString(), "HH:mm"),
|
||||
QTime::fromString(object.value("end_time").toString(), "HH:mm"),
|
||||
object.value("enabled").toBool(true)
|
||||
});
|
||||
}
|
||||
emit timeSlotsFetched(timeSlots);
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::uploadTimeSlots(const QVector<TimeSlot> &timeSlots)
|
||||
{
|
||||
QJsonArray array;
|
||||
for (const TimeSlot &slot : timeSlots) {
|
||||
QJsonObject object;
|
||||
object["name"] = slot.name;
|
||||
object["start_time"] = slot.startTime.toString("HH:mm");
|
||||
object["end_time"] = slot.endTime.toString("HH:mm");
|
||||
object["enabled"] = slot.enabled;
|
||||
array.append(object);
|
||||
}
|
||||
|
||||
QJsonObject payload;
|
||||
payload["time_slots"] = array;
|
||||
QNetworkReply *reply = m_networkManager.post(createRequest(buildUrl("/timeslots")),
|
||||
QJsonDocument(payload).toJson(QJsonDocument::Compact));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("时间段同步失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit timeSlotsUploaded(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit timeSlotsUploaded(object.value("success").toBool(), object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchMappingConfig()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/mapping_config")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载映射配置失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject root = QJsonDocument::fromJson(data).object();
|
||||
QMap<QString, QString> studentMap;
|
||||
QMap<QString, QString> classMap;
|
||||
|
||||
const QJsonObject studentObject = root.value("student_name_map").toObject();
|
||||
for (auto it = studentObject.begin(); it != studentObject.end(); ++it) {
|
||||
studentMap[it.key()] = it.value().toString();
|
||||
}
|
||||
|
||||
const QJsonObject classObject = root.value("class_name_map").toObject();
|
||||
for (auto it = classObject.begin(); it != classObject.end(); ++it) {
|
||||
classMap[it.key()] = it.value().toString();
|
||||
}
|
||||
|
||||
emit mappingConfigFetched(studentMap, classMap);
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::uploadMappingConfig(const QMap<QString, QString> &studentNameMap,
|
||||
const QMap<QString, QString> &classNameMap)
|
||||
{
|
||||
QJsonObject studentObject;
|
||||
for (auto it = studentNameMap.begin(); it != studentNameMap.end(); ++it) {
|
||||
studentObject[it.key()] = it.value();
|
||||
}
|
||||
|
||||
QJsonObject classObject;
|
||||
for (auto it = classNameMap.begin(); it != classNameMap.end(); ++it) {
|
||||
classObject[it.key()] = it.value();
|
||||
}
|
||||
|
||||
QJsonObject payload;
|
||||
payload["student_name_map"] = studentObject;
|
||||
payload["class_name_map"] = classObject;
|
||||
|
||||
QNetworkReply *reply = m_networkManager.post(createRequest(buildUrl("/mapping_config")),
|
||||
QJsonDocument(payload).toJson(QJsonDocument::Compact));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("映射保存失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit mappingConfigUploaded(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit mappingConfigUploaded(object.value("success").toBool(), object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchFaceIdMapping()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/face_id_mapping")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载人脸映射失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject mappings = QJsonDocument::fromJson(data).object().value("mappings").toObject();
|
||||
QMap<QString, QString> mapping;
|
||||
for (auto it = mappings.begin(); it != mappings.end(); ++it) {
|
||||
mapping[it.key()] = it.value().toString();
|
||||
}
|
||||
emit faceIdMappingFetched(mapping);
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::uploadFaceIdMapping(const QMap<QString, QString> &mapping)
|
||||
{
|
||||
QJsonObject mappings;
|
||||
for (auto it = mapping.begin(); it != mapping.end(); ++it) {
|
||||
if (!it.value().trimmed().isEmpty()) {
|
||||
mappings[it.key()] = it.value();
|
||||
}
|
||||
}
|
||||
|
||||
QJsonObject payload;
|
||||
payload["mappings"] = mappings;
|
||||
QNetworkReply *reply = m_networkManager.post(createRequest(buildUrl("/face_id_mapping")),
|
||||
QJsonDocument(payload).toJson(QJsonDocument::Compact));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("人脸映射保存失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit faceIdMappingUploaded(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit faceIdMappingUploaded(object.value("success").toBool(), object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchFacePhotos()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/face_photos")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载照片列表失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList ids;
|
||||
const QJsonArray photos = QJsonDocument::fromJson(data).object().value("photos").toArray();
|
||||
for (const QJsonValue &value : photos) {
|
||||
ids.push_back(value.toObject().value("id").toString());
|
||||
}
|
||||
emit facePhotosFetched(ids);
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::fetchAllStudentIds()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/all_student_ids")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emitNetworkError(QStringLiteral("加载学生ID失败"), reply);
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
QStringList ids;
|
||||
const QJsonArray array = QJsonDocument::fromJson(data).object().value("student_ids").toArray();
|
||||
for (const QJsonValue &value : array) {
|
||||
ids.push_back(value.toString());
|
||||
}
|
||||
emit allStudentIdsFetched(ids);
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::deleteFace(const QString &studentId, bool deleteRecords)
|
||||
{
|
||||
QString url = m_serverUrl + "/api/face/" + studentId;
|
||||
if (deleteRecords) {
|
||||
url += "?delete_records=true";
|
||||
}
|
||||
QNetworkReply *reply = m_networkManager.deleteResource(createRequest(url));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply, studentId]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
if (isConnectionError(reply->error())) {
|
||||
emit connectionUnavailable(QStringLiteral("删除失败: %1").arg(reply->errorString()));
|
||||
}
|
||||
emit faceDeleted(false, studentId, 0, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
emit faceDeleted(object.value("success").toBool(),
|
||||
studentId,
|
||||
object.value("records_deleted").toInt(),
|
||||
object.value("message").toString());
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
|
||||
void ApiClient::checkHealth()
|
||||
{
|
||||
QNetworkReply *reply = m_networkManager.get(createRequest(buildUrl("/health")));
|
||||
connect(reply, &QNetworkReply::finished, this, [this, reply]() {
|
||||
const QByteArray data = reply->readAll();
|
||||
if (reply->error() != QNetworkReply::NoError) {
|
||||
emit healthChecked(false, reply->errorString());
|
||||
reply->deleteLater();
|
||||
return;
|
||||
}
|
||||
const QJsonObject object = QJsonDocument::fromJson(data).object();
|
||||
const bool healthy = object.value("status").toString() == "healthy";
|
||||
emit healthChecked(healthy, healthy ? QStringLiteral("连接成功") : QStringLiteral("服务器无响应"));
|
||||
reply->deleteLater();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#ifndef API_CLIENT_H
|
||||
#define API_CLIENT_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QByteArray>
|
||||
#include <QMap>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
#include "AttendanceTypes.h"
|
||||
|
||||
class ApiClient : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ApiClient(QObject *parent = nullptr);
|
||||
|
||||
void setServerUrl(const QString &url);
|
||||
QString serverUrl() const;
|
||||
|
||||
void fetchRecords(int limit = 1000, int offset = 0);
|
||||
void fetchStatistics();
|
||||
void clearAllRecords();
|
||||
void deleteRecord(const QString ×tamp, const QString &studentId);
|
||||
void fetchTimeSlots();
|
||||
void uploadTimeSlots(const QVector<TimeSlot> &timeSlots);
|
||||
void fetchMappingConfig();
|
||||
void uploadMappingConfig(const QMap<QString, QString> &studentNameMap,
|
||||
const QMap<QString, QString> &classNameMap);
|
||||
void fetchFaceIdMapping();
|
||||
void uploadFaceIdMapping(const QMap<QString, QString> &mapping);
|
||||
void fetchFacePhotos();
|
||||
void fetchAllStudentIds();
|
||||
void deleteFace(const QString &studentId, bool deleteRecords);
|
||||
void checkHealth();
|
||||
|
||||
signals:
|
||||
void recordsFetched(const QVector<AttendanceRecord> &records);
|
||||
void statisticsFetched(int total, int today, const QString &serverTime);
|
||||
void recordsCleared(bool success, const QString &message);
|
||||
void recordDeleted(bool success, const QString &message);
|
||||
void timeSlotsFetched(const QVector<TimeSlot> &timeSlots);
|
||||
void timeSlotsUploaded(bool success, const QString &message);
|
||||
void mappingConfigFetched(const QMap<QString, QString> &studentNameMap,
|
||||
const QMap<QString, QString> &classNameMap);
|
||||
void mappingConfigUploaded(bool success, const QString &message);
|
||||
void faceIdMappingFetched(const QMap<QString, QString> &mapping);
|
||||
void faceIdMappingUploaded(bool success, const QString &message);
|
||||
void facePhotosFetched(const QStringList &photoIds);
|
||||
void allStudentIdsFetched(const QStringList &studentIds);
|
||||
void faceDeleted(bool success, const QString &studentId, int recordsDeleted, const QString &message);
|
||||
void healthChecked(bool healthy, const QString &message);
|
||||
void connectionUnavailable(const QString &message);
|
||||
void networkError(const QString &message);
|
||||
|
||||
private:
|
||||
QString buildUrl(const QString &endpoint, const QMap<QString, QString> ¶ms = {}) const;
|
||||
QNetworkRequest createRequest(const QString &url, bool encryptedResponse = false) const;
|
||||
QVector<AttendanceRecord> parseRecords(const QByteArray &data) const;
|
||||
QByteArray maybeDecrypt(const QByteArray &data, bool encrypted) const;
|
||||
void emitNetworkError(const QString &prefix, QNetworkReply *reply);
|
||||
bool isConnectionError(QNetworkReply::NetworkError error) const;
|
||||
|
||||
QNetworkAccessManager m_networkManager;
|
||||
QString m_serverUrl;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,734 @@
|
||||
#include "AppController.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QFile>
|
||||
#include <QJsonArray>
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QSettings>
|
||||
#include <QStandardPaths>
|
||||
#include <QStringConverter>
|
||||
#include <QSet>
|
||||
#include <QTextStream>
|
||||
#include <QUrl>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
constexpr int ReconnectIntervalMs = 5000;
|
||||
|
||||
QDate parseDate(const QString &value, const QDate &fallback)
|
||||
{
|
||||
const QDate date = QDate::fromString(value, Qt::ISODate);
|
||||
return date.isValid() ? date : fallback;
|
||||
}
|
||||
|
||||
bool isSuccessStatus(const QString &status)
|
||||
{
|
||||
const QString normalized = status.trimmed().toLower();
|
||||
return normalized == "success"
|
||||
|| normalized == "succeeded"
|
||||
|| normalized == "ok"
|
||||
|| normalized == "pass"
|
||||
|| normalized == "passed"
|
||||
|| normalized == "present"
|
||||
|| normalized == "recognized"
|
||||
|| normalized == "verified"
|
||||
|| normalized == "valid"
|
||||
|| normalized == "200"
|
||||
|| normalized == "true"
|
||||
|| normalized == "1"
|
||||
|| normalized == QStringLiteral("成功")
|
||||
|| normalized == QStringLiteral("签到成功")
|
||||
|| normalized == QStringLiteral("正常");
|
||||
}
|
||||
}
|
||||
|
||||
AppController::AppController(QObject *parent)
|
||||
: QObject(parent)
|
||||
{
|
||||
loadLocalState();
|
||||
m_apiClient.setServerUrl(m_serverUrl);
|
||||
m_reconnectTimer.setSingleShot(true);
|
||||
|
||||
connect(&m_reconnectTimer, &QTimer::timeout, this, [this]() {
|
||||
runReconnectCheck();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::recordsFetched, this, [this](const QVector<AttendanceRecord> &records) {
|
||||
m_allRecords = records;
|
||||
updateFilterOptions();
|
||||
rebuildFilteredRecords();
|
||||
rebuildStatistics();
|
||||
setStatusMessage(QStringLiteral("已加载 %1 条记录").arg(records.size()));
|
||||
emit summaryChanged();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::statisticsFetched, this, [this](int total, int today, const QString &) {
|
||||
m_totalRecords = total;
|
||||
m_todayRecords = today;
|
||||
emit summaryChanged();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::recordsCleared, this, [this](bool success, const QString &message) {
|
||||
setStatusMessage(success ? QStringLiteral("已清空全部记录") : QStringLiteral("清空失败: %1").arg(message));
|
||||
if (success) {
|
||||
refreshAll();
|
||||
}
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::recordDeleted, this, [this](bool success, const QString &message) {
|
||||
setStatusMessage(success ? QStringLiteral("记录删除成功") : QStringLiteral("记录删除失败: %1").arg(message));
|
||||
if (success) {
|
||||
refreshRecords();
|
||||
}
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::timeSlotsFetched, this, [this](const QVector<TimeSlot> &timeSlots) {
|
||||
if (!timeSlots.isEmpty()) {
|
||||
m_timeSlotModel.setTimeSlots(timeSlots);
|
||||
saveTimeSlotsToLocal();
|
||||
setStatusMessage(QStringLiteral("已从服务器同步时间段"));
|
||||
}
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::timeSlotsUploaded, this, [this](bool success, const QString &message) {
|
||||
setStatusMessage(success ? QStringLiteral("时间段已保存并同步") : QStringLiteral("时间段同步失败: %1").arg(message));
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::mappingConfigFetched, this,
|
||||
[this](const QMap<QString, QString> &studentNameMap, const QMap<QString, QString> &classNameMap) {
|
||||
m_studentMappingModel.setMap(studentNameMap);
|
||||
m_classMappingModel.setMap(classNameMap);
|
||||
saveMappingsToLocal();
|
||||
rebuildFilteredRecords();
|
||||
rebuildStatistics();
|
||||
setStatusMessage("映射配置已同步");
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::mappingConfigUploaded, this, [this](bool success, const QString &message) {
|
||||
setStatusMessage(success ? QStringLiteral("映射配置已保存") : QStringLiteral("映射保存失败: %1").arg(message));
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::faceIdMappingFetched, this, [this](const QMap<QString, QString> &mapping) {
|
||||
m_faceIdMapping = mapping;
|
||||
mergeFaceItems();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::faceIdMappingUploaded, this, [this](bool success, const QString &message) {
|
||||
setStatusMessage(success ? QStringLiteral("人脸映射已保存") : QStringLiteral("人脸映射保存失败: %1").arg(message));
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::facePhotosFetched, this, [this](const QStringList &photoIds) {
|
||||
m_facePhotoIds = photoIds;
|
||||
mergeFaceItems();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::allStudentIdsFetched, this, [this](const QStringList &studentIds) {
|
||||
m_allStudentIds = studentIds;
|
||||
mergeFaceItems();
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::faceDeleted, this, [this](bool success, const QString &studentId, int recordsDeleted, const QString &message) {
|
||||
if (success) {
|
||||
setStatusMessage(recordsDeleted > 0
|
||||
? QStringLiteral("已删除 %1,并清除 %2 条记录").arg(studentId).arg(recordsDeleted)
|
||||
: QStringLiteral("已删除 %1").arg(studentId));
|
||||
refreshFaces();
|
||||
refreshRecords();
|
||||
} else {
|
||||
setStatusMessage(QStringLiteral("删除失败: %1").arg(message));
|
||||
}
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::healthChecked, this, [this](bool healthy, const QString &message) {
|
||||
const bool wasReconnecting = m_reconnecting;
|
||||
m_reconnectCheckInFlight = false;
|
||||
if (healthy) {
|
||||
stopReconnect();
|
||||
setStatusMessage(wasReconnecting ? QStringLiteral("服务器已恢复连接,正在同步数据") : QStringLiteral("服务器连接正常"));
|
||||
if (wasReconnecting) {
|
||||
refreshAll();
|
||||
refreshFaces();
|
||||
}
|
||||
} else {
|
||||
startReconnect(message);
|
||||
}
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::networkError, this, [this](const QString &message) {
|
||||
setStatusMessage(message);
|
||||
});
|
||||
|
||||
connect(&m_apiClient, &ApiClient::connectionUnavailable, this, [this](const QString &message) {
|
||||
startReconnect(message);
|
||||
});
|
||||
|
||||
m_reconnecting = true;
|
||||
emit reconnectStateChanged();
|
||||
runReconnectCheck();
|
||||
}
|
||||
|
||||
RecordListModel *AppController::recordModel() { return &m_recordModel; }
|
||||
StatisticsListModel *AppController::statisticsModel() { return &m_statisticsModel; }
|
||||
TimeSlotListModel *AppController::timeSlotModel() { return &m_timeSlotModel; }
|
||||
MappingListModel *AppController::studentMappingModel() { return &m_studentMappingModel; }
|
||||
MappingListModel *AppController::classMappingModel() { return &m_classMappingModel; }
|
||||
FaceListModel *AppController::faceModel() { return &m_faceModel; }
|
||||
|
||||
QStringList AppController::classOptions() const { return m_classOptions; }
|
||||
QStringList AppController::studentOptions() const { return m_studentOptions; }
|
||||
QString AppController::serverUrl() const { return m_serverUrl; }
|
||||
QString AppController::statusMessage() const { return m_statusMessage; }
|
||||
bool AppController::reconnecting() const { return m_reconnecting; }
|
||||
int AppController::reconnectAttempt() const { return m_reconnectAttempt; }
|
||||
int AppController::totalRecords() const { return m_totalRecords; }
|
||||
int AppController::todayRecords() const { return m_todayRecords; }
|
||||
int AppController::filteredCount() const { return m_filteredRecords.size(); }
|
||||
int AppController::loadedRecords() const { return m_allRecords.size(); }
|
||||
int AppController::statisticsStudentCount() const { return m_statisticsStudentCount; }
|
||||
int AppController::statisticsRecordCount() const { return m_statisticsRecordCount; }
|
||||
|
||||
QString AppController::statisticsRangeText() const
|
||||
{
|
||||
return QString("%1 至 %2")
|
||||
.arg(m_statisticsStartDate.toString("yyyy-MM-dd"))
|
||||
.arg(m_statisticsEndDate.toString("yyyy-MM-dd"));
|
||||
}
|
||||
|
||||
void AppController::setServerUrl(const QString &url)
|
||||
{
|
||||
const QString trimmed = url.trimmed();
|
||||
if (trimmed.isEmpty() || trimmed == m_serverUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_serverUrl = trimmed;
|
||||
m_apiClient.setServerUrl(trimmed);
|
||||
saveServerUrl();
|
||||
emit serverUrlChanged();
|
||||
stopReconnect();
|
||||
setStatusMessage("服务器地址已更新,正在检查连接");
|
||||
m_reconnecting = true;
|
||||
emit reconnectStateChanged();
|
||||
runReconnectCheck();
|
||||
}
|
||||
|
||||
void AppController::refreshAll()
|
||||
{
|
||||
refreshRecords();
|
||||
m_apiClient.fetchStatistics();
|
||||
m_apiClient.fetchTimeSlots();
|
||||
m_apiClient.fetchMappingConfig();
|
||||
}
|
||||
|
||||
void AppController::refreshRecords()
|
||||
{
|
||||
m_apiClient.fetchRecords(1000, 0);
|
||||
}
|
||||
|
||||
void AppController::applyFilters(const QString &classId,
|
||||
const QString &studentId,
|
||||
const QString &nameFilter,
|
||||
int dateFilterIndex,
|
||||
const QString &specificDate)
|
||||
{
|
||||
m_selectedClassId = classId.isEmpty() ? QStringLiteral("全部") : classId;
|
||||
m_selectedStudentId = studentId.isEmpty() ? QStringLiteral("全部") : studentId;
|
||||
m_nameFilter = nameFilter.trimmed();
|
||||
m_dateFilterIndex = dateFilterIndex;
|
||||
m_specificDate = parseDate(specificDate, QDate::currentDate());
|
||||
rebuildFilteredRecords();
|
||||
}
|
||||
|
||||
void AppController::updateStatisticsRange(const QString &startDate, const QString &endDate)
|
||||
{
|
||||
m_statisticsStartDate = parseDate(startDate, QDate::currentDate().addDays(-30));
|
||||
m_statisticsEndDate = parseDate(endDate, QDate::currentDate());
|
||||
rebuildStatistics();
|
||||
}
|
||||
|
||||
bool AppController::exportFilteredRecords(const QString &fileUrl)
|
||||
{
|
||||
const QString filePath = QUrl(fileUrl).toLocalFile();
|
||||
if (filePath.isEmpty()) {
|
||||
setStatusMessage("导出路径无效");
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
setStatusMessage("无法创建导出文件");
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
out.setEncoding(QStringConverter::Utf8);
|
||||
out << "# 考勤记录导出\n";
|
||||
out << "# 导出时间: " << QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss") << "\n";
|
||||
out << "# 记录总数: " << m_filteredRecords.size() << "\n";
|
||||
out << "签到时间,班级编号,班级名称,学号,姓名,置信度,状态\n";
|
||||
|
||||
for (const AttendanceRecord &record : m_filteredRecords) {
|
||||
out << record.timestamp << ","
|
||||
<< record.classId << ","
|
||||
<< mappedClassName(record) << ","
|
||||
<< record.studentId << ","
|
||||
<< (mappedStudentName(record).isEmpty() ? record.originalName : mappedStudentName(record)) << ","
|
||||
<< QString::number(record.confidence, 'f', 4) << ","
|
||||
<< record.status << "\n";
|
||||
}
|
||||
|
||||
setStatusMessage(QStringLiteral("已导出 %1 条记录").arg(m_filteredRecords.size()));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool AppController::exportStatistics(const QString &fileUrl)
|
||||
{
|
||||
const QString filePath = QUrl(fileUrl).toLocalFile();
|
||||
if (filePath.isEmpty()) {
|
||||
setStatusMessage("统计导出路径无效");
|
||||
return false;
|
||||
}
|
||||
|
||||
QFile file(filePath);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
setStatusMessage("无法创建统计文件");
|
||||
return false;
|
||||
}
|
||||
|
||||
QTextStream out(&file);
|
||||
out.setEncoding(QStringConverter::Utf8);
|
||||
out << "考勤统计报表," << statisticsRangeText() << "\n";
|
||||
out << "学号,姓名,总签到次数,今日签到,最后签到时间,平均置信度\n";
|
||||
|
||||
for (int row = 0; row < m_statisticsModel.rowCount(); ++row) {
|
||||
const QModelIndex index = m_statisticsModel.index(row);
|
||||
out << m_statisticsModel.data(index, StatisticsListModel::StudentIdRole).toString() << ","
|
||||
<< m_statisticsModel.data(index, StatisticsListModel::NameRole).toString() << ","
|
||||
<< m_statisticsModel.data(index, StatisticsListModel::TotalCountRole).toInt() << ","
|
||||
<< m_statisticsModel.data(index, StatisticsListModel::TodayCountRole).toInt() << ","
|
||||
<< m_statisticsModel.data(index, StatisticsListModel::LastCheckInRole).toString() << ","
|
||||
<< QString::number(m_statisticsModel.data(index, StatisticsListModel::AverageConfidenceRole).toDouble(), 'f', 4) << "\n";
|
||||
}
|
||||
|
||||
setStatusMessage("统计报表已导出");
|
||||
return true;
|
||||
}
|
||||
|
||||
void AppController::clearAllRecords()
|
||||
{
|
||||
m_apiClient.clearAllRecords();
|
||||
}
|
||||
|
||||
void AppController::saveTimeSlots()
|
||||
{
|
||||
saveTimeSlotsToLocal();
|
||||
m_apiClient.uploadTimeSlots(m_timeSlotModel.timeSlots());
|
||||
}
|
||||
|
||||
void AppController::reloadTimeSlotsFromServer()
|
||||
{
|
||||
m_apiClient.fetchTimeSlots();
|
||||
}
|
||||
|
||||
void AppController::loadMappingsFromServer()
|
||||
{
|
||||
m_apiClient.fetchMappingConfig();
|
||||
}
|
||||
|
||||
void AppController::saveMappings()
|
||||
{
|
||||
saveMappingsToLocal();
|
||||
m_apiClient.uploadMappingConfig(m_studentMappingModel.toMap(), m_classMappingModel.toMap());
|
||||
rebuildFilteredRecords();
|
||||
rebuildStatistics();
|
||||
}
|
||||
|
||||
void AppController::refreshFaces()
|
||||
{
|
||||
m_apiClient.fetchFaceIdMapping();
|
||||
m_apiClient.fetchFacePhotos();
|
||||
m_apiClient.fetchAllStudentIds();
|
||||
}
|
||||
|
||||
void AppController::saveFaceMappings()
|
||||
{
|
||||
QMap<QString, QString> mapping;
|
||||
const QVector<FaceListModel::Item> items = m_faceModel.items();
|
||||
for (const FaceListModel::Item &item : items) {
|
||||
if (!item.studentId.trimmed().isEmpty()) {
|
||||
mapping[item.faceId] = item.studentId.trimmed();
|
||||
}
|
||||
}
|
||||
m_faceIdMapping = mapping;
|
||||
m_apiClient.uploadFaceIdMapping(mapping);
|
||||
}
|
||||
|
||||
void AppController::deleteFace(const QString &studentId, bool deleteRecords)
|
||||
{
|
||||
m_apiClient.deleteFace(studentId, deleteRecords);
|
||||
}
|
||||
|
||||
void AppController::testConnection()
|
||||
{
|
||||
setStatusMessage("正在检查服务器连接");
|
||||
m_apiClient.checkHealth();
|
||||
}
|
||||
|
||||
void AppController::loadLocalState()
|
||||
{
|
||||
QSettings settings;
|
||||
m_serverUrl = settings.value("server/url", "http://27.106.113.205:8082").toString();
|
||||
loadMappingsFromLocal();
|
||||
loadTimeSlotsFromLocal();
|
||||
}
|
||||
|
||||
void AppController::saveServerUrl()
|
||||
{
|
||||
QSettings settings;
|
||||
settings.setValue("server/url", m_serverUrl);
|
||||
}
|
||||
|
||||
void AppController::saveMappingsToLocal()
|
||||
{
|
||||
const QString path = localSettingsPath("mapping_config.json");
|
||||
QFile file(path);
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonObject root;
|
||||
QJsonObject studentObject;
|
||||
const QMap<QString, QString> studentMap = m_studentMappingModel.toMap();
|
||||
for (auto it = studentMap.begin(); it != studentMap.end(); ++it) {
|
||||
studentObject[it.key()] = it.value();
|
||||
}
|
||||
|
||||
QJsonObject classObject;
|
||||
const QMap<QString, QString> classMap = m_classMappingModel.toMap();
|
||||
for (auto it = classMap.begin(); it != classMap.end(); ++it) {
|
||||
classObject[it.key()] = it.value();
|
||||
}
|
||||
|
||||
root["student_name_map"] = studentObject;
|
||||
root["class_name_map"] = classObject;
|
||||
file.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
|
||||
}
|
||||
|
||||
void AppController::loadMappingsFromLocal()
|
||||
{
|
||||
QFile file(localSettingsPath("mapping_config.json"));
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
m_studentMappingModel.clear();
|
||||
m_classMappingModel.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const QJsonObject root = QJsonDocument::fromJson(file.readAll()).object();
|
||||
QMap<QString, QString> studentMap;
|
||||
const QJsonObject studentObject = root.value("student_name_map").toObject();
|
||||
for (auto it = studentObject.begin(); it != studentObject.end(); ++it) {
|
||||
studentMap[it.key()] = it.value().toString();
|
||||
}
|
||||
|
||||
QMap<QString, QString> classMap;
|
||||
const QJsonObject classObject = root.value("class_name_map").toObject();
|
||||
for (auto it = classObject.begin(); it != classObject.end(); ++it) {
|
||||
classMap[it.key()] = it.value().toString();
|
||||
}
|
||||
|
||||
m_studentMappingModel.setMap(studentMap);
|
||||
m_classMappingModel.setMap(classMap);
|
||||
}
|
||||
|
||||
void AppController::saveTimeSlotsToLocal()
|
||||
{
|
||||
QFile file(localSettingsPath("timeslot_config.json"));
|
||||
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
||||
return;
|
||||
}
|
||||
|
||||
QJsonArray array;
|
||||
for (const TimeSlot &slot : m_timeSlotModel.timeSlots()) {
|
||||
QJsonObject object;
|
||||
object["name"] = slot.name;
|
||||
object["start_time"] = slot.startTime.toString("HH:mm");
|
||||
object["end_time"] = slot.endTime.toString("HH:mm");
|
||||
object["enabled"] = slot.enabled;
|
||||
array.append(object);
|
||||
}
|
||||
|
||||
QJsonObject root;
|
||||
root["time_slots"] = array;
|
||||
file.write(QJsonDocument(root).toJson(QJsonDocument::Indented));
|
||||
}
|
||||
|
||||
void AppController::loadTimeSlotsFromLocal()
|
||||
{
|
||||
QFile file(localSettingsPath("timeslot_config.json"));
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
m_timeSlotModel.setTimeSlots(defaultTimeSlots());
|
||||
return;
|
||||
}
|
||||
|
||||
QVector<TimeSlot> timeSlots;
|
||||
const QJsonArray array = QJsonDocument::fromJson(file.readAll()).object().value("time_slots").toArray();
|
||||
for (const QJsonValue &value : array) {
|
||||
const QJsonObject object = value.toObject();
|
||||
timeSlots.push_back({
|
||||
object.value("name").toString(),
|
||||
QTime::fromString(object.value("start_time").toString(), "HH:mm"),
|
||||
QTime::fromString(object.value("end_time").toString(), "HH:mm"),
|
||||
object.value("enabled").toBool(true)
|
||||
});
|
||||
}
|
||||
|
||||
m_timeSlotModel.setTimeSlots(timeSlots.isEmpty() ? defaultTimeSlots() : timeSlots);
|
||||
}
|
||||
|
||||
QString AppController::localSettingsPath(const QString &fileName) const
|
||||
{
|
||||
const QString dirPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
||||
QDir().mkpath(dirPath);
|
||||
return dirPath + "/" + fileName;
|
||||
}
|
||||
|
||||
void AppController::updateFilterOptions()
|
||||
{
|
||||
QSet<QString> classIds;
|
||||
QSet<QString> studentIds;
|
||||
|
||||
for (const AttendanceRecord &record : m_allRecords) {
|
||||
classIds.insert(record.classId);
|
||||
studentIds.insert(record.studentId);
|
||||
}
|
||||
|
||||
m_classOptions = {"全部"};
|
||||
QStringList classValues = classIds.values();
|
||||
std::sort(classValues.begin(), classValues.end());
|
||||
m_classOptions.append(classValues);
|
||||
|
||||
m_studentOptions = {"全部"};
|
||||
QStringList studentValues = studentIds.values();
|
||||
std::sort(studentValues.begin(), studentValues.end());
|
||||
m_studentOptions.append(studentValues);
|
||||
|
||||
emit filtersChanged();
|
||||
}
|
||||
|
||||
void AppController::rebuildFilteredRecords()
|
||||
{
|
||||
QVector<RecordListModel::Item> items;
|
||||
m_filteredRecords.clear();
|
||||
const QDate today = QDate::currentDate();
|
||||
|
||||
for (const AttendanceRecord &record : m_allRecords) {
|
||||
if (m_selectedClassId != "全部" && record.classId != m_selectedClassId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (m_selectedStudentId != "全部" && record.studentId != m_selectedStudentId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QString displayName = mappedStudentName(record);
|
||||
const QString candidateName = displayName.isEmpty() ? record.originalName : displayName;
|
||||
if (!m_nameFilter.isEmpty() && !candidateName.contains(m_nameFilter, Qt::CaseInsensitive)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const QDate recordDate = record.dateTime().date();
|
||||
if (m_dateFilterIndex == 1 && recordDate != today) {
|
||||
continue;
|
||||
}
|
||||
if (m_dateFilterIndex == 2 && recordDate.daysTo(today) > 7) {
|
||||
continue;
|
||||
}
|
||||
if (m_dateFilterIndex == 3 && recordDate.daysTo(today) > 30) {
|
||||
continue;
|
||||
}
|
||||
if (m_dateFilterIndex == 4 && recordDate != m_specificDate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
m_filteredRecords.push_back(record);
|
||||
}
|
||||
|
||||
std::sort(m_filteredRecords.begin(), m_filteredRecords.end(), [](const AttendanceRecord &left, const AttendanceRecord &right) {
|
||||
return left.timestamp > right.timestamp;
|
||||
});
|
||||
|
||||
items.reserve(m_filteredRecords.size());
|
||||
for (const AttendanceRecord &record : m_filteredRecords) {
|
||||
items.push_back({
|
||||
record.timestamp,
|
||||
record.classId,
|
||||
mappedClassName(record),
|
||||
record.studentId,
|
||||
mappedStudentName(record).isEmpty() ? record.originalName : mappedStudentName(record),
|
||||
record.confidence,
|
||||
record.status,
|
||||
isSuccessStatus(record.status)
|
||||
});
|
||||
}
|
||||
|
||||
m_recordModel.setItems(items);
|
||||
emit summaryChanged();
|
||||
}
|
||||
|
||||
void AppController::rebuildStatistics()
|
||||
{
|
||||
QMap<QString, StudentStatistics> statisticsMap;
|
||||
const QDate today = QDate::currentDate();
|
||||
|
||||
for (const AttendanceRecord &record : m_allRecords) {
|
||||
const QDate date = record.dateTime().date();
|
||||
if (date < m_statisticsStartDate || date > m_statisticsEndDate) {
|
||||
continue;
|
||||
}
|
||||
|
||||
StudentStatistics &stats = statisticsMap[record.studentId];
|
||||
stats.studentId = record.studentId;
|
||||
stats.name = mappedStudentName(record).isEmpty() ? record.originalName : mappedStudentName(record);
|
||||
stats.totalCount += 1;
|
||||
stats.averageConfidence += record.confidence;
|
||||
if (date == today) {
|
||||
stats.todayCount += 1;
|
||||
}
|
||||
if (!stats.lastCheckIn.isValid() || record.dateTime() > stats.lastCheckIn) {
|
||||
stats.lastCheckIn = record.dateTime();
|
||||
}
|
||||
}
|
||||
|
||||
QVector<StatisticsListModel::Item> items;
|
||||
items.reserve(statisticsMap.size());
|
||||
m_statisticsStudentCount = statisticsMap.size();
|
||||
m_statisticsRecordCount = 0;
|
||||
|
||||
for (auto it = statisticsMap.begin(); it != statisticsMap.end(); ++it) {
|
||||
StudentStatistics stats = it.value();
|
||||
m_statisticsRecordCount += stats.totalCount;
|
||||
if (stats.totalCount > 0) {
|
||||
stats.averageConfidence /= static_cast<double>(stats.totalCount);
|
||||
}
|
||||
items.push_back({
|
||||
stats.studentId,
|
||||
stats.name,
|
||||
stats.totalCount,
|
||||
stats.todayCount,
|
||||
stats.lastCheckIn.isValid() ? stats.lastCheckIn.toString("yyyy-MM-dd HH:mm:ss") : QStringLiteral("无"),
|
||||
stats.averageConfidence
|
||||
});
|
||||
}
|
||||
|
||||
std::sort(items.begin(), items.end(), [](const StatisticsListModel::Item &left, const StatisticsListModel::Item &right) {
|
||||
return left.studentId < right.studentId;
|
||||
});
|
||||
|
||||
m_statisticsModel.setItems(items);
|
||||
emit statisticsChanged();
|
||||
}
|
||||
|
||||
QString AppController::mappedStudentName(const AttendanceRecord &record) const
|
||||
{
|
||||
return m_studentMappingModel.toMap().value(record.studentId);
|
||||
}
|
||||
|
||||
QString AppController::mappedClassName(const AttendanceRecord &record) const
|
||||
{
|
||||
return m_classMappingModel.toMap().value(record.classId, record.classId);
|
||||
}
|
||||
|
||||
void AppController::mergeFaceItems()
|
||||
{
|
||||
QSet<QString> allIds;
|
||||
for (const QString &id : m_facePhotoIds) {
|
||||
allIds.insert(id);
|
||||
}
|
||||
for (const QString &id : m_allStudentIds) {
|
||||
allIds.insert(id);
|
||||
}
|
||||
|
||||
QStringList sortedIds = allIds.values();
|
||||
std::sort(sortedIds.begin(), sortedIds.end());
|
||||
|
||||
QVector<FaceListModel::Item> items;
|
||||
items.reserve(sortedIds.size());
|
||||
for (const QString &id : sortedIds) {
|
||||
const bool hasPhoto = m_facePhotoIds.contains(id);
|
||||
const QString photoUrl = hasPhoto
|
||||
? QString("%1/api/face_photo/%2?ts=%3").arg(m_serverUrl, id, QString::number(QDateTime::currentMSecsSinceEpoch()))
|
||||
: QString();
|
||||
items.push_back({
|
||||
id,
|
||||
m_faceIdMapping.value(id, id),
|
||||
hasPhoto,
|
||||
photoUrl
|
||||
});
|
||||
}
|
||||
|
||||
m_faceModel.setItems(items);
|
||||
}
|
||||
|
||||
void AppController::setStatusMessage(const QString &message)
|
||||
{
|
||||
if (message == m_statusMessage) {
|
||||
return;
|
||||
}
|
||||
m_statusMessage = message;
|
||||
emit statusMessageChanged();
|
||||
}
|
||||
|
||||
void AppController::startReconnect(const QString &reason)
|
||||
{
|
||||
if (!m_reconnecting) {
|
||||
m_reconnecting = true;
|
||||
m_reconnectAttempt = 0;
|
||||
emit reconnectStateChanged();
|
||||
}
|
||||
|
||||
const QString detail = reason.trimmed();
|
||||
if (m_reconnectCheckInFlight || m_reconnectTimer.isActive()) {
|
||||
setStatusMessage(detail.isEmpty()
|
||||
? QStringLiteral("服务器连接失败,正在等待自动重连")
|
||||
: QStringLiteral("服务器连接失败,正在等待自动重连: %1").arg(detail));
|
||||
return;
|
||||
}
|
||||
|
||||
setStatusMessage(detail.isEmpty()
|
||||
? QStringLiteral("服务器连接失败,%1 秒后自动重连").arg(ReconnectIntervalMs / 1000)
|
||||
: QStringLiteral("服务器连接失败,%1 秒后自动重连: %2").arg(ReconnectIntervalMs / 1000).arg(detail));
|
||||
scheduleReconnect();
|
||||
}
|
||||
|
||||
void AppController::stopReconnect()
|
||||
{
|
||||
m_reconnectTimer.stop();
|
||||
m_reconnectCheckInFlight = false;
|
||||
if (!m_reconnecting && m_reconnectAttempt == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_reconnecting = false;
|
||||
m_reconnectAttempt = 0;
|
||||
emit reconnectStateChanged();
|
||||
}
|
||||
|
||||
void AppController::scheduleReconnect()
|
||||
{
|
||||
m_reconnectTimer.start(ReconnectIntervalMs);
|
||||
}
|
||||
|
||||
void AppController::runReconnectCheck()
|
||||
{
|
||||
if (!m_reconnecting || m_reconnectCheckInFlight) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_reconnectCheckInFlight = true;
|
||||
++m_reconnectAttempt;
|
||||
emit reconnectStateChanged();
|
||||
setStatusMessage(QStringLiteral("正在重连服务器,第 %1 次").arg(m_reconnectAttempt));
|
||||
m_apiClient.checkHealth();
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
#ifndef APP_CONTROLLER_H
|
||||
#define APP_CONTROLLER_H
|
||||
|
||||
#include <QDate>
|
||||
#include <QMap>
|
||||
#include <QObject>
|
||||
#include <QStringList>
|
||||
#include <QTimer>
|
||||
#include <QVector>
|
||||
#include "ApiClient.h"
|
||||
#include "FaceListModel.h"
|
||||
#include "MappingListModel.h"
|
||||
#include "RecordListModel.h"
|
||||
#include "StatisticsListModel.h"
|
||||
#include "TimeSlotListModel.h"
|
||||
|
||||
class AppController : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(RecordListModel* recordModel READ recordModel CONSTANT)
|
||||
Q_PROPERTY(StatisticsListModel* statisticsModel READ statisticsModel CONSTANT)
|
||||
Q_PROPERTY(TimeSlotListModel* timeSlotModel READ timeSlotModel CONSTANT)
|
||||
Q_PROPERTY(MappingListModel* studentMappingModel READ studentMappingModel CONSTANT)
|
||||
Q_PROPERTY(MappingListModel* classMappingModel READ classMappingModel CONSTANT)
|
||||
Q_PROPERTY(FaceListModel* faceModel READ faceModel CONSTANT)
|
||||
Q_PROPERTY(QStringList classOptions READ classOptions NOTIFY filtersChanged)
|
||||
Q_PROPERTY(QStringList studentOptions READ studentOptions NOTIFY filtersChanged)
|
||||
Q_PROPERTY(QString serverUrl READ serverUrl WRITE setServerUrl NOTIFY serverUrlChanged)
|
||||
Q_PROPERTY(QString statusMessage READ statusMessage NOTIFY statusMessageChanged)
|
||||
Q_PROPERTY(bool reconnecting READ reconnecting NOTIFY reconnectStateChanged)
|
||||
Q_PROPERTY(int reconnectAttempt READ reconnectAttempt NOTIFY reconnectStateChanged)
|
||||
Q_PROPERTY(int totalRecords READ totalRecords NOTIFY summaryChanged)
|
||||
Q_PROPERTY(int todayRecords READ todayRecords NOTIFY summaryChanged)
|
||||
Q_PROPERTY(int filteredCount READ filteredCount NOTIFY summaryChanged)
|
||||
Q_PROPERTY(int loadedRecords READ loadedRecords NOTIFY summaryChanged)
|
||||
Q_PROPERTY(int statisticsStudentCount READ statisticsStudentCount NOTIFY statisticsChanged)
|
||||
Q_PROPERTY(int statisticsRecordCount READ statisticsRecordCount NOTIFY statisticsChanged)
|
||||
Q_PROPERTY(QString statisticsRangeText READ statisticsRangeText NOTIFY statisticsChanged)
|
||||
|
||||
public:
|
||||
explicit AppController(QObject *parent = nullptr);
|
||||
|
||||
RecordListModel *recordModel();
|
||||
StatisticsListModel *statisticsModel();
|
||||
TimeSlotListModel *timeSlotModel();
|
||||
MappingListModel *studentMappingModel();
|
||||
MappingListModel *classMappingModel();
|
||||
FaceListModel *faceModel();
|
||||
|
||||
QStringList classOptions() const;
|
||||
QStringList studentOptions() const;
|
||||
QString serverUrl() const;
|
||||
QString statusMessage() const;
|
||||
bool reconnecting() const;
|
||||
int reconnectAttempt() const;
|
||||
int totalRecords() const;
|
||||
int todayRecords() const;
|
||||
int filteredCount() const;
|
||||
int loadedRecords() const;
|
||||
int statisticsStudentCount() const;
|
||||
int statisticsRecordCount() const;
|
||||
QString statisticsRangeText() const;
|
||||
|
||||
void setServerUrl(const QString &url);
|
||||
|
||||
Q_INVOKABLE void refreshAll();
|
||||
Q_INVOKABLE void refreshRecords();
|
||||
Q_INVOKABLE void applyFilters(const QString &classId,
|
||||
const QString &studentId,
|
||||
const QString &nameFilter,
|
||||
int dateFilterIndex,
|
||||
const QString &specificDate);
|
||||
Q_INVOKABLE void updateStatisticsRange(const QString &startDate, const QString &endDate);
|
||||
Q_INVOKABLE bool exportFilteredRecords(const QString &fileUrl);
|
||||
Q_INVOKABLE bool exportStatistics(const QString &fileUrl);
|
||||
Q_INVOKABLE void clearAllRecords();
|
||||
Q_INVOKABLE void saveTimeSlots();
|
||||
Q_INVOKABLE void reloadTimeSlotsFromServer();
|
||||
Q_INVOKABLE void loadMappingsFromServer();
|
||||
Q_INVOKABLE void saveMappings();
|
||||
Q_INVOKABLE void refreshFaces();
|
||||
Q_INVOKABLE void saveFaceMappings();
|
||||
Q_INVOKABLE void deleteFace(const QString &studentId, bool deleteRecords);
|
||||
Q_INVOKABLE void testConnection();
|
||||
|
||||
signals:
|
||||
void filtersChanged();
|
||||
void serverUrlChanged();
|
||||
void statusMessageChanged();
|
||||
void reconnectStateChanged();
|
||||
void summaryChanged();
|
||||
void statisticsChanged();
|
||||
|
||||
private:
|
||||
void loadLocalState();
|
||||
void saveServerUrl();
|
||||
void saveMappingsToLocal();
|
||||
void loadMappingsFromLocal();
|
||||
void saveTimeSlotsToLocal();
|
||||
void loadTimeSlotsFromLocal();
|
||||
QString localSettingsPath(const QString &fileName) const;
|
||||
void updateFilterOptions();
|
||||
void rebuildFilteredRecords();
|
||||
void rebuildStatistics();
|
||||
QString mappedStudentName(const AttendanceRecord &record) const;
|
||||
QString mappedClassName(const AttendanceRecord &record) const;
|
||||
void mergeFaceItems();
|
||||
void setStatusMessage(const QString &message);
|
||||
void startReconnect(const QString &reason);
|
||||
void stopReconnect();
|
||||
void scheduleReconnect();
|
||||
void runReconnectCheck();
|
||||
|
||||
ApiClient m_apiClient;
|
||||
QTimer m_reconnectTimer;
|
||||
RecordListModel m_recordModel;
|
||||
StatisticsListModel m_statisticsModel;
|
||||
TimeSlotListModel m_timeSlotModel;
|
||||
MappingListModel m_studentMappingModel;
|
||||
MappingListModel m_classMappingModel;
|
||||
FaceListModel m_faceModel;
|
||||
|
||||
QVector<AttendanceRecord> m_allRecords;
|
||||
QVector<AttendanceRecord> m_filteredRecords;
|
||||
QStringList m_classOptions;
|
||||
QStringList m_studentOptions;
|
||||
QString m_serverUrl;
|
||||
QString m_statusMessage;
|
||||
bool m_reconnecting = false;
|
||||
bool m_reconnectCheckInFlight = false;
|
||||
int m_reconnectAttempt = 0;
|
||||
int m_totalRecords = 0;
|
||||
int m_todayRecords = 0;
|
||||
|
||||
QString m_selectedClassId = "全部";
|
||||
QString m_selectedStudentId = "全部";
|
||||
QString m_nameFilter;
|
||||
int m_dateFilterIndex = 1;
|
||||
QDate m_specificDate = QDate::currentDate();
|
||||
|
||||
QDate m_statisticsStartDate = QDate::currentDate().addDays(-30);
|
||||
QDate m_statisticsEndDate = QDate::currentDate();
|
||||
int m_statisticsStudentCount = 0;
|
||||
int m_statisticsRecordCount = 0;
|
||||
|
||||
QMap<QString, QString> m_faceIdMapping;
|
||||
QStringList m_facePhotoIds;
|
||||
QStringList m_allStudentIds;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef ATTENDANCE_TYPES_H
|
||||
#define ATTENDANCE_TYPES_H
|
||||
|
||||
#include <QDate>
|
||||
#include <QDateTime>
|
||||
#include <QString>
|
||||
#include <QTime>
|
||||
#include <QVector>
|
||||
|
||||
struct AttendanceRecord
|
||||
{
|
||||
QString timestamp;
|
||||
QString studentId;
|
||||
QString originalName;
|
||||
double confidence = 0.0;
|
||||
QString status;
|
||||
QString classId = "0";
|
||||
|
||||
QDateTime dateTime() const
|
||||
{
|
||||
return QDateTime::fromString(timestamp, "yyyy-MM-dd HH:mm:ss");
|
||||
}
|
||||
};
|
||||
|
||||
struct StudentStatistics
|
||||
{
|
||||
QString studentId;
|
||||
QString name;
|
||||
int totalCount = 0;
|
||||
int todayCount = 0;
|
||||
QDateTime lastCheckIn;
|
||||
double averageConfidence = 0.0;
|
||||
};
|
||||
|
||||
struct TimeSlot
|
||||
{
|
||||
QString name;
|
||||
QTime startTime;
|
||||
QTime endTime;
|
||||
bool enabled = true;
|
||||
};
|
||||
|
||||
inline QVector<TimeSlot> defaultTimeSlots()
|
||||
{
|
||||
return {
|
||||
{"上午签到", QTime(8, 0), QTime(9, 30), true},
|
||||
{"中午签退", QTime(12, 0), QTime(13, 0), true},
|
||||
{"下午签到", QTime(14, 0), QTime(15, 0), true},
|
||||
{"晚上签退", QTime(17, 0), QTime(18, 30), true}
|
||||
};
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(AttendanceRecord)
|
||||
Q_DECLARE_METATYPE(StudentStatistics)
|
||||
Q_DECLARE_METATYPE(TimeSlot)
|
||||
Q_DECLARE_METATYPE(QVector<AttendanceRecord>)
|
||||
Q_DECLARE_METATYPE(QVector<TimeSlot>)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,48 @@
|
||||
#include "CryptoUtils.h"
|
||||
#include "crypto/tiny_aes.hpp"
|
||||
|
||||
namespace {
|
||||
QByteArray removePkcs7Padding(const QByteArray &data)
|
||||
{
|
||||
if (data.isEmpty()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const int padding = static_cast<unsigned char>(data.back());
|
||||
if (padding < 1 || padding > AES_BLOCKLEN || padding > data.size()) {
|
||||
return data;
|
||||
}
|
||||
|
||||
for (int i = data.size() - padding; i < data.size(); ++i) {
|
||||
if (static_cast<unsigned char>(data.at(i)) != padding) {
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
return data.left(data.size() - padding);
|
||||
}
|
||||
}
|
||||
|
||||
QByteArray CryptoUtils::decryptAes128CbcBase64(const QByteArray &encryptedBase64)
|
||||
{
|
||||
static const uint8_t key[AES_KEYLEN] = {
|
||||
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
|
||||
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
|
||||
};
|
||||
|
||||
static const uint8_t iv[AES_BLOCKLEN] = {
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
|
||||
};
|
||||
|
||||
const QByteArray encrypted = QByteArray::fromBase64(encryptedBase64);
|
||||
if (encrypted.isEmpty() || (encrypted.size() % AES_BLOCKLEN) != 0) {
|
||||
return {};
|
||||
}
|
||||
|
||||
QByteArray buffer = encrypted;
|
||||
AES_ctx ctx;
|
||||
AES_init_ctx_iv(&ctx, key, iv);
|
||||
AES_CBC_decrypt_buffer(&ctx, reinterpret_cast<uint8_t*>(buffer.data()), static_cast<size_t>(buffer.size()));
|
||||
return removePkcs7Padding(buffer);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef CRYPTO_UTILS_H
|
||||
#define CRYPTO_UTILS_H
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
class CryptoUtils
|
||||
{
|
||||
public:
|
||||
static QByteArray decryptAes128CbcBase64(const QByteArray &encryptedBase64);
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "FaceListModel.h"
|
||||
|
||||
FaceListModel::FaceListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int FaceListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
QVariant FaceListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const Item &item = m_items.at(index.row());
|
||||
switch (role) {
|
||||
case FaceIdRole: return item.faceId;
|
||||
case StudentIdRole: return item.studentId;
|
||||
case HasPhotoRole: return item.hasPhoto;
|
||||
case PhotoUrlRole: return item.photoUrl;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool FaceListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (role == StudentIdRole) {
|
||||
m_items[index.row()].studentId = value.toString().trimmed();
|
||||
emit dataChanged(index, index, {role});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
Qt::ItemFlags FaceListModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return Qt::NoItemFlags;
|
||||
}
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> FaceListModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{FaceIdRole, "faceId"},
|
||||
{StudentIdRole, "studentId"},
|
||||
{HasPhotoRole, "hasPhoto"},
|
||||
{PhotoUrlRole, "photoUrl"}
|
||||
};
|
||||
}
|
||||
|
||||
void FaceListModel::setItems(const QVector<Item> &items)
|
||||
{
|
||||
beginResetModel();
|
||||
m_items = items;
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QVector<FaceListModel::Item> FaceListModel::items() const
|
||||
{
|
||||
return m_items;
|
||||
}
|
||||
|
||||
void FaceListModel::updateStudentId(int index, const QString &studentId)
|
||||
{
|
||||
if (index < 0 || index >= m_items.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_items[index].studentId = studentId.trimmed();
|
||||
emit dataChanged(this->index(index), this->index(index), {StudentIdRole});
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
#ifndef FACE_LIST_MODEL_H
|
||||
#define FACE_LIST_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QByteArray>
|
||||
#include <QHash>
|
||||
#include <QModelIndex>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
class FaceListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
FaceIdRole = Qt::UserRole + 1,
|
||||
StudentIdRole,
|
||||
HasPhotoRole,
|
||||
PhotoUrlRole
|
||||
};
|
||||
|
||||
explicit FaceListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
struct Item {
|
||||
QString faceId;
|
||||
QString studentId;
|
||||
bool hasPhoto = false;
|
||||
QString photoUrl;
|
||||
};
|
||||
|
||||
void setItems(const QVector<Item> &items);
|
||||
QVector<Item> items() const;
|
||||
Q_INVOKABLE void updateStudentId(int index, const QString &studentId);
|
||||
|
||||
private:
|
||||
QVector<Item> m_items;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,132 @@
|
||||
#include "MappingListModel.h"
|
||||
#include <algorithm>
|
||||
|
||||
MappingListModel::MappingListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int MappingListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
QVariant MappingListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const Item &item = m_items.at(index.row());
|
||||
switch (role) {
|
||||
case KeyRole: return item.key;
|
||||
case ValueRole: return item.value;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool MappingListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
Item &item = m_items[index.row()];
|
||||
switch (role) {
|
||||
case KeyRole: item.key = value.toString().trimmed(); break;
|
||||
case ValueRole: item.value = value.toString().trimmed(); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
emit dataChanged(index, index, {role});
|
||||
return true;
|
||||
}
|
||||
|
||||
Qt::ItemFlags MappingListModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return Qt::NoItemFlags;
|
||||
}
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> MappingListModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{KeyRole, "entryKey"},
|
||||
{ValueRole, "entryValue"}
|
||||
};
|
||||
}
|
||||
|
||||
void MappingListModel::setMap(const QMap<QString, QString> &map)
|
||||
{
|
||||
QVector<Item> items;
|
||||
items.reserve(map.size());
|
||||
for (auto it = map.begin(); it != map.end(); ++it) {
|
||||
items.push_back({it.key(), it.value()});
|
||||
}
|
||||
|
||||
std::sort(items.begin(), items.end(), [](const Item &left, const Item &right) {
|
||||
bool leftNumeric = false;
|
||||
bool rightNumeric = false;
|
||||
int leftValue = left.key.toInt(&leftNumeric);
|
||||
int rightValue = right.key.toInt(&rightNumeric);
|
||||
if (leftNumeric && rightNumeric) {
|
||||
return leftValue < rightValue;
|
||||
}
|
||||
return left.key < right.key;
|
||||
});
|
||||
|
||||
beginResetModel();
|
||||
m_items = items;
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QMap<QString, QString> MappingListModel::toMap() const
|
||||
{
|
||||
QMap<QString, QString> map;
|
||||
for (const Item &item : m_items) {
|
||||
if (!item.key.trimmed().isEmpty()) {
|
||||
map[item.key.trimmed()] = item.value.trimmed();
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
void MappingListModel::addEntry(const QString &key, const QString &value)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), m_items.size(), m_items.size());
|
||||
m_items.push_back({key.trimmed(), value.trimmed()});
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void MappingListModel::updateEntry(int index, const QString &key, const QString &value)
|
||||
{
|
||||
if (index < 0 || index >= m_items.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_items[index] = {key.trimmed(), value.trimmed()};
|
||||
emit dataChanged(this->index(index), this->index(index));
|
||||
}
|
||||
|
||||
void MappingListModel::removeEntry(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_items.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginRemoveRows(QModelIndex(), index, index);
|
||||
m_items.removeAt(index);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void MappingListModel::clear()
|
||||
{
|
||||
beginResetModel();
|
||||
m_items.clear();
|
||||
endResetModel();
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef MAPPING_LIST_MODEL_H
|
||||
#define MAPPING_LIST_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QByteArray>
|
||||
#include <QHash>
|
||||
#include <QMap>
|
||||
#include <QModelIndex>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
class MappingListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
KeyRole = Qt::UserRole + 1,
|
||||
ValueRole
|
||||
};
|
||||
|
||||
explicit MappingListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void setMap(const QMap<QString, QString> &map);
|
||||
QMap<QString, QString> toMap() const;
|
||||
|
||||
Q_INVOKABLE void addEntry(const QString &key, const QString &value);
|
||||
Q_INVOKABLE void updateEntry(int index, const QString &key, const QString &value);
|
||||
Q_INVOKABLE void removeEntry(int index);
|
||||
Q_INVOKABLE void clear();
|
||||
|
||||
private:
|
||||
struct Item {
|
||||
QString key;
|
||||
QString value;
|
||||
};
|
||||
|
||||
QVector<Item> m_items;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "RecordListModel.h"
|
||||
|
||||
RecordListModel::RecordListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int RecordListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
QVariant RecordListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const Item &item = m_items.at(index.row());
|
||||
switch (role) {
|
||||
case TimestampRole: return item.timestamp;
|
||||
case ClassIdRole: return item.classId;
|
||||
case ClassNameRole: return item.className;
|
||||
case StudentIdRole: return item.studentId;
|
||||
case NameRole: return item.name;
|
||||
case ConfidenceRole: return item.confidence;
|
||||
case ConfidenceTextRole: return QString::number(item.confidence * 100.0, 'f', 2) + "%";
|
||||
case StatusRole: return item.status;
|
||||
case StatusTextRole: return item.success ? QStringLiteral("成功") : QStringLiteral("失败");
|
||||
case SuccessRole: return item.success;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> RecordListModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{TimestampRole, "timestamp"},
|
||||
{ClassIdRole, "classId"},
|
||||
{ClassNameRole, "className"},
|
||||
{StudentIdRole, "studentId"},
|
||||
{NameRole, "name"},
|
||||
{ConfidenceRole, "confidence"},
|
||||
{ConfidenceTextRole, "confidenceText"},
|
||||
{StatusRole, "status"},
|
||||
{StatusTextRole, "statusText"},
|
||||
{SuccessRole, "success"}
|
||||
};
|
||||
}
|
||||
|
||||
void RecordListModel::setItems(const QVector<Item> &items)
|
||||
{
|
||||
beginResetModel();
|
||||
m_items = items;
|
||||
endResetModel();
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef RECORD_LIST_MODEL_H
|
||||
#define RECORD_LIST_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QByteArray>
|
||||
#include <QHash>
|
||||
#include <QModelIndex>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
class RecordListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
TimestampRole = Qt::UserRole + 1,
|
||||
ClassIdRole,
|
||||
ClassNameRole,
|
||||
StudentIdRole,
|
||||
NameRole,
|
||||
ConfidenceRole,
|
||||
ConfidenceTextRole,
|
||||
StatusRole,
|
||||
StatusTextRole,
|
||||
SuccessRole
|
||||
};
|
||||
|
||||
explicit RecordListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
struct Item {
|
||||
QString timestamp;
|
||||
QString classId;
|
||||
QString className;
|
||||
QString studentId;
|
||||
QString name;
|
||||
double confidence = 0.0;
|
||||
QString status;
|
||||
bool success = false;
|
||||
};
|
||||
|
||||
void setItems(const QVector<Item> &items);
|
||||
|
||||
private:
|
||||
QVector<Item> m_items;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,53 @@
|
||||
#include "StatisticsListModel.h"
|
||||
|
||||
StatisticsListModel::StatisticsListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int StatisticsListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_items.size();
|
||||
}
|
||||
|
||||
QVariant StatisticsListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_items.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const Item &item = m_items.at(index.row());
|
||||
switch (role) {
|
||||
case StudentIdRole: return item.studentId;
|
||||
case NameRole: return item.name;
|
||||
case TotalCountRole: return item.totalCount;
|
||||
case TodayCountRole: return item.todayCount;
|
||||
case LastCheckInRole: return item.lastCheckIn;
|
||||
case AverageConfidenceRole: return item.averageConfidence;
|
||||
case AverageConfidenceTextRole: return QString::number(item.averageConfidence * 100.0, 'f', 2) + "%";
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> StatisticsListModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{StudentIdRole, "studentId"},
|
||||
{NameRole, "name"},
|
||||
{TotalCountRole, "totalCount"},
|
||||
{TodayCountRole, "todayCount"},
|
||||
{LastCheckInRole, "lastCheckIn"},
|
||||
{AverageConfidenceRole, "averageConfidence"},
|
||||
{AverageConfidenceTextRole, "averageConfidenceText"}
|
||||
};
|
||||
}
|
||||
|
||||
void StatisticsListModel::setItems(const QVector<Item> &items)
|
||||
{
|
||||
beginResetModel();
|
||||
m_items = items;
|
||||
endResetModel();
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef STATISTICS_LIST_MODEL_H
|
||||
#define STATISTICS_LIST_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QByteArray>
|
||||
#include <QHash>
|
||||
#include <QModelIndex>
|
||||
#include <QString>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
|
||||
class StatisticsListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
StudentIdRole = Qt::UserRole + 1,
|
||||
NameRole,
|
||||
TotalCountRole,
|
||||
TodayCountRole,
|
||||
LastCheckInRole,
|
||||
AverageConfidenceRole,
|
||||
AverageConfidenceTextRole
|
||||
};
|
||||
|
||||
explicit StatisticsListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
struct Item {
|
||||
QString studentId;
|
||||
QString name;
|
||||
int totalCount = 0;
|
||||
int todayCount = 0;
|
||||
QString lastCheckIn;
|
||||
double averageConfidence = 0.0;
|
||||
};
|
||||
|
||||
void setItems(const QVector<Item> &items);
|
||||
|
||||
private:
|
||||
QVector<Item> m_items;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,146 @@
|
||||
#include "TimeSlotListModel.h"
|
||||
#include <QJsonObject>
|
||||
|
||||
TimeSlotListModel::TimeSlotListModel(QObject *parent)
|
||||
: QAbstractListModel(parent)
|
||||
{
|
||||
}
|
||||
|
||||
int TimeSlotListModel::rowCount(const QModelIndex &parent) const
|
||||
{
|
||||
if (parent.isValid()) {
|
||||
return 0;
|
||||
}
|
||||
return m_timeSlots.size();
|
||||
}
|
||||
|
||||
QVariant TimeSlotListModel::data(const QModelIndex &index, int role) const
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_timeSlots.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const TimeSlot &slot = m_timeSlots.at(index.row());
|
||||
switch (role) {
|
||||
case NameRole: return slot.name;
|
||||
case StartTimeRole: return slot.startTime.toString("HH:mm");
|
||||
case EndTimeRole: return slot.endTime.toString("HH:mm");
|
||||
case EnabledRole: return slot.enabled;
|
||||
default: return {};
|
||||
}
|
||||
}
|
||||
|
||||
bool TimeSlotListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
||||
{
|
||||
if (!index.isValid() || index.row() < 0 || index.row() >= m_timeSlots.size()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
TimeSlot &slot = m_timeSlots[index.row()];
|
||||
switch (role) {
|
||||
case NameRole: slot.name = value.toString(); break;
|
||||
case StartTimeRole: slot.startTime = QTime::fromString(value.toString(), "HH:mm"); break;
|
||||
case EndTimeRole: slot.endTime = QTime::fromString(value.toString(), "HH:mm"); break;
|
||||
case EnabledRole: slot.enabled = value.toBool(); break;
|
||||
default: return false;
|
||||
}
|
||||
|
||||
emit dataChanged(index, index, {role});
|
||||
return true;
|
||||
}
|
||||
|
||||
Qt::ItemFlags TimeSlotListModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
if (!index.isValid()) {
|
||||
return Qt::NoItemFlags;
|
||||
}
|
||||
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
QHash<int, QByteArray> TimeSlotListModel::roleNames() const
|
||||
{
|
||||
return {
|
||||
{NameRole, "name"},
|
||||
{StartTimeRole, "startTime"},
|
||||
{EndTimeRole, "endTime"},
|
||||
{EnabledRole, "enabled"}
|
||||
};
|
||||
}
|
||||
|
||||
void TimeSlotListModel::setTimeSlots(const QVector<TimeSlot> &timeSlots)
|
||||
{
|
||||
beginResetModel();
|
||||
m_timeSlots = timeSlots;
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
QVector<TimeSlot> TimeSlotListModel::timeSlots() const
|
||||
{
|
||||
return m_timeSlots;
|
||||
}
|
||||
|
||||
QJsonArray TimeSlotListModel::toJsonArray() const
|
||||
{
|
||||
QJsonArray array;
|
||||
for (const TimeSlot &slot : m_timeSlots) {
|
||||
QJsonObject object;
|
||||
object["name"] = slot.name;
|
||||
object["start_time"] = slot.startTime.toString("HH:mm");
|
||||
object["end_time"] = slot.endTime.toString("HH:mm");
|
||||
object["enabled"] = slot.enabled;
|
||||
array.append(object);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
QVariantMap TimeSlotListModel::get(int index) const
|
||||
{
|
||||
if (index < 0 || index >= m_timeSlots.size()) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const TimeSlot &slot = m_timeSlots.at(index);
|
||||
return {
|
||||
{"name", slot.name},
|
||||
{"startTime", slot.startTime.toString("HH:mm")},
|
||||
{"endTime", slot.endTime.toString("HH:mm")},
|
||||
{"enabled", slot.enabled}
|
||||
};
|
||||
}
|
||||
|
||||
void TimeSlotListModel::addTimeSlot(const QString &name, const QString &startTime, const QString &endTime, bool enabled)
|
||||
{
|
||||
beginInsertRows(QModelIndex(), m_timeSlots.size(), m_timeSlots.size());
|
||||
m_timeSlots.push_back({name.trimmed(), QTime::fromString(startTime, "HH:mm"), QTime::fromString(endTime, "HH:mm"), enabled});
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void TimeSlotListModel::updateTimeSlot(int index, const QString &name, const QString &startTime, const QString &endTime, bool enabled)
|
||||
{
|
||||
if (index < 0 || index >= m_timeSlots.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
TimeSlot &slot = m_timeSlots[index];
|
||||
slot.name = name.trimmed();
|
||||
slot.startTime = QTime::fromString(startTime, "HH:mm");
|
||||
slot.endTime = QTime::fromString(endTime, "HH:mm");
|
||||
slot.enabled = enabled;
|
||||
emit dataChanged(this->index(index), this->index(index));
|
||||
}
|
||||
|
||||
void TimeSlotListModel::removeTimeSlot(int index)
|
||||
{
|
||||
if (index < 0 || index >= m_timeSlots.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
beginRemoveRows(QModelIndex(), index, index);
|
||||
m_timeSlots.removeAt(index);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void TimeSlotListModel::resetToDefault()
|
||||
{
|
||||
setTimeSlots(defaultTimeSlots());
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#ifndef TIMESLOT_LIST_MODEL_H
|
||||
#define TIMESLOT_LIST_MODEL_H
|
||||
|
||||
#include <QAbstractListModel>
|
||||
#include <QByteArray>
|
||||
#include <QHash>
|
||||
#include <QJsonArray>
|
||||
#include <QModelIndex>
|
||||
#include <QVariant>
|
||||
#include <QVariantMap>
|
||||
#include <QVector>
|
||||
#include "AttendanceTypes.h"
|
||||
|
||||
class TimeSlotListModel : public QAbstractListModel
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum Roles {
|
||||
NameRole = Qt::UserRole + 1,
|
||||
StartTimeRole,
|
||||
EndTimeRole,
|
||||
EnabledRole
|
||||
};
|
||||
|
||||
explicit TimeSlotListModel(QObject *parent = nullptr);
|
||||
|
||||
int rowCount(const QModelIndex &parent = QModelIndex()) const override;
|
||||
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
|
||||
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
|
||||
Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
QHash<int, QByteArray> roleNames() const override;
|
||||
|
||||
void setTimeSlots(const QVector<TimeSlot> &timeSlots);
|
||||
QVector<TimeSlot> timeSlots() const;
|
||||
QJsonArray toJsonArray() const;
|
||||
|
||||
Q_INVOKABLE QVariantMap get(int index) const;
|
||||
Q_INVOKABLE void addTimeSlot(const QString &name, const QString &startTime, const QString &endTime, bool enabled);
|
||||
Q_INVOKABLE void updateTimeSlot(int index, const QString &name, const QString &startTime, const QString &endTime, bool enabled);
|
||||
Q_INVOKABLE void removeTimeSlot(int index);
|
||||
Q_INVOKABLE void resetToDefault();
|
||||
|
||||
private:
|
||||
QVector<TimeSlot> m_timeSlots;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,217 @@
|
||||
/*
|
||||
* Tiny AES128 C++ 移植版本
|
||||
* 与STM32端使用相同的AES实现,保证兼容性
|
||||
*/
|
||||
|
||||
#ifndef _TINY_AES_HPP_
|
||||
#define _TINY_AES_HPP_
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
|
||||
#define AES_BLOCKLEN 16
|
||||
#define AES_KEYLEN 16
|
||||
#define AES_keyExpSize 176
|
||||
|
||||
struct AES_ctx
|
||||
{
|
||||
uint8_t RoundKey[AES_keyExpSize];
|
||||
};
|
||||
|
||||
static const uint8_t sbox[256] = {
|
||||
0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
|
||||
0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
|
||||
0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
|
||||
0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
|
||||
0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
|
||||
0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
|
||||
0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
|
||||
0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
|
||||
0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
|
||||
0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
|
||||
0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
|
||||
0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
|
||||
0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
|
||||
0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
|
||||
0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
|
||||
0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16
|
||||
};
|
||||
|
||||
static const uint8_t rsbox[256] = {
|
||||
0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
|
||||
0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
|
||||
0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
|
||||
0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
|
||||
0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
|
||||
0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
|
||||
0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
|
||||
0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
|
||||
0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
|
||||
0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
|
||||
0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
|
||||
0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
|
||||
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
|
||||
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
|
||||
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
|
||||
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d
|
||||
};
|
||||
|
||||
static const uint8_t Rcon[11] = {
|
||||
0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36
|
||||
};
|
||||
|
||||
#define getSBoxValue(num) (sbox[(num)])
|
||||
#define getSBoxInvert(num) (rsbox[(num)])
|
||||
|
||||
inline void KeyExpansion(uint8_t *RoundKey, const uint8_t *Key)
|
||||
{
|
||||
unsigned i, j, k;
|
||||
uint8_t tempa[4];
|
||||
for (i = 0; i < 4; ++i) {
|
||||
RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
|
||||
RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
|
||||
RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
|
||||
RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
|
||||
}
|
||||
|
||||
for (i = 4; i < 44; ++i) {
|
||||
k = (i - 1) * 4;
|
||||
tempa[0] = RoundKey[k + 0];
|
||||
tempa[1] = RoundKey[k + 1];
|
||||
tempa[2] = RoundKey[k + 2];
|
||||
tempa[3] = RoundKey[k + 3];
|
||||
|
||||
if (i % 4 == 0) {
|
||||
const uint8_t u8tmp = tempa[0];
|
||||
tempa[0] = tempa[1];
|
||||
tempa[1] = tempa[2];
|
||||
tempa[2] = tempa[3];
|
||||
tempa[3] = u8tmp;
|
||||
|
||||
tempa[0] = getSBoxValue(tempa[0]);
|
||||
tempa[1] = getSBoxValue(tempa[1]);
|
||||
tempa[2] = getSBoxValue(tempa[2]);
|
||||
tempa[3] = getSBoxValue(tempa[3]);
|
||||
tempa[0] ^= Rcon[i / 4];
|
||||
}
|
||||
|
||||
j = i * 4;
|
||||
k = (i - 4) * 4;
|
||||
RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
|
||||
RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
|
||||
RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
|
||||
RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
|
||||
}
|
||||
}
|
||||
|
||||
static void AddRoundKey(uint8_t round, uint8_t *state, const uint8_t *RoundKey)
|
||||
{
|
||||
uint8_t i, j;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
for (j = 0; j < 4; ++j) {
|
||||
state[i * 4 + j] ^= RoundKey[(round * 16) + (i * 4 + j)];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void InvSubBytes(uint8_t *state)
|
||||
{
|
||||
for (uint8_t i = 0; i < 16; ++i) {
|
||||
state[i] = getSBoxInvert(state[i]);
|
||||
}
|
||||
}
|
||||
|
||||
static void InvShiftRows(uint8_t *state)
|
||||
{
|
||||
uint8_t temp;
|
||||
temp = state[13];
|
||||
state[13] = state[9];
|
||||
state[9] = state[5];
|
||||
state[5] = state[1];
|
||||
state[1] = temp;
|
||||
|
||||
temp = state[2];
|
||||
state[2] = state[10];
|
||||
state[10] = temp;
|
||||
temp = state[6];
|
||||
state[6] = state[14];
|
||||
state[14] = temp;
|
||||
|
||||
temp = state[3];
|
||||
state[3] = state[7];
|
||||
state[7] = state[11];
|
||||
state[11] = state[15];
|
||||
state[15] = temp;
|
||||
}
|
||||
|
||||
static uint8_t xtime(uint8_t x)
|
||||
{
|
||||
return ((x << 1) ^ (((x >> 7) & 1U) * 0x1bU));
|
||||
}
|
||||
|
||||
static uint8_t Multiply(uint8_t x, uint8_t y)
|
||||
{
|
||||
return (((y & 1U) * x) ^
|
||||
(((y >> 1U) & 1U) * xtime(x)) ^
|
||||
(((y >> 2U) & 1U) * xtime(xtime(x))) ^
|
||||
(((y >> 3U) & 1U) * xtime(xtime(xtime(x)))) ^
|
||||
(((y >> 4U) & 1U) * xtime(xtime(xtime(xtime(x))))));
|
||||
}
|
||||
|
||||
static void InvMixColumns(uint8_t *state)
|
||||
{
|
||||
int i;
|
||||
uint8_t a, b, c, d;
|
||||
for (i = 0; i < 4; ++i) {
|
||||
a = state[i * 4 + 0];
|
||||
b = state[i * 4 + 1];
|
||||
c = state[i * 4 + 2];
|
||||
d = state[i * 4 + 3];
|
||||
|
||||
state[i * 4 + 0] = Multiply(a, 0x0eU) ^ Multiply(b, 0x0bU) ^ Multiply(c, 0x0dU) ^ Multiply(d, 0x09U);
|
||||
state[i * 4 + 1] = Multiply(a, 0x09U) ^ Multiply(b, 0x0eU) ^ Multiply(c, 0x0bU) ^ Multiply(d, 0x0dU);
|
||||
state[i * 4 + 2] = Multiply(a, 0x0dU) ^ Multiply(b, 0x09U) ^ Multiply(c, 0x0eU) ^ Multiply(d, 0x0bU);
|
||||
state[i * 4 + 3] = Multiply(a, 0x0bU) ^ Multiply(b, 0x0dU) ^ Multiply(c, 0x09U) ^ Multiply(d, 0x0eU);
|
||||
}
|
||||
}
|
||||
|
||||
inline void InvCipher(uint8_t *state, const uint8_t *RoundKey)
|
||||
{
|
||||
uint8_t round = 0;
|
||||
AddRoundKey(10, state, RoundKey);
|
||||
|
||||
for (round = 9; ; --round) {
|
||||
InvShiftRows(state);
|
||||
InvSubBytes(state);
|
||||
AddRoundKey(round, state, RoundKey);
|
||||
if (round == 0) {
|
||||
break;
|
||||
}
|
||||
InvMixColumns(state);
|
||||
}
|
||||
}
|
||||
|
||||
inline void AES_init_ctx_iv(struct AES_ctx *ctx, const uint8_t *key, const uint8_t *iv)
|
||||
{
|
||||
KeyExpansion(ctx->RoundKey, key);
|
||||
memcpy(ctx->RoundKey + AES_keyExpSize - AES_BLOCKLEN, iv, AES_BLOCKLEN);
|
||||
}
|
||||
|
||||
inline void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, size_t length)
|
||||
{
|
||||
size_t i;
|
||||
uint8_t storeNextIv[AES_BLOCKLEN];
|
||||
uint8_t *Iv = ctx->RoundKey + AES_keyExpSize - AES_BLOCKLEN;
|
||||
|
||||
for (i = 0; i < length; i += AES_BLOCKLEN) {
|
||||
memcpy(storeNextIv, buf + i, AES_BLOCKLEN);
|
||||
InvCipher(buf + i, ctx->RoundKey);
|
||||
|
||||
for (uint8_t j = 0; j < AES_BLOCKLEN; ++j) {
|
||||
buf[i + j] ^= Iv[j];
|
||||
}
|
||||
memcpy(Iv, storeNextIv, AES_BLOCKLEN);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||