From 15a9f2cf695879ca6b19688e113709155163998a Mon Sep 17 00:00:00 2001 From: www Date: Tue, 31 Aug 2021 13:10:42 +0800 Subject: [PATCH] v2.0 --- README.md | 2 +- admin/controller/article.js | 51 ++--- admin/controller/base.js | 18 +- admin/controller/cate.js | 30 ++- admin/controller/comment.js | 25 ++- admin/controller/index.js | 21 +- admin/controller/link.js | 36 ++- admin/controller/site.js | 17 +- admin/controller/user.js | 37 ++- admin/middleware/auth.js | 21 +- admin/model/article.js | 25 ++- admin/model/cate.js | 17 +- admin/model/comment.js | 25 ++- admin/model/link.js | 60 ++--- admin/model/site.js | 25 ++- admin/model/user.js | 35 +-- admin/service/cookie.js | 9 +- admin/utils.js | 30 +-- admin/view/article_index.htm | 2 +- admin/view/comment_index.htm | 2 +- admin/view/index_index.htm | 2 +- admin/view/site_index.htm | 12 +- app/controller/article.js | 21 +- app/controller/base.js | 27 +-- app/controller/cate.js | 25 ++- app/controller/comment.js | 36 +-- app/controller/index.js | 13 +- app/model/article.js | 13 +- app/model/cate.js | 5 +- app/pagination/cate.js | 9 +- app/view/layout.htm | 4 +- config/app.js | 8 - config/routes.js | 4 +- config/utils.js | 6 - config/view.js | 9 + melog.sql | 76 +++---- package-lock.json | 422 ++++++++++++++++++----------------- package.json | 10 +- server.js | 2 +- 39 files changed, 584 insertions(+), 608 deletions(-) delete mode 100644 config/utils.js create mode 100644 config/view.js diff --git a/README.md b/README.md index 3999462..8c8117c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ ![melog](https://me.i-i.me/static/images/melog_360.png "melog") -melog,一个基于iijs(nodejs)构建的简单轻量级blog系统。代码极简,无需编译,方便二次开发。 +melog,一个基于jj.js(nodejs)构建的简单轻量级blog系统。代码极简,无需编译,方便二次开发。 项目地址:[https://github.com/yafoo/melog](https://github.com/yafoo/melog "https://github.com/yafoo/melog") diff --git a/admin/controller/article.js b/admin/controller/article.js index 5fe770d..575cd5f 100644 --- a/admin/controller/article.js +++ b/admin/controller/article.js @@ -1,18 +1,19 @@ const Base = require('./base'); -class Article extends Base { +class Article extends Base +{ async index() { const condition = {}; - const keys = this.ctx.query.keys; - if(keys !== undefined) { - condition['concat(a.title, a.writer)'] = ['like', '%' + keys + '%']; + const keyword = this.ctx.query.keyword; + 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) : ''; - this.assign('keys', keys); - this.assign('list', list); - this.assign('pagination', pagination); - await this.fetch(); + const pagination = total ? this.$pagination.render(total) : ''; + this.$assign('keyword', keyword); + this.$assign('list', list); + this.$assign('pagination', pagination); + await this.$fetch(); } async add() { @@ -23,61 +24,55 @@ class Article extends Base { article = await this.$model.article.getOne({id}); } - this.assign('cate_list', cate_list); - this.assign('article', article); + this.$assign('cate_list', cate_list); + this.$assign('article', article); const comment_option = [ {value: 0, name: '默认'}, {value: 1, name: '开启'}, {value: -1, name: '关闭'} ]; - this.assign('comment_option', comment_option); + this.$assign('comment_option', comment_option); - await this.fetch(); + await this.$fetch(); } async save() { if(this.ctx.method != 'POST'){ - return this.error('非法请求!'); + return this.$error('非法请求!'); } const data = this.ctx.request.body; const aid = data.id; delete data.id; if(aid) { - data.update_time = Math.round(new Date() / 1000); const result = await this.$model.article.update(data, {id: aid}); if(result) { - this.success('保存成功!', 'index'); + this.$success('保存成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } else { - data.add_time = Math.round(new Date() / 1000); const result = await this.$model.article.add(data); if(result) { - this.success('新增成功!', 'index'); + this.$success('新增成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } } async delete() { const id = parseInt(this.ctx.query.id); - const article = await this.$model.article.getOne({id}); - if(!article) { - return this.error('数据不存在!'); - } - + try { await this.db.startTrans(async () => { await this.$model.article.delete({id}); - await this.$model.comment.delete({article_id: article.id}); + await this.$model.comment.delete({article_id: id}); }); - this.success('删除成功!', 'index'); + this.$success('删除成功!', 'index'); } catch (e) { - this.error('删除失败!'); + this.$error('删除失败!'); } } } diff --git a/admin/controller/base.js b/admin/controller/base.js index ab337ed..3a88a9d 100644 --- a/admin/controller/base.js +++ b/admin/controller/base.js @@ -1,22 +1,20 @@ -const {Controller} = require('iijs'); -const pjson = require('../../package.json'); +const {Controller} = require('jj.js'); -class Base extends Controller { +class Base extends Controller +{ async _init() { this.user_id = this.$service.cookie.get('user'); if(this.user_id) { this.user = await this.$model.user.getOne({id: this.user_id}); - this.assign('user', this.user); + this.$assign('user', this.user); } this.site = await this.$model.site.getConfig(); - this.site.VERSION = pjson.version; - this.site.APP_TIME = this.ctx.APP_TIME; - this.assign('site', this.site); + this.$assign('site', this.site); - this.assign('title', '管理中心'); - this.assign('description', this.site.description); - this.assign('keywords', this.site.keywords); + this.$assign('title', '管理中心'); + this.$assign('description', this.site.description); + this.$assign('keywords', this.site.keywords); } } diff --git a/admin/controller/cate.js b/admin/controller/cate.js index 45b4f14..e2aef53 100644 --- a/admin/controller/cate.js +++ b/admin/controller/cate.js @@ -1,10 +1,11 @@ const Base = require('./base'); -class Cate extends Base { +class Cate extends Base +{ async index() { const list = await this.$model.cate.getList(undefined, 100, 'sort', 'asc'); - this.assign('list', list); - await this.fetch(); + this.$assign('list', list); + await this.$fetch(); } async add() { @@ -14,13 +15,13 @@ class Cate extends Base { cate = await this.$model.cate.getOne({id}); } - this.assign('cate', cate); - await this.fetch(); + this.$assign('cate', cate); + await this.$fetch(); } async save() { if(this.ctx.method != 'POST'){ - return this.error('非法请求!'); + return this.$error('非法请求!'); } const data = this.ctx.request.body; @@ -29,33 +30,28 @@ class Cate extends Base { if(id) { const result = await this.$model.cate.update(data, {id}); if(result) { - this.success('保存成功!', 'index'); + this.$success('保存成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } else { const result = await this.$model.cate.add(data); if(result) { - this.success('新增成功!', 'index'); + this.$success('新增成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } } async delete() { const id = parseInt(this.ctx.query.id); - const cate = await this.$model.cate.getOne({id}); - if(!cate) { - return this.error('数据不存在!'); - } const result = await this.$model.cate.delete({id}); - if(result) { - this.success('删除成功!', 'index'); + this.$success('删除成功!', 'index'); } else { - this.error('删除失败!'); + this.$error('删除失败!'); } } } diff --git a/admin/controller/comment.js b/admin/controller/comment.js index ba56ca8..3822b87 100644 --- a/admin/controller/comment.js +++ b/admin/controller/comment.js @@ -1,28 +1,29 @@ const Base = require('./base'); -class Comment extends Base { +class Comment extends Base +{ async index() { const condition = {}; - const keys = this.ctx.query.keys; - if(keys !== undefined) { - condition['concat(comment.uname, comment.email, comment.url, comment.content, comment.ip)'] = ['like', '%' + keys + '%']; + const keyword = this.ctx.query.keyword; + 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) : ''; - this.assign('keys', keys); - this.assign('list', list); - this.assign('pagination', pagination); - await this.fetch(); + const pagination = total ? this.$pagination.render(total) : ''; + this.$assign('keyword', keyword); + this.$assign('list', list); + this.$assign('pagination', pagination); + await this.$fetch(); } async delete() { const id = parseInt(this.ctx.query.id); + const result = await this.$model.comment.delComment(id); - if(result === true) { - this.success('删除成功!', 'index'); + this.$success('删除成功!', 'index'); } else { - this.error(result); + this.$error(result); } } } diff --git a/admin/controller/index.js b/admin/controller/index.js index 3f958f6..799a20a 100644 --- a/admin/controller/index.js +++ b/admin/controller/index.js @@ -1,8 +1,9 @@ const Base = require('./base'); -class Index extends Base { +class Index extends Base +{ async index() { - await this.fetch(); + await this.$fetch(); } async login() { @@ -10,30 +11,30 @@ class Index extends Base { const email = this.ctx.request.body.email; const password = this.ctx.request.body.password; if(!email) { - this.error('邮箱不能为空!'); + this.$error('邮箱不能为空!'); } else if(!this.ctx.request.body.password) { - this.error('密码不能为空!'); + this.$error('密码不能为空!'); } const msg = await this.$model.user.login(email, password); if(msg !== true) { - this.error(msg); + this.$error(msg); } else { - this.success('登录成功!', 'index'); + this.$success('登录成功!', 'index'); } } else { - this.assign('title', '登录'); - await this.fetch(); + this.$assign('title', '登录'); + await this.$fetch(); } } async logout() { await this.$model.user.logout(); - this.success('退出成功!', 'index') + this.$success('退出成功!', 'index') } async register() { - this.error('注册功能未开放!'); + this.$error('注册功能未开放!'); } } diff --git a/admin/controller/link.js b/admin/controller/link.js index 4acea1d..8bb72c7 100644 --- a/admin/controller/link.js +++ b/admin/controller/link.js @@ -1,10 +1,11 @@ const Base = require('./base'); -class Link extends Base { +class Link extends Base +{ async index() { const list = await this.$model.link.getList(); - this.assign('list', list); - await this.fetch(); + this.$assign('list', list); + await this.$fetch(); } async add() { @@ -16,15 +17,15 @@ class Link extends Base { link = await this.$model.link.getOne({id}); } - this.assign('pid', pid); - this.assign('link_list', link_list); - this.assign('link', link); - await this.fetch(); + this.$assign('pid', pid); + this.$assign('link_list', link_list); + this.$assign('link', link); + await this.$fetch(); } async save() { if(this.ctx.method != 'POST'){ - return this.error('非法请求!'); + return this.$error('非法请求!'); } const data = this.ctx.request.body; @@ -33,16 +34,16 @@ class Link extends Base { if(id) { const result = await this.$model.link.update(data, {id}); if(result) { - this.success('保存成功!', 'index'); + this.$success('保存成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } else { const result = await this.$model.link.add(data); if(result) { - this.success('新增成功!', 'index'); + this.$success('新增成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } } @@ -50,19 +51,14 @@ class Link extends Base { async delete() { const id = parseInt(this.ctx.query.id); if(id == 1 || id == 2) { - return this.error('系统固定链接不可删除!'); - } - const link = await this.$model.link.getOne({id}); - if(!link) { - return this.error('数据不存在!'); + return this.$error('系统固定链接不可删除!'); } const result = await this.$model.link.delete({id}); - if(result) { - this.success('删除成功!', 'index'); + this.$success('删除成功!', 'index'); } else { - this.error('删除失败!'); + this.$error('删除失败!'); } } } diff --git a/admin/controller/site.js b/admin/controller/site.js index 3430c1c..b2c97c1 100644 --- a/admin/controller/site.js +++ b/admin/controller/site.js @@ -1,27 +1,28 @@ const Base = require('./base'); -class Site extends Base { +class Site extends Base +{ async index() { - if(this.ctx.method == 'POST'){ + if(this.ctx.method == 'POST') { const data = this.ctx.request.body; - const list = await this.$model.site.db.column('value', 'sname'); + const list = await this.$model.site.db.column('value', 'kname'); try { await this.$model.site.db.startTrans(); Object.keys(data).forEach(async key => { if(list[key] !== undefined && list[key] !== data[key]) { - await this.$model.site.update({value: data[key]}, {sname: key}); + await this.$model.site.update({value: data[key]}, {kname: key}); } }); await this.$model.site.db.commit(); - this.success('保存成功!'); + this.$success('保存成功!'); } catch(e) { await this.$model.site.db.rollback(); - this.error(e.msg); + this.$error(e.msg); } } else { const list = await this.$model.site.getList(); - this.assign('list', list); - await this.fetch(); + this.$assign('list', list); + await this.$fetch(); } } } diff --git a/admin/controller/user.js b/admin/controller/user.js index ed16652..6a83da1 100644 --- a/admin/controller/user.js +++ b/admin/controller/user.js @@ -1,10 +1,11 @@ const Base = require('./base'); -class User extends Base { +class User extends Base +{ async index() { const list = await this.$model.user.getList(undefined, 100, 'id'); - this.assign('list', list); - await this.fetch(); + this.$assign('list', list); + await this.$fetch(); } async add() { @@ -14,13 +15,13 @@ class User extends Base { user = await this.$model.user.getOne({id}); } - this.assign('user', user); - await this.fetch(); + this.$assign('user', user); + await this.$fetch(); } async save() { if(this.ctx.method != 'POST'){ - return this.error('非法请求!'); + return this.$error('非法请求!'); } const data = this.ctx.request.body; @@ -28,27 +29,27 @@ class User extends Base { delete data.id; if(data.password != data.password2) { - return this.error('两次输入密码不一致!'); + return this.$error('两次输入密码不一致!'); } delete data.password2; if(id) { const result = await this.$model.user.update(data, {id}); if(result) { - this.success('保存成功!', 'index'); + this.$success('保存成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } else { if(!data.email || !data.password) { - return this.error('账号或密码不能为空!'); + return this.$error('账号或密码不能为空!'); } const result = await this.$model.user.add(data); if(result) { - this.success('新增成功!', 'index'); + this.$success('新增成功!', 'index'); } else { - this.error('保存失败!'); + this.$error('保存失败!'); } } } @@ -56,20 +57,14 @@ class User extends Base { async delete() { const id = parseInt(this.ctx.query.id); if(id == 1) { - return this.error('管理员账号请手工在数据库删除!'); - } - - const user = await this.$model.user.getOne({id}); - if(!user) { - return this.error('数据不存在!'); + return this.$error('管理员账号请手工在数据库删除!'); } const result = await this.$model.user.delete({id}); - if(result) { - this.success('删除成功!', 'index'); + this.$success('删除成功!', 'index'); } else { - this.error('删除失败!'); + this.$error('删除失败!'); } } } diff --git a/admin/middleware/auth.js b/admin/middleware/auth.js index 8536560..d2eba24 100644 --- a/admin/middleware/auth.js +++ b/admin/middleware/auth.js @@ -1,6 +1,7 @@ -const {Middleware} = require('iijs'); +const {Middleware} = require('jj.js'); -class Auth extends Middleware { +class Auth extends Middleware +{ async index() { await this.alias(); } @@ -12,11 +13,11 @@ class Auth extends Middleware { if(admin_auth == 1 && this.ctx.params.app == 'admin') { await this.login(); - } else if(this.ctx.params.app == admin_alias) { + } else if(this.ctx.params.app === admin_alias) { admin_auth != 1 && this.$service.cookie.set('admin_auth', 1); - this.redirect('index/index'); + this.$redirect('index/index'); } else if(this.ctx.params.app != 'admin') { - await this.next(); + await this.$next(); } } @@ -24,17 +25,19 @@ class Auth extends Middleware { async login() { if(this.$service.cookie.get('user')) { if(this.ctx.params.controller == 'index' && this.ctx.params.action == 'login') { - this.redirect('index/index'); + this.$redirect('index/index'); } else { + // 清理params this.ctx.params = {}; - await this.next(); + await this.$next(); } } else { if(this.ctx.params.controller == 'index' && this.ctx.params.action == 'login') { + // 清理params this.ctx.params = {}; - await this.next(); + await this.$next(); } else { - this.redirect('index/login'); + this.$redirect('index/login'); } } } diff --git a/admin/model/article.js b/admin/model/article.js index b0befd8..cbee9b8 100644 --- a/admin/model/article.js +++ b/admin/model/article.js @@ -1,30 +1,37 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class Article extends Model { +class Article extends Model +{ async getList(condition, rows=10, order='id', sort='desc'){ return await this.db.field('id,cate_id,user_id,title,writer,source,source_url,click,keywords,description,add_time,update_time').where(condition).order(order, sort).limit(rows).select(); } - async getOne(condition){ + async getOne(condition) { return await this.db.where(condition).find(); } - async add(data){ + async add(data) { + if(!data.add_time) { + data.add_time = this.utils.time(); + } return await this.db.insert(data); } - async update(data, condition){ + async update(data, condition) { + if(!data.update_time) { + data.update_time = this.utils.time(); + } return await this.db.update(data, condition); } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } // 后台文章列表 - async getArticleList(condition){ - const page = this.$$pagination.curPage; - const pageSize = this.$$pagination.options.pageSize; + 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() diff --git a/admin/model/cate.js b/admin/model/cate.js index f5118b4..2696d2d 100644 --- a/admin/model/cate.js +++ b/admin/model/cate.js @@ -1,27 +1,28 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class Cate extends Model { - async getList(condition, rows=10, order='id', sort='desc'){ +class Cate extends Model +{ + async getList(condition, rows=10, order='id', sort='desc') { return await this.db.where(condition).order(order, sort).limit(rows).select(); } - async getOne(condition){ + async getOne(condition) { return await this.db.where(condition).find(); } - async add(data){ + async add(data) { return await this.db.insert(data); } - async update(data, condition){ + async update(data, condition) { return await this.db.update(data, condition); } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } - async getCate(rows){ + async getCate(rows) { return await this.db.order('sort', 'asc').limit(rows).select(); } } diff --git a/admin/model/comment.js b/admin/model/comment.js index 10b552b..f2c66b8 100644 --- a/admin/model/comment.js +++ b/admin/model/comment.js @@ -1,10 +1,11 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class Comment extends Model { +class Comment extends Model +{ // 后台评论管理 - async getCommentList(condition){ - const page = this.$$pagination.curPage; - const pageSize = this.$$pagination.options.pageSize; + 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() @@ -13,7 +14,7 @@ class Comment extends Model { } // 文章评论列表 - async getPageList(article_id, page=1){ + async getPageList(article_id, page=1) { const comment_ids = await this.db.where({article_id, pid: 0}).order('id', 'desc').page(page, 10).column('id'); if(!comment_ids.length) { return []; @@ -21,18 +22,22 @@ class Comment extends Model { return await this.db.field('id,pid,article_id,user_id,uname,url,content,add_time').where({comment_id: ['in', comment_ids]}).order('id', 'asc').limit(100).select(); } - async getOne(condition){ + async getOne(condition) { return await this.db.where(condition).find(); } // 新增评论 - async add(data){ + async add(data) { + if(!data.add_time) { + data.add_time = this.$utils.time(); + } try { await this.db.startTrans(async () => { const result = await this.db.insert(data); data.comment_id || await this.db.update({comment_id: result.insertId}, {id: result.insertId}); const comment_total = await this.db.where({article_id: data.article_id}).count(); - // 框架不完美,此处需写 $admin.model + // 框架不完美,此处需写 this.$admin.model + // this.$model 写法,在前台调用时会定位到前台model this.$admin.model.article.updateComment(data.article_id, comment_total); }); return true; @@ -41,7 +46,7 @@ class Comment extends Model { } } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } diff --git a/admin/model/link.js b/admin/model/link.js index 13d4931..9712f06 100644 --- a/admin/model/link.js +++ b/admin/model/link.js @@ -1,78 +1,44 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); +const {toTree, toTreeArray} = require('../utils'); -class Link extends Model { - async getList(condition){ +class Link extends Model +{ + async getList(condition) { const list = await this.db.where(condition).order('sort', 'asc').select(); - return this.toTree(list); + return toTree(list); } - async getOne(condition){ + async getOne(condition) { return await this.db.where(condition).find(); } - async add(data){ + async add(data) { return await this.db.insert(data); } - async update(data, condition){ + async update(data, condition) { return await this.db.update(data, condition); } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } // 获取列表带缓存 async getLinks(pid) { const link = await this.db.order('sort', 'asc').cache(600).select(); - return this.toTreeArray(link, pid); + return toTreeArray(link, pid); } // 友情链接 - async getFriends(){ + async getFriendLinks() { return await this.getLinks(1); } // 底部导航 - async getFlinks(rows){ + async getFootLinks(rows) { return await this.getLinks(2); } - - /** - * 按父子孙分级整理 - * @param list - * @param int pid - * @return array - */ - toTreeArray(list, pid=0){ - const arr = []; - list.forEach(v => { - if(v.pid == pid){ - v.child = this.toTreeArray(list, v.id); - arr.push(v); - } - }); - return arr; - } - - /** - * 按父子孙平级排列 - * @param list - * @param int pid - * @param int level - * @return array - */ - toTree(list, pid=0, level=0) { - let arr = []; - list.forEach(v => { - if(v.pid == pid){ - v.level = level + 1; - arr.push(v); - arr = arr.concat(this.toTree(list, v.id, level + 1)); - } - }); - return arr; - } } module.exports = Link; \ No newline at end of file diff --git a/admin/model/site.js b/admin/model/site.js index d5af72d..c578d69 100644 --- a/admin/model/site.js +++ b/admin/model/site.js @@ -1,29 +1,34 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); +const pjson = require('../../package.json'); -class Site extends Model { - async getList(condition, order='id', sort='asc'){ +class Site extends Model +{ + async getList(condition, order='id', sort='asc') { return await this.db.where(condition).order(order, sort).select(); } - async add(data){ + async add(data) { return await this.db.insert(data); } - async update(data, condition){ + async update(data, condition) { return await this.db.update(data, condition); } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } - async getAdminAlias(){ - return await this.db.cache(600).where({sname: 'admin_alias'}).value('value'); + async getAdminAlias() { + return await this.db.cache(600).where({kname: 'admin_alias'}).value('value'); } // 获取站点设置 - async getConfig(){ - return await this.db.cache(600).column('value', 'sname'); + async getConfig() { + const result = await this.db.cache(600).column('value', 'kname'); + result.VERSION = pjson.version; + result.APP_TIME = this.ctx.APP_TIME; + return result; } } diff --git a/admin/model/user.js b/admin/model/user.js index 1f6ea3a..4963089 100644 --- a/admin/model/user.js +++ b/admin/model/user.js @@ -1,25 +1,26 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class User extends Model { - async getList(condition, rows=10, order='id', sort='asc'){ +class User extends Model +{ + async getList(condition, rows=10, order='id', sort='asc') { return await this.db.where(condition).order(order, sort).limit(rows).select(); } - async getOne(condition){ + async getOne(condition) { return await this.db.where(condition).find(); } - async add(data){ + async add(data) { const user_data = {...data}; user_data.salt = this.$utils.randomString(8); user_data.password = this.passmd5(user_data.password, user_data.salt); - user_data.add_time = Math.round(new Date() / 1000); + user_data.add_time = this.$utils.time(); return await this.db.insert(user_data); } - async update(data, condition){ + async update(data, condition) { const user_data = {...data}; if(user_data.password) { @@ -28,20 +29,20 @@ class User extends Model { } else { delete user_data.password; } - user_data.update_time = Math.round(new Date() / 1000); + user_data.update_time = this.$utils.time(); return await this.db.update(user_data, condition); } - async delete(condition){ + async delete(condition) { return await this.db.delete(condition); } - async lock(id){ + async lock(id) { return await this.db.where({id}).inc('is_lock'); } - async login(email, password){ + async login(email, password) { const user = await this.getOne({email}); if(!user) { @@ -57,24 +58,24 @@ class User extends Model { return user.is_lock < -2 ? '账号或密码错误!' : '密码剩余次数:' + (1 - user.is_lock); } - await this.db.update({is_lock: -5, login_time: Math.round(new Date() / 1000)}, {id: user.id}); + await this.db.update({is_lock: -5, login_time: this.$utils.time()}, {id: user.id}); this.$service.cookie.set('user', user.id); return true; } - async logout(){ + async logout() { this.$service.cookie.delete('user'); return true; } - is_lock(user){ + is_lock(user) { return user.is_lock > 0; } // 加密密码 - passmd5(password, salt){ - const utils = this.$$utils; - return utils.md5(salt + utils.md5(salt + utils.md5(password + salt) + salt)); + passmd5(password, salt) { + const md5 = this.$utils.md5; + return md5(salt + md5(salt + md5(password + salt) + salt)); } } diff --git a/admin/service/cookie.js b/admin/service/cookie.js index 46a847e..9a12bae 100644 --- a/admin/service/cookie.js +++ b/admin/service/cookie.js @@ -1,11 +1,12 @@ -const {Cookie: LibCookie, utils} = require('iijs'); +const {Cookie: LibCookie} = require('jj.js'); let cookieEncode = ''; -class Cookie extends LibCookie { +class Cookie extends LibCookie +{ constructor(...args) { super(...args); cookieEncode || (cookieEncode = this.$utils.randomString(16)); - this.cookieEncode = this.ctx.$.config.cookie.cookieEncode || cookieEncode; + this.cookieEncode = this.$config.cookie.cookieEncode || cookieEncode; } set(key, value, options) { @@ -25,7 +26,7 @@ class Cookie extends LibCookie { } encode(value) { - return utils.md5(this.cookieEncode + value).substr(0, 16); + return this.$utils.md5(this.cookieEncode + value).substr(0, 16); } } diff --git a/admin/utils.js b/admin/utils.js index aa1cb11..f2ed405 100644 --- a/admin/utils.js +++ b/admin/utils.js @@ -1,3 +1,4 @@ +// 获取随机字符串 function randomString(len) {   len = len || 32;   var $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; @@ -9,12 +10,7 @@ function randomString(len) {   return pwd; } -/** - * 按父子孙分级整理 - * @param list - * @param int pid - * @return array - */ +// 按父子孙分级整理 function toTreeArray(list, pid=0){ const arr = []; list.forEach(v => { @@ -26,13 +22,7 @@ function toTreeArray(list, pid=0){ return arr; } -/** - * 按父子孙平级排列 - * @param list - * @param int pid - * @param int level - * @return array - */ +// 按父子孙平级排列 function toTree(list, pid=0, level=0) { let arr = []; list.forEach(v => { @@ -45,11 +35,7 @@ function toTree(list, pid=0, level=0) { return arr; } -/** - * @getIP - * @desc 获取用户 ip 地址 - * @param {Object} req - 请求 - */ +// 获取用户ip地址 function getIP(req) { return req.headers['x-forwarded-for'] || // 判断是否有反向代理 IP req.connection.remoteAddress || // 判断 connection 的远程 IP @@ -57,4 +43,10 @@ function getIP(req) { req.connection.socket.remoteAddress; } -module.exports = {randomString, toTreeArray, toTree, getIP} \ No newline at end of file +// md5 +const md5 = require('jj.js').utils.md5; + +// 获取时间戳 +const time = () => Math.round(new Date() / 1000); + +module.exports = {randomString, toTreeArray, toTree, getIP, md5, time} \ No newline at end of file diff --git a/admin/view/article_index.htm b/admin/view/article_index.htm index dd0dde7..9c45d77 100644 --- a/admin/view/article_index.htm +++ b/admin/view/article_index.htm @@ -5,7 +5,7 @@
- +
diff --git a/admin/view/comment_index.htm b/admin/view/comment_index.htm index 6a07736..9ce36c6 100644 --- a/admin/view/comment_index.htm +++ b/admin/view/comment_index.htm @@ -5,7 +5,7 @@
- +
diff --git a/admin/view/index_index.htm b/admin/view/index_index.htm index 8246c13..ad05274 100644 --- a/admin/view/index_index.htm +++ b/admin/view/index_index.htm @@ -16,7 +16,7 @@ text-align: center; border-radius: 2px; line-height: 22px; - font-size: 14px; + font-size: 16px; } {{/block}} diff --git a/admin/view/site_index.htm b/admin/view/site_index.htm index a216293..939f25e 100644 --- a/admin/view/site_index.htm +++ b/admin/view/site_index.htm @@ -25,9 +25,9 @@
{{if item.type == 'input'}} - + {{else}} - + {{/if}}
@@ -41,9 +41,9 @@
{{if item.type == 'input'}} - + {{else}} - + {{/if}}
@@ -57,9 +57,9 @@
{{if item.type == 'input'}} - + {{else}} - + {{/if}}
diff --git a/app/controller/article.js b/app/controller/article.js index 6452771..b3cacae 100644 --- a/app/controller/article.js +++ b/app/controller/article.js @@ -1,7 +1,8 @@ const Base = require('./base'); const md = require('markdown-it')(); -class Article extends Base { +class Article extends Base +{ async article() { const aid = parseInt(this.ctx.params.id); const model_article = this.$model.article; @@ -26,18 +27,18 @@ class Article extends Base { // markdown article.content = md.render(article.content); - this.assign('title', article.title + ' - ' + cate.cate_name + ' - ' + this.site.webname); - this.assign('description', article.description); - this.assign('keywords', article.keywords); + this.$assign('title', article.title + ' - ' + cate.cate_name + ' - ' + this.site.webname); + this.$assign('description', article.description); + this.$assign('keywords', article.keywords); - this.assign('cate', cate); - this.assign('article', article); - this.assign('prevOne', prevOne); - this.assign('nextOne', nextOne); + this.$assign('cate', cate); + this.$assign('article', article); + this.$assign('prevOne', prevOne); + this.$assign('nextOne', nextOne); - this.assign('is_comment', this.site.is_comment + article.is_comment >= 1); + this.$assign('is_comment', this.site.is_comment + article.is_comment >= 1); - await this.fetch(); + await this.$fetch(); } } diff --git a/app/controller/base.js b/app/controller/base.js index 4aa738c..5e6e556 100644 --- a/app/controller/base.js +++ b/app/controller/base.js @@ -1,7 +1,8 @@ -const {Controller} = require('iijs'); +const {Controller} = require('jj.js'); const pjson = require('../../package.json'); -class Base extends Controller { +class Base extends Controller +{ async _init() { const nav = await this.$model.cate.getCateList(); const model_article = this.$model.article; @@ -12,20 +13,20 @@ class Base extends Controller { model_article.getHot() ]); + // 站点配置 this.site = await this.$admin.model.site.getConfig(); - this.site.VERSION = pjson.version; - this.site.APP_TIME = this.ctx.APP_TIME; - this.assign('site', this.site); + this.$assign('site', this.site); - const flinks = await this.$admin.model.link.getFlinks(); - this.assign('flinks', flinks); + // 底部链接 + const foot_links = await this.$admin.model.link.getFootLinks(); + this.$assign('foot_links', foot_links); - 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('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); } } diff --git a/app/controller/cate.js b/app/controller/cate.js index 24b4627..cb933cd 100644 --- a/app/controller/cate.js +++ b/app/controller/cate.js @@ -1,19 +1,20 @@ const Base = require('./base'); -class Cate extends Base { +class Cate extends Base +{ async _init() { // admin跳过 if(this.ctx.APP == 'admin') { - await this.next(); - return 'exit'; + await this.$next(); + return false; } // 栏目不存在跳过 const CateArr = await this.$model.cate.getCateArr(); if(!~CateArr.indexOf(this.ctx.params.cate)) { this.ctx.params = {}; - await this.next(); - return 'exit'; + await this.$next(); + return false; } await super._init(); @@ -26,16 +27,16 @@ class Cate extends Base { const [total, list] = await this.$model.article.getPageList({cate_id: cate.id}); const pagination = total ? this.$pagination.cate.render(total) : ''; - this.assign('title', cate.cate_name + ' - ' + this.site.webname); - this.assign('description', cate.description); - this.assign('keywords', cate.keywords); + 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('cate', cate); + this.$assign('list', list); - this.assign('pagination', pagination); + this.$assign('pagination', pagination); - await this.fetch(); + await this.$fetch(); } } diff --git a/app/controller/comment.js b/app/controller/comment.js index 3dd5bc6..2b59825 100644 --- a/app/controller/comment.js +++ b/app/controller/comment.js @@ -1,6 +1,7 @@ -const {Controller} = require('iijs'); +const {Controller} = require('jj.js'); -class Comment extends Controller { +class Comment extends Controller +{ async _init() { this.site = await this.$admin.model.site.getConfig(); } @@ -10,66 +11,65 @@ class Comment extends Controller { const page = parseInt(this.ctx.query.page) || 1; const article = await this.$model.article.getArticle({id}, 'id,is_comment'); if(!article) { - return this.error('文章不存在或已删除!'); + return this.$error('文章不存在或已删除!'); } if(article.is_comment + this.site.is_comment < 1) { if(this.site.is_comment == 0) { - return this.error('系统已关闭评论功能!'); + return this.$error('系统已关闭评论功能!'); } else { - return this.error('本文已关闭评论功能!'); + return this.$error('本文已关闭评论功能!'); } } const list = await this.$admin.model.comment.getPageList(id, page); - this.success(this.$utils.toTreeArray(list).reverse()); + this.$success(this.$utils.toTreeArray(list).reverse()); } async post() { if(this.ctx.method != 'POST'){ - return this.error('非法请求!'); + return this.$error('非法请求!'); } const data = this.ctx.request.body; if(!data.uname) { - return this.error('昵称不能为空!'); + return this.$error('昵称不能为空!'); } if(!data.email) { - return this.error('邮箱不能为空!'); + return this.$error('邮箱不能为空!'); } if(!data.content) { - return this.error('评论内容不能为空!'); + return this.$error('评论内容不能为空!'); } data.article_id = parseInt(data.article_id); const article = await this.$model.article.getArticle({id: data.article_id}, 'id,is_comment'); if(!article) { - return this.error('文章不存在或已删除!'); + return this.$error('文章不存在或已删除!'); } if(article.is_comment + this.site.is_comment < 1) { if(this.site.is_comment == 0) { - return this.error('系统已关闭评论功能!'); + return this.$error('系统已关闭评论功能!'); } else { - return this.error('本文已关闭评论功能!'); + return this.$error('本文已关闭评论功能!'); } } + data.pid = parseInt(data.pid); if(data.pid) { const reply = await this.$admin.model.comment.getOne({id: data.pid}); if(!reply) { - return this.error('原评论不存在或已删除!'); + return this.$error('原评论不存在或已删除!'); } data.comment_id = reply.comment_id; } data.user_id = this.$service.cookie.get('user') || 0; - data.ip = this.$utils.getIP(this.ctx.req); - data.add_time = Math.round(new Date() / 1000); const result = await this.$admin.model.comment.add(data); if(result) { - this.success('评论成功!'); + this.$success('评论成功!'); } else { - this.error('评论失败!'); + this.$error('评论失败!'); } } } diff --git a/app/controller/index.js b/app/controller/index.js index 089ad39..7248557 100644 --- a/app/controller/index.js +++ b/app/controller/index.js @@ -1,13 +1,16 @@ const Base = require('./base'); -class Index extends Base { +class Index extends Base +{ async index() { + // 首页列表 const list = await this.$model.article.getIndexList(); - const friends = await this.$admin.model.link.getFriends(); + // 友情链接 + const friend_links = await this.$admin.model.link.getFriendLinks(); - this.assign('list', list); - this.assign('friends', friends); - await this.fetch(); + this.$assign('list', list); + this.$assign('friend_links', friend_links); + await this.$fetch(); } } diff --git a/app/model/article.js b/app/model/article.js index 89a2a4e..17dad40 100644 --- a/app/model/article.js +++ b/app/model/article.js @@ -1,8 +1,9 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class Article extends Model { +class Article extends Model +{ // 获取一篇文章 - async getArticle(condition, fields=''){ + async getArticle(condition, fields='') { return await this.db.field(fields).where(condition).find(); } @@ -12,7 +13,7 @@ class Article extends Model { } // 栏目文章列表及分页 - async getPageList(condition){ + async getPageList(condition) { const page = this.$pagination.cate.curPage; const pageSize = this.$pagination.cate.options.pageSize; const [total, list] = await Promise.all([ @@ -23,12 +24,12 @@ class Article extends Model { } // 最新文章 - async getNew(rows=8){ + async getNew(rows=8) { return await this.db.field('id,title,click').order('id', 'desc').limit(rows).cache(600).select(); } // 热点文章 - async getHot(rows=8){ + async getHot(rows=8) { return await this.db.field('id,title,click').order('click', 'desc').limit(rows).cache(600).select(); } diff --git a/app/model/cate.js b/app/model/cate.js index c3ee343..a3e2723 100644 --- a/app/model/cate.js +++ b/app/model/cate.js @@ -1,6 +1,7 @@ -const {Model} = require('iijs'); +const {Model} = require('jj.js'); -class Cate extends Model { +class Cate extends Model +{ // 获取一个分类 async getCate(condition) { return await this.db.where(condition).cache(600).find(); diff --git a/app/pagination/cate.js b/app/pagination/cate.js index 29b07ce..a5f640e 100644 --- a/app/pagination/cate.js +++ b/app/pagination/cate.js @@ -1,13 +1,14 @@ -const {Pagination} = require('iijs'); +const {Pagination} = require('jj.js'); -class Cate extends Pagination { +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}'}), + urlIndex: this.$url.build(':cate'), + urlPage: this.$url.build(':cate_page', {page: '${page}'}), ...opts }); return this; diff --git a/app/view/layout.htm b/app/view/layout.htm index fddbeb3..97dd8c2 100644 --- a/app/view/layout.htm +++ b/app/view/layout.htm @@ -26,8 +26,8 @@ {{block 'content'}}{{/block}}