前端是我用AI写的后代是我自学并用AI修改了一下,网上的教学我看不懂,有没有人帮我弄一下例如前后端如何交互?别人该如何使用等?谢谢麻烦
后端
#include <iostream>
#include <string>
#include <vector>
#include <mutex>
#include <thread>
#include <fstream>
#include <httplib.h>
#include <json.hpp>
using namespace std;
using json = nlohmann::json;
using namespace httplib;
#define MAX 1000
// 互斥锁用于线程安全
mutex mtx;
// 学生结构体
struct student {
string name;
int yw = 0, sx = 0, yy = 0, sex = 0, phone = 0;
int he() { return yw + sx + yy; }
};
// 导员结构体
struct teacher {
int Id = 0, Age = 0, Sex = 0, Phone = 0;
string Name;
student stu;
};
// 通讯录结构体
struct book {
teacher array[MAX];
int abs = 0; // 当前记录数
};
// 全局数据存储实例
book g_book;
bool g_running = true;
// JSON持久化存储
void save_data_to_file() {
lock_guard<mutex> lock(mtx);
ofstream file("data.json");
if (file.is_open()) {
json j;
j["abs"] = g_book.abs;
for (int i = 0; i < g_book.abs; ++i) {
json t;
t["Id"] = g_book.array[i].Id;
t["Name"] = g_book.array[i].Name;
t["Age"] = g_book.array[i].Age;
t["Sex"] = g_book.array[i].Sex;
t["Phone"] = g_book.array[i].Phone;
j["teachers"].push_back(t);
}
file << j.dump(4);
file.close();
}
}
// 从JSON文件加载数据
void load_data_from_file() {
lock_guard<mutex> lock(mtx);
ifstream file("data.json");
if (file.is_open()) {
try {
json j;
file >> j;
g_book.abs = j["abs"];
for (int i = 0; i < g_book.abs; ++i) {
auto& t = j["teachers"][i];
g_book.array[i].Id = t["Id"];
g_book.array[i].Name = t["Name"];
g_book.array[i].Age = t["Age"];
g_book.array[i].Sex = t["Sex"];
g_book.array[i].Phone = t["Phone"];
}
}
catch (...) {
// 文件格式错误或不存在,使用默认值
g_book.abs = 0;
}
file.close();
}
}
// 添加导员信息(修改为支持线程安全和持久化)
void tian(book* p) {
lock_guard<mutex> lock(mtx);
if (p->abs == MAX) {
cout << "人数已满(1000)不可添加\n";
}
else {
string Name;
cout << "输入导员姓名:";
cin >> Name;
p->array[p->abs].Name = Name;
int Sex;
cout << "输入导员性别:1---男 2---女:";
while (true) {
cin >> Sex;
if (Sex == 1 || Sex == 2) {
p->array[p->abs].Sex = Sex;
break;
}
else {
cout << "输入错误,请重新输入: ";
}
}
int Age = 0;
cout << "输入导员年龄:";
cin >> Age;
p->array[p->abs].Age = Age;
int Phone = 0;
cout << "输入导员电话:";
cin >> Phone;
p->array[p->abs].Phone = Phone;
p->array[p->abs].Id = p->abs + 1;
p->abs++;
cout << "添加完成" << endl;
save_data_to_file();
}
system("pause");
system("cls");
}
void xian(book* p) {
lock_guard<mutex> lock(mtx);
if (p->abs == 0) {
cout << "什么都没有" << endl;
}
else {
for (int i = 0; i < p->abs; i++) {
cout << "导员ID:" << p->array[i].Id << "\t"
<< "导员姓名:" << p->array[i].Name << "\t"
<< "导员性别:" << (p->array[i].Sex == 1 ? "男" : "女") << "\t"
<< "导员年龄:" << p->array[i].Age << "\t"
<< "导员电话:" << p->array[i].Phone << endl;
}
}
system("pause");
system("cls");
}
// 辅助函数:查找导员索引
int find_index(book* p, string name) {
for (int i = 0; i < p->abs; i++) {
if (p->array[i].Name == name) {
return i;
}
}
return -1;
}
void shan(book* p) {
lock_guard<mutex> lock(mtx);
string b;
cout << "删除人姓名:" << endl;
cin >> b;
int ret = find_index(p, b);
if (ret != -1) {
for (int i = ret; i < p->abs - 1; i++) {
p->array[i] = p->array[i + 1];
}
p->abs--;
cout << "删除成功" << endl;
save_data_to_file(); // 保存数据
}
else {
cout << "查无此人 \n";
}
system("pause");
system("cls");
}
void cha(book* p) {
lock_guard<mutex> lock(mtx);
string b;
cout << "查找人姓名:" << endl;
cin >> b;
int ret = find_index(p, b);
if (ret != -1) {
cout << "导员姓名:" << p->array[ret].Name << "\t"
<< "导员性别:" << (p->array[ret].Sex == 1 ? "男" : "女") << "\t"
<< "导员年龄:" << p->array[ret].Age << "\t"
<< "导员电话:" << p->array[ret].Phone << endl;
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
void gai(book* p) {
lock_guard<mutex> lock(mtx);
string b;
cout << "修改人姓名:" << endl;
cin >> b;
int ret = find_index(p, b);
if (ret != -1) {
string Name;
cout << "修改导员姓名:";
cin >> Name;
p->array[ret].Name = Name;
int Sex;
cout << "修改导员性别:1---男 2---女:";
while (true) {
cin >> Sex;
if (Sex == 1 || Sex == 2) {
p->array[ret].Sex = Sex;
break;
}
else {
cout << "输入错误,请重新输入: ";
}
}
int Age = 0;
cout << "修改导员年龄:";
cin >> Age;
p->array[ret].Age = Age;
int Phone = 0;
cout << "修改导员电话:";
cin >> Phone;
p->array[ret].Phone = Phone;
cout << "修改完成" << endl;
save_data_to_file(); // 保存数据
}
else {
cout << "查无此人" << endl;
}
system("pause");
system("cls");
}
// 清空所有信息
void qing(book* p) {
lock_guard<mutex> lock(mtx);
p->abs = 0;
cout << "清空完成" << endl;
save_data_to_file(); // 保存数据
system("pause");
system("cls");
}
// API处理函数:获取所有导员信息
void handle_get_teachers(const Request& req, Response& res) {
lock_guard<mutex> lock(mtx);
json response;
response["success"] = true;
response["data"] = json::array();
for (int i = 0; i < g_book.abs; ++i) {
const auto& t = g_book.array[i];
response["data"].push_back({
{"id", t.Id},
{"name", t.Name},
{"sex", t.Sex},
{"age", t.Age},
{"phone", t.Phone}
});
}
res.set_content(response.dump(), "application/json");
}
// API处理函数:添加导员信息
void handle_add_teacher(const Request& req, Response& res) {
lock_guard<mutex> lock(mtx);
json response;
try {
auto req_data = json::parse(req.body);
if (g_book.abs >= MAX) {
response["success"] = false;
response["message"] = "人数已满,无法添加";
res.status = 400;
}
else {
teacher t;
t.Id = g_book.abs + 1;
t.Name = req_data["name"];
t.Sex = req_data["sex"];
t.Age = req_data["age"];
t.Phone = req_data["phone"];
g_book.array[g_book.abs++] = t;
save_data_to_file();
response["success"] = true;
response["message"] = "添加成功";
response["data"] = { {"id", t.Id} };
res.status = 201;
}
}
catch (...) {
response["success"] = false;
response["message"] = "请求格式错误";
res.status = 400;
}
res.set_content(response.dump(), "application/json");
}
// API处理函数:删除导员信息
void handle_delete_teacher(const Request& req, Response& res) {
lock_guard<mutex> lock(mtx);
json response;
int id = stoi(req.matches[1]);
bool found = false;
for (int i = 0; i < g_book.abs; ++i) {
if (g_book.array[i].Id == id) {
// 移动数组元素
for (int j = i; j < g_book.abs - 1; ++j) {
g_book.array[j] = g_book.array[j + 1];
}
g_book.abs--;
found = true;
save_data_to_file();
break;
}
}
if (found) {
response["success"] = true;
response["message"] = "删除成功";
}
else {
response["success"] = false;
response["message"] = "未找到该导员";
res.status = 404;
}
res.set_content(response.dump(), "application/json");
}
// API处理函数:更新导员信息
void handle_update_teacher(const Request& req, Response& res) {
lock_guard<mutex> lock(mtx);
json response;
int id = stoi(req.matches[1]);
try {
auto req_data = json::parse(req.body);
bool found = false;
for (int i = 0; i < g_book.abs; ++i) {
if (g_book.array[i].Id == id) {
g_book.array[i].Name = req_data["name"];
g_book.array[i].Sex = req_data["sex"];
g_book.array[i].Age = req_data["age"];
g_book.array[i].Phone = req_data["phone"];
found = true;
save_data_to_file();
break;
}
}
if (found) {
response["success"] = true;
response["message"] = "更新成功";
}
else {
response["success"] = false;
response["message"] = "未找到该导员";
res.status = 404;
}
}
catch (...) {
response["success"] = false;
response["message"] = "请求格式错误";
res.status = 400;
}
res.set_content(response.dump(), "application/json");
}
// API处理函数:获取统计数据
void handle_get_statistics(const Request& req, Response& res) {
lock_guard<mutex> lock(mtx);
json response;
response["success"] = true;
int total = g_book.abs;
int male = 0, female = 0;
double avg_age = 0;
for (int i = 0; i < total; ++i) {
if (g_book.array[i].Sex == 1) male++;
else female++;
avg_age += g_book.array[i].Age;
}
if (total > 0) avg_age /= total;
response["data"] = {
{"total", total},
{"maleCount", male},
{"femaleCount", female},
{"avgAge", avg_age}
};
res.set_content(response.dump(), "application/json");
}
// 启动HTTP服务器
void start_http_server() {
Server svr;
// 配置CORS
svr.set_default_headers({
{"Access-Control-Allow-Origin", "*"},
{"Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"},
{"Access-Control-Allow-Headers", "Content-Type"}
});
// 处理预检请求
svr.Options("/*", [](const Request& req, Response& res) {
res.status = 200;
});
// 注册API端点
svr.Get("/api/teachers", handle_get_teachers);
svr.Post("/api/teachers", handle_add_teacher);
svr.Delete(R"(/api/teachers/(\d+))", [](const Request& req, Response& res) {
handle_delete_teacher(req, res);
});
svr.Put(R"(/api/teachers/(\d+))", [](const Request& req, Response& res) {
handle_update_teacher(req, res);
});
svr.Get("/api/statistics", handle_get_statistics);
// 启动服务器(在8080端口)
cout << "HTTP服务器已启动,监听端口: 8080" << endl;
svr.listen("0.0.0.0", 8080);
}
// 控制台菜单循环
void console_menu() {
book b;
book* p = &g_book; // 使用全局数据
int n = 0;
while (g_running) {
cout << "===== 导员信息管理系统 =====" << endl;
cout << "1、添加 2、显示 3、删除 4、查找" << endl;
cout << "5、修改 6、清空 0、退出" << endl;
cout << "============================" << endl;
cout << "请选择功能: ";
cin >> n;
switch (n) {
case 1: tian(p); break;
case 2: xian(p); break;
case 3: shan(p); break;
case 4: cha(p); break;
case 5: gai(p); break;
case 6: qing(p); break;
case 0:
cout << "欢迎下次使用!" << endl;
g_running = false;
system("pause");
return;
default:
cout << "无效选择,请重试" << endl;
system("pause");
system("cls");
break;
}
}
}
int main() {
// 加载数据
load_data_from_file();
// 启动HTTP服务器线程
thread server_thread(start_http_server);
server_thread.detach(); // 分离线程
// 启动控制台菜单
console_menu();
// 程序退出时保存数据
save_data_to_file();
return 0;
}
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>导员信息管理系统
<script src="https://cdn.tailwindcss.com/3.3.3">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.7.2/css/all.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.9.1/chart.min.js">
<style>
/* 完整样式定义 */
@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@500;600;700&family=Inter:wght@400;500;600&family=Roboto+Mono:wght@400;500&display=swap');
:root {
--primary-dark: #1A237E;
--primary: #283593;
--accent: #D4AF37;
--accent-light: #FFD700;
--feedback: #80DEEA;
--success: #00E676;
--warning: #FFC107;
--error: #FF4081;
--bg: #121A29;
--text-primary: #F5F5F7;
--text-secondary: #B0BEC5;
--text-hint: #78909C;
--card-bg: rgba(255, 255, 255, 0.05);
--card-border: rgba(255, 255, 255, 0.1);
}
body {
font-family: 'Inter', sans-serif;
background: linear-gradient(to bottom, var(--primary-dark), var(--primary));
color: var(--text-primary);
margin: 0;
padding: 0;
min-height: 100vh;
background-attachment: fixed;
background-image:
radial-gradient(circle at 10% 20%, rgba(255,255,255,0.02) 0%, transparent 20%),
radial-gradient(circle at 90% 80%, rgba(255,255,255,0.02) 0%, transparent 20%);
}
/* 所有样式定义完整保留 */
<body class="min-h-screen">
<!-- 顶部导航栏 -->
<nav class="glass-nav sticky top-0 z-50 py-4 px-6">
<!-- 导航内容 -->
<!-- 移动端菜单 -->
<div class="md:hidden fixed inset-0 bg-black bg-opacity-50 z-40 hidden" id="mobileMenuOverlay">
<div class="md:hidden fixed top-16 right-4 bg-gray-900 bg-opacity-90 backdrop-filter backdrop-blur-lg rounded-lg shadow-xl py-2 w-56 hidden z-50 dropdown-enter" id="mobileMenu">
<!-- 移动端菜单内容 -->
<!-- 主内容区 -->
<main class="container mx-auto py-8 px-4 md:px-6">
<!-- 主界面 - 无登录验证 -->
<div id="mainContent">
<!-- 选项卡导航 -->
<div class="tab-container">
<div class="tab active" data-tab="dashboard">首页
<div class="tab" data-tab="teachers">导员列表
<div class="tab" data-tab="config">API配置
<div class="tab" data-tab="debug">调试工具
<!-- 仪表盘内容 -->
<div id="dashboardTab" class="tab-content">
<!-- 数据概览与图表 -->
<!-- 导员列表 -->
<div id="teachersTab" class="tab-content hidden">
<!-- 导员管理功能 -->
<!-- API配置 -->
<div id="configTab" class="tab-content hidden">
<!-- 配置表单 -->
<!-- 调试工具 -->
<div id="debugTab" class="tab-content hidden">
<!-- 调试功能 -->
<!-- 页脚 -->
<footer class="bg-black bg-opacity-20 py-6 mt-12">
<!-- 页脚内容 -->
<!-- 添加/编辑导员模态框 -->
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden" id="counselorModal">
<!-- 模态框内容 -->
<!-- 确认对话框 -->
<div class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50 hidden" id="confirmDialog">
<!-- 确认对话框内容 -->
<!-- 提示信息 -->
<div class="fixed top-4 left-1/2 transform -translate-x-1/2 px-6 py-3 rounded-lg shadow-lg hidden flex items-center z-50" id="toast">
<!-- 提示信息内容 -->
<script>
// API服务模块
const ApiService = (function() {
// API服务实现代码
})();
// UI控制器
const UIController = (function() {
// UI控制实现代码
})();
// 主控制器
const MainController = (function(api, ui) {
// 主控制实现代码
})(ApiService, UIController);
// 初始化应用
document.addEventListener('DOMContentLoaded', function() {
MainController.init();
});