Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getToken 函数是不是能在内存里缓存一下token 呢 #4

Open
secreter opened this issue Oct 8, 2019 · 2 comments
Open

getToken 函数是不是能在内存里缓存一下token 呢 #4

secreter opened this issue Oct 8, 2019 · 2 comments
Labels
help wanted Extra attention is needed

Comments

@secreter
Copy link

secreter commented Oct 8, 2019

频繁的读取不太好,可以试试缓存在内存里或者写在文件里

@unclexiao
Copy link
Owner

通过access_token推送服务通知之后,还可以继续使用原来的access_token么?之前我试了会报失效,所以每次都重新获取。

如果支持多次使用的话,是可以放到内存或者文件里,但这属于业务或者框架的事情,跟本插件无关。

@unclexiao unclexiao added the help wanted Extra attention is needed label Nov 25, 2019
@szy0syz
Copy link
Contributor

szy0syz commented May 7, 2020

自己改下即可。

 async getToken() {
    let token = await this.service.cache.get('access_token');
    if (token) return token;

    const { appId, appSecret } = this.app.config.mp;
    const url = `${tokenUri}?grant_type=client_credential&appid=${appId}&secret=${appSecret}`;
    const res = await this.ctx.curl(url, {
      dataType: 'json',
    });

    token = this.app._.get(res, 'data.access_token');
    if (!token) throw new Error('获取公众号access_token失败');
    this.service.cache.set('access_token', token, 7200 - 100);

    return token;
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants