Skip to content

Commit

Permalink
增加搜索功能
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Sep 2, 2021
1 parent 15a9f2c commit 481de6b
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 119 deletions.
3 changes: 1 addition & 2 deletions admin/controller/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class Article extends Base
if(keyword !== undefined) {
condition['concat(a.title, a.writer)'] = ['like', '%' + keyword + '%'];
}
const [total, list] = await this.$model.article.getArticleList(condition);
const pagination = total ? this.$pagination.render(total) : '';
const [list, pagination] = await this.$model.article.getArticleList(condition);
this.$assign('keyword', keyword);
this.$assign('list', list);
this.$assign('pagination', pagination);
Expand Down
3 changes: 1 addition & 2 deletions admin/controller/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class Comment extends Base
if(keyword !== undefined) {
condition['concat(comment.uname, comment.email, comment.url, comment.content, comment.ip)'] = ['like', '%' + keyword + '%'];
}
const [total, list] = await this.$model.comment.getCommentList(condition);
const pagination = total ? this.$pagination.render(total) : '';
const [list, pagination] = await this.$model.comment.getCommentList(condition);
this.$assign('keyword', keyword);
this.$assign('list', list);
this.$assign('pagination', pagination);
Expand Down
3 changes: 2 additions & 1 deletion admin/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ class Article extends Model
this.db.table('article a').where(condition).cache(60).count('id'),
this.db.table('article a').field('a.id,a.cate_id,a.user_id,a.title,a.writer,a.click,a.description,a.add_time,c.cate_name,c.cate_dir').join('cate c', 'a.cate_id=c.id').where(condition).order('a.id', 'desc').page(page, pageSize).select()
]);
return [total, list];
const pagination = total ? this.$pagination.render(total) : '';
return [list, pagination];
}

// 更新评论总数
Expand Down
3 changes: 2 additions & 1 deletion admin/model/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class Comment extends Model
this.db.table('comment comment').where(condition).cache(60).count('id'),
this.db.table('comment comment').field('comment.*,a.title').join('article a', 'comment.article_id=a.id').where(condition).order('comment.id', 'desc').page(page, pageSize).select()
]);
return [total, list];
const pagination = total ? this.$pagination.render(total) : '';
return [list, pagination];
}

// 文章评论列表
Expand Down
5 changes: 1 addition & 4 deletions app/controller/cate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ class Cate extends Base
async cate() {
const cate_dir = this.ctx.params.cate;
const cate = await this.$model.cate.getCate({cate_dir});

const [total, list] = await this.$model.article.getPageList({cate_id: cate.id});
const pagination = total ? this.$pagination.cate.render(total) : '';
const [list, pagination] = await this.$model.article.getPageList({cate_id: cate.id});

this.$assign('title', cate.cate_name + ' - ' + this.site.webname);
this.$assign('description', cate.description);
this.$assign('keywords', cate.keywords);

this.$assign('cate', cate);
this.$assign('list', list);

this.$assign('pagination', pagination);

await this.$fetch();
Expand Down
36 changes: 36 additions & 0 deletions app/controller/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Base = require('./base');

class Search extends Base
{
async search() {
const url = this.ctx.url;
if(!~url.indexOf('/search/')) {
this.$redirect(url.replace('/search', '/search/'), 301);
return false;
}

const keyword = this.ctx.query.keyword;
let [list, pagination] = [[], ''];

if(!keyword) {
this.$assign('title', '搜索' + ' - ' + this.site.webname);
this.$assign('search_title', '搜索');
} else {
const condition = {};
condition['concat(a.title, a.writer, a.keywords, a.description)'] = ['like', '%' + keyword + '%'];
[list, pagination] = await this.$model.article.getSearchList(condition);

this.$assign('title', keyword + ' - ' + this.site.webname);
this.$assign('search_title', keyword);
this.$assign('description', `关于 《${keyword}》的博文文章`);
this.$assign('keywords', keyword);
}

this.$assign('keyword', keyword);
this.$assign('list', list);
this.$assign('pagination', pagination);
await this.$fetch();
}
}

module.exports = Search;
14 changes: 13 additions & 1 deletion app/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class Article extends Model
this.db.where(condition).cache(600).count('id'),
this.db.field('id,cate_id,user_id,title,writer,source,click,keywords,description,add_time').where(condition).order('id', 'desc').page(page, pageSize).cache(600).select()
]);
return [total, list];
const pagination = total ? this.$pagination.cate.render(total) : '';
return [list, pagination];
}

// 最新文章
Expand All @@ -42,6 +43,17 @@ class Article extends Model
async nextOne(cur_id, condition) {
return await this.db.field('id,title').where({id: ['<', cur_id]}).where(condition).order('id', 'desc').cache(600).find();
}

// 搜索文章列表及分页
async getSearchList(condition, pageSize=10) {
const page = this.$pagination.search.curPage;
const [total, list] = await Promise.all([
this.db.table('article a').where(condition).cache(600).count('id'),
this.db.table('article a').field('a.id,a.cate_id,a.user_id,a.title,a.writer,a.keywords,a.click,a.description,a.add_time,c.cate_name,c.cate_dir').join('cate c', 'a.cate_id=c.id').where(condition).order('a.id', 'desc').page(page, pageSize).cache(600).select()
]);
const pagination = total ? this.$pagination.search.init({pageSize}).render(total) : '';
return [list, pagination];
}
}

module.exports = Article;
14 changes: 14 additions & 0 deletions app/pagination/search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {Pagination} = require('jj.js');

class Search extends Pagination
{
init(opts) {
super.init({
pageSize: 10,
...opts
});
return this;
}
}

module.exports = Search;
38 changes: 19 additions & 19 deletions app/view/article_article.htm
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@ <h1>{{article.title}}</h1>
<article class="content">
{{@article.content}}
</article>
<hr class="hr">
<hr class="m-hr">
<ul class="prevnext">
{{if prevOne}}<li>上一篇: <a href="{{url(':article', {id: prevOne.id})}}">{{prevOne.title}}</a></li>{{/if}}
{{if nextOne}}<li>下一篇: <a href="{{url(':article', {id: nextOne.id})}}">{{nextOne.title}}</a></li>{{/if}}
</ul>
{{if is_comment}}
<hr class="hr">
<hr class="m-hr">
<div class="comment">
<h3 class="comment_title">评论</h3>
<form class="comment_form" action="{{url('comment/post')}}">
<h3 class="title">评论</h3>
<form class="comment-form" action="{{url('comment/post')}}">
<input type="hidden" name="article_id" value="{{article.id}}" />
<input type="hidden" name="pid" value="0" />
<div class="comment_input">
<input type="text" name="uname" placeholder="昵称(必填)" />
<input type="email" name="email" placeholder="邮箱(必填)" />
<input type="url" name="url" placeholder="网址(选填)" />
<div class="comment-input">
<input class="input" type="text" name="uname" placeholder="昵称(必填)" />
<input class="input" type="email" name="email" placeholder="邮箱(必填)" />
<input class="input" type="url" name="url" placeholder="网址(选填)" />
</div>
<div class="comment_area">
<textarea name="content" placeholder="写点什么吧"></textarea>
<div class="comment-area">
<textarea class="input c-area" name="content" placeholder="写点什么吧"></textarea>
</div>
<div class="comment_button">
<span class="comment_submit">提交</span><span class="comment_cancel">取消回复</span>
<div class="comment-button">
<span class="button">提交</span><span class="button danger cancel">取消回复</span>
</div>
</form>
<div class="comment_list" data-url="{{url('comment/list', {id: article.id})}}">
<div class="comment_item">
<div class="comment_face" data-id="[id]" data-uname="[uname]">[face]<span>@</span></div>
<div class="comment_body">
<div class="comment_user"><a href="[url]" target="_blank">[uname]</a> &nbsp;<span>[add_time]</span></div>
<div class="comment_content">[content]</div>
<div class="comment-list" data-url="{{url('comment/list', {id: article.id})}}">
<div class="comment-item">
<div class="face" data-id="[id]" data-uname="[uname]">[face]<span>@</span></div>
<div class="comment-body">
<div class="comment-user"><a href="[url]" target="_blank">[uname]</a> &nbsp;<span>[add_time]</span></div>
<div class="comment-content">[content]</div>
[reply]
</div>
</div>
</div>
<div class="comment_load">加载更多评论</div>
<div class="comment-more">加载更多评论</div>
</div>
{{/if}}
</main>
Expand Down
12 changes: 8 additions & 4 deletions app/view/aside.htm
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
<aside class="aside">
<hr class="hr">
<hr class="m-hr">
<form class="box s-form" action="/search/">
<dt><div class="title"><input class="input s-input" type="text" name="keyword" value="{{keyword}}" /></div><span class="button s-button">搜索</span></dt>
</form>
<hr class="m-hr">
<dl class="box">
<dt><h3 class="title">最新博文</h3><span class="extra"></span></dt>
<dt><h3 class="title">最新博文</h3></dt>
<dd>
<ul>{{each latest item}}
<li><a href="{{url(':article', {id: item.id})}}">{{item.title}}</a></li>{{/each}}
</ul>
</dd>
</dl>
<hr class="hr">
<hr class="m-hr">
<dl class="box">
<dt><h3 class="title">热点博文</h3><span class="extra"></span></dt>
<dt><h3 class="title">热点博文</h3></dt>
<dd>
<ul>{{each hot item}}
<li><a href="{{url(':article', {id: item.id})}}">{{item.title}}</a></li>{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion app/view/cate_cate.htm
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>{{cate.cate_name}}</h1>
</ul>
</dl>{{/each}}
</div>
{{if pagination}}<hr class="hr">{{/if}}
{{if pagination}}<hr class="m-hr">{{/if}}
{{@pagination}}
</main>
{{include './aside.htm'}}
Expand Down
33 changes: 33 additions & 0 deletions app/view/search_search.htm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{{extend './layout.htm'}}

{{block 'header'}}
<div class="container jumbotron">
<h1>{{search_title}}</h1>
<p>{{description}}</p>
</div>
{{/block}}

{{block 'content'}}
<div class="container row">
<main class="main">
<div class="list">
{{each list item}}
<dl class="list-item">
<dt><h3 class="title"><a href="{{url(':article', {id: item.id})}}">{{item.title}}</a></h3></dt>
<dd>
{{item.description}}
</dd>
<ul>
<li>分类: <a href="{{url(':cate', {cate: item.cate_dir})}}">{{item.cate_name}}</a></li>
<li>时间: {{item.add_time | dateFormat 'YYYY-mm-dd'}}</li>
<li>浏览: {{item.click}}</li>
<li>关键词: {{item.keywords}}</li>
</ul>
</dl>{{/each}}
</div>
{{if pagination}}<hr class="m-hr">{{/if}}
{{@pagination}}
</main>
{{include './aside.htm'}}
</div>
{{/block}}
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
route = [
{url: '/', path: 'index/index'},
{url: '/article/:id.html', path: 'article/article', name: 'article'},
{url: '/search', path: 'search/search', name: 'search'},
{url: '/:cate/', path: 'cate/cate', name: 'cate'}, // 会匹配到后台地址,所以程序内需执行this.$next()
{url: '/:cate/list_:page.html', path: 'cate/cate', name: 'cate_page'},
{url: '/:app/:controller?/:action?', path: 'admin/auth/index', type: 'middleware'} // 后台登录验证
Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
"homepage": "https://github.com/yafoo/melog#readme",
"dependencies": {
"jj.js": "^0.2.2",
"jj.js": "^0.2.3",
"markdown-it": "^12.2.0"
}
}
Loading

0 comments on commit 481de6b

Please sign in to comment.