Files
psychological-counseling/api/submit.php
litianming 08ce60344d Initial commit: 心理咨询预约系统
PHP 前后端一体:首页咨询师列表、预约表单、管理后台、二维码准入与邮件通知;敏感配置与提交数据通过 .gitignore 排除。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-04 10:17:09 +08:00

76 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
require __DIR__ . '/bootstrap.php';
require __DIR__ . '/mailer.php';
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
jsonError('方法不允许', 405);
}
$body = getRequestBody();
$doctorId = trim((string) ($body['doctorId'] ?? ''));
$doctorName = trim((string) ($body['doctorName'] ?? ''));
$nickname = trim((string) ($body['nickname'] ?? ''));
$gender = trim((string) ($body['gender'] ?? ''));
$contact = trim((string) ($body['contact'] ?? ''));
$description = trim((string) ($body['description'] ?? ''));
if ($nickname === '') {
jsonError('请填写昵称');
}
if (!in_array($gender, ['男', '女'], true)) {
jsonError('请选择性别');
}
if ($contact === '') {
jsonError('请填写联系电话');
}
if (!validatePhone($contact)) {
jsonError('请输入正确的11位手机号码');
}
if ($description === '') {
jsonError('请填写需求简述');
}
if (mb_strlen($description) > 200) {
jsonError('需求简述不能超过200字');
}
$record = [
'id' => uniqid('sub_', true),
'createdAt' => date('c'),
'doctorId' => $doctorId,
'doctorName' => $doctorName,
'nickname' => $nickname,
'gender' => $gender,
'contact' => $contact,
'description' => $description,
];
$submissions = readSubmissions();
$submissions[] = $record;
if (!saveSubmissions($submissions)) {
jsonError(getSaveSubmissionsError(), 500);
}
$response = [
'ok' => true,
'message' => '提交成功',
'id' => $record['id'],
'total' => count($submissions),
];
http_response_code(200);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response, JSON_UNESCAPED_UNICODE);
finishResponseAndContinue();
$mailSent = sendSubmissionMail($submissions);
if (!$mailSent) {
error_log('[submit] 数据已保存邮件通知未发送ID=' . $record['id']);
}
exit;