Base.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use think\Request;
  5. class Base extends Controller//公共控制器
  6. {
  7. protected function _initialize()
  8. {
  9. $column = $this->tops();//栏目信息
  10. $ta = $this->tags();//标签信息
  11. $this->assign([
  12. 'column'=>$column,//栏目信息
  13. 'ta'=>$ta,//标签信息
  14. ]);
  15. }
  16. protected function tops(){//头部栏目数据
  17. $co = db('column')->order('id asc')->select();
  18. return $co;
  19. }
  20. protected function tags(){//热门标签
  21. $tag = db('article')->field('id,keywords')->where('keywords','not null')->select();
  22. //***解决重复关键词(keywords)只显示其中的一个
  23. $qc=[];
  24. foreach ($tag as $ka){
  25. $key = explode(",",$ka['keywords']);
  26. foreach ($key as $k){
  27. $qc[]=$k;
  28. }
  29. }
  30. $tags = array_unique($qc);
  31. //_______________________________________________end
  32. return $tags;
  33. }
  34. public function search(){//热门标签搜索
  35. $keywords=input('keywords');
  36. if($keywords){
  37. $map['title']=['like','%'.$keywords.'%'];
  38. $ke['keywords']=['like','%'.$keywords.'%'];
  39. $searchres=db('article')->where($map)->whereOr($ke)->select();
  40. $this->assign(array(
  41. 'searchres'=>$searchres,
  42. 'keywords'=>$keywords,
  43. ));
  44. }else{
  45. echo "<script>alert('非法请求!');location.href='".url('/index')."'</script>";
  46. }
  47. return view();
  48. }
  49. public function searchtop(){//头部关键词搜索
  50. if(request()->isPost()) {
  51. //在top.html中的form表单下加一个隐藏域 <input type="hidden" name="TOKEN" value="{:session('TOKEN')}"
  52. //每次搜索时判断这个存入session的隐藏token值
  53. $post_token = input('post.TOKEN');//因为是提交方式是post所以就用post.name(TOKEN)来接收token传过来的值
  54. if(!checkToken($post_token)){//如果验证token不一致则提示请勿重复条件页面(token值在每次完成一个搜索时创建一个新的token)
  55. // $this->error('请不要重复提交页面',url('index/index/index'));
  56. echo "<script>alert('请不要重复提交页面!');location.href='".url('/index')."'</script>";
  57. }
  58. $keywords = input('keywords');
  59. if($keywords){
  60. $map['title']=['like','%'.$keywords.'%'];
  61. $ke['keywords']=['like','%'.$keywords.'%'];
  62. $search=db('article')->where($map)->whereOr($ke)->select();
  63. if($search){
  64. createToken();//搜索成功时创建一个新token
  65. $this->assign(array(
  66. 'search'=>$search,
  67. 'keywords'=>$keywords,
  68. ));
  69. }else{
  70. createToken();//搜索失败时创建一个新token
  71. $this->assign(array(
  72. 'search'=>null,
  73. 'keywords'=>'没有搜索到关键词!',
  74. ));
  75. }
  76. }
  77. }else{
  78. echo "<script>alert('非法请求');location.href='".url('/index')."'</script>";
  79. }
  80. return view();
  81. }
  82. }