Skip to content

Commit 73f024a

Browse files
committed
fix doc
1 parent 14d6753 commit 73f024a

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,24 @@ var OAuth = require('wechat').OAuth;
146146
var oauthApi = new OAuth('appid', 'secret');
147147
```
148148

149+
以上即可满足单进程使用。
150+
当多进程时,token需要全局维护,以下为保存token的接口。
151+
152+
```js
153+
var oauthApi = new OAuth('appid', 'secret', function (openid, callback) {
154+
// 传入一个根据openid获取对应的全局token的方法
155+
fs.readFile(openid +':access_token.txt', 'utf8', function (err, txt) {
156+
if (err) {return callback(err);}
157+
callback(null, JSON.parse(txt));
158+
});
159+
}, function (openid, token, callback) {
160+
// 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
161+
// 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
162+
// 持久化时请注意,每个openid都对应一个唯一的token!
163+
fs.writeFile(openid + ':access_token.txt', JSON.stringify(token), callback);
164+
});
165+
```
166+
149167
生成引导用户点击的URL
150168

151169
```js
@@ -161,7 +179,7 @@ oauthApi.getAccessToken('code', function (err, result) {
161179
});
162180
```
163181

164-
如果我们生成引导用户点击的URL中`scope`参数值为`snsapi_userinfo`,接下来我们就可以使用`openid`换取用户详细信息(必须在调用getAccessToken方法之后
182+
如果我们生成引导用户点击的URL中`scope`参数值为`snsapi_userinfo`,接下来我们就可以使用`openid`换取用户详细信息(必须在getAccessToken方法执行完成之后
165183

166184
```js
167185
oauthApi.getUser('openid', function (err, result) {

0 commit comments

Comments
 (0)