Index.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @file Index.php
  5. * @date 2019-02-27 14:49:36
  6. * @author huwhis<huuwhois>
  7. * @version 0.0.6
  8. */
  9. namespace app\controller;
  10. use think\facade\View;
  11. use app\controller\Base;
  12. use app\model\Article;
  13. use app\model\GuestBook;
  14. class Index extends Base
  15. {
  16. public function index()
  17. {
  18. return View::fetch();
  19. }
  20. /**
  21. * 关于&留言
  22. */
  23. public function about()
  24. {
  25. return View::fetch();
  26. }
  27. public function guestBook()
  28. {
  29. $list = GuestBook::order('id desc')->paginate();
  30. View::assign('list', $list);
  31. return View::fetch();
  32. }
  33. public function saveGuestBook()
  34. {
  35. $param = $this->request->param('', '', ['strip_tags', 'htmlspecialchars']);
  36. if (empty($param['name']) or empty($param['contact'])) {
  37. return json(['msg' => "请填写必填字段(姓名,电子邮件)", 'code' => 1]);
  38. }
  39. if (empty($param['content'])) {
  40. return json(['msg' => "请留下内容", 'code' => 1]);
  41. }
  42. try {
  43. $bgu = new GuestBook();
  44. $bgu->save([
  45. 'content' => $param['content'],
  46. 'name' => $param['name'],
  47. 'contact' => $param['contact'],
  48. 'url' => $param['url'],
  49. 'time' => time(),
  50. ]);
  51. $bgu->datetime = date("Y-m-d", $bgu->time);
  52. return json(['msg' => "保存成功", 'code' => 0, 'data' => $bgu]);
  53. } catch (\Exception $e) {
  54. $msg = $e->getMessage();
  55. return json(['msg' => $msg, 'code' => 1]);
  56. }
  57. }
  58. }