-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
268 lines (238 loc) · 8.53 KB
/
Copy pathserver.js
File metadata and controls
268 lines (238 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const app = express();
const PORT = process.env.PORT || 3000;
// 中间件
app.use(cors());
app.use(express.json());
app.use(express.static(path.join(__dirname)));
// 数据文件路径
const DATA_FILE = path.join(__dirname, 'data.json');
// 初始化数据
function initData() {
if (!fs.existsSync(DATA_FILE)) {
const initialData = {
bookings: [],
staff: [
{
id: 1,
name: "林佳铭",
rank: "#1",
age: 28,
height: "183cm",
weight: "72kg",
status: "partial",
description: "首席服务师,接受高级定制需求。精通各种技巧,经验丰富,客户满意度最高。",
tags: ["首席服务师", "高级定制", "专家级技巧", "深度交流", "场景定制"],
stats: { experience: "5年", rating: "4.9", clients: "280+" },
priceMultiplier: 1.5
},
{
id: 2,
name: "陈逸飞",
rank: "#2",
age: 25,
height: "178cm",
weight: "68kg",
status: "available",
description: "擅长舒缓按摩与基础服务,耐心细致,适合初次体验者。",
tags: ["舒缓按摩", "基础服务", "新人友好", "耐心细致"],
stats: { experience: "3年", rating: "4.7", clients: "150+" },
priceMultiplier: 1.0
},
{
id: 3,
name: "王浩然",
rank: "#3",
age: 26,
height: "180cm",
weight: "70kg",
status: "available",
description: "专业形体训练师,提供健身指导与体能训练服务。",
tags: ["形体训练", "健身指导", "体能训练", "专业指导"],
stats: { experience: "4年", rating: "4.8", clients: "200+" },
priceMultiplier: 1.2
},
{
id: 4,
name: "张子轩",
rank: "#4",
age: 24,
height: "175cm",
weight: "65kg",
status: "busy",
description: "情感陪伴专家,擅长倾听与心理疏导。",
tags: ["情感陪伴", "心理疏导", "倾听专家", "温柔体贴"],
stats: { experience: "2年", rating: "4.6", clients: "120+" },
priceMultiplier: 0.9
}
],
services: [
{
id: 1,
name: "基础陪伴服务",
description: "标准陪伴服务,包括基础交流与情感支持",
basePrice: 200,
duration: 2,
category: "basic",
tags: ["基础服务", "情感陪伴", "标准时长"]
},
{
id: 2,
name: "高级定制服务",
description: "个性化定制服务,根据客户需求提供专属体验",
basePrice: 500,
duration: 3,
category: "premium",
tags: ["高级定制", "专属体验", "个性化服务"]
},
{
id: 3,
name: "专业按摩服务",
description: "专业级按摩放松服务,缓解身心疲劳",
basePrice: 300,
duration: 2,
category: "special",
tags: ["专业按摩", "身心放松", "疲劳缓解"]
},
{
id: 4,
name: "形体训练指导",
description: "个性化形体训练与健身指导服务",
basePrice: 400,
duration: 2,
category: "training",
tags: ["形体训练", "健身指导", "专业教练"]
}
]
};
fs.writeFileSync(DATA_FILE, JSON.stringify(initialData, null, 2));
console.log('数据文件已初始化');
}
}
// 读取数据
function readData() {
try {
const data = fs.readFileSync(DATA_FILE, 'utf8');
return JSON.parse(data);
} catch (error) {
console.error('读取数据文件失败:', error);
return { bookings: [], staff: [], services: [] };
}
}
// 写入数据
function writeData(data) {
try {
fs.writeFileSync(DATA_FILE, JSON.stringify(data, null, 2));
return true;
} catch (error) {
console.error('写入数据文件失败:', error);
return false;
}
}
// API 路由
// 获取所有数据
app.get('/api/data', (req, res) => {
const data = readData();
res.json(data);
});
// 获取服务人员列表
app.get('/api/staff', (req, res) => {
const data = readData();
res.json(data.staff);
});
// 获取服务项目列表
app.get('/api/services', (req, res) => {
const data = readData();
res.json(data.services);
});
// 获取预约记录
app.get('/api/bookings', (req, res) => {
const data = readData();
res.json(data.bookings);
});
// 创建预约
app.post('/api/bookings', (req, res) => {
const data = readData();
const booking = {
id: 'BK' + Date.now(),
...req.body,
status: 'pending',
createdAt: new Date().toISOString()
};
data.bookings.push(booking);
if (writeData(data)) {
res.json({ success: true, booking });
} else {
res.status(500).json({ success: false, error: '保存预约失败' });
}
});
// 更新预约状态
app.put('/api/bookings/:id', (req, res) => {
const data = readData();
const bookingIndex = data.bookings.findIndex(b => b.id === req.params.id);
if (bookingIndex !== -1) {
data.bookings[bookingIndex] = { ...data.bookings[bookingIndex], ...req.body };
if (writeData(data)) {
res.json({ success: true, booking: data.bookings[bookingIndex] });
} else {
res.status(500).json({ success: false, error: '更新预约失败' });
}
} else {
res.status(404).json({ success: false, error: '预约不存在' });
}
});
// 删除预约
app.delete('/api/bookings/:id', (req, res) => {
const data = readData();
const bookingIndex = data.bookings.findIndex(b => b.id === req.params.id);
if (bookingIndex !== -1) {
data.bookings.splice(bookingIndex, 1);
if (writeData(data)) {
res.json({ success: true });
} else {
res.status(500).json({ success: false, error: '删除预约失败' });
}
} else {
res.status(404).json({ success: false, error: '预约不存在' });
}
});
// 搜索服务人员
app.get('/api/staff/search', (req, res) => {
const { q } = req.query;
const data = readData();
if (!q) {
return res.json(data.staff);
}
const filteredStaff = data.staff.filter(staff =>
staff.name.toLowerCase().includes(q.toLowerCase()) ||
staff.description.toLowerCase().includes(q.toLowerCase()) ||
staff.tags.some(tag => tag.toLowerCase().includes(q.toLowerCase()))
);
res.json(filteredStaff);
});
// 获取统计数据
app.get('/api/stats', (req, res) => {
const data = readData();
const stats = {
totalStaff: data.staff.length,
totalServices: data.services.length,
totalBookings: data.bookings.length,
pendingBookings: data.bookings.filter(b => b.status === 'pending').length,
confirmedBookings: data.bookings.filter(b => b.status === 'confirmed').length,
cancelledBookings: data.bookings.filter(b => b.status === 'cancelled').length
};
res.json(stats);
});
// 默认路由 - 服务静态文件
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});
// 初始化并启动服务器
initData();
app.listen(PORT, () => {
console.log(`服务器运行在 http://localhost:${PORT}`);
console.log('男男性服务全档案系统已启动');
});