Index.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 think\Request;
  12. use app\index\controller\Base;
  13. use app\common\model\Article;
  14. use app\common\model\GuestBook;
  15. class Index extends Base
  16. {
  17. public function index()
  18. {
  19. // PHP $cid = 1;
  20. $php_data = Article::getListByCid(1, 7);
  21. // 其他学习笔记 $cid != 1;
  22. $other_data = Article::getListByCid(-1, 7);
  23. // Top文章
  24. $top_data = Article::getTop(3);
  25. // 所有文章
  26. $all_data = Article::getListByCid(0, 9);
  27. View::assign([
  28. 'php_data' => $php_data,
  29. 'other_data' => $other_data,
  30. 'top_data' => $top_data,
  31. 'all_data' => $all_data,
  32. ]);
  33. return View::fetch();
  34. }
  35. /**
  36. * 关于&留言
  37. */
  38. public function about()
  39. {
  40. return View::fetch();
  41. }
  42. public function guestBook()
  43. {
  44. $data = GuestBook::getGuestBooks();
  45. View::assign('data', $data);
  46. return View::fetch();
  47. }
  48. public function saveGuestBook(Request $request = null)
  49. {
  50. $param = $request->param();
  51. if (empty($param['author']) or empty($param['email'])) {
  52. View::assign('data', ['msg'=>"请填写必填字段(姓名,电子邮件)", 'code'=>1]);
  53. return View::fetch('error');
  54. }
  55. if (empty($param['comment'])) {
  56. View::assign('data', ['msg'=>"请留下内容", 'code'=>1]);
  57. return View::fetch('error');
  58. }
  59. array_pop($param);
  60. try {
  61. $bgu = new GuestBook();
  62. $res = $bgu->save([
  63. 'comment' => $param['comment'],
  64. 'author' => $param['author'],
  65. 'email' => $param['email'],
  66. 'url' => $param['url'],
  67. 'time' => time(),
  68. ]);
  69. } catch (\Exception $e) {
  70. $msg = $e->getMessage();
  71. View::assign('data', ['msg'=>$msg, 'code'=>1]);
  72. return View::fetch('error');
  73. }
  74. $this->redirect(url('blog/index/guestbook'), 302);
  75. }
  76. }