1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
var dateUtils = require("date-utils");
var crypto = require('crypto');
var blueBird = require('bluebird');
var http = require('http');
var querystring = require('querystring');
var dateFormat = "YYYY-MM-DD HH24:MI:SS";
var apiUrl = "http://api.coo8.com/ApiControl";
var apiVer = "2.0";
var appSecret = "*";
var venderId = "*";
var signMethod = "md5";
var md5 = crypto.createHash('md5');
var execute = function(requestData, callback) {
requestData.timestamp = (new Date()).toFormat(dateFormat);
requestData.venderId = venderId;
requestData.v = apiVer;
requestData.signMethod = signMethod;
requestData.sign = signRequest(requestData);
console.log(requestData);
var url = apiUrl + '?' + querystring.stringify(requestData);
http.get(url, function(res) {
var data = [];
res.on('data', function(chunk) { data.push(chunk); });
res.on('end', function() {
var body = data.join('');
try {
var json = JSON.parse(body);
callback(null, json);
}
catch (ex) {
console.log(body);
}
})
}).on('error', callback);
};
var execAsync = blueBird.promisify(execute);
var signRequest = function(requestData) {
var builder = [appSecret];
Object.keys(requestData).sort().forEach(function(key) {
var value = requestData[key];
if (key == 'sign' || value === null || value === undefined)
return;
builder.push(key);
builder.push(value);
});
builder.push(appSecret);
var textToSign = builder.join('');
return md5.update(textToSign).digest('hex').toUpperCase();
}
execAsync({ "method": "coo8.carriers.get"}).then(function(data) {
console.log(JSON.stringify(data, null, 2));
}).catch(function(err) {
console.log(err);
})
|