作者 sgj

search result

... ... @@ -10,6 +10,7 @@ namespace app\portal\controller;
use app\admin\model\HospitalModel;
use app\admin\model\JobModel;
use app\portal\model\PortalPostModel;
use app\user\model\UserModel;
use cmf\controller\HomeBaseController;
use EasyWeChat\Foundation\Application;
... ... @@ -27,6 +28,9 @@ class IndexController extends WeChatBaseController
$targetUrl=url('register');
header('location:'. $targetUrl);
}
$Article=new PortalPostModel();
$article=$Article->getRecommend()->toArray();
$this->assign('article',$article);
return $this->fetch();
}
... ...
... ... @@ -10,6 +10,8 @@
// +----------------------------------------------------------------------
namespace app\portal\controller;
use app\portal\model\PortalPostModel;
use app\portal\model\SearchModel;
use cmf\controller\HomeBaseController;
class SearchController extends HomeBaseController
... ... @@ -27,6 +29,14 @@ class SearchController extends HomeBaseController
}
public function index(){
$user_id=cmf_get_current_user_id();
if (empty($user_id)){
$this->error();
}
$map['user_id']=$user_id;
$Search=new SearchModel();
$keyword=$Search->getKeyword($user_id)->toArray();
$this->assign('keyword',$keyword);
return $this->fetch();
}
... ... @@ -34,5 +44,26 @@ class SearchController extends HomeBaseController
return $this->fetch();
}
public function searchResult(){
$keyword=input('keyword/s');
$this->assign('keyword',$keyword);
if(empty($keyword)){
$this->error('无效关键词');
}
/*记录到搜索库*/
$user_id=cmf_get_current_user_id();
$Serarch=new SearchModel();
$Serarch->serarchAdd($user_id,$keyword);
$Article=new PortalPostModel();
$result=$Article->getArticleByKeyword($keyword)->toArray();
if (empty($result)){
$this->error('没有找到相应的内容!');
}
$this->assign('result',$result);
return $this->fetch();
}
}
... ...
... ... @@ -356,6 +356,27 @@ class PortalPostModel extends Model
return $article;
}
public function getRecommend(){
$map['recommended']='1';
$info=$this->field('id,post_title,post_content,more,published_time,create_time')->where($map)->select();
return $info;
}
/**
* 获取搜索到的文章
* @param $keyword
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getArticleByKeyword($keyword){
$map['post_keywords']=['like',$keyword.'%'];
$map1['post_title']=['like',$keyword.'%'];
$result=$this->field('id,post_title,post_content,more,published_time,create_time')
->where($map)
->whereOr($map1)
->select();
return $result;
}
}
... ...
<?php
/**
* Created by PhpStorm.
* auther: sgj
* Date: 2018/12/11
* Time: 16:33
*/
namespace app\portal\model;
use think\Model;
use traits\model\SoftDelete;
class SearchModel extends Model
{
use SoftDelete;
protected $deleteTime = 'delete_time';
// 开启自动写入时间戳字段
// protected $autoWriteTimestamp = true;
// protected $insert =['add_time'];
protected function setAddTimeAttr()
{
return time();
}
/**
* 获取搜索关键字
* @param $user_id
* @return false|\PDOStatement|string|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function getKeyword($user_id,$limit='8'){
$return=$this->where('user_id',$user_id)->limit($limit)->select();
return $return;
}
/**
* 关键字添加信息
* @param $user_id
* @param $keyword
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function serarchAdd($user_id,$keyword){
$data['user_id']=$user_id;
$data['name']=$keyword;
/*判断之前是否有相关数据*/
$info=$this->getKeyword($user_id)->toArray();
if (!empty($info)){
foreach ($info as $k=>$v) {
if ($v['name']==$keyword){
$this::destroy($v);
}
}
}
$this->insert($data);
}
}
... ...
... ... @@ -117,7 +117,7 @@
</span>&nbsp;
</foreach>
</td>
<td>{$vo.user_nickname}</td>
<td>{$vo.user_login}</td>
<td>{$vo.post_hits|default=0}</td>
<td>
<notempty name="vo.comment_count">
... ...
... ... @@ -39,6 +39,7 @@
<script type="text/javascript" src="__TMPL__/public/assets/js/jquery-2.1.0.js"></script>
<script>
$(document).ready(function () {
$('.search').click(function (res) {
console.log(res);
... ...
... ... @@ -57,83 +57,22 @@
<div class="content_item">
<div class="content_title">最新活动</div>
<div class="content">
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
<volist name="article" id="vo">
<a href="{:url('portal/article/index',['id'=>$vo.id])}">
<div class="item_box">
<div>
<div class="item_title">{$vo.post_title}</div>
<div class="item_content">
{:getDescirpe($vo.post_content)}...
</div>
<div>{:date('Y.m.d',$vo.create_time)}</div>
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
<div>
<img src="{:cmf_get_image_preview_url($vo.more.thumbnail)}" alt="">
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
<div class="item_box">
<div>
<div class="item_title">人参一斤 不如糙米一升</div>
<div class="item_content">
精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...精致,是一种对生活热爱且认真负责的态度。如今人们在选择食物的时候...
</div>
<div>2018.08.15</div>
</div>
<div>
<img src="__TMPL__/public/assets/images/dpic06@2x.png" alt="">
</div>
</div>
</a>
</volist>
</div>
</div>
</div>
... ...
... ... @@ -14,7 +14,7 @@
<div class="search_item">
<div class="iconfont icon-sousuo"></div>
<div class="input_box">
<input type="text" placeholder="输入搜索相关内容">
<input id="keyword" type="text" placeholder="输入搜索相关内容">
<!--<div class="iconfont icon-cha"></div>-->
</div>
</div>
... ... @@ -29,9 +29,9 @@
</div>
</div>
<div class="content_item">
<div class="lable_item">红豆薏米的功效</div>
<div class="lable_item">健康管理</div>
<div class="lable_item">如何补钙</div>
<volist name="keyword" id="vo" empty="暂无数据">
<a href="{:url('portal/search/searchResult',['keyword'=>$vo.name])}"> <div class="lable_item">{$vo.name}</div></a>
</volist>
</div>
</div>
</div>
... ... @@ -40,9 +40,13 @@
<script>
$(document).ready(function () {
$('.search').click(function (res) {
$.()
console.log(res);
// window.location.href="{:url('portal/serarch')}"
var keyword1=$('#keyword').val()
console.log(keyword1);
if (!keyword1){
alert('请输入正确的关键词!');
return;
}
window.location.href="{:url('portal/search/searchResult')}"+'?keyword='+keyword1;
})
})
</script>
... ...
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>搜索</title>
<link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_859074_afg7uk9zst.css"/>
<link href="__TMPL__/public/assets/css/init.css" rel="stylesheet" type="text/css">
<link rel="stylesheet" href="__TMPL__/public/assets/css/search.css">
</head>
<body style="background-color: #fff;">
<div class="warp">
<div class="serach_box" style="border-bottom: 1px solid #EBEBEB;">
<div class="search_item">
<div class="iconfont icon-sousuo"></div>
<div class="input_box">
<input type="text" value="{$keyword}" placeholder="输入搜索相关内容">
<!--<div class="iconfont icon-cha"></div>-->
</div>
</div>
<a href="{:url('portal/search/index')}"> <div style="color: #000;">取消</div></a>
</div>
<!-- begin:搜索结果 -->
<div class="search_result">
<ul class="reco_public">
<volist name="result" id="vo">
<a href="{:url('portal/article/index',['id'=>$vo.id])}">
<li>
<div class="mmend_right">
<p class="mmend_title">{$vo.post_title}</p>
<p class="mmend_name"> {:getDescirpe($vo.post_content)}...</p>
<p class="mmend_num">{:date('Y.m.d',$vo.create_time)}</p>
</div>
<div class="mmend_left"><img src="__TMPL__/public/assets/images/apic01@2x.png"></div>
</li>
</a>
</volist>
</ul>
</div>
</div>
<script type="text/javascript" src="__TMPL__/public/assets/js/jquery-2.1.0.js"></script>
<script type="text/javascript" src="__TMPL__/public/assets/js/fontsize.js"></script>
<script>
</script>
</body>
</html>
... ...