chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
app/home/controller/Contact.php
@@ -2,12 +2,48 @@
namespace app\home\controller;
use think\Exception;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;
use app\BaseController;
use think\response\Json;
class Contact extends BaseController {
    public function index() {
        return View::fetch('index');
    }
    /**
     * 保存留言
     * @return Json
     */
    public function save_message(): Json {
        try {
            if (!Request::isPost()) {
                throw new Exception('请求方式错误');
            }
            $data            = Request::post();
            $validate        = new \app\home\validate\Contact();
            $validate_result = $validate->check($data);
            if (!$validate_result) {
                throw new Exception($validate->getError());
            }
            $data['create_time'] = time();
            $new_id              = Db::name('online_message')->insertGetId($data);
            if (!$new_id) {
                throw new Exception('保存失败');
            }
            $result = [
                'code'    => 200,
                'message' => '保存成功',
            ];
        } catch (Exception $exc) {
            $result = [
                'code'    => $exc->getCode(),
                'message' => $exc->getMessage(),
            ];
        }
        return json($result);
    }
}