chengkun
2025-09-15 3c9050e82e582414dc7b208c8283fe47be37eeba
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
<?php
 
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);
    }
}