Skip to content

@iwillwen /gist:3891616
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
var fs = require('fs');
var moment = require('moment');
var config = require('../../config');
var utils = require('../utils');
var eventproxy = require('eventproxy').EventProxy;
var asynclist = require('asynclist');
var loginedRequire = utils.loginedRequire;
module.exports = function(web) {
var upgradePage = fs.readFileSync(web.meta('views') + '/pages/upgrade.html');
var _ = web._;
var conn = web.meta('conn');
// 页面内容
var pages_root = web.meta('views') + '/pages';
var reg_sidebar = fs.readFileSync(pages_root + '/reg_sidebar.html');
var login_sidebar = fs.readFileSync(pages_root + '/login_sidebar.html');
var website = web.meta('website')();
// 注册
web.get('/reg', function(req, res) {
res.render('user/reg', {
website: website.website, // 网站信息
title: '用户注册',
sidebar: {
title: '已有帐户?',
content: reg_sidebar
},
local: {}
}, {
layout: 'layout_ad'
});
});
web.post('/reg', function(req, res, next) {
conn.query(
'SELECT id FROM users WHERE ' +
"email='" + req.body.useremail + "'" +
' OR ' +
"username='" + req.body.username + "';"
, function(err, _res) {
if (err) {
return next(err);
}
_res.fetchAll(function(_err, users) {
if (users.length > 0) {
return res.render('user/reg', {
website: website.website, // 网站信息
title: '用户注册',
error: '用户已存在',
sidebar: {
title: '已有帐户?',
content: reg_sidebar
},
local: {}
}, {
layout: 'layout_ad'
});
} else {
var sql = 'INSERT INTO users' +
'(email, username, password, childbirth, childname, address, sendtime, mobile, ' +
(req.body.tel !== '' ? ' telephone,' : '') + ' name, im, remark)' +
'VALUES' +
'(\'' + [req.body.useremail, req.body.username, utils.md5(req.body.userpassword), req.body.baochu1 + req.body.baochu2 + req.body.baochu3, req.body.baoxing, req.body.add1 + req.body.add2 + req.body.add3, req.body.sendtime, req.body.mob].join('\',\'') + '\', \'' + (req.body.tel ? req.body.tel + '\', \'' : '') + [req.body.xing, req.body.qq, req.body.beizhu].join('\',\'') + '\');';
conn.query(sql, function(err, _res) {
if (err) {
return res.sendError(503);
}
req.session.user = req.body.username;
conn.query(
"SELECT id " +
"FROM users " +
"WHERE email='" + req.body.useremail + "' LIMIT 1;",
function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(err, row) {
if (err)
return res.sendError(503);
req.session.user_id = row[0].id;
res.redirect('/');
});
});
});
}
});
});
});
web.get('/user/login', function(req, res) {
var view = {
website: website.website,
title: '用户登录',
sidebar: {
title: '还没有帐户?',
content: login_sidebar
},
local: {}
};
if (req.query.error) {
view.error = req.query.error;
}
res.render('user/login', view, {
layout: 'layout_ad'
});
});
web.post('/user/login', function(req, res, next) {
conn.query("SELECT password,username,id,vip,first FROM users WHERE email='" + req.body.useremail + "' LIMIT 1;", function(err, _res) {
if (err) {
return next(err);
}
_res.fetchAll(function(_err, users) {
if (err) {
return next(err);
}
if (users.length === 0) {
return res.redirect('/user/login?error=没有这个用户');
}
if (utils.md5(req.body.password) === users[0].password) {
req.session.user = users[0].username;
req.session.user_id = users[0].id;
req.session.vip = users[0].vip;
req.session.first = users[0].first;
res.redirect('/');
} else {
res.redirect('/user/login?error=密码错误');
}
});
});
});
web.get({
'/user/logout': _(loginedRequire, function(req, res) {
req.session.user = undefined;
res.redirect('/');
}),
'/user/cart': _(loginedRequire, function(req, res) {
var sql =
"SELECT " +
"a.id,a.cover,a.title,a.reserve " +
"FROM books AS a,shopping AS b " +
"WHERE b.user_id=" + req.session.user_id + " AND a.id=b.book_id AND b.fin=0;";
conn.query(sql, function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(_err, orderedBooks) {
if (_err)
res.sendError(503);
for (var i = 0; i < orderedBooks.length; i++) {
if (orderedBooks[i].reserve < 1) {
orderedBooks[i].booked = 1;
} else {
orderedBooks[i].booked = 0;
}
}
res.render('user/cart', {
website: website.website,
title: '借书架',
local: {
books: orderedBooks
}
}, {
layout: 'layout_simple'
});
});
});
}),
'/user/cart/delete/:book_id': _(loginedRequire, function(req, res) {
var sql =
"DELETE FROM shopping " +
"WHERE book_id=" + req.params.book_id + ";";
conn.query(sql, function(err) {
if (err) return res.sendError(503);
res.redirect('/user/cart');
});
}),
'/user/cart/finish': _(loginedRequire, function(req, res) {
var sql =
"SELECT a.book_id AS book_id, b.reserve AS reserve " +
"FROM shopping AS a, books AS b " +
"WHERE a.user_id=" + req.session.user_id + " AND b.id=a.book_id;";
conn.query(sql, function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(err, want) {
if (err)
return res.sendError(503);
conn.query(
"INSERT INTO orders " +
"(user_id, books_id) " +
"VALUES " +
"(" + req.session.user_id + ", '" + want.map(function(a){return a.book_id}).join(",") + "');",
function(err) {
if (err)
return res.sendError(503);
var proxy = new eventproxy();
proxy.assign('shopping', 'user', function(shopping, user) {
res.render('page', {
website: website.website,
title: '完成',
local: {
title: '完成',
content: '订单已提交'
}
}, {
layout: 'layout_simple'
});
});
conn.query(
"UPDATE shopping " +
"SET fin=1 " +
"WHERE user_id=" + req.session.user_id,
function(err) {
if (err)
return res.sendError(503);
proxy.trigger('shopping', true);
});
conn.query(
"SELECT vip " +
"FROM users " +
"WHERE id=" + req.session.user_id + ";",
function(err, resl) {
if (err)
return res.sendError(503);
resl.fetchAll(function(err, row) {
if (err)
return;
if (parseInt(row[0].vip) == 0) {
conn.query(
"UPDATE users SET first=1 WHERE id=" + req.session.user_id + ";",
function(err) {
if (err)
return res.sendError(503);
proxy.trigger('user', true);
});
} else {
proxy.trigger('user', true);
}
})
});
});
});
});
}),
'/user/favourite': _(loginedRequire, function(req, res) {
var sql =
"SELECT " +
"a.id,a.cover,a.title " +
"FROM books AS a,favourites AS b " +
"WHERE b.user_id=" + req.session.user_id + " AND a.id=b.book_id;";
conn.query(sql, function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(_err, favourite) {
if (_err)
res.sendError(503);
for (var i = 0; i < favourite.length; i++) {
if (favourite[i].reserve < 1) {
favourite[i].booked = 1;
} else {
favourite[i].booked = 0;
}
}
res.render('user/favourite', {
website: website.website,
title: '收藏单',
local: {
books: favourite
}
}, {
layout: 'layout_simple'
});
});
});
}),
'/user/favourite/delete/:id': _(loginedRequire, function(req, res) {
conn.query(
"DELETE FROM favourites " +
"WHERE book_id=" + req.params.id + " AND user_id=" + req.session.user_id + ";",
function(err) {
if (err)
return res.sendError(503);
res.redirect(req.headers.referer);
});
}),
'/user/history': _(loginedRequire, function(req, res) {
var sql =
"SELECT " +
"a.id,a.cover,a.title " +
"FROM books AS a,shopping AS b " +
"WHERE b.user_id=" + req.session.user_id + " AND a.id=b.book_id AND fin=1;";
conn.query(sql, function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(_err, orderedBooks) {
if (_err)
res.sendError(503);
for (var i = 0; i < orderedBooks.length; i++) {
if (orderedBooks[i].reserve < 1) {
orderedBooks[i].booked = 1;
} else {
orderedBooks[i].booked = 0;
}
}
res.render('user/cart', {
website: website.website,
title: '历史借阅单',
local: {
books: orderedBooks
}
}, {
layout: 'layout_simple'
});
});
});
}),
'/user/upgrade': _(loginedRequire, function(req, res) {
conn.query(
"SELECT vip_time,vip FROM users " +
"WHERE id=" + req.session.user_id + " LIMIT 1;",
function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(err, row) {
if (err)
return res.sendError(503);
if (row[0].vip == 0) {
res.render('page', {
website: website.website,
title: '账户升级',
local: {
title: '账户升级',
content: upgradePage.toString().replace("<%=vip_time%>", moment(row[0].vip_time).format('YYYY-MM-DD'))
}
}, {
layout: 'layout_simple'
});
} else {
res.render('page', {
website: website.website,
title: '账户升级',
local: {
title: '账户升级',
content: '<h2>您已是会员用户,无需升级!</h2><br><button onclick="history.back()">返回</button>'
}
}, {
layout: 'layout_simple'
});
}
});
});
}),
'/user/manage': _(loginedRequire, function(req, res) {
conn.query(
"SELECT * " +
"FROM users " +
"WHERE id=" + req.session.user_id + " " +
"LIMIT 1;",
function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(err, row) {
if (err)
return res.sendError(503);
res.render('user/manage', {
website: website.website,
title: '我的帐户',
local: {
user: row[0],
moment: moment
}
}, {
layout: 'layout_simple'
});
});
});
}),
'/user/edit': _(loginedRequire, function(req, res) {
conn.query(
"SELECT * " +
"FROM users " +
"WHERE id=" + req.session.user_id + " " +
"LIMIT 1;",
function(err, _res) {
if (err)
return res.sendError(503);
_res.fetchAll(function(err, row) {
if (err || row.length < 1)
return res.sendError(503);
res.render('user/edit', {
website: website.website, // 网站信息
title: '修改账户信息',
sidebar: {
title: '',
content: ''
},
local: {
currentUser: row[0]
}
}, {
layout: 'layout_ad'
});
});
});
})
});
web.post({
'/user/edit': _(loginedRequire, function(req, res) {
conn.query(
"UPDATE users " +
"SET " +
"email='" + req.body.useremail + "', username='" + req.body.username + "', password='" + utils.md5(req.body.userpassword) +
"', childbirth=" + req.body.baochu1 + req.body.baochu2 + req.body.baochu3 + ", childname='" + req.body.baoxing + "', address='" + req.body.add1 + req.body.add2 + req.body.add3 +
"', sendtime=" + req.body.sendtime + ", mobile='" + req.body.mob + "' " + (req.body.tel !== '' ? ", telephone='" + req.body.tel + "'" : '') + ", name='" + req.body.xing +
"', im='" + req.body.qq + "', remark='" + req.body.beizhu + "' WHERE id=" + req.session.user_id + ";",
function(err) {
if (err)
return res.sendError(503);
res.render('page', {
website: website.website,
title: '完成',
local: {
title: '完成',
content: '<h1>修改完成</h1>'
}
}, {
layout: 'layout_simple'
});
});
}),
'/user/upgrade': _(loginedRequire, function(req, res) {
var key = Math.random().toString(32).substr(2).toUpperCase();
var date = moment().add('M', parseInt(req.body.date)).format('YYYY-MM-DD');
conn.query(
"INSERT INTO viporders " +
"(user_id, date, order_key) " +
"VALUE " +
"(" + req.session.user_id + ", '" + date + "','" + key + "')",
function(err) {
if (err)
return res.sendError(503);
res.render('page', {
website: website.website,
title: '申请提交成功',
local: {
title: '申请提交成功',
content: [
'<p>申请提交成功,请尽快把相应款项转帐到¥¥¥。</p>',
'<br>',
'<p>订单标识号:' + key + '</p>'
].join('\r\n')
}
}, {
layout: 'layout_simple'
});
});
})
});
};
@AKIo0O

针对DB操作。写个抽象类来处理比较好。类似Java里的hibernate。
比如UserDAO。new UserDAO().save(user);new UserDAO().update(user); 获取userList = new UserDAO().getAll()

这样代码就好看多了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.