chaoyu/app/controller/Message.php

43 lines
1.2 KiB
PHP
Executable File

<?php
namespace app\controller;
use app\model\Message as MMessage;
use app\validate\Message as VMessage;
use think\exception\ValidateException;
/**
* 留言
* Class Message
* @package app\controller
*/
class Message extends Base
{
// 新增留言
public function add()
{
if(request()->isPost()) {
$msgData = [
'company_name' => trim(input('post.company_name', '')),
'name' => trim(input('post.name', '')),
'phone' => trim(input('post.phone', '')),
'email' => trim(input('post.email', '')),
'content' => trim(input('post.content', '')),
];
// 安全过滤
$msgData = array_map('strip_tags', $msgData);
try {
validate(VMessage::class)->check($msgData);
$msgData['ip'] = request()->ip();
$msgData['create_time'] = time();
MMessage::create($msgData);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
} else {
return $this->json(1, '非法请求');
}
}
}