Index.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. use app\common\service\MakeHtmkFile;
  16. use think\facade\Config;
  17. class Index extends Base
  18. {
  19. public function index()
  20. {
  21. // PHP $cid = 1;
  22. $php_data = Article::getListByCid(1, 7);
  23. // 其他学习笔记 $cid != 1;
  24. $other_data = Article::getListByCid(-1, 7);
  25. // Top文章
  26. $top_data = Article::getTop(3);
  27. // 所有文章
  28. $all_data = Article::getListByCid(0, 9);
  29. View::assign([
  30. 'php_data' => $php_data,
  31. 'other_data' => $other_data,
  32. 'top_data' => $top_data,
  33. 'all_data' => $all_data,
  34. ]);
  35. $html = View::fetch();
  36. if ($this->html) {
  37. $this->makeHtmlFile($html);
  38. }
  39. return $html;
  40. }
  41. /**
  42. * 关于&留言
  43. */
  44. public function about()
  45. {
  46. $html = View::fetch();
  47. if ($this->html) {
  48. $this->makeHtmlFile($html);
  49. }
  50. return $html;
  51. }
  52. public function guestBook()
  53. {
  54. $list = GuestBook::order('id desc')->paginate();
  55. View::assign('list', $list);
  56. return View::fetch();
  57. }
  58. public function saveGuestBook()
  59. {
  60. $param = $this->request->param('','',['strip_tags','htmlspecialchars']);
  61. if (empty($param['name']) or empty($param['contact'])) {
  62. return json(['msg'=>"请填写必填字段(姓名,电子邮件)", 'code'=>1]);
  63. }
  64. if (empty($param['content'])) {
  65. return json(['msg'=>"请留下内容", 'code'=>1]);
  66. }
  67. try {
  68. $bgu = new GuestBook();
  69. $bgu->save([
  70. 'content' => $param['content'],
  71. 'name' => $param['name'],
  72. 'contact' => $param['contact'],
  73. 'url' => $param['url'],
  74. 'time' => time(),
  75. ]);
  76. $bgu->datetime = date("Y-m-d", $bgu->time);
  77. return json(['msg'=>"保存成功", 'code'=>0, 'data'=>$bgu]);
  78. } catch (\Exception $e) {
  79. $msg = $e->getMessage();
  80. return json(['msg'=>$msg, 'code'=>1]);
  81. }
  82. }
  83. }