diff --git a/admin/controller/article.js b/admin/controller/article.js index 478d908..e0196e7 100644 --- a/admin/controller/article.js +++ b/admin/controller/article.js @@ -5,14 +5,15 @@ 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); @@ -20,13 +21,15 @@ class Article extends Base 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}); diff --git a/admin/controller/comment.js b/admin/controller/comment.js index a76f6dc..8727774 100644 --- a/admin/controller/comment.js +++ b/admin/controller/comment.js @@ -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(); } diff --git a/admin/controller/upload.js b/admin/controller/upload.js index e2ebbaf..ddc2853 100644 --- a/admin/controller/upload.js +++ b/admin/controller/upload.js @@ -1,5 +1,6 @@ const Base = require('./base'); const {utils} = require('jj.js'); +const path = require('path'); class Upload extends Base { @@ -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(); } @@ -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; diff --git a/admin/model/article.js b/admin/model/article.js index cfa7ded..6713cf9 100644 --- a/admin/model/article.js +++ b/admin/model/article.js @@ -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(); } // 更新评论总数 diff --git a/admin/model/comment.js b/admin/model/comment.js index 72f8e26..1a10835 100644 --- a/admin/model/comment.js +++ b/admin/model/comment.js @@ -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(); } // 文章评论列表 diff --git a/admin/model/upload.js b/admin/model/upload.js index d84fdac..ca6070d 100644 --- a/admin/model/upload.js +++ b/admin/model/upload.js @@ -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(); diff --git a/app/controller/article.js b/app/controller/article.js index b3cacae..e081a8c 100644 --- a/app/controller/article.js +++ b/app/controller/article.js @@ -11,11 +11,9 @@ 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) ]); @@ -23,9 +21,9 @@ class Article extends Base // 更新点击(页面及数据库) 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); @@ -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(); } diff --git a/app/controller/base.js b/app/controller/base.js index 5e6e556..c5fde89 100644 --- a/app/controller/base.js +++ b/app/controller/base.js @@ -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); } } diff --git a/app/controller/cate.js b/app/controller/cate.js index 3455605..789c605 100644 --- a/app/controller/cate.js +++ b/app/controller/cate.js @@ -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; @@ -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); @@ -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(); } diff --git a/app/controller/search.js b/app/controller/search.js index a9a2d73..0551f26 100644 --- a/app/controller/search.js +++ b/app/controller/search.js @@ -1,3 +1,4 @@ +const Renderer = require('markdown-it/lib/renderer'); const Base = require('./base'); class Search extends Base @@ -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); diff --git a/app/model/article.js b/app/model/article.js index 23b64f0..36771fd 100644 --- a/app/model/article.js +++ b/app/model/article.js @@ -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(); } // 最新文章 @@ -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; \ No newline at end of file diff --git a/app/model/cate.js b/app/model/cate.js index a3e2723..d01db65 100644 --- a/app/model/cate.js +++ b/app/model/cate.js @@ -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); diff --git a/app/pagination/cate.js b/app/pagination/cate.js index a5f640e..b82ead2 100644 --- a/app/pagination/cate.js +++ b/app/pagination/cate.js @@ -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; diff --git a/app/pagination/search.js b/app/pagination/search.js deleted file mode 100644 index b5761b1..0000000 --- a/app/pagination/search.js +++ /dev/null @@ -1,14 +0,0 @@ -const {Pagination} = require('jj.js'); - -class Search extends Pagination -{ - init(opts) { - super.init({ - pageSize: 10, - ...opts - }); - return this; - } -} - -module.exports = Search; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index dd18138..82a7c3a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,7 +10,7 @@ "license": "MIT", "dependencies": { "jimp": "^0.16.1", - "jj.js": "^0.2.4", + "jj.js": "^0.4.3", "markdown-it": "^12.2.0" } }, @@ -1117,9 +1117,9 @@ } }, "node_modules/jj.js": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/jj.js/-/jj.js-0.2.4.tgz", - "integrity": "sha512-yyHj+T1Y9Drv9gIrlFNxUwcMOQXzOrpQlh8yeZS3nvzi1IByP0pRMCGHNbKNq/CGvVd1QJYXe6ifykOzsyMruQ==", + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/jj.js/-/jj.js-0.4.3.tgz", + "integrity": "sha512-3NeRABU4R4o6+yOmSDdDD5M0yW/8BTB9gws009j9ISa5fXu554UIYEjkTvmufOIYzUyQJ1ZXfhGfXZq7h/Cqew==", "dependencies": { "@koa/router": "^10.1.0", "art-template": "^4.13.2", @@ -2770,9 +2770,9 @@ } }, "jj.js": { - "version": "0.2.4", - "resolved": "https://registry.npmjs.org/jj.js/-/jj.js-0.2.4.tgz", - "integrity": "sha512-yyHj+T1Y9Drv9gIrlFNxUwcMOQXzOrpQlh8yeZS3nvzi1IByP0pRMCGHNbKNq/CGvVd1QJYXe6ifykOzsyMruQ==", + "version": "0.4.3", + "resolved": "https://registry.npmjs.org/jj.js/-/jj.js-0.4.3.tgz", + "integrity": "sha512-3NeRABU4R4o6+yOmSDdDD5M0yW/8BTB9gws009j9ISa5fXu554UIYEjkTvmufOIYzUyQJ1ZXfhGfXZq7h/Cqew==", "requires": { "@koa/router": "^10.1.0", "art-template": "^4.13.2", diff --git a/package.json b/package.json index 15ebd73..6bf937d 100644 --- a/package.json +++ b/package.json @@ -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" } }