You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
V3D-nodejs/app.js

200 lines
5.8 KiB

6 years ago
var express = require('express');
var multer = require('multer');
6 years ago
var bodyParser = require('body-parser');
6 years ago
var session = require('express-session');
6 years ago
var fs = require('fs');
var gm = require('gm');
6 years ago
var db = require('./db');
6 years ago
var app = express();
6 years ago
var storage = multer.diskStorage({
destination: function (req, res, cb) {
var fileType = req.params["fileType"];
//console.info(fileType);
6 years ago
cb(null, 'uploads/' + fileType);
}
});
var upload = multer({ storage: storage });
6 years ago
app.use(bodyParser.urlencoded({ extended: true }));
6 years ago
app.use(bodyParser.json({ limit: '50mb' }));
6 years ago
app.use(express.static('static'));
6 years ago
app.use(session({
secret: 'tonkia', // 对session id 相关的cookie 进行签名
resave: true,
saveUninitialized: false, // 是否保存未初始化的会话
cookie: {
maxAge: 1000 * 60 * 60 * 24 * 30, // 设置 session 的有效时间,单位毫秒
6 years ago
}
}));
//权限判断:拦截器
app.use(['/create', '/logout', '/home', '/saveScheme', '/getScheme'], function (req, res, next) {
6 years ago
if (req.session.user)
next();
else
res.redirect('/');
});
6 years ago
6 years ago
//临时存储数据
//var data = { name: '默认方案', components: [], img: null };
6 years ago
6 years ago
//主页加载:登录页面
6 years ago
app.get('/', function (req, res) {
6 years ago
res.sendFile(__dirname + "/views/login.html");
});
6 years ago
//方案创建
app.get('/create', function (req, res) {
res.sendFile(__dirname + "/views/createScheme.html");
});
app.get('/create/:id', function (req, res) {
res.sendFile(__dirname + "/views/createScheme.html");
});
6 years ago
//登录
app.post('/', function (req, res) {
var account = req.body.account;
var password = req.body.password;
db.queryUserByAccount(account, function (user) {
//用户验证
if (account == user.account && password == user.password) {
req.session.user = user;
res.send('1');
} else {
res.send('0');
}
});
6 years ago
});
6 years ago
//用户注销
app.get('/logout', function (req, res) {
req.session.user = null;
res.redirect('/');
});
//用户主页
6 years ago
app.get('/home', function (req, res) {
6 years ago
res.sendFile(__dirname + "/views/home.html");
6 years ago
});
app.post('/home', function (req, res) {
//初始化主页数据
//用户信息
var user = req.session.user;
db.querySchemeByUid(user.id, function (schemeList) {
delete user.password;
var homeData = {
user: user,
schemeList: schemeList
}
res.send(homeData);
});
});
6 years ago
//接受文件上传,并且返回文件名
6 years ago
app.post('/upload/:fileType', upload.single('file'), function (req, res) {
//console.info(req.file)
6 years ago
res.send(req.file);
});
6 years ago
//返回缩略图
app.get('/files/thumbnail/:filename', function (req, res) {
var filename = req.params['filename'];
6 years ago
// fs.exists(__dirname + "/uploads/thumbnail/" + filename, function (exist) {
// if (exist) {
// res.sendFile(__dirname + "/uploads/thumbnail/" + filename);
// } else {
// gm('./uploads/' + filename).resize(40, 40).write("./uploads/thumbnail/" + filename, function (err) {
// if (!err)
// console.info(err);
// res.sendFile(__dirname + "/uploads/thumbnail/" + filename);
// })
// }
// });
6 years ago
res.sendFile(__dirname + "/uploads/texture/" + filename);
});
//返回文件
app.get('/files/:fileType/:fileName', function (req, res) {
//TODO 根据session判断是否有权限获取文件
var fileType = req.params['fileType'];
var fileName = req.params['fileName'];
res.sendFile(__dirname + "/uploads/" + fileType + "/" + fileName);
6 years ago
});
6 years ago
//------------------------------------------------------------------------------------------------
6 years ago
//保存方案
app.post('/saveScheme', function (req, res) {
var data = req.body;
6 years ago
var id = data.id;
var img = data.img;
delete data.img;
6 years ago
//如果有id存在就update
if (id) {
db.updateScheme(req.session.user.id, id, data.name, JSON.stringify(data), img, function (row) {
if (row.affectedRows > 0) {
var returnData = {
affectedRows: row.affectedRows
}
res.send(returnData);
} else {
6 years ago
}
});
} else {
//如果id不存在就插入数据返回id
db.insertScheme(req.session.user.id, data.name, JSON.stringify(data), img, function (row) {
if (row.affectedRows > 0) {
var returnData = {
affectedRows: row.affectedRows,
insertId: row.insertId
}
res.send(returnData);
} else {
//插入失败
}
});
}
// console.info(data);
6 years ago
});
6 years ago
//------------------------------------------------------------------------------------------------
//获取初始方案
6 years ago
app.post('/getScheme', function (req, res) {
var data = { name: '默认方案', components: [], img: null, id: null, maxHeight: 10, height: 2, maxDistance: 30, distance: 10 };
replyData = {
msg: 0,
data: data
}
res.send(replyData);
6 years ago
});
6 years ago
6 years ago
//根据局ID获取初始方案
app.post('/getScheme/:id', function (req, res) {
var id = req.params['id'];
var user = req.session.user;
if (id) {
db.getSchemeById(user.id, id, function (row) {
if (row.length > 0) {
var data = JSON.parse(row[0].data);
6 years ago
data.id = id;
replyData = {
msg: 0,
data: data
}
res.send(replyData);
} else {
6 years ago
//TODO 找不到方案
replyData = {
msg: 1
}
res.send(replyData);
}
})
} else {
}
});
6 years ago
app.listen(80);