diff --git a/node_modules/api.js b/node_modules/api.js index 9150ebe1..838c2fca 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -14,6 +14,9 @@ var Api = { baseUrl: 'http://localhost:9000/api', getUrl: function(url, param) { var url = this.baseUrl + '/' + url; + var token = User.getToken(); + param = param || {}; + param.token = token; if(param) { var paramStr = ''; for(var i in param) { @@ -25,19 +28,6 @@ var Api = { } return url + '?' + paramStr; }, - isOk: function(ret) { - // 数组 - if(length in ret) { - return true; - } - if(ret == 'object') { - if(!ret.Ok) { // 指明了Ok - return false; - } - return true; - } - return false; - }, // 登录 auth: function(email, pwd, callback) { var me = this; @@ -47,7 +37,7 @@ var Api = { var ret = response.body; // 登录成功, 保存token log(ret); - if(me.isOk(ret)) { + if(Common.isOk(ret)) { ret.Pwd = pwd; User.setCurUser(ret); callback && callback(ret); @@ -94,9 +84,10 @@ var Api = { }, getSyncNotebooks: function(afterUsn, maxEntry, callback) { var me = this; - needle.get(this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) { + var url = this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry}); + needle.get(url, function(error, response) { var ret = response.body; - if(me.isOk(ret)) { + if(Common.isOk(ret)) { callback && callback(ret); } else { callback && callback(false); @@ -105,9 +96,11 @@ var Api = { }, getSyncNotes: function(afterUsn, maxEntry, callback) { var me = this; - needle.get(this.getUrl('note/getSyncNotes', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) { + var url = this.getUrl('note/getSyncNotes', {afterUsn: afterUsn, maxEntry: maxEntry}); + log(url); + needle.get(url, function(error, response) { var ret = response.body; - if(me.isOk(ret)) { + if(Common.isOk(ret)) { callback && callback(ret); } else { callback && callback(false); diff --git a/node_modules/common.js b/node_modules/common.js index 20335d8d..75ba0a9c 100644 --- a/node_modules/common.js +++ b/node_modules/common.js @@ -18,6 +18,22 @@ var Common = { this._uuid++; return ((new Date()).getTime()) + '_' + this._uuid; }, + isOk: function(ret) { + if(!ret) { + return ret; + } + // 数组 + if('length' in ret) { + return true; + } + if(ret == 'object') { + if(!ret.Ok) { // 指明了Ok + return false; + } + return true; + } + return false; + }, // FileReaderWeb 是 web上的FileReader, 可能与nodejs这个有冲突 pasteImage: function(event, FileReaderWeb, callback) { var me = this; diff --git a/node_modules/note.js b/node_modules/note.js index 70e5f85f..db8b2a99 100644 --- a/node_modules/note.js +++ b/node_modules/note.js @@ -117,6 +117,59 @@ var Note = { return callback && callback(notes); }); }, + + // 得到笔记 + getNote: function(noteId, callback) { + var me = this; + Notes.findOne({NoteId: noteId}, function(err, doc) { + if(err || !doc) { + log('不存在'); + callback && callback(false); + } else { + callback && callback(doc); + } + }); + }, + + //---------------- + // 同步 + //---------------- + + // 强制删除 + deleteNoteForce: function(noteId, callback) { + var me = this; + Notes.remove({NoteId: noteId}, function(err, n) { + if(err) { + callback && callback(false); + } else { + callback && callback(true); + } + }); + }, + // 添加笔记本, note object + addNoteForce: function(note, callback) { + note.InitSync = true; // 刚同步完, 表示content, images, attach没有同步 + Notes.insert(note, function (err, newDoc) { // Callback is optional + if(err) { + console.log(err); + callback && callback(false); + } else { + callback && callback(newDoc); + } + }); + }, + // 更新笔记本 + updateNoteForce: function(note, callback) { + note.IsDirty = false; + Notes.update({NoteId: note.NoteId}, {$set: note}, {}, function (err, updates) { // Callback is optional + if(err) { + console.log(err); + callback && callback(false); + } else { + callback && callback(note); + } + }); + }, }; module.exports = Note; \ No newline at end of file diff --git a/node_modules/notebook.js b/node_modules/notebook.js index 1ceafc64..e6f86d43 100644 --- a/node_modules/notebook.js +++ b/node_modules/notebook.js @@ -106,7 +106,8 @@ var Notebook = { Title: title, Seq: -1, UserId: User.getCurActiveUserId(), - ParentNotebookId: parentNotebookId + ParentNotebookId: parentNotebookId, + IsDirty: true, // 必须, 同步后才为非dirty状态 // TODO UrlTitle } if(notebookId) { @@ -134,6 +135,58 @@ var Notebook = { log(count); NB.update({NotebookId: notebookId}, {$set: {NumberNotes: count}}, {}) }); - } + }, + + // 得到笔记本 + getNotebook: function(notebookId, callback) { + var me = this; + NB.findOne({NotebookId: notebookId}, function(err, doc) { + if(err || !doc) { + log('不存在'); + callback && callback(false); + } else { + callback && callback(doc); + } + }); + }, + + //---------------- + // 同步 + //---------------- + + // 强制删除 + deleteNotebookForce: function(notebookId, callback) { + var me = this; + NB.remove({NotebookId: notebookId}, function(err, n) { + if(err) { + callback && callback(false); + } else { + callback && callback(true); + } + }); + }, + // 添加笔记本, notebook object + addNotebookForce: function(notebook, callback) { + NB.insert(notebook, function (err, newDoc) { // Callback is optional + if(err) { + console.log(err); + callback && callback(false); + } else { + callback && callback(newDoc); + } + }); + }, + // 更新笔记本 + updateNotebookForce: function(notebook, callback) { + notebook.IsDirty = false; + NB.update({NotebookId: notebook.NotebookId}, {$set: notebook}, {}, function (err, updates) { // Callback is optional + if(err) { + console.log(err); + callback && callback(false); + } else { + callback && callback(notebook); + } + }); + }, }; module.exports = Notebook; \ No newline at end of file diff --git a/node_modules/sync.js b/node_modules/sync.js index 0ecc8578..b375a93c 100644 --- a/node_modules/sync.js +++ b/node_modules/sync.js @@ -6,6 +6,7 @@ var needle = require('needle'); var fs = require('fs'); var Api = require('api'); var Notebook = require('notebook'); +var Note = require('note'); function log(o) { console.log(o); @@ -19,32 +20,51 @@ var Sync = { note: {adds: [], deletes: [], updates: [], conflicts: []}, tag: {} }, + + // notebook _syncNotebookIsLastChunk: false, _totalSyncNotebookNum: 0, // 需要同步的数量 _tocalHasSyncNotebookNum: 0, // 已同步的数量 - _notebookMaxEntry: 1000, + _notebookMaxEntry: 1, + + // note + _syncNoteIsLastChunk: false, + _totalSyncNoteNum: 0, // 需要同步的数量 + _noteMaxEntry: 1, _initSyncInfo: function() { var me = this; + // notebook me._syncNotebookIsLastChunk = false; me._totalSyncNotebookNum = 0; - me._tocalHasSyncNotebookNum = 0; + me._totalHasSyncNotebookNum = 0; me._lockNotebook = 1; + // note + me._syncNoteIsLastChunk = false; + me._totalSyncNoteNum = 0; + me._totalHasSyncNoteNum = 0; + me._lockNote = 1; + me._syncInfo = { - notebook: {adds: [], deletes: [], updates: []}, - note: {adds: [], deletes: [], updates: [], conflicts: []}, - tag: {} + notebook: {ok: false, adds: [], deletes: [], updates: []}, + note: {ok: false, adds: [], deletes: [], updates: [], conflicts: []}, + tag: {ok: false} }; }, + //--------------- + // notebook + //--------------- + // 增加, 有锁 _lockNotebook: 1, _addSyncNotebookNum: function() { - if(me._lock) { + var me = this; + if(me._lockNotebook) { me._lockNotebook = 0; - me._tocalHasSyncNotebookNum++; + me._totalHasSyncNotebookNum++; me._lockNotebook = 1; } else { me._addSyncNotebookNum(); @@ -53,13 +73,17 @@ var Sync = { // 同步笔记本 _syncNotebookToLocal: function(notebooks, callback) { + var me = this; function canCall() { // 是最后一块, 且 me._addSyncNotebookNum(); - if(me._syncNotebookIsLastChunk && me._tocalHasSyncNotebookNum == me._tocalSyncNotebookNum) { + log(me._totalHasSyncNotebookNum + ' ' + me._totalSyncNotebookNum); + if(me._syncNotebookIsLastChunk && me._totalHasSyncNotebookNum >= me._totalSyncNotebookNum) { + me._syncInfo.notebook.ok = true; callback && callback(true); } } + if(!notebooks || notebooks.length == 0) { return canCall(); } @@ -68,56 +92,65 @@ var Sync = { var notebook = notebooks[i]; // 得到本地的, 与之对比 - var usn = notebook.Usn; - var notebookId = notebook.NotebookId; + (function(notebook) { - // 1) 服务器端删除了, 本地肯定删除 - if(!notebook.CreatedTime) { - // TODO - Notebook.deleteNotebookForce(notebookId, function() { - // TODO 添加到信息 - canCall(); - }); - return; - } - // 2) 查看本地的, 与本地合并 - Notebook.GetNotebook(notebookId, function(notebookLocal) { - // 2.1 本地没有, 表示是新建 - if(!notebookLocal) { - // TODO - Notebook.addNotebookForce(notebook, function() { - // TODO 添加到信息 + var usn = notebook.Usn; + var notebookId = notebook.NotebookId; + + // 1) 服务器端删除了, 本地肯定删除 + if(notebook.IsDeleted) { + log('delete: '); + log(notebook); + Notebook.deleteNotebookForce(notebookId, function() { + me._syncInfo.notebook.deletes.push(notebookId); canCall(); }); - } else { - // 2.2 本地是否修改了, 需要合并, 使用服务端的数据 - if(notebook.isDirty) { - // 2.3 服务器是最新的, 用服务器的 - } else { - } - // 这里都是用服务器端的数据, 不处理冲突 - notebook.UpdateNotebookForce(notebookId, notebook.Title, notebook.ParentNotebookId, function() { - // TODO 添加到信息 - canCall(); - }) + return; } - }); - - callback && callback(true); + // 2) 查看本地的, 与本地合并 + Notebook.getNotebook(notebookId, function(notebookLocal) { + // 2.1 本地没有, 表示是新建 + if(!notebookLocal) { + log('add: ...') + // TODO + Notebook.addNotebookForce(notebook, function(notebook) { + me._syncInfo.notebook.adds.push(notebook); + canCall(); + }); + } else { + // 2.2 本地是否修改了, 需要合并, 使用服务端的数据 + if(notebook.IsDirty) { + log('冲突....') + // 2.3 服务器是最新的, 用服务器的 + } else { + } + // 这里都是用服务器端的数据, 不处理冲突 + Notebook.updateNotebookForce(notebook, function(notebook) { + if(notebook) { + me._syncInfo.notebook.updates.push(notebook); + } + canCall(); + }) + } + }); + })(notebook); } }, syncNotebook: function(afterUsn, callback) { var me = this; Api.getSyncNotebooks(afterUsn, me._notebookMaxEntry, function(notebooks) { - if(notebooks) { - me._tocalSyncNotebookNum += notebooks.length; + log('syncNotebook') + log(notebooks); + if(Common.isOk(notebooks)) { + me._totalSyncNotebookNum += notebooks.length; // 证明可能还有要同步的 if(notebooks.length == me._notebookMaxEntry) { me._syncNotebookToLocal(notebooks); var last = notebooks[notebooks.length-1]; me.syncNotebook(last.Usn, callback); } else { + log('no more'); me._syncNotebookIsLastChunk = true; me._syncNotebookToLocal(notebooks, callback); } @@ -130,10 +163,117 @@ var Sync = { }); }, - // 同步笔记 - syncNote: function(afterUsn, callback) { - callback && callback(true); + + //------------- + // note + //------------- + + // 增加, 有锁 + _lockNote: 1, + _addSyncNoteNum: function() { + var me = this; + if(me._lockNote) { + me._lockNote = 0; + me._totalHasSyncNoteNum++; + me._lockNote = 1; + } else { + me._addSyncNoteNum(); + } }, + + // 同步笔记本 + _syncNoteToLocal: function(notes, callback) { + var me = this; + function canCall() { + // 是最后一块, 且 + me._addSyncNoteNum(); + log(me._totalHasSyncNoteNum + ' ' + me._totalSyncNoteNum); + if(me._syncNoteIsLastChunk && me._totalHasSyncNoteNum >= me._totalSyncNoteNum) { + me._syncInfo.note.ok = true; + callback && callback(true); + } + } + + if(!notes || notes.length == 0) { + return canCall(); + } + + for(var i in notes) { + var note = notes[i]; + // 得到本地的, 与之对比 + + (function(note) { + + var usn = note.Usn; + var noteId = note.NoteId; + + // 1) 服务器端删除了, 本地肯定删除 + if(note.IsDeleted) { + log('delete: '); + log(note); + Note.deleteNoteForce(noteId, function() { + me._syncInfo.note.deletes.push(noteId); + canCall(); + }); + return; + } + // 2) 查看本地的, 与本地合并 + Note.getNote(noteId, function(noteLocal) { + // 2.1 本地没有, 表示是新建 + if(!noteLocal) { + log('add: ...') + // TODO + Note.addNoteForce(note, function(note) { + me._syncInfo.note.adds.push(note); + canCall(); + }); + } else { + // 2.2 本地是否修改了, 需要合并, 使用服务端的数据 + if(note.IsDirty) { + log('冲突....') + // 2.3 服务器是最新的, 用服务器的 + } else { + } + // 这里都是用服务器端的数据, 不处理冲突 + Note.updateNoteForce(note, function(note) { + if(note) { + me._syncInfo.note.updates.push(note); + } + canCall(); + }) + } + }); + })(note); + } + }, + + syncNote: function(afterUsn, callback) { + var me = this; + Api.getSyncNotes(afterUsn, me._noteMaxEntry, function(notes) { + log('syncNote') + log(notes); + if(Common.isOk(notes)) { + me._totalSyncNoteNum += notes.length; + // 证明可能还有要同步的 + if(notes.length == me._noteMaxEntry) { + me._syncNoteToLocal(notes); + var last = notes[notes.length-1]; + me.syncNote(last.Usn, callback); + } else { + log('no more'); + me._syncNoteIsLastChunk = true; + me._syncNoteToLocal(notes, callback); + } + } else { + // 同步失败 + me._syncInfo.note.ok = false; + me._syncInfo.note.msg = "cann't get all chunks"; + callback && callback(false); + } + }); + }, + + // 同步标签 syncTag: function(afterUsn, callback) { callback && callback(true); @@ -148,6 +288,7 @@ var Sync = { // 同步笔记本 me.syncNotebook(-1, function(ok) { if(ok) { + log('------------------') // 同步笔记 me.syncNote(-1, function(ok) { if(ok) { @@ -160,6 +301,7 @@ var Sync = { } }); } else { + log('no-------') callback && callback(me._syncInfo); } }); @@ -169,6 +311,7 @@ var Sync = { incrSync: function() { var me = this; me._initSyncInfo(); + log('full sync start'); // 得到当前LastSyncUsn User.getLastSyncInfo(function(lastSyncUsn, lastSyncTime) { @@ -191,4 +334,4 @@ var Sync = { } }; -module.exports = Sync \ No newline at end of file +module.exports = Sync; \ No newline at end of file diff --git a/node_modules/user.js b/node_modules/user.js index 90fc3888..6056860f 100644 --- a/node_modules/user.js +++ b/node_modules/user.js @@ -74,6 +74,9 @@ var User = { getCurActiveUserId: function() { return this.userId || "user1"; }, + getToken: function() { + return this.token || "user1"; + }, getCurUserImagesPath: function() { return Evt.getBasePath() + '/' + this.getCurUserImagesAppPath(); }, diff --git a/public/js/app/page.js b/public/js/app/page.js index 90e1a2bd..4dfbeb33 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -1295,8 +1295,13 @@ LeaAce = { }; // 全量同步 -function fullSync() { - +function fullSync(callback) { + log('full sync'); + SyncService.fullSync(function(ret) { + log('after....') + log(ret); + callback && callback(); + }); } // note.html调用 @@ -1340,7 +1345,9 @@ function initPage() { UserService.init(function(userInfo) { if(userInfo) { UserInfo = userInfo; - _init(); + fullSync(function() { + _init(); + }); } else { location.href = 'login.html'; } diff --git a/public/js/app/service.js b/public/js/app/service.js index 3f5fd8ec..7744dc0d 100644 --- a/public/js/app/service.js +++ b/public/js/app/service.js @@ -8,13 +8,13 @@ var Service = { userService: require('user'), tagService: require('tag'), apiService: require('api'), - syncServie: require('sync'); + syncServie: require('sync') }; // 全局变量 var ApiService = Service.apiService; var UserService = Service.userService; -var SyncService = Service.syncService; +var SyncService = Service.syncServie; // 分发服务 // route = /note/notebook @@ -51,8 +51,6 @@ $(document).on('contextmenu', function (e) { }); var gui = require('nw.gui'); -console.log("life") -console.log(gui); function Menu() { this.menu = new gui.Menu(); this.cut = new gui.MenuItem({