diff --git a/app.js b/app.js index 1a3d0d5..0a7c118 100644 --- a/app.js +++ b/app.js @@ -124,41 +124,60 @@ app.get('/files/:fileType/:fileName', function (req, res) { var fileName = req.params['fileName']; res.sendFile(__dirname + "/uploads/" + fileType + "/" + fileName); }); - +//------------------------------------------------------------------------------------------------ //保存方案 app.post('/saveScheme', function (req, res) { var data = req.body; + var id = data.id; 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 { + //如果有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 { - } - }); + } + }); + } 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); }); - -//获取方案 +//------------------------------------------------------------------------------------------------ +//获取初始方案 app.post('/getScheme', function (req, res) { - var data = { name: '默认方案', components: [], img: null }; + var data = { name: '默认方案', components: [], img: null, id: null }; res.send(data); }); +//根据局ID获取初始方案 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); + data.id = id; res.send(data); } else { - //找不到方案 + //TODO 找不到方案 } }) } else { diff --git a/db.js b/db.js index a6a4c5b..112c014 100644 --- a/db.js +++ b/db.js @@ -70,6 +70,25 @@ module.exports.insertScheme = function (uid, name, data, img, callback) { }); }; + +module.exports.updateScheme = function (uid, id, name, data, img, callback) { + connection.getConnection(function (err, conn) { + if (err) { + console.info(err); + } else { + conn.query('UPDATE scheme SET name=?,data=?,img=? WHERE uid=? AND id =?', [name, data, img, uid, id], 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) { @@ -85,4 +104,5 @@ module.exports.getSchemeById = function (uid, id, callback) { }); } }); -}; \ No newline at end of file +}; + diff --git a/static/img/webbg.jpg b/static/img/webbg.jpg index 51d7e70..18d83c0 100644 Binary files a/static/img/webbg.jpg and b/static/img/webbg.jpg differ diff --git a/static/js/createScheme.js b/static/js/createScheme.js index e072cd3..848a048 100644 --- a/static/js/createScheme.js +++ b/static/js/createScheme.js @@ -1,6 +1,11 @@ //全局变量 var renderer, scene, camera, controls; +//加载参数 var totalFinish = 0; +//相机参数 +var height = 2; +var distance = 10; +var angel = 0; //整个页面维护的数据 //var data = { name: '默认方案', components: [] }; //TODO 相机参数 @@ -208,15 +213,16 @@ function initScene() { } function initCamera() { + //相机设置 camera = new THREE.PerspectiveCamera(45, $('#viewField').innerWidth() / $('#viewField').innerHeight()); - camera.position.set(0, 5, 10); + camera.position.set(distance * Math.sin(angel), height, distance * Math.cos(angel)); //让相机对着场景中央 - camera.lookAt(scene.position); + camera.lookAt(0, height, 0); //相机控制,控制的相机和监听的dom - controls = new THREE.OrbitControls(camera, $('#viewField')[0]); - controls.target.set(0, 0, 0); - controls.update(); + // controls = new THREE.OrbitControls(camera, $('#viewField')[0]); + // controls.target.set(0, 0, 0); + // controls.update(); } function initRenderer() { @@ -314,7 +320,10 @@ function initTexture() { //初始化所有事件 function initEvent() { $('#saveScheme').click(function () { - var saveData = data; + //复制一个saveData + var saveData = {}; + $.extend(saveData, data); + //去除aveData中多余的数据 for (var index in saveData.components) { for (textureIndex in saveData.components[index].textures) { delete saveData.components[index].textures[textureIndex].textureObj; @@ -323,15 +332,32 @@ function initEvent() { 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(saveData) + data: JSON.stringify(saveData), + success: function (returnData) { + console.info(returnData); + if (returnData.affectedRows > 0) { + //获取保存后的id + if (returnData.insertId) + data.id = returnData.insertId; + new $.zui.Messager('保存成功!', { + type: 'success', + close: false, + actions: [{ + icon: 'times', + text: '确定' + }] + }).show(); + } + } }); }); diff --git a/views/createScheme.html b/views/createScheme.html index 0e7d480..59956a9 100644 --- a/views/createScheme.html +++ b/views/createScheme.html @@ -102,76 +102,79 @@ - -