Index.php 1.8 KB

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