Index.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. $html = View::fetch();
  34. return $html;
  35. }
  36. /**
  37. * 关于&留言
  38. */
  39. public function about()
  40. {
  41. $html = View::fetch();
  42. return $html;
  43. }
  44. public function guestBook()
  45. {
  46. $list = GuestBook::order('id desc')->paginate();
  47. View::assign('list', $list);
  48. return View::fetch();
  49. }
  50. public function saveGuestBook()
  51. {
  52. $param = $this->request->param('','',['strip_tags','htmlspecialchars']);
  53. if (empty($param['name']) or empty($param['contact'])) {
  54. return json(['msg'=>"请填写必填字段(姓名,电子邮件)", 'code'=>1]);
  55. }
  56. if (empty($param['content'])) {
  57. return json(['msg'=>"请留下内容", 'code'=>1]);
  58. }
  59. try {
  60. $bgu = new GuestBook();
  61. $bgu->save([
  62. 'content' => $param['content'],
  63. 'name' => $param['name'],
  64. 'contact' => $param['contact'],
  65. 'url' => $param['url'],
  66. 'time' => time(),
  67. ]);
  68. $bgu->datetime = date("Y-m-d", $bgu->time);
  69. return json(['msg'=>"保存成功", 'code'=>0, 'data'=>$bgu]);
  70. } catch (\Exception $e) {
  71. $msg = $e->getMessage();
  72. return json(['msg'=>$msg, 'code'=>1]);
  73. }
  74. }
  75. }