Skip to content

Commit

Permalink
v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
www committed Aug 31, 2021
1 parent dd9748c commit 15a9f2c
Show file tree
Hide file tree
Showing 39 changed files with 584 additions and 608 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
51 changes: 23 additions & 28 deletions admin/controller/article.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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('删除失败!');
}
}
}
Expand Down
18 changes: 8 additions & 10 deletions admin/controller/base.js
Original file line number Diff line number Diff line change
@@ -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);
}
}

Expand Down
30 changes: 13 additions & 17 deletions admin/controller/cate.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
Expand All @@ -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('删除失败!');
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions admin/controller/comment.js
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Expand Down
21 changes: 11 additions & 10 deletions admin/controller/index.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
const Base = require('./base');

class Index extends Base {
class Index extends Base
{
async index() {
await this.fetch();
await this.$fetch();
}

async login() {
if(this.ctx.method == 'POST'){
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('注册功能未开放!');
}
}

Expand Down
36 changes: 16 additions & 20 deletions admin/controller/link.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
Expand All @@ -33,36 +34,31 @@ 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('保存失败!');
}
}
}

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('删除失败!');
}
}
}
Expand Down
Loading

0 comments on commit 15a9f2c

Please sign in to comment.