-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
134 lines (113 loc) · 3.07 KB
/
index.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
var fs = require('fs');
var WechatAPI = require('wechat-api');
var wsem = funtion(config){
this.api = new WechatAPI(config.app_id, config.app_secret, function (callback) {
if (config.redis) {
config.redis.get('foo', access_token (err, result) {
if (err) {return callback(err);}
callback(null, JSON.parse(result));
});
}else{
// 传入一个获取全局token的方法
fs.readFile('access_token.txt', 'utf8', function (err, txt) {
if (err) {return callback(err);}
callback(null, JSON.parse(txt));
});
}
}, function (token, callback) {
// 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
// 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
if (config.redis) {
config.redis.set('access_token', JSON.stringify(token));
callback(null, JSON.stringify(token));
}else{
fs.writeFile('access_token.txt', JSON.stringify(token), callback);
}
});
this.cfg = require('./config_default');
require('extend')(this.cfg, config);
return this;
};
wsem.prototype.config = function(){
return function(req, res, next) {
var param = {
debug: true,
jsApiList: jsApiList,
url: req.query.url
};
param = this.cfg;
// var result = {
// debug: param.debug,
// appId: that.appid,
// timestamp: timestamp,
// nonceStr: nonceStr,
// signature: signature,
// jsApiList: param.jsApiList
// };
api.getJsConfig(param, function(err, result){
req.wechat_config_result = result;
req.wechat_config_err = err;
next();
});
}
}
wsem.prototype.error = function(){
return function(req, res, next) {
if(req.wechat_config_err) {
res.json({
status:{
code: -1,
msg : err
}
})
}
next();
}
}
wsem.prototype.json = function(){
return function(req, res, next) {
return res.json(req.wechat_config_result);
}
}
wsem.prototype.jade = function(opt) {
require('extend')(this.cfg, req.wechat_config_result);
var jade = opt.jade ? opt.jade : 'share';
return function(req, res, next){
return res.render(jade, {
wechat_config: JSON.stringify(this.cfg)
});
}
}
wsem.prototype.config_with_json = function(req, res, next) {
api.getJsConfig(param, function(err, result){
req.wechat_config_result = result;
req.wechat_config_err = err;
if(req.wechat_config_err) {
res.json({
status:{
code: -1,
msg : err
}
})
}
return this.json();
});
}
wsem.prototype.config_with_jade = function(opt){
return function(req, res, next) {
api.getJsConfig(param, function(err, result){
req.wechat_config_result = result;
req.wechat_config_err = err;
if(req.wechat_config_err) {
res.json({
status:{
code: -1,
msg : err
}
})
}
return this.jade(opt);
});
}
}
module.exports = wsem;