80 lines
3.9 KiB
PHP
80 lines
3.9 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use Core\Db;
|
|
|
|
class ContactController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$sent = false;
|
|
$error = '';
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
|
|
// 蜜罐:机器人常会填写隐藏字段,正常用户看不到也不会填
|
|
if (trim((string)$this->post('website')) !== '') {
|
|
ip_rate_register($ip, 'contact', 900); // 仍计入限速窗口,避免探测
|
|
$sent = true; // 静默当作成功,避免机器人得知被拦截
|
|
} elseif (ip_rate_blocked($ip, 'contact', 5, 900)) {
|
|
$error = '提交过于频繁,请 15 分钟后再试。';
|
|
} else {
|
|
ip_rate_register($ip, 'contact', 900); // 真实提交尝试计入限速窗口(含后续校验失败)
|
|
if (!csrf_check()) {
|
|
$error = '表单已过期,请重试。';
|
|
} elseif (!captcha_check($this->post('captcha'))) {
|
|
$error = '验证码错误,请重新计算。';
|
|
} else {
|
|
$name = trim($this->post('name'));
|
|
$phone = trim($this->post('phone'));
|
|
$msg = trim($this->post('message'));
|
|
// 服务端校验:长度与联系电话格式(防垃圾/注入)
|
|
if (mb_strlen($name) < 2 || mb_strlen($name) > 40) {
|
|
$error = '请填写有效的姓名(2-40 字)。';
|
|
} elseif (!preg_match('/^[0-9+\-\s]{5,20}$/', $phone)) {
|
|
$error = '请填写有效的联系电话(5-20 位)。';
|
|
} elseif (mb_strlen($msg) < 5 || mb_strlen($msg) > 1000) {
|
|
$error = '请填写需求描述(5-1000 字)。';
|
|
} else {
|
|
$this->saveLead(compact('name', 'phone', 'msg') + ['at' => date('Y-m-d H:i:s')]);
|
|
$sent = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
$seo = page_seo('contact', [
|
|
'title' => '联系我们',
|
|
'description' => '联系酷冰甲,获取降温服定制方案与专属报价。我们支持企业批量采购、LOGO刺绣、尺寸与面料定制,提供在线咨询、电话与邮件多种沟通方式。7天打样、全国发货,专业团队一对一对接您的高温防护需求,从选型到交付全程跟进,确保交付准时可靠,让合作更省心、更可靠。',
|
|
'keywords' => '联系酷冰甲,降温服定制,降温服报价,降温服采购,企业定制,降温服厂家,酷冰甲联系',
|
|
'og_type' => 'website',
|
|
]);
|
|
$captcha = captcha_make();
|
|
return $this->view('contact/index', [
|
|
'pageSeo' => [
|
|
'title' => $seo['title'],
|
|
'description' => $seo['description'],
|
|
'keywords' => $seo['keywords'],
|
|
'og_type' => $seo['og_type'] ?: 'website',
|
|
'og_image' => $seo['og_image'],
|
|
'canonical' => $seo['canonical'],
|
|
'noindex' => $seo['noindex'],
|
|
'breadcrumb' => [
|
|
['name' => '首页', 'url' => site_url()],
|
|
['name' => '联系我们', 'url' => absolute_url()],
|
|
],
|
|
],
|
|
'sent' => $sent,
|
|
'error' => $error,
|
|
'captcha' => $captcha,
|
|
]);
|
|
}
|
|
|
|
private function saveLead(array $data): void
|
|
{
|
|
if (Db::driver() !== 'file') return; // MySQL 模式可由后台扩展
|
|
$file = Db::fileDir() . '/leads.json';
|
|
$rows = is_file($file) ? json_decode(file_get_contents($file), true) ?: [] : [];
|
|
$rows[] = $data;
|
|
file_put_contents($file, json_encode($rows, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
|
|
}
|
|
}
|