This commit is contained in:
life
2015-01-20 10:41:03 +08:00
parent 5bf1d1b5ec
commit 8a989b3726
62 changed files with 6888 additions and 3 deletions

71
node_modules/api.js generated vendored Normal file
View 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;