Skip to content

Commit

Permalink
迅搜
Browse files Browse the repository at this point in the history
  • Loading branch information
yidashi committed Jul 24, 2016
1 parent 65abc04 commit 6e6f65c
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 31 deletions.
2 changes: 1 addition & 1 deletion backend/components/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Events extends \common\components\Events implements BootstrapInterface
public function listeners()
{
return array_merge(parent::listeners(), [
'backend\listeners\AdminLog' => 'yii\db\BaseActiveRecord.afterUpdate',
'backend\listeners\AdminLog' => 'yii\db\ActiveRecord.afterUpdate',
]);

}
Expand Down
2 changes: 1 addition & 1 deletion backend/models/search/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function rules()
{
return [
[['id', 'category_id', 'created_at', 'updated_at', 'status'], 'integer'],
[['title', 'category', 'cover'], 'integer'],
[['title', 'category', 'cover'], 'string'],
];
}

Expand Down
45 changes: 45 additions & 0 deletions common/behaviors/XsBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/7/24
* Time: 下午10:53
*/

namespace common\behaviors;


use common\models\Article;
use common\models\Search;
use yii\base\Behavior;
use yii\db\ActiveRecord;

class XsBehavior extends Behavior
{
public function events()
{
return [
ActiveRecord::EVENT_AFTER_INSERT => [$this, 'afterSaveInternal'],
ActiveRecord::EVENT_AFTER_UPDATE => [$this, 'afterSaveInternal']
];
}

public function afterSaveInternal($event)
{
$article = Article::findOne(['id' => $event->sender->id]);
if (!empty($article)) {
if ($event->name == 'afterInsert') {
$search = new Search();
$search->article_id = $event->sender->id;
$search->status = Article::STATUS_ACTIVE;
} else {
$search = Search::findOne($event->sender->id);
$search->status = $article->status;
}
$search->title = $article->title;
$search->content = $event->sender->content;
$search->updated_at = $article->updated_at;
$search->save();
}
}
}
5 changes: 5 additions & 0 deletions common/config/main-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
'encryption' => env('MAIL_ENCRYPTION')
],
],
'xunsearch' => [
'class' => 'hightman\xunsearch\Connection',
'iniDirectory' => '@common/config', // 搜索 ini 文件目录,默认:@vendor/hightman/xunsearch/app
'charset' => 'utf-8', // 指定项目使用的默认编码,默认即时 utf-8,可不指定
],
],
];
if (YII_ENV_DEV) {
Expand Down
20 changes: 20 additions & 0 deletions common/config/search.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
project.name = yii2cmf
project.default_charset = utf-8
server.index = 8383
server.search = 8384

[article_id]
type = id

[title]
type = title

[content]
type = body

[status]
index = self
tokenizer = full

[updated_at]
type = numeric
4 changes: 3 additions & 1 deletion common/models/ArticleData.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace common\models;
use common\behaviors\XsBehavior;
use common\models\behaviors\ArticleDataBehavior;
use yii\helpers\Markdown;
use yii\helpers\StringHelper;
Expand Down Expand Up @@ -62,7 +63,8 @@ public function attributeLabels()
public function behaviors()
{
return [
ArticleDataBehavior::className()
ArticleDataBehavior::className(),
XsBehavior::className()
];
}
}
31 changes: 31 additions & 0 deletions common/models/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/7/24
* Time: 下午8:13
*/

namespace common\models;


use hightman\xunsearch\ActiveRecord;
use yii\data\ActiveDataProvider;

class Search extends ActiveRecord
{
public function search($q)
{
$query = self::find()->where($q)->andWhere(['status' => 1]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'updated_at' => SORT_DESC,
]
]
]);

return $dataProvider;
}
}
9 changes: 6 additions & 3 deletions common/models/behaviors/ArticleDataBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace common\models\behaviors;

use frontend\models\Search;
use Yii;
use yii\base\Behavior;
use yii\db\ActiveRecord;
Expand All @@ -32,9 +33,11 @@ public function events()
public function afterSaveInternal($event)
{
$article = Article::findOne(['id' => $event->sender->id]);
if (!empty($article) && empty($article->description)) {
$article->description = $this->generateDesc($event->sender->processedContent);
$article->save();
if (!empty($article)) {
if (empty($article->description)) {
$article->description = $this->generateDesc($event->sender->processedContent);
$article->save();
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@
"yiisoft/yii2-imagine": "^2.0",
"fabpot/goutte": "^3.1",
"kartik-v/yii2-widget-select2": "*",
"udokmeci/yii2-beanstalk": "dev-master"
"udokmeci/yii2-beanstalk": "dev-master",
"hightman/xunsearch": "*@beta"
},
"require-dev": {
"yiisoft/yii2-codeception": "*",
Expand Down
65 changes: 62 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion console/controllers/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,17 @@ public function actionInstall()
$this->setEnv('YII_DEBUG', $appStatus == 'prod' ? 'false' : 'true');
$this->setEnv('YII_ENV', $appStatus);
file_put_contents(Yii::getAlias($this->installFile), time());
$this->stdout("\n ... 应用构建成功.\n\n", Console::FG_GREEN);
$success = <<<STR
+=================================================+
| Installation completed successfully, Thanks you |
| 安装成功,感谢选择和使用 yii2cmf |
+-------------------------------------------------+
| 说明和注意事项: |
| 一些基本的设置可以在.env文件里修改
+=================================================+
STR;

$this->stdout($success, Console::FG_GREEN);
}

public function actionReset()
Expand Down
34 changes: 34 additions & 0 deletions frontend/components/Search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Created by PhpStorm.
* User: yidashi
* Date: 16/7/24
* Time: 下午9:57
*/

namespace frontend\components;


use common\models\Article;
use yii\base\Component;
use common\models\Search as SearchModel;
use yii\data\ActiveDataProvider;

class Search extends Component
{
public function search($q)
{
$search = new SearchModel();
return $search->search($q);
}

/**
* 如果不用迅搜,则替换这个方法
*/
/*public function search($q)
{
return new ActiveDataProvider([
'query' => Article::find()->published()->andWhere(['like', 'title', $q])
]);
}*/
}
3 changes: 2 additions & 1 deletion frontend/config/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@
'class' => 'common\components\PluginManager',
],
'notify' => \frontend\components\notify\Handler::className(),
'events' => \frontend\components\Events::className()
'events' => \frontend\components\Events::className(),
'search' => 'frontend\\components\\Search'
],
'as ThemeBehavior' => \frontend\behaviors\ThemeBehavior::className(),
'as RouteBehavior' => \frontend\behaviors\RouteBehavior::className(),
Expand Down
21 changes: 7 additions & 14 deletions frontend/controllers/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,20 @@

use common\models\Article;
use yii\data\ActiveDataProvider;
use yii\data\Pagination;
use yii\web\BadRequestHttpException;
use yii\web\Controller;

class SearchController extends Controller
{
/**
* 简单先来一个搜索,搜索太大太深
* @param $q string 关键词
* @return string
* @throws BadRequestHttpException
*/
public function actionIndex($q)

public function actionSearch()
{
if (empty($q)) {
throw new BadRequestHttpException('搜索关键词不能为空');
}
$dataProvider = new ActiveDataProvider([
'query' => Article::find()->published()->andWhere(['like', 'title', $q])
]);
$q = \Yii::$app->request->get('q');
if (empty($q)) $this->goHome();
$dataProvider = \Yii::$app->search->search($q);
return $this->render('index', [
'dataProvider' => $dataProvider,
'dataProvider' => $dataProvider,
'q' => $q
]);
}
Expand Down
7 changes: 2 additions & 5 deletions frontend/themes/basic/search/_item.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
?>
<div class="media-body">
<h4 class="media-heading">
<a href="<?= Url::to(['article/view', 'id' => $model->id]) ?>">
<a href="<?= Url::to(['article/view', 'id' => $model->article_id]) ?>">
<?= Html::weight($q, $model->title) ?>
</a>
</h4>
<div class="media-action">
<span class="views"><?= Html::icon('eye')?> 浏览 <?= $model->trueView?></span>
<span class="comments"><?= Html::a(Html::icon('comments-o') . '评论' . $model->comment, ['article/view', 'id' => $model->id, '#' => 'comments'])?></span>
</div>

</div>

0 comments on commit 6e6f65c

Please sign in to comment.