From 4bb8c5f233519b843cf971fe7566a60ece9bde64 Mon Sep 17 00:00:00 2001 From: life Date: Sat, 21 Nov 2015 16:45:07 +0800 Subject: [PATCH] =?UTF-8?q?=E7=99=BB=E5=BD=95,=20=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E6=9C=AC=E5=9C=B0=E5=B8=90=E6=88=B7=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- login.html | 10 +-- node_modules/api.js | 3 +- node_modules/user.js | 121 ++++++++++++++++++--------------- public/js/app/page.js | 8 +-- public/js/app/service_login.js | 2 +- 5 files changed, 77 insertions(+), 67 deletions(-) diff --git a/login.html b/login.html index a8919a16..36987d53 100644 --- a/login.html +++ b/login.html @@ -182,11 +182,11 @@ $(function() { } UserService.login(email, pwd, host, function(ret) { if (ret) { - setTimeout(function() { - $body.removeClass('loading'); - goToMainPage(); - gui.getCurrentWindow().close(); - }, 2000); + // setTimeout(function() { + $body.removeClass('loading'); + goToMainPage(); + gui.getCurrentWindow().close(); + // }, 2000); } // 不成功, 则用api登录 else { diff --git a/node_modules/api.js b/node_modules/api.js index b3b835f5..15f86bfc 100644 --- a/node_modules/api.js +++ b/node_modules/api.js @@ -97,6 +97,7 @@ var Api = { if(error) { return callback && callback(false); } + // needle.get('http://localhost/phpinfo.php?email=xx', {emai: email, pwd: pwd}, function(error, response) { var ret = response.body; // 登录成功, 保存token @@ -105,7 +106,7 @@ var Api = { if(Common.isOk(ret)) { ret.Pwd = Common.md5(pwd, ret.UserId); ret['Host'] = Evt.leanoteUrl; - User.setCurUser(ret); + // User.setCurUser(ret); callback && callback(ret); } else { // console.log('log failed'); diff --git a/node_modules/user.js b/node_modules/user.js index 0a5090ba..e254328e 100644 --- a/node_modules/user.js +++ b/node_modules/user.js @@ -30,37 +30,23 @@ User = { // add local account support flag // see https://github.com/leanote/desktop-app/issues/36 local: null, - // 登录后保存当前 - setCurUser: function(user) { - var me = this; - if(user) { - this.token = user.Token; - this.userId = user.UserId; - this.email = user.Email; - this.username = user.Username; - this.host = user.Host; // http://leanote.com, http://localhost - this.local = user.IsLocal; - // 保存到数据库中 - this.saveCurUser(user); - - // 判断当前用户是否有文件夹 - me.setUserDataPath(); - } - }, // 注销 logout: function (callback) { var me = this; - db.users.update({_id: me.userId}, {$set:{IsActive: false}}, function () { - if (me.isLocal()) { - return callback(); - } - if(!Api) { - Api = require('api'); - } - Api.logout(function() { + function u(callback) { + db.users.update({_id: me.userId}, {$set:{IsActive: false}}, function () { callback(); }); + } + if (me.isLocal()) { + return u(callback); + } + if(!Api) { + Api = require('api'); + } + Api.logout(function() { + u(callback); }); }, @@ -104,7 +90,9 @@ User = { // 远程验证 Api.auth(username, password, host, function(ret) { if(ret.Ok) { - callback(true); + User.saveCurUser(ret, function () { + callback(true); + }); } else { callback(false); } @@ -131,37 +119,36 @@ User = { // 创建默认的笔记本 if (!err) { // 设为当前user - me.saveCurUser(doc); + me.saveCurUser(doc, function () { + // 为该用户初始化数据库 + db.initDBForUser(user.UserId); - // 为该用户初始化数据库 - db.initDBForUser(user.UserId); - - me.userId = user.UserId; - var Notebook = require('notebook'); - var notebookId = Common.objectId(); - Notebook.addNotebook(notebookId, 'Leanote', '', function (notebook) { - if (notebook) { - var Note = require('note'); - var Tag = require('tag'); - Tag.addOrUpdateTag('Leanote'); - Tag.addOrUpdateTag('Welcome'); - Note.updateNoteOrContent({ - IsNew: true, - NoteId: Common.objectId(), - "NotebookId": notebookId, - "Title": "Welcome to Leanote 欢迎来到Leanote", - "Content": "

Leanote, Not Just A NotePad!

Welcome!

Leanote, 不只是笔记

欢迎!", - "Desc": "Leanote, Not Just A NotePad! Welcome", - "Tags": ['Leanote', 'Welcome'] - }, function () { + me.userId = user.UserId; + var Notebook = require('notebook'); + var notebookId = Common.objectId(); + Notebook.addNotebook(notebookId, 'Leanote', '', function (notebook) { + if (notebook) { + var Note = require('note'); + var Tag = require('tag'); + Tag.addOrUpdateTag('Leanote'); + Tag.addOrUpdateTag('Welcome'); + Note.updateNoteOrContent({ + IsNew: true, + NoteId: Common.objectId(), + "NotebookId": notebookId, + "Title": "Welcome to Leanote 欢迎来到Leanote", + "Content": "

Leanote, Not Just A NotePad!

Welcome!

Leanote, 不只是笔记

欢迎!", + "Desc": "Leanote, Not Just A NotePad! Welcome", + "Tags": ['Leanote', 'Welcome'] + }, function () { + callback(true); + }); + } + else { callback(true); - }); - } - else { - callback(true); - } + } + }); }); - callback(true); } else { callback(false); } @@ -173,17 +160,39 @@ User = { } }); }, + // 不同host的userId可能一样, 潜在的bug + /** + * 登录后保存为当前用户 + * @param {db.User} user [description] + * @param {Function} callback [description] + * @return {[type]} [description] + */ saveCurUser: function(user, callback) { + var me = this; + + me.token = user.Token; + me.userId = user.UserId; + me.email = user.Email; + me.username = user.Username; + me.host = user.Host; // http://leanote.com, http://localhost + me.local = user.IsLocal; + // 判断当前用户是否有文件夹 + me.setUserDataPath(); + + // 1. // 设置其它用户为 not active db.users.update({_id: {$ne: user.UserId}}, {$set: {IsActive: false}}, {multi: true}, function(err, n) { - // 当前用户是否在数据库中 + + // 2. 当前用户是否在数据库中 db.users.count({_id: user.UserId}, function(err, count) { if(err || count == 0) { // 添加一个 user['_id'] = user.UserId; user['IsActive'] = true; user['LastLoginTime'] = new Date(); + // 新添加的都是HasDB + user['HasDB'] = true; db.users.insert(user, function(err, doc) { callback && callback(true); }); @@ -216,7 +225,7 @@ User = { console.log('不存在'); callback && callback(false); } else { - // me.setCurUser(doc); + me.token = user.Token; me.userId = user.UserId; me.email = user.Email; diff --git a/public/js/app/page.js b/public/js/app/page.js index 588111c7..f61e9c1b 100644 --- a/public/js/app/page.js +++ b/public/js/app/page.js @@ -2016,11 +2016,11 @@ function userMenu(allUsers) { label: getMsg('Logout'), click: function(e) { Loading.show(); - onClose(function () { - UserService.logout(function () { + UserService.logout(function () { + onClose(function () { toLogin(); - }); - }); + }); + }); } }); diff --git a/public/js/app/service_login.js b/public/js/app/service_login.js index 1cf8e5d1..77c004f5 100644 --- a/public/js/app/service_login.js +++ b/public/js/app/service_login.js @@ -12,7 +12,7 @@ var Service = { }; var db = require('db'); -db.initForLogin(); +db.initGlobal(); // 全局变量 var ApiService = Service.apiService;