登录, 创建本地帐户优化

This commit is contained in:
life
2015-11-21 16:45:07 +08:00
parent f16e95e86f
commit 4bb8c5f233
5 changed files with 77 additions and 67 deletions

View File

@@ -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 {

3
node_modules/api.js generated vendored
View File

@@ -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');

121
node_modules/user.js generated vendored
View File

@@ -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": "<h2>Leanote, Not Just A NotePad!</h2>Welcome!<h2>Leanote, 不只是笔记</h2>欢迎!",
"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": "<h2>Leanote, Not Just A NotePad!</h2>Welcome!<h2>Leanote, 不只是笔记</h2>欢迎!",
"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;

View File

@@ -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();
});
});
});
});
}
});

View File

@@ -12,7 +12,7 @@ var Service = {
};
var db = require('db');
db.initForLogin();
db.initGlobal();
// 全局变量
var ApiService = Service.apiService;