Skip to content

Commit

Permalink
实现管理页面中的图片菜单操作
Browse files Browse the repository at this point in the history
  • Loading branch information
layerssss committed Jan 23, 2013
1 parent 91d7425 commit f4a5d69
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,5 @@ pip-log.txt
.idea
node_modules
global.js
public/picmenu*.jpg

1 change: 1 addition & 0 deletions routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = function(app){
app.get('/admin/shop/edit/:id', user.auth_admin, admin.shop_edit);
app.post('/admin/shop/edit/:id', user.auth_admin, admin.shop_edit);
app.get('/admin/shop/picmenu/:id', user.auth_admin, admin.shop_picmenu);
app.all('/admin/shop/picmenu/:id/upload', user.auth_admin, admin.shop_picmenu_upload);
app.get('/admin/food/add',user.auth_admin, admin.food_add);
app.post('/admin/food/add',user.auth_admin, admin.food_add);
app.get('/admin/food/edit/:id',user.auth_admin, admin.food_edit);
Expand Down
25 changes: 22 additions & 3 deletions routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ exports.shop_add = function (req, res) {
db.shop.insert(shop, function (err, result) {
if (!err) {
console.log(result);
res.redirect('/admin/shop');
if(req.files.picmenu && req.files.picmenu.size){
fs.createReadStream(req.files.picmenu.path).pipe(fs.createWriteStream(path.join(__dirname, '..', 'public', 'picmenu' + result[0]._id + '.jpg'))).on('close', function(){
res.redirect('/admin/shop');
});
} else {
res.redirect('/admin/shop');
}
}
});
}
Expand Down Expand Up @@ -103,15 +109,28 @@ exports.shop_edit = function (req, res) {

exports.shop_picmenu = function (req, res) {
db.shop.findOne({"_id":db.ObjectID.createFromHexString(req.params.id)}, function (err, shop) {
fs.exists(path.join(__dirname, '..', 'picmenu' + shop.id), function (exists){
shop.picmenu = exists ? 'picmenu' + shop.id : '';
fs.exists(path.join(__dirname, '..', 'public', 'picmenu' + req.params.id + '.jpg'), function (exists){
shop.picmenu = exists ? '/picmenu' + req.params.id + '.jpg': '';
console.log(shop);
res.render('admin/shop/picmenu', {
"shop": shop
});
});
});
};

exports.shop_picmenu_upload = function(req, res){
if(req.files.picmenu && req.files.picmenu.size){
fs.createReadStream(req.files.picmenu.path).pipe(fs.createWriteStream(path.join(__dirname, '..', 'public', 'picmenu' + req.params.id + '.jpg'))).on('close', function(){
res.redirect('back');
});
} else {
fs.unlink(path.join(__dirname, '..', 'public', 'picmenu' + req.params.id + '.jpg'), function (err) {
res.redirect('back');
});
}
};

exports.food_add = function (req, res) {
if (req.method == 'GET') {
db.shop.findOne({'_id':db.ObjectID.createFromHexString(req.query['shop_id'])}, function (err, shop) {
Expand Down
1 change: 1 addition & 0 deletions views/admin/shop/add.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

<div class="controls">
<input type="file" name="picmenu" id="picmenu">
<small>(亲,我只认jpg格式的图片哦)</small>
</div>
</div>
<div class="control-group">
Expand Down
5 changes: 3 additions & 2 deletions views/admin/shop/picmenu.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
暂无图片菜单
<% } %></div>
<div class="actions">
<form method="post" action="replace" enctype="multipart/form-data">
<form method="post" action="upload" enctype="multipart/form-data">
<input type="file" name="picmenu" />
<button type="submit" class="btn btn-primary">更换图片菜单</button>
<small>(亲,我只认jpg格式的图片哦)</small>
<% if(shop.picmenu){ %>
<a class="btn btn-primary btn-danger" href="remove">删除图片菜单</a>
<a class="btn btn-primary btn-danger" href="upload">删除图片菜单</a>
<% } %>
<form>
</div>
Expand Down

0 comments on commit f4a5d69

Please sign in to comment.