-
Notifications
You must be signed in to change notification settings - Fork 10
/
shipper.js
51 lines (42 loc) · 1.17 KB
/
shipper.js
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
// 数据的IO操作
var fs = require('fs');
var access_token_filename = 'destinations/wx_access_token.json';
var ticket_filename = 'destinations/wx_ticket.json';
var worker = {};
// write access_token to file
worker.ship = function (data, callback) {
var json = { access_token: data.access_token };
fs.writeFile(access_token_filename, JSON.stringify(json), function (err) {
callback(err);
});
};
//从文件中获取 access_token
worker.get = function (callback) {
fs.readFile(access_token_filename, function (err, data) {
if (!err) {
var data_json = JSON.parse(data);
callback(null, data_json.access_token);
} else {
callback(err, {});
}
});
};
// write ticket to file
worker.shipTicket = function (data, callback) {
var json = { ticket: data.ticket };
fs.writeFile(ticket_filename, JSON.stringify(json), function (err) {
callback(err);
});
}
// 从文件中获取 ticket
worker.ticket = function(callback) {
fs.readFile(ticket_filename, function (err, data) {
if (err) {
callback(err, {});
return;
}
var data_json = JSON.parse(data);
callback(null, data_json.ticket);
});
}
module.exports = worker;