123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace app\index\controller;
- use think\Controller;
- use think\Request;
- class Base extends Controller//公共控制器
- {
- protected function _initialize()
- {
- $column = $this->tops();//栏目信息
- $ta = $this->tags();//标签信息
- $this->assign([
- 'column'=>$column,//栏目信息
- 'ta'=>$ta,//标签信息
- ]);
-
- }
- protected function tops(){//头部栏目数据
- $co = db('column')->order('id asc')->select();
- return $co;
- }
- protected function tags(){//热门标签
- $tag = db('article')->field('id,keywords')->where('keywords','not null')->select();
- //***解决重复关键词(keywords)只显示其中的一个
- $qc=[];
- foreach ($tag as $ka){
- $key = explode(",",$ka['keywords']);
- foreach ($key as $k){
- $qc[]=$k;
- }
- }
- $tags = array_unique($qc);
- //_______________________________________________end
- return $tags;
- }
- public function search(){//热门标签搜索
- $keywords=input('keywords');
- if($keywords){
- $map['title']=['like','%'.$keywords.'%'];
- $ke['keywords']=['like','%'.$keywords.'%'];
- $searchres=db('article')->where($map)->whereOr($ke)->select();
- $this->assign(array(
- 'searchres'=>$searchres,
- 'keywords'=>$keywords,
- ));
- }else{
- echo "<script>alert('非法请求!');location.href='".url('/index')."'</script>";
- }
- return view();
- }
- public function searchtop(){//头部关键词搜索
- if(request()->isPost()) {
- //在top.html中的form表单下加一个隐藏域 <input type="hidden" name="TOKEN" value="{:session('TOKEN')}"
- //每次搜索时判断这个存入session的隐藏token值
- $post_token = input('post.TOKEN');//因为是提交方式是post所以就用post.name(TOKEN)来接收token传过来的值
- if(!checkToken($post_token)){//如果验证token不一致则提示请勿重复条件页面(token值在每次完成一个搜索时创建一个新的token)
- // $this->error('请不要重复提交页面',url('index/index/index'));
- echo "<script>alert('请不要重复提交页面!');location.href='".url('/index')."'</script>";
- }
- $keywords = input('keywords');
- if($keywords){
- $map['title']=['like','%'.$keywords.'%'];
- $ke['keywords']=['like','%'.$keywords.'%'];
- $search=db('article')->where($map)->whereOr($ke)->select();
- if($search){
- createToken();//搜索成功时创建一个新token
- $this->assign(array(
- 'search'=>$search,
- 'keywords'=>$keywords,
- ));
- }else{
- createToken();//搜索失败时创建一个新token
- $this->assign(array(
- 'search'=>null,
- 'keywords'=>'没有搜索到关键词!',
- ));
- }
- }
- }else{
- echo "<script>alert('非法请求');location.href='".url('/index')."'</script>";
- }
- return view();
- }
-
-
- }
|