优化异步流程并增加一些注释
This commit is contained in:
@@ -6,11 +6,11 @@
|
||||
"start": "node --harmony-proxies ./bin/www"
|
||||
},
|
||||
"dependencies": {
|
||||
"async": "^1.5.2",
|
||||
"body-parser": "~1.13.2",
|
||||
"connect-ensure-login": "^0.1.1",
|
||||
"cookie-parser": "~1.3.5",
|
||||
"debug": "~2.2.0",
|
||||
"eventproxy": "^0.3.4",
|
||||
"express": "~4.13.1",
|
||||
"express-session": "^1.13.0",
|
||||
"jade": "~1.11.0",
|
||||
|
||||
@@ -91,11 +91,11 @@ exports.getPosts = function (params, callback) {
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取首页的文章数
|
||||
* 获取首页文章的页数
|
||||
* @param params 参数对象
|
||||
* @param callback 回调函数
|
||||
*/
|
||||
exports.getPostsCount = function (params, callback) {
|
||||
exports.getPageCount = function (params, callback) {
|
||||
var cache_key = tool.generateKey('posts_count', params);
|
||||
redisClient.getItem(cache_key, function (err, pageCount) {
|
||||
if (err) {
|
||||
|
||||
320
routes/admin.js
320
routes/admin.js
@@ -2,7 +2,7 @@ var express = require('express');
|
||||
var router = express.Router();
|
||||
var path = require('path');
|
||||
var fs = require('fs');
|
||||
var eventproxy = require('eventproxy');
|
||||
var async = require('async');
|
||||
var upload = require('jquery-file-upload-middleware');
|
||||
var post = require('../proxy/post');
|
||||
var category = require('../proxy/category');
|
||||
@@ -12,11 +12,13 @@ var moment = require('moment');
|
||||
var shortid = require('shortid');
|
||||
var redisClient = require('../utility/redisClient');
|
||||
|
||||
//上传配置
|
||||
upload.configure({
|
||||
uploadDir: path.join(__dirname, '../public/images/'),
|
||||
uploadUrl: '/images'
|
||||
});
|
||||
|
||||
//网站统计页面
|
||||
router.get('/', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -30,6 +32,7 @@ router.get('/', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//分类管理页面
|
||||
router.get('/categorymanage', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -43,6 +46,7 @@ router.get('/categorymanage', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//获取分类数据,不含所有和未分类,不走缓存
|
||||
router.post('/getCategories', function (req, res, next) {
|
||||
category.getAll(false, false, function (err, data) {
|
||||
if (err) {
|
||||
@@ -53,6 +57,7 @@ router.post('/getCategories', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//保存分类数据
|
||||
router.post('/saveCategories', function (req, res, next) {
|
||||
var jsonArray = JSON.parse(req.body.json.substr(1, req.body.json.length - 2));
|
||||
category.save(jsonArray, function (err) {
|
||||
@@ -64,6 +69,7 @@ router.post('/saveCategories', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//文章管理页面
|
||||
router.get('/articlemanage', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -77,6 +83,7 @@ router.get('/articlemanage', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//获取分类数据,包含所有和未分类,不走缓存
|
||||
router.post('/getCateFilter', function (req, res, next) {
|
||||
category.getAll(true, false, function (err, data) {
|
||||
if (err) {
|
||||
@@ -87,9 +94,9 @@ router.post('/getCateFilter', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//获取文章列表数据
|
||||
router.post('/getArticles', function (req, res, next) {
|
||||
var ep = new eventproxy(),
|
||||
filter,
|
||||
var filter,
|
||||
params = {
|
||||
pageIndex: req.body.pageNumber,
|
||||
pageSize: req.body.pageSize,
|
||||
@@ -97,69 +104,83 @@ router.post('/getArticles', function (req, res, next) {
|
||||
sortOrder: req.body.sortOrder,
|
||||
searchText: req.body.searchText
|
||||
};
|
||||
|
||||
if (req.body.filter) {
|
||||
filter = JSON.parse(req.body.filter);
|
||||
params.cateId = filter.CateName;
|
||||
params.uniqueId = filter.UniqueId;
|
||||
params.title = filter.Title;
|
||||
}
|
||||
|
||||
ep.all('posts', 'count', 'categories', function (posts, count, categories) {
|
||||
var post,
|
||||
async.parallel([
|
||||
//获取文章列表
|
||||
function (cb) {
|
||||
post.getArticles(params, function (err, posts) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, posts);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取文章总数
|
||||
function (cb) {
|
||||
post.getArticlesCount(params, function (err, count) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, count);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取分类
|
||||
function (cb) {
|
||||
category.getAll(true, false, function (err, categories) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, categories);
|
||||
}
|
||||
});
|
||||
}
|
||||
], function (err, results) {
|
||||
var posts,
|
||||
count,
|
||||
categories,
|
||||
post,
|
||||
cateId,
|
||||
cateItem,
|
||||
result = [];
|
||||
posts.forEach(function (item) {
|
||||
post = {
|
||||
UniqueId: item._id,
|
||||
Alias: item.Alias,
|
||||
Title: item.Title,
|
||||
CreateTime: moment(item.CreateTime).format('YYYY-MM-DD HH:mm:ss'),
|
||||
ModifyTime: moment(item.ModifyTime).format('YYYY-MM-DD HH:mm:ss'),
|
||||
Summary: item.Summary,
|
||||
ViewCount: item.ViewCount,
|
||||
Source: item.Source,
|
||||
Url: item.Url,
|
||||
IsDraft: item.IsDraft,
|
||||
IsActive: item.IsActive
|
||||
};
|
||||
cateId = item.CategoryId;
|
||||
cateItem = tool.jsonQuery(categories, {"_id": cateId});
|
||||
if (cateItem) {
|
||||
post.CategoryAlias = cateItem.Alias;
|
||||
post.CateName = cateItem.CateName;
|
||||
}
|
||||
result.push(post);
|
||||
});
|
||||
|
||||
res.json({
|
||||
rows: result,
|
||||
total: count
|
||||
});
|
||||
});
|
||||
|
||||
post.getArticles(params, function (err, posts) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('posts', posts);
|
||||
}
|
||||
});
|
||||
|
||||
post.getArticlesCount(params, function (err, count) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('count', count);
|
||||
}
|
||||
});
|
||||
|
||||
category.getAll(true, false, function (err, categories) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('categories', categories);
|
||||
posts = results[0];
|
||||
count = results[1];
|
||||
categories = results[2];
|
||||
posts.forEach(function (item) {
|
||||
post = {
|
||||
UniqueId: item._id,
|
||||
Alias: item.Alias,
|
||||
Title: item.Title,
|
||||
CreateTime: moment(item.CreateTime).format('YYYY-MM-DD HH:mm:ss'),
|
||||
ModifyTime: moment(item.ModifyTime).format('YYYY-MM-DD HH:mm:ss'),
|
||||
Summary: item.Summary,
|
||||
ViewCount: item.ViewCount,
|
||||
Source: item.Source,
|
||||
Url: item.Url,
|
||||
IsDraft: item.IsDraft,
|
||||
IsActive: item.IsActive
|
||||
};
|
||||
cateId = item.CategoryId;
|
||||
cateItem = tool.jsonQuery(categories, {"_id": cateId});
|
||||
if (cateItem) {
|
||||
post.CategoryAlias = cateItem.Alias;
|
||||
post.CateName = cateItem.CateName;
|
||||
}
|
||||
result.push(post);
|
||||
});
|
||||
res.json({
|
||||
rows: result,
|
||||
total: count
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -221,32 +242,44 @@ router.get('/editArticle/:id', function (req, res, next) {
|
||||
if (!id) {
|
||||
res.redirect('/admin/articlemanage');
|
||||
}
|
||||
var ep = new eventproxy();
|
||||
ep.all('settings', 'article', function (settings, article) {
|
||||
res.render('admin/editarticle', {
|
||||
settings: settings,
|
||||
post: article,
|
||||
title: settings['SiteName'] + ' - 编辑文章'
|
||||
});
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
async.parallel([
|
||||
//获取分类
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, settings);
|
||||
}
|
||||
});
|
||||
},
|
||||
//根据文章Id获取文章
|
||||
function (cb) {
|
||||
post.getById(id, function (err, article) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else if (!article) {
|
||||
next();
|
||||
} else {
|
||||
cb(null, article);
|
||||
}
|
||||
})
|
||||
}
|
||||
], function (err, results) {
|
||||
var settings,
|
||||
article;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
settings = results[0];
|
||||
article = results[1];
|
||||
res.render('admin/editarticle', {
|
||||
settings: settings,
|
||||
post: article,
|
||||
title: settings['SiteName'] + ' - 编辑文章'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
post.getById(id, function (err, article) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else if (!article) {
|
||||
next();
|
||||
} else {
|
||||
ep.emit('article', article);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
//删除文章
|
||||
@@ -271,6 +304,7 @@ router.post('/undoArticle', function (req, res, next) {
|
||||
})
|
||||
});
|
||||
|
||||
//评论管理页面
|
||||
router.get('/comments', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -284,6 +318,7 @@ router.get('/comments', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//留言管理页面
|
||||
router.get('/guestbook', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -297,37 +332,52 @@ router.get('/guestbook', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//关于管理页面
|
||||
router.get('/aboutmanage', function (req, res, next) {
|
||||
var ep = new eventproxy();
|
||||
ep.all('settings', 'about', function (settings, about) {
|
||||
res.render('admin/aboutmanage', {
|
||||
title: settings['SiteName'] + ' - 关于管理',
|
||||
about: about,
|
||||
settings: settings
|
||||
});
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/about.json'), function (err, about) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('about', about);
|
||||
async.parallel([
|
||||
//获取关于数据
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/about.json'), function (err, about) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, about);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取配置
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, about);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
], function (err, results) {
|
||||
var settings,
|
||||
about;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
settings = results[0];
|
||||
about = results[1];
|
||||
res.render('admin/aboutmanage', {
|
||||
title: settings['SiteName'] + ' - 关于管理',
|
||||
about: about,
|
||||
settings: settings
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//上传图片
|
||||
router.post('/uploadimg', function (req, res, next) {
|
||||
upload.fileHandler()(req, res, next);
|
||||
});
|
||||
|
||||
//保存关于数据
|
||||
router.post('/saveAbout', function (req, res, next) {
|
||||
tool.setConfig(path.join(__dirname, '../config/about.json'), {
|
||||
FirstLine: req.body.FirstLine,
|
||||
@@ -342,6 +392,7 @@ router.post('/saveAbout', function (req, res, next) {
|
||||
res.end();
|
||||
});
|
||||
|
||||
//缓存管理页面
|
||||
router.get('/cachemanage', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -355,6 +406,7 @@ router.get('/cachemanage', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//根据缓存key获取缓存
|
||||
router.post('/getcache', function (req, res, next) {
|
||||
redisClient.getItem(req.body.key, function (err, data) {
|
||||
if (err) {
|
||||
@@ -369,6 +421,7 @@ router.post('/getcache', function (req, res, next) {
|
||||
})
|
||||
});
|
||||
|
||||
//清除指定key的缓存
|
||||
router.post('/clearcache', function (req, res, next) {
|
||||
redisClient.removeItem(req.body.key, function (err) {
|
||||
if (err) {
|
||||
@@ -379,6 +432,7 @@ router.post('/clearcache', function (req, res, next) {
|
||||
})
|
||||
});
|
||||
|
||||
//异常管理页面
|
||||
router.get('/exception', require('connect-ensure-login').ensureLoggedIn(), function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -392,48 +446,61 @@ router.get('/exception', require('connect-ensure-login').ensureLoggedIn(), funct
|
||||
});
|
||||
});
|
||||
|
||||
//获取异常数据
|
||||
router.post('/getExceptions', function (req, res, next) {
|
||||
var ep = new eventproxy(),
|
||||
params = {
|
||||
pageIndex: req.body.pageNumber,
|
||||
pageSize: req.body.pageSize,
|
||||
sortName: req.body.sortName,
|
||||
sortOrder: req.body.sortOrder
|
||||
};
|
||||
|
||||
ep.all('logs', 'count', function (logs, count) {
|
||||
var result = [];
|
||||
logs.forEach(function (item) {
|
||||
result.push({
|
||||
message: item.message,
|
||||
time: moment(item.timestamp).format('YYYY-MM-DD HH:mm:ss.SSS'),
|
||||
level: item.level,
|
||||
meta: item.meta
|
||||
var params = {
|
||||
pageIndex: req.body.pageNumber,
|
||||
pageSize: req.body.pageSize,
|
||||
sortName: req.body.sortName,
|
||||
sortOrder: req.body.sortOrder
|
||||
};
|
||||
async.parallel([
|
||||
//获取异常列表
|
||||
function (cb) {
|
||||
log.getAll(params, function (err, logs) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, logs);
|
||||
}
|
||||
});
|
||||
});
|
||||
res.json({
|
||||
rows: result,
|
||||
total: count
|
||||
});
|
||||
});
|
||||
|
||||
log.getAll(params, function (err, logs) {
|
||||
},
|
||||
//获取异常数据总数
|
||||
function (cb) {
|
||||
log.getAllCount(params, function (err, count) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, count);
|
||||
}
|
||||
})
|
||||
}
|
||||
], function (err, results) {
|
||||
var logs,
|
||||
count,
|
||||
result = [];
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('logs', logs);
|
||||
logs = results[0];
|
||||
count = results[1];
|
||||
logs.forEach(function (item) {
|
||||
result.push({
|
||||
message: item.message,
|
||||
time: moment(item.timestamp).format('YYYY-MM-DD HH:mm:ss.SSS'),
|
||||
level: item.level,
|
||||
meta: item.meta
|
||||
});
|
||||
});
|
||||
res.json({
|
||||
rows: result,
|
||||
total: count
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
log.getAllCount(params, function (err, count) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('count', count);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
//系统设置页面
|
||||
router.get('/settings', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -447,6 +514,7 @@ router.get('/settings', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//保存系统设置
|
||||
router.post('/saveSettings', function (req, res, next) {
|
||||
tool.setConfig(path.join(__dirname, '../config/settings.json'), {
|
||||
SiteName: req.body.SiteName,
|
||||
|
||||
@@ -8,14 +8,17 @@ var logger = require('../utility/logger');
|
||||
|
||||
passport.use(new Strategy(
|
||||
{
|
||||
usernameField: 'UserName',
|
||||
passwordField: 'Password'
|
||||
usernameField: 'UserName',//页面上的用户名字段的name属性值
|
||||
passwordField: 'Password'//页面上的密码字段的name属性值
|
||||
},
|
||||
function (username, password, cb) {
|
||||
var account = require('../config/account');
|
||||
//自己判断用户是否有效
|
||||
if (username === account.UserName && password === account.Password) {
|
||||
//验证通过
|
||||
return cb(null, account);
|
||||
} else {
|
||||
//验证失败
|
||||
return cb(null, false);
|
||||
}
|
||||
}));
|
||||
@@ -33,6 +36,7 @@ passport.deserializeUser(function (id, cb) {
|
||||
}
|
||||
});
|
||||
|
||||
//后台登录页面
|
||||
router.get('/login', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -46,6 +50,7 @@ router.get('/login', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//提交登录请求
|
||||
router.post('/login', function (req, res, next) {
|
||||
passport.authenticate('local', function (err, user, info) {
|
||||
if (err) {
|
||||
@@ -57,11 +62,13 @@ router.post('/login', function (req, res, next) {
|
||||
message: '用户名或密码错误!'
|
||||
});
|
||||
} else {
|
||||
//登录操作
|
||||
req.logIn(user, function (err) {
|
||||
var returnTo = '/admin';
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
//尝试跳转之前的页面
|
||||
if (req.session.returnTo) {
|
||||
returnTo = req.session.returnTo;
|
||||
}
|
||||
@@ -75,6 +82,7 @@ router.post('/login', function (req, res, next) {
|
||||
})(req, res, next);
|
||||
});
|
||||
|
||||
//退出登录
|
||||
router.post('/logout',
|
||||
function (req, res) {
|
||||
req.logout();
|
||||
|
||||
358
routes/blog.js
358
routes/blog.js
@@ -2,7 +2,7 @@ var express = require('express');
|
||||
var router = express.Router();
|
||||
var path = require('path');
|
||||
var util = require('util');
|
||||
var eventproxy = require('eventproxy');
|
||||
var async = require('async');
|
||||
var category = require('../proxy/category');
|
||||
var post = require('../proxy/post');
|
||||
var tool = require('../utility/tool');
|
||||
@@ -11,110 +11,165 @@ var url = require('url');
|
||||
|
||||
//分类页面
|
||||
router.get('/:category?', function (req, res, next) {
|
||||
var currentCate = req.params.category || '',
|
||||
ep = new eventproxy();
|
||||
|
||||
ep.all('settings', 'categories', function (settings, categories) {
|
||||
var cate = tool.jsonQuery(categories, {"Alias": currentCate});
|
||||
if (cate) {
|
||||
res.render('blog/index', {
|
||||
cateData: categories,
|
||||
settings: settings,
|
||||
title: settings['SiteName'],
|
||||
currentCate: currentCate,
|
||||
isRoot: false
|
||||
var currentCate = req.params.category || '';
|
||||
async.parallel([
|
||||
//获取配置
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, settings);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取分类
|
||||
function (cb) {
|
||||
category.getAll(function (err, categories) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, categories);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
], function (err, results) {
|
||||
var settings,
|
||||
categories,
|
||||
cate;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
}
|
||||
});
|
||||
|
||||
category.getAll(function (err, categories) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('categories', categories);
|
||||
settings = results[0];
|
||||
categories = results[1];
|
||||
cate = tool.jsonQuery(categories, {"Alias": currentCate});
|
||||
if (cate) {
|
||||
res.render('blog/index', {
|
||||
cateData: categories,
|
||||
settings: settings,
|
||||
title: settings['SiteName'],
|
||||
currentCate: currentCate,
|
||||
isRoot: false
|
||||
});
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
//获取文章数据
|
||||
router.post('/getPosts', function (req, res, next) {
|
||||
var ep = new eventproxy();
|
||||
ep.all('posts', 'pageCount', 'categories', function (posts, pageCount, categories) {
|
||||
var i = 0,
|
||||
len = posts.length,
|
||||
async.parallel([
|
||||
//获取文章列表和文章页数
|
||||
function (cb) {
|
||||
async.waterfall([
|
||||
//1. 根据分类alias获取分类对象
|
||||
function (cb) {
|
||||
category.getByAlias(req.body.CateAlias, function (err, category) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, category);
|
||||
}
|
||||
});
|
||||
},
|
||||
//2. 传入分类对象查询文章
|
||||
function (category, cb) {
|
||||
var params = {
|
||||
cateId: category._id,
|
||||
pageIndex: req.body.PageIndex,
|
||||
pageSize: req.body.PageSize,
|
||||
sortBy: req.body.SortBy,
|
||||
keyword: req.body.Keyword,
|
||||
filterType: req.body.FilterType
|
||||
};
|
||||
async.parallel([
|
||||
//文章列表
|
||||
function (cb) {
|
||||
post.getPosts(params, function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, data);
|
||||
}
|
||||
});
|
||||
},
|
||||
//文章页数
|
||||
function (cb) {
|
||||
post.getPageCount(params, function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, data);
|
||||
}
|
||||
});
|
||||
}
|
||||
], function (err, results) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, results);
|
||||
}
|
||||
});
|
||||
}
|
||||
], function (err, result) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, result);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取分类
|
||||
function (cb) {
|
||||
category.getAll(function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, data);
|
||||
}
|
||||
})
|
||||
}
|
||||
], function (err, results) {
|
||||
var posts,
|
||||
pageCount,
|
||||
categories,
|
||||
i,
|
||||
len,
|
||||
cateId,
|
||||
cateItem,
|
||||
result = [];
|
||||
for (; i < len; i++) {
|
||||
result[i] = {
|
||||
Source: posts[i].Source,
|
||||
Alias: posts[i].Alias,
|
||||
Title: posts[i].Title,
|
||||
Url: posts[i].Url,
|
||||
PublishDate: moment(posts[i].CreateTime).format('YYYY-MM-DD'),
|
||||
Host: posts[i].Url ? url.parse(posts[i].Url).host : '',
|
||||
Summary: posts[i].Summary,
|
||||
UniqueId: posts[i].UniqueId,
|
||||
ViewCount: posts[i].ViewCount
|
||||
};
|
||||
cateId = posts[i].CategoryId;
|
||||
cateItem = tool.jsonQuery(categories, {"_id": cateId});
|
||||
if (cateItem) {
|
||||
result[i].CategoryAlias = cateItem.Alias;
|
||||
result[i].CateName = cateItem.CateName;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
posts = results[0][0];
|
||||
pageCount = results[0][1];
|
||||
categories = results[1];
|
||||
i = 0;
|
||||
len = posts.length;
|
||||
for (; i < len; i++) {
|
||||
result[i] = {
|
||||
Source: posts[i].Source,
|
||||
Alias: posts[i].Alias,
|
||||
Title: posts[i].Title,
|
||||
Url: posts[i].Url,
|
||||
PublishDate: moment(posts[i].CreateTime).format('YYYY-MM-DD'),
|
||||
Host: posts[i].Url ? url.parse(posts[i].Url).host : '',
|
||||
Summary: posts[i].Summary,
|
||||
UniqueId: posts[i].UniqueId,
|
||||
ViewCount: posts[i].ViewCount
|
||||
};
|
||||
cateId = posts[i].CategoryId;
|
||||
cateItem = tool.jsonQuery(categories, {"_id": cateId});
|
||||
if (cateItem) {
|
||||
result[i].CategoryAlias = cateItem.Alias;
|
||||
result[i].CateName = cateItem.CateName;
|
||||
}
|
||||
}
|
||||
}
|
||||
res.send({posts: result, pageCount: pageCount});
|
||||
});
|
||||
|
||||
category.getByAlias(req.body.CateAlias, function (err, category) {
|
||||
var params;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
params = {
|
||||
cateId: category._id,
|
||||
pageIndex: req.body.PageIndex,
|
||||
pageSize: req.body.PageSize,
|
||||
sortBy: req.body.SortBy,
|
||||
keyword: req.body.Keyword,
|
||||
filterType: req.body.FilterType
|
||||
};
|
||||
post.getPosts(params, function (err, data) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('posts', data);
|
||||
}
|
||||
});
|
||||
|
||||
post.getPostsCount(params, function (err, data) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('pageCount', data);
|
||||
}
|
||||
});
|
||||
res.send({posts: result, pageCount: pageCount});
|
||||
}
|
||||
});
|
||||
|
||||
category.getAll(function (err, data) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('categories', data);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
//根据文章alias获取预览数据
|
||||
@@ -131,66 +186,83 @@ router.post('/getPreviewContent', function (req, res, next) {
|
||||
//文章详细页
|
||||
router.get('/:category/:article', function (req, res, next) {
|
||||
var alias = req.params.article,
|
||||
cateAlias = req.params.category,
|
||||
ep = new eventproxy();
|
||||
|
||||
ep.all('settings', 'article', 'categories', function (settings, article, categories) {
|
||||
var trueCateAlias = tool.jsonQuery(categories, {"_id": article.CategoryId}).Alias;
|
||||
if (cateAlias !== trueCateAlias) {
|
||||
res.redirect(util.format('/blog/%s/%s', trueCateAlias, alias));
|
||||
cateAlias = req.params.category;
|
||||
async.parallel([
|
||||
//获取配置
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, settings);
|
||||
}
|
||||
});
|
||||
},
|
||||
//根据文章alias获取文章对象
|
||||
function (cb) {
|
||||
post.getPostByAlias(alias, function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else if (data === null) {
|
||||
next();
|
||||
} else {
|
||||
cb(null, data);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取分类
|
||||
function (cb) {
|
||||
category.getAll(function (err, data) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, data);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
var labels = article.Labels,
|
||||
], function (err, results) {
|
||||
var settings,
|
||||
article,
|
||||
categories,
|
||||
trueCateAlias,
|
||||
labels,
|
||||
labelList = [];
|
||||
if (labels) {
|
||||
labels = JSON.parse(labels);
|
||||
labels.forEach(function (lbl) {
|
||||
labelList.push(lbl.text);
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
settings = results[0];
|
||||
article = results[1];
|
||||
categories = results[2];
|
||||
trueCateAlias = tool.jsonQuery(categories, {"_id": article.CategoryId}).Alias;
|
||||
if (cateAlias !== trueCateAlias) {
|
||||
res.redirect(util.format('/blog/%s/%s', trueCateAlias, alias));
|
||||
}
|
||||
|
||||
labels = article.Labels;
|
||||
if (labels) {
|
||||
labels = JSON.parse(labels);
|
||||
labels.forEach(function (lbl) {
|
||||
labelList.push(lbl.text);
|
||||
});
|
||||
}
|
||||
var post = {
|
||||
UniqueId: article.UniqueId,
|
||||
Title: article.Title,
|
||||
CategoryAlias: cateAlias,
|
||||
CateName: tool.jsonQuery(categories, {"_id": article.CategoryId}).CateName,
|
||||
CreateTimeStr: moment(article.CreateTime).format('YYYY-MM-DD hh:mm'),
|
||||
ViewCount: article.ViewCount,
|
||||
LabelList: labelList,
|
||||
Summary: article.Summary,
|
||||
Content: article.Content
|
||||
};
|
||||
res.render('blog/article', {
|
||||
post: post,
|
||||
settings: settings,
|
||||
title: settings['SiteName'] + ' - ' + article.Title
|
||||
});
|
||||
}
|
||||
var post = {
|
||||
UniqueId: article.UniqueId,
|
||||
Title: article.Title,
|
||||
CategoryAlias: cateAlias,
|
||||
CateName: tool.jsonQuery(categories, {"_id": article.CategoryId}).CateName,
|
||||
CreateTimeStr: moment(article.CreateTime).format('YYYY-MM-DD hh:mm'),
|
||||
ViewCount: article.ViewCount,
|
||||
LabelList: labelList,
|
||||
Summary: article.Summary,
|
||||
Content: article.Content
|
||||
};
|
||||
res.render('blog/article', {
|
||||
post: post,
|
||||
settings: settings,
|
||||
title: settings['SiteName'] + ' - ' + article.Title
|
||||
});
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
}
|
||||
});
|
||||
|
||||
post.getPostByAlias(alias, function (err, data) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else if (data === null) {
|
||||
next();
|
||||
} else {
|
||||
ep.emit('article', data);
|
||||
}
|
||||
});
|
||||
|
||||
category.getAll(function (err, data) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('categories', data);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -1,36 +1,47 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
var path = require('path');
|
||||
var eventproxy = require('eventproxy');
|
||||
var async = require('async');
|
||||
var category = require('../proxy/category');
|
||||
var tool = require('../utility/tool');
|
||||
|
||||
router.get('/', function (req, res, next) {
|
||||
var ep = new eventproxy();
|
||||
|
||||
ep.all('settings', 'categories', function (settings, categories) {
|
||||
res.render('blog/index', {
|
||||
cateData: categories,
|
||||
settings: settings,
|
||||
title: settings['SiteName'],
|
||||
currentCate: '',
|
||||
isRoot: true
|
||||
});
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
async.parallel([
|
||||
//获取配置
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, settings);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取分类
|
||||
function (cb) {
|
||||
category.getAll(function (err, categories) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, categories);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
category.getAll(function (err, categories) {
|
||||
], function (err, results) {
|
||||
var settings,
|
||||
categories;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('categories', categories);
|
||||
settings = results[0];
|
||||
categories = results[1];
|
||||
res.render('blog/index', {
|
||||
cateData: categories,
|
||||
settings: settings,
|
||||
title: settings['SiteName'],
|
||||
currentCate: '',
|
||||
isRoot: true
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
var express = require('express');
|
||||
var router = express.Router();
|
||||
var path = require('path');
|
||||
var eventproxy = require('eventproxy');
|
||||
var async = require('async');
|
||||
var tool = require('../utility/tool');
|
||||
|
||||
//留言页面
|
||||
router.get('/guestbook', function (req, res, next) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -17,29 +18,42 @@ router.get('/guestbook', function (req, res, next) {
|
||||
});
|
||||
});
|
||||
|
||||
//关于页面
|
||||
router.get('/about', function (req, res, next) {
|
||||
var ep = new eventproxy();
|
||||
ep.all('settings', 'about', function (settings, about) {
|
||||
res.render('misc/about', {
|
||||
title: settings['SiteName'] + ' - 关于',
|
||||
about: about,
|
||||
settings: settings
|
||||
});
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/about.json'), function (err, about) {
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('about', about);
|
||||
async.parallel([
|
||||
//获取关于数据
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/about.json'), function (err, about) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, about);
|
||||
}
|
||||
});
|
||||
},
|
||||
//获取配置
|
||||
function (cb) {
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
if (err) {
|
||||
cb(err);
|
||||
} else {
|
||||
cb(null, settings);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
tool.getConfig(path.join(__dirname, '../config/settings.json'), function (err, settings) {
|
||||
], function (err, results) {
|
||||
var about,
|
||||
settings;
|
||||
if (err) {
|
||||
next(err);
|
||||
} else {
|
||||
ep.emit('settings', settings);
|
||||
about = results[0];
|
||||
settings = results[1];
|
||||
res.render('misc/about', {
|
||||
title: settings['SiteName'] + ' - 关于',
|
||||
about: about,
|
||||
settings: settings
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
10
routes/ue.js
10
routes/ue.js
@@ -5,6 +5,8 @@ var path = require('path');
|
||||
var tool = require('../utility/tool');
|
||||
var multer = require('multer');
|
||||
var shortid = require('shortid');
|
||||
|
||||
//图片上传配置
|
||||
var storageImg = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
if (req.query.uniqueId) {
|
||||
@@ -34,6 +36,8 @@ var storageImg = multer.diskStorage({
|
||||
cb(null, fullName)
|
||||
}
|
||||
});
|
||||
|
||||
//附件上传配置
|
||||
var storageFile = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
if (req.query.uniqueId) {
|
||||
@@ -73,6 +77,7 @@ router.get('/', function (req, res, next) {
|
||||
files,
|
||||
stat;
|
||||
switch (req.query.action) {
|
||||
//获取配置
|
||||
case 'config':
|
||||
tool.getConfig(path.join(__dirname, '../config/ue.json'), function (err, settings) {
|
||||
if (err) {
|
||||
@@ -82,6 +87,7 @@ router.get('/', function (req, res, next) {
|
||||
}
|
||||
});
|
||||
break;
|
||||
//图片管理
|
||||
case 'listimage':
|
||||
rootFiles = fs.readdirSync(rootPath);
|
||||
rootFiles.forEach(function (rootFile) {
|
||||
@@ -109,6 +115,7 @@ router.get('/', function (req, res, next) {
|
||||
size: parseInt(req.query.size)
|
||||
});
|
||||
break;
|
||||
//附件管理
|
||||
case 'listfile':
|
||||
rootFiles = fs.readdirSync(rootPath);
|
||||
rootFiles.forEach(function (rootFile) {
|
||||
@@ -142,6 +149,7 @@ router.get('/', function (req, res, next) {
|
||||
router.post('/', function (req, res, next) {
|
||||
var uploadFile;
|
||||
switch (req.query.action) {
|
||||
//上传图片
|
||||
case 'uploadimage':
|
||||
uploadFile = multer({storage: storageImg}).single('upfile');
|
||||
uploadFile(req, res, function (err) {
|
||||
@@ -158,6 +166,7 @@ router.post('/', function (req, res, next) {
|
||||
}
|
||||
});
|
||||
break;
|
||||
//上传涂鸦
|
||||
case 'uploadscrawl':
|
||||
var dataBuffer = new Buffer(req.body.upfile, 'base64'),
|
||||
fileName = shortid.generate() + '.png';
|
||||
@@ -193,6 +202,7 @@ router.post('/', function (req, res, next) {
|
||||
next(new Error('参数uniqueId不存在!'));
|
||||
}
|
||||
break;
|
||||
//上传附件
|
||||
case 'uploadfile':
|
||||
uploadFile = multer({storage: storageFile}).single('upfile');
|
||||
uploadFile(req, res, function (err) {
|
||||
|
||||
Reference in New Issue
Block a user