Skip to content

Commit

Permalink
更新jj.js到v0.4.3,优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Sep 12, 2021
1 parent 8e0e740 commit 21f8782
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 109 deletions.
7 changes: 5 additions & 2 deletions admin/controller/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,31 @@ class Article extends Base
async index() {
const cate_id = this.ctx.query.cate_id;
const keyword = this.ctx.query.keyword;

const condition = {};

if(cate_id > 0) {
condition['a.cate_id'] = cate_id;
}
if(keyword !== undefined) {
condition['concat(a.title, a.writer)'] = ['like', '%' + keyword + '%'];
}

const cate_list = await this.$model.cate.getCate();
const [list, pagination] = await this.$model.article.getArticleList(condition);

this.$assign('cate_id', cate_id);
this.$assign('keyword', keyword);
this.$assign('cate_list', cate_list);
this.$assign('list', list);
this.$assign('pagination', pagination);
this.$assign('pagination', pagination.render());

await this.$fetch();
}

async add() {
const cate_list = await this.$model.cate.getCate();
const id = parseInt(this.ctx.query.id);

let article = {};
if(id) {
article = await this.$model.article.getOne({id});
Expand Down
4 changes: 3 additions & 1 deletion admin/controller/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ class Comment extends Base
condition['concat(comment.uname, comment.email, comment.url, comment.content, comment.ip)'] = ['like', '%' + keyword + '%'];
}
const [list, pagination] = await this.$model.comment.getCommentList(condition);

this.$assign('keyword', keyword);
this.$assign('list', list);
this.$assign('pagination', pagination);
this.$assign('pagination', pagination.render());

await this.$fetch();
}

Expand Down
5 changes: 3 additions & 2 deletions admin/controller/upload.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Base = require('./base');
const {utils} = require('jj.js');
const path = require('path');

class Upload extends Base
{
Expand All @@ -13,7 +14,7 @@ class Upload extends Base

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

Expand Down Expand Up @@ -92,7 +93,7 @@ class Upload extends Base
}

try {
const upload_dir = this.ctx._.__node.path + this.$config.app.static_dir + this.site.upload;
const upload_dir = path.join(this.$config.app.base_dir,this.$config.app.static_dir,this.site.upload);
const img_path = upload_dir + file.image;
const thumb_path = upload_dir + file.thumb;

Expand Down
9 changes: 1 addition & 8 deletions admin/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ class Article extends Model

// 后台文章列表
async getArticleList(condition) {
const page = this.$pagination.curPage;
const pageSize = this.$pagination.options.pageSize;
const [total, list] = await Promise.all([
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()
]);
const pagination = total ? this.$pagination.render(total) : '';
return [list, pagination];
return await 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').pagination();
}

// 更新评论总数
Expand Down
9 changes: 1 addition & 8 deletions admin/model/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,7 @@ class Comment extends Model
{
// 后台评论管理
async getCommentList(condition) {
const page = this.$pagination.curPage;
const pageSize = this.$pagination.options.pageSize;
const [total, list] = await Promise.all([
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()
]);
const pagination = total ? this.$pagination.render(total) : '';
return [list, pagination];
return await this.db.table('comment comment').field('comment.*,a.title').join('article a', 'comment.article_id=a.id').where(condition).order('comment.id', 'desc').pagination();
}

// 文章评论列表
Expand Down
8 changes: 1 addition & 7 deletions admin/model/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ class Upload extends Model

// 后台分页列表
async getPageList(condition) {
const page = this.$pagination.curPage;
const pageSize = this.$pagination.options.pageSize;
const [total, list] = await Promise.all([
this.db.where(condition).count('id'),
this.db.where(condition).order('id', 'desc').page(page, pageSize).select()
]);
const pagination = total ? this.$pagination.render(total) : '';
const [list, pagination] = await this.db.where(condition).order('id', 'desc').pagination();

if(list && list.length) {
const site_config = await this.$model.site.getConfig();
Expand Down
12 changes: 5 additions & 7 deletions app/controller/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,19 @@ class Article extends Base
const article = await model_article.getArticle({id: aid});
if(!article) return;

// 栏目信息
const cate = await this.$model.cate.getCate({id: article.cate_id});

// 上一篇、下一篇
const [prevOne, nextOne] = await Promise.all([
// 栏目信息、上一篇、下一篇
const [cate, prevOne, nextOne] = await Promise.all([
this.$model.cate.getCate({id: article.cate_id}),
model_article.prevOne(aid),
model_article.nextOne(aid)
]);

// 更新点击(页面及数据库)
article.click++;
model_article.db.where({id: article.id}).inc('click');

// markdown
article.content = md.render(article.content);
const is_comment = this.site.is_comment + article.is_comment >= 1 ? true : false;

this.$assign('title', article.title + ' - ' + cate.cate_name + ' - ' + this.site.webname);
this.$assign('description', article.description);
Expand All @@ -36,7 +34,7 @@ class Article extends Base
this.$assign('prevOne', prevOne);
this.$assign('nextOne', nextOne);

this.$assign('is_comment', this.site.is_comment + article.is_comment >= 1);
this.$assign('is_comment', is_comment);

await this.$fetch();
}
Expand Down
21 changes: 9 additions & 12 deletions app/controller/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,26 @@ const pjson = require('../../package.json');
class Base extends Controller
{
async _init() {
const nav = await this.$model.cate.getCateList();
// 文章模型
const model_article = this.$model.article;

// 最新、热门列表
const [latest, hot] = await Promise.all([
// 站点配置、顶部导航、最新、热门列表、底部链接
const [site_config, nav, latest, hot, foot_links] = await Promise.all([
this.$admin.model.site.getConfig(),
this.$model.cate.getCateList(),
model_article.getNew(),
model_article.getHot()
model_article.getHot(),
this.$admin.model.link.getFootLinks()
]);

// 站点配置
this.site = await this.$admin.model.site.getConfig();
this.$assign('site', this.site);

// 底部链接
const foot_links = await this.$admin.model.link.getFootLinks();
this.$assign('foot_links', foot_links);

this.$assign('site', this.site = site_config);
this.$assign('title', this.site.webname + ' - ' + this.site.description);
this.$assign('description', this.site.description);
this.$assign('keywords', this.site.keywords);
this.$assign('nav', nav);
this.$assign('latest', latest);
this.$assign('hot', hot);
this.$assign('foot_links', foot_links);
}
}

Expand Down
13 changes: 7 additions & 6 deletions app/controller/cate.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ const Base = require('./base');
class Cate extends Base
{
async _init() {
// admin跳过
// admin跳过(分类路由会匹配到后台admin)
if(this.ctx.APP == 'admin') {
await this.$next();
return false;
}

this._cate_dir = this.ctx.params.cate;

// 栏目不存在跳过
const CateArr = await this.$model.cate.getCateArr();
if(!~CateArr.indexOf(this.ctx.params.cate)) {
const cate_dirs = await this.$model.cate.getCateDirList();
if(!~cate_dirs.indexOf(this._cate_dir)) {
this.ctx.params = {};
await this.$next();
return false;
Expand All @@ -21,8 +23,7 @@ class Cate extends Base
}

async cate() {
const cate_dir = this.ctx.params.cate;
const cate = await this.$model.cate.getCate({cate_dir});
const cate = await this.$model.cate.getCate({cate_dir: this._cate_dir});
const [list, pagination] = await this.$model.article.getPageList({cate_id: cate.id});

this.$assign('title', cate.cate_name + ' - ' + this.site.webname);
Expand All @@ -31,7 +32,7 @@ class Cate extends Base

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

await this.$fetch();
}
Expand Down
2 changes: 2 additions & 0 deletions app/controller/search.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Renderer = require('markdown-it/lib/renderer');
const Base = require('./base');

class Search extends Base
Expand All @@ -19,6 +20,7 @@ class Search extends Base
const condition = {};
condition['concat(a.title, a.writer, a.keywords, a.description)'] = ['like', '%' + keyword + '%'];
[list, pagination] = await this.$model.article.getSearchList(condition);
pagination = pagination.render();

this.$assign('title', keyword + ' - ' + this.site.webname);
this.$assign('search_title', keyword);
Expand Down
39 changes: 13 additions & 26 deletions app/model/article.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,24 @@ const {Model} = require('jj.js');

class Article extends Model
{
// 获取一篇文章
async getArticle(condition, fields='') {
return await this.db.field(fields).where(condition).find();
}

// 首页文章列表
async getIndexList(condition, rows=10, page, pageSize){
return await 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').limit(rows).page(page, pageSize).cache(600).select();
async getIndexList(condition, rows=10){
return await 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').limit(rows).cache(600).select();
}

// 栏目文章列表及分页
async getPageList(condition) {
const page = this.$pagination.cate.curPage;
const pageSize = this.$pagination.cate.options.pageSize;
const [total, list] = await Promise.all([
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()
]);
const pagination = total ? this.$pagination.cate.render(total) : '';
return [list, pagination];
return await this.db.field('id,cate_id,user_id,title,writer,source,click,keywords,description,add_time').where(condition).order('id', 'desc').cache(600).pagination(this.$pagination.cate);
}

// 搜索文章列表及分页
async getSearchList(condition, pageSize=10) {
return await 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').cache(600).pagination(undefined, pageSize);
}

// 获取一篇文章
async getArticle(condition, fields) {
return await this.db.field(fields).where(condition).find();
}

// 最新文章
Expand All @@ -43,17 +41,6 @@ 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;
6 changes: 3 additions & 3 deletions app/model/cate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Cate extends Model
return await this.db.where(condition).cache(600).find();
}

// 顶部导航
// 博客顶部导航
async getCateList(rows) {
return await this.db.order('sort', 'asc').limit(rows).cache(600).select();
}

// 分类数组
async getCateArr() {
// 分类目录地址
async getCateDirList() {
const rows = await this.getCateList();
return rows.reduce((arr, cate) => {
return arr.concat(cate.cate_dir);
Expand Down
8 changes: 3 additions & 5 deletions app/pagination/cate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ class Cate extends Pagination
{
init(opts) {
super.init({
pageType: 'params',
pageKey: 'page',
pageSize: 10,
urlIndex: this.$url.build(':cate'),
urlPage: this.$url.build(':cate_page', {page: '${page}'}),
key_origin: 'params',
url_index: ':cate',
url_page: ':cate_page',
...opts
});
return this;
Expand Down
14 changes: 0 additions & 14 deletions app/pagination/search.js

This file was deleted.

14 changes: 7 additions & 7 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 @@ -24,7 +24,7 @@
"homepage": "https://github.com/yafoo/melog#readme",
"dependencies": {
"jimp": "^0.16.1",
"jj.js": "^0.2.4",
"jj.js": "^0.4.3",
"markdown-it": "^12.2.0"
}
}

0 comments on commit 21f8782

Please sign in to comment.