mirror of
https://github.com/leanote/desktop-app.git
synced 2025-10-17 16:45:21 +00:00
api
This commit is contained in:
71
node_modules/api.js
generated
vendored
Normal file
71
node_modules/api.js
generated
vendored
Normal file
@@ -0,0 +1,71 @@
|
||||
var db = require('db');
|
||||
var Common = require('common');
|
||||
var User = require('user');
|
||||
var Tags = db.tags;
|
||||
var needle = require('needle');
|
||||
var fs = require('fs');
|
||||
|
||||
var log = console.log;
|
||||
|
||||
// 远程数据服务
|
||||
var Api = {
|
||||
baseUrl: 'http://localhost:9000/api',
|
||||
getUrl: function(url) {
|
||||
return this.baseUrl + '/' + url;
|
||||
},
|
||||
isOk: function(ret) {
|
||||
return typeof ret == 'object' && ret.Ok;
|
||||
},
|
||||
// 登录
|
||||
auth: function(email, pwd, callback) {
|
||||
var me = this;
|
||||
needle.get(this.getUrl('auth/login'), {emai: email, pwd: pwd}, function(error, response) {
|
||||
var ret = response.body;
|
||||
// 登录成功, 保存token
|
||||
if(me.isOk(ret)) {
|
||||
User.setCurUser(ret);
|
||||
log(ret);
|
||||
callback && callback(ret);
|
||||
} else {
|
||||
log('log failed');
|
||||
callback && callback(false);
|
||||
}
|
||||
});
|
||||
},
|
||||
post: function() {
|
||||
var me = this;
|
||||
var options = {
|
||||
headers: { 'X-Custom-Header': 'Bumbaway atuna' }
|
||||
}
|
||||
// you can pass params as a string or as an object.
|
||||
needle.post(me.getUrl('auth/login'), 'foo=bar', options, function(err, resp) {
|
||||
var ret = resp.body;
|
||||
log(ret);
|
||||
});
|
||||
},
|
||||
// get图片
|
||||
getImage: function(callback) {
|
||||
needle.get('http://localhost:9000/images/logo.png', function(err, resp) {
|
||||
// log(resp.body);
|
||||
/*
|
||||
{ 'accept-ranges': 'bytes',
|
||||
'content-disposition': 'inline; filename="logo.png"',
|
||||
'content-length': '8583',
|
||||
'content-type': 'image/png',
|
||||
date: 'Mon, 19 Jan 2015 15:01:47 GMT',
|
||||
*/
|
||||
// log(resp.headers);
|
||||
fs.writeFile('/Users/life/Desktop/aa.png', resp.body);
|
||||
});
|
||||
},
|
||||
uploadImage: function() {
|
||||
var data = {
|
||||
foo: 'bar',
|
||||
image: { file: '/Users/life/Desktop/aa.png', content_type: 'image/png' }
|
||||
}
|
||||
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
|
||||
});
|
||||
}
|
||||
};
|
||||
module.exports = Api;
|
Reference in New Issue
Block a user