-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
219 lines (187 loc) · 6.03 KB
/
Copy pathprocess.php
File metadata and controls
219 lines (187 loc) · 6.03 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
<?php
// 减肥记录管理系统 - 表单处理文件
// 启用详细错误报告
error_reporting(E_ALL);
ini_set('display_errors', 0); // AJAX请求时不显示错误
ini_set('log_errors', 1);
ini_set('error_log', __DIR__ . '/error.log');
// 设置响应头为JSON
header('Content-Type: application/json; charset=utf-8');
session_start();
require_once 'config.php';
require_once 'includes/WeightService.php';
require_once 'Security.class.php';
// 初始化服务
$service = new WeightService();
$service->initialize();
// 处理不同的操作
if (isset($_POST['action'])) {
$action = $_POST['action'];
switch ($action) {
case 'add_record':
handleAddRecord($service);
break;
case 'update_record':
handleUpdateRecord($service);
break;
case 'delete_record':
handleDeleteRecord($service);
break;
default:
setError('无效的操作');
break;
}
} else {
setError('未指定操作');
}
/**
* 处理添加记录
*/
function handleAddRecord($service) {
// 验证输入数据
if ((!isset($_POST['morning_date']) && !isset($_POST['evening_date'])) ||
(!isset($_POST['morning_weight']) && !isset($_POST['evening_weight']))) {
setError('缺少必要参数');
return;
}
// 根据提交的表单确定日期
$date = trim(isset($_POST['morning_date']) ? $_POST['morning_date'] : $_POST['evening_date']);
// 获取体重数据,如果未提供则设为0
$morningWeight = isset($_POST['morning_weight']) ? floatval($_POST['morning_weight']) : 0;
$eveningWeight = isset($_POST['evening_weight']) ? floatval($_POST['evening_weight']) : 0;
// 数据验证
if (empty($date) || !Security::validateDate($date)) {
setError('日期格式无效');
return;
}
// 检查至少有一个体重值大于0
if ($morningWeight <= 0 && $eveningWeight <= 0) {
setError('至少需要填写一个体重值,且必须大于0');
return;
}
if (($morningWeight > 0 && !Security::validateWeight($morningWeight)) ||
($eveningWeight > 0 && !Security::validateWeight($eveningWeight))) {
setError('体重值无效,请检查输入');
return;
}
// 添加记录
if ($service->addRecord($date, $morningWeight, $eveningWeight)) {
$records = $service->getAllRecords();
$stats = $service->getStatistics($records);
setSuccessWithData('体重记录保存成功', $records, $stats);
} else {
setError('保存体重记录失败');
}
}
/**
* 处理更新记录
*/
function handleUpdateRecord($service) {
if (!isset($_POST['id']) || !isset($_POST['record_date']) || !isset($_POST['morning_weight']) || !isset($_POST['evening_weight'])) {
setError('缺少必要参数');
return;
}
$id = intval($_POST['id']);
$date = trim($_POST['record_date']);
$morningWeight = floatval($_POST['morning_weight']);
$eveningWeight = floatval($_POST['evening_weight']);
// 数据验证
if ($id <= 0) {
setError('无效的记录ID');
return;
}
if (empty($date) || !Security::validateDate($date)) {
setError('日期格式无效');
return;
}
if ($morningWeight <= 0 && $eveningWeight <= 0) {
setError('至少需要填写一个体重值,且必须大于0');
return;
}
if (($morningWeight > 0 && !Security::validateWeight($morningWeight)) ||
($eveningWeight > 0 && !Security::validateWeight($eveningWeight))) {
setError('体重值无效,请检查输入');
return;
}
// 更新记录
if ($service->updateRecord($id, $date, $morningWeight, $eveningWeight)) {
$records = $service->getAllRecords();
$stats = $service->getStatistics($records);
setSuccessWithData('体重记录更新成功', $records, $stats);
} else {
setError('更新体重记录失败');
}
}
/**
* 处理删除记录
*/
function handleDeleteRecord($service) {
if (!isset($_POST['id'])) {
setError('缺少必要参数');
return;
}
$id = intval($_POST['id']);
if ($id <= 0) {
setError('无效的记录ID');
return;
}
// 删除记录
if ($service->deleteRecord($id)) {
$records = $service->getAllRecords();
$stats = $service->getStatistics($records);
setSuccessWithData('体重记录删除成功', $records, $stats);
} else {
setError('删除体重记录失败');
}
}
/**
* 设置成功消息并返回JSON响应(带数据)
*/
function setSuccessWithData($message, $records, $stats) {
jsonResponseWithData(true, $message, $records, $stats);
}
/**
* 返回JSON响应(带数据)
*/
function jsonResponseWithData($success, $message, $records = null, $stats = null) {
$response = [
'success' => $success,
'message' => $message
];
// 如果有数据,添加到响应中
if ($records !== null) {
$response['records'] = $records;
}
if ($stats !== null) {
$response['stats'] = $stats;
}
echo json_encode($response, JSON_UNESCAPED_UNICODE);
exit();
}
/**
* 返回JSON响应
*/
function jsonResponse($success, $message) {
echo json_encode([
'success' => $success,
'message' => $message
], JSON_UNESCAPED_UNICODE);
exit();
}
/**
* 设置成功消息并返回JSON响应
*/
function setSuccess($message) {
$_SESSION['success_message'] = $message;
$_SESSION['message_type'] = 'success';
jsonResponse(true, $message);
}
/**
* 设置错误消息并返回JSON响应
*/
function setError($message) {
$_SESSION['error_message'] = $message;
$_SESSION['message_type'] = 'error';
jsonResponse(false, $message);
}
?>