Created
July 3, 2014 04:54
-
-
Save getchenge/eba5a2a5ecb896e31fce to your computer and use it in GitHub Desktop.
通过api获取数据
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var md5 = require('MD5'), | |
_ = require('lodash'), | |
async = require('async'), | |
https = require('http'), | |
request = require('request'), | |
app_id = '100', | |
keys = 'testkeys123', | |
api_host = 'http://test.5wei.com/'; | |
function apiGenerate(params, local_path, cb, filter) { //get data | |
var temp = [], | |
str = [], | |
query = params; | |
params['app_id'] = app_id; | |
temp = _.keys(params).sort(); | |
for (var i in temp) { | |
str.push(encodeURIComponent(temp[i]) + '=' + encodeURIComponent(params[temp[i]])); | |
} | |
str = str.join('&'); | |
str += keys; | |
query['sign'] = md5(str); | |
request.post(api_host + '/api/' + local_path, {form: query}, function (error, response, body) { //pull or push data | |
if (response && response.statusCode == 200) { | |
var data = JSON.parse(body), custom = {}; //customize data | |
if (data['error_code']) { | |
custom['error_code'] = data['error_code']; | |
custom['error_message'] = data['error_message']; | |
} else { | |
custom = filter ? filter.call(null, data) : data; | |
} | |
cb(error, custom); | |
} else { | |
try { | |
console.log('error: ' + response.statusCode) | |
} catch (e) { | |
console.log('no response'); | |
} | |
} | |
}) | |
} | |
exports['orders'] = function (params, cb) { //订单列表 | |
var local_path = 'webserver/get_orders.json'; | |
var cid = params['clearing_id']; | |
var filter = function (data) { | |
var custom = {}; | |
custom['orders'] = data['orders']; | |
if (data['extras']) { | |
custom['menu_name'] = data['extras']['menu'][0]['name']; | |
custom['menu_id'] = data['extras']['menu'][0]['menu_id']; | |
} | |
if (cid)custom['clearing_id'] = cid; | |
return custom; | |
}; | |
apiGenerate(params, local_path, cb, filter); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment