同步笔记本

This commit is contained in:
life
2015-01-21 20:45:29 +08:00
parent 8a989b3726
commit 1674b9f91d
9 changed files with 326 additions and 85 deletions

59
node_modules/api.js generated vendored
View File

@@ -5,26 +5,51 @@ var Tags = db.tags;
var needle = require('needle');
var fs = require('fs');
var log = console.log;
function log(o) {
console.log(o);
}
// 远程数据服务
var Api = {
baseUrl: 'http://localhost:9000/api',
getUrl: function(url) {
return this.baseUrl + '/' + url;
getUrl: function(url, param) {
var url = this.baseUrl + '/' + url;
if(param) {
var paramStr = '';
for(var i in param) {
paramStr += i + '=' + param[i] + '&';
}
}
if(url.indexOf('?') >= 0) {
return url + '&' + paramStr;
}
return url + '?' + paramStr;
},
isOk: function(ret) {
return typeof ret == 'object' && ret.Ok;
// 数组
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;
needle.get(this.getUrl('auth/login'), {emai: email, pwd: pwd}, function(error, response) {
log({emai: email, pwd: pwd});
needle.get(this.getUrl('auth/login', {email: email, pwd: pwd}), function(error, response) {
// needle.get('http://localhost/phpinfo.php?email=xx', {emai: email, pwd: pwd}, function(error, response) {
var ret = response.body;
// 登录成功, 保存token
log(ret);
if(me.isOk(ret)) {
ret.Pwd = pwd;
User.setCurUser(ret);
log(ret);
callback && callback(ret);
} else {
log('log failed');
@@ -66,6 +91,28 @@ var Api = {
needle.post('http://localhost/phpinfo.php', data, { multipart: true }, function(err, resp, body) {
// needle will read the file and include it in the form-data as binary
});
},
getSyncNotebooks: function(afterUsn, maxEntry, callback) {
var me = this;
needle.get(this.getUrl('notebook/getSyncNotebooks', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) {
var ret = response.body;
if(me.isOk(ret)) {
callback && callback(ret);
} else {
callback && callback(false);
}
});
},
getSyncNotes: function(afterUsn, maxEntry, callback) {
var me = this;
needle.get(this.getUrl('note/getSyncNotes', {afterUsn: afterUsn, maxEntry: maxEntry}), function(error, response) {
var ret = response.body;
if(me.isOk(ret)) {
callback && callback(ret);
} else {
callback && callback(false);
}
});
}
};
module.exports = Api;