diff --git a/app.js b/app.js index 867cda2..1a3d0d5 100644 --- a/app.js +++ b/app.js @@ -5,12 +5,13 @@ var session = require('express-session'); var fs = require('fs'); var gm = require('gm'); +var db = require('./db'); var app = express(); var storage = multer.diskStorage({ destination: function (req, res, cb) { var fileType = req.params["fileType"]; - console.info(fileType); + //console.info(fileType); cb(null, 'uploads/' + fileType); } }); @@ -24,12 +25,12 @@ app.use(session({ resave: true, saveUninitialized: false, // 是否保存未初始化的会话 cookie: { - maxAge: 1000 * 60 * 2, // 设置 session 的有效时间,单位毫秒 + maxAge: 1000 * 60 * 60 * 24 * 30, // 设置 session 的有效时间,单位毫秒 } })); -//权限判断 -app.use(['/create', '/logout', '/home'], function (req, res, next) { +//权限判断:拦截器 +app.use(['/create', '/logout', '/home', '/saveScheme', '/getScheme'], function (req, res, next) { if (req.session.user) next(); else @@ -37,8 +38,7 @@ app.use(['/create', '/logout', '/home'], function (req, res, next) { }); //临时存储数据 -var data = { name: '默认方案', components: [] }; -var user = { account: 'tonkia', userName: 'tonkia xx', password: '123' }; +//var data = { name: '默认方案', components: [], img: null }; //主页加载:登录页面 app.get('/', function (req, res) { @@ -50,17 +50,23 @@ 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"); +}); + //登录 app.post('/', function (req, res) { var account = req.body.account; var password = req.body.password; - //用户验证 - if (account == user.account && password == user.password) { - req.session.user = user; - res.send('1'); - } else { - res.send('0'); - } + db.queryUserByAccount(account, function (user) { + //用户验证 + if (account == user.account && password == user.password) { + req.session.user = user; + res.send('1'); + } else { + res.send('0'); + } + }); }); //用户注销 @@ -74,9 +80,23 @@ app.get('/home', function (req, res) { res.sendFile(__dirname + "/views/home.html"); }); +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); + }); +}); + //接受文件上传,并且返回文件名 app.post('/upload/:fileType', upload.single('file'), function (req, res) { - console.info(req.file) + //console.info(req.file) res.send(req.file); }); @@ -107,13 +127,43 @@ app.get('/files/:fileType/:fileName', function (req, res) { //保存方案 app.post('/saveScheme', function (req, res) { - data = req.body; - console.info(data); + var data = req.body; + var img = data.img; + delete data.img; + db.insertScheme(req.session.user.id, data.name, JSON.stringify(data), img, function (res) { + //console.info(res); + if (res.affectedRows > 0) { + + } else { + + } + }); + // console.info(data); }); //获取方案 app.post('/getScheme', function (req, res) { + var data = { name: '默认方案', components: [], img: null }; res.send(data); }); +app.post('/getScheme/:id', function (req, res) { + var id = req.params['id']; + var user = req.session.user; + // console.info(user.id + ":" + id); + if (id) { + db.getSchemeById(user.id, id, function (row) { + if (row.length > 0) { + // console.info(row[0].data); + var data = JSON.parse(row[0].data); + res.send(data); + } else { + //找不到方案 + } + }) + } else { + + } +}); + app.listen(80); \ No newline at end of file diff --git a/db.js b/db.js new file mode 100644 index 0000000..f8f2218 --- /dev/null +++ b/db.js @@ -0,0 +1,86 @@ +var mysql = require('mysql'); + +var connection = mysql.createPool({ + host: '127.0.0.1',//主机 + user: 'root',//MySQL认证用户名 + password: '123456',//MySQL认证用户密码 + port: '3306',//端口号 + database: '3dmodel' +}); + +//根据用户名获取user +module.exports.queryUserByAccount = function (account, callback) { + //创建一个connection + connection.getConnection(function (err, conn) { + if (err) { + console.info(err); + } else { + conn.query('SELECT id,name,account,password FROM user WHERE account = ?', [account], function (err, rows) { + if (err) { + console.log(err); + return; + } + user = { + id: rows[0].id, + name: rows[0].name, + account: rows[0].account, + password: rows[0].password + } + callback(user); + conn.release(); + }); + + } + }); +}; + +module.exports.querySchemeByUid = function (uid, callback) { + connection.getConnection(function (err, conn) { + if (err) { + console.info(err); + } else { + conn.query('SELECT id,name,img FROM scheme WHERE uid = ?', [uid], function (err, rows) { + if (err) { + console.log(err); + return; + } + callback(rows); + conn.release(); + }); + } + }); +}; + +module.exports.insertScheme = function (uid, name, data, img, callback) { + connection.getConnection(function (err, conn) { + if (err) { + console.info(err); + } else { + conn.query('INSERT INTO scheme(uid,name,data,img) VALUES(?,?,?,?)', [uid, name, data, img], function (err, rows) { + if (err) { + console.log(err); + return; + } + callback(rows); + conn.release(); + }); + } + }); +}; + +module.exports.getSchemeById = function (uid, id, callback) { + connection.getConnection(function (err, conn) { + if (err) { + console.info(err); + } else { + conn.query('SELECT data FROM scheme WHERE uid=? AND id =?', [uid, id], function (err, rows) { + if (err) { + console.log(err); + return; + } + callback(rows); + conn.release(); + }); + } + }); +}; \ No newline at end of file diff --git a/static/js/createScheme.js b/static/js/createScheme.js index efd9e16..e072cd3 100644 --- a/static/js/createScheme.js +++ b/static/js/createScheme.js @@ -19,10 +19,13 @@ $().ready(function () { //--------------------------------------------------------------------------------------------------------------------- //获取数据 初始化data function initData() { + var url = document.location.toString(); + var arrUrl = url.split("create"); + var id = arrUrl[arrUrl.length - 1]; //ajax请求数据 $.ajax({ type: "POST", - url: "/getScheme", + url: "/getScheme" + id, async: false, //同步请求,不然data会出问题 success: function (remotaData) { //给全局data赋值 @@ -218,7 +221,7 @@ function initCamera() { function initRenderer() { //初始化渲染器 - renderer = new THREE.WebGLRenderer({ antialias: true }); + renderer = new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true }); //设置像素值 renderer.setPixelRatio(window.devicePixelRatio); //设置渲染范围为屏幕的大小 @@ -311,11 +314,24 @@ function initTexture() { //初始化所有事件 function initEvent() { $('#saveScheme').click(function () { + var saveData = data; + for (var index in saveData.components) { + for (textureIndex in saveData.components[index].textures) { + delete saveData.components[index].textures[textureIndex].textureObj; + } + for (modelIndex in saveData.components[index].models) { + delete saveData.components[index].models[modelIndex].modelObj; + } + } + var image = new Image(); + let imgData = renderer.domElement.toDataURL("image/jpeg");//这里可以选择png格式jpeg格式 + saveData.img = imgData; + console.info(saveData); $.ajax({ type: "POST", url: "/saveScheme", contentType: "application/json; charset=utf-8", - data: JSON.stringify(data) + data: JSON.stringify(saveData) }); }); diff --git a/views/createScheme.html b/views/createScheme.html index 294f0fb..0e7d480 100644 --- a/views/createScheme.html +++ b/views/createScheme.html @@ -3,7 +3,7 @@ - 创建方案 + 3D MODELS SYSTEM - CREATE diff --git a/views/home.html b/views/home.html index f0c150a..a0b39e0 100644 --- a/views/home.html +++ b/views/home.html @@ -5,21 +5,27 @@ - + 3D MODELS SYSTEM - HOME - -