lrf402788946 4 years ago
parent
commit
9229434627
1 changed files with 14 additions and 16 deletions
  1. 14 16
      app/service/weixin.js

+ 14 - 16
app/service/weixin.js

@@ -15,7 +15,8 @@ class WeixinAuthService extends AxiosService {
   constructor(ctx) {
     super(ctx, {}, _.get(ctx.app.config, 'wxapi'));
     this.prefix = 'visit-auth:';
-    this.jsapiKey = 'visit-access_token';
+    this.jsapiKey = 'visit-jsapi_ticket';
+    this.access_tokenKey = 'visit-access_token';
     this.wxInfo = ctx.app.config.wxapi;
     this.authBackUrl = `${ctx.app.config.baseUrl}/api/visit/authBack`;
   }
@@ -35,14 +36,6 @@ class WeixinAuthService extends AxiosService {
     const val = JSON.stringify({ ...others, redirect_uri });
     await this.app.redis.set(key, val, 'EX', 600);
     const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${this.authBackUrl}&response_type=code&scope=snsapi_userinfo&state=${state}#wechat_redirect`;
-    // let backUrl;
-    // if (this.authBackUrl.startsWith('http')) {
-    //   backUrl = encodeURI(`${this.authBackUrl}?state=${state}`);
-    // } else {
-    //   backUrl = encodeURI(`${this.ctx.protocol}://${this.ctx.host}${this.authBackUrl}?state=${state}`);
-    // }
-    // const url = `${baseUrl}/api/auth?appid=${appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
-    // console.log(url);
     this.ctx.redirect(url);
   }
 
@@ -62,15 +55,16 @@ class WeixinAuthService extends AxiosService {
       grant_type: 'authorization_code',
     };
     const req = await this.httpGet(url, params);
-    console.log(req);
     if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
     const openid = _.get(req, 'openid');
+    const access_token = await this.app.redis.get(this.access_tokenKey);
+    if (!access_token) await this.app.redis.set(this.access_tokenKey, _.get(req, 'access_token'), 'EX', 6000);
     if (!openid) {
       this.ctx.logger.error(JSON.stringify(req.data));
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
     }
     // 获取微信的用户信息
-    const res = await this.httpGet('https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid, openid });
+    const res = await this.httpGet('https://api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { access_token, appid, openid });
     const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]); // 昵称,头像,openid
     console.log(object);
     // 验证获取openid结束,接下来应该返回前端
@@ -110,11 +104,15 @@ class WeixinAuthService extends AxiosService {
     let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
     const { appid, appSecret } = this.wxInfo;
     if (!jsapi_ticket) {
-      // 1,重新获取access_token
-      const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
-      const req = await this.ctx.curl(atUrl, { method: 'GET', dataType: 'json' });
-      if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
-      const access_token = _.get(req, 'data.access_token');
+      // 1,获取access_token
+      let access_token = await this.app.redis.get(this.access_tokenKey);
+      if (!access_token) {
+        // 没有就重新请求
+        const atUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${appSecret}`;
+        const req = await this.ctx.curl(atUrl, { method: 'GET', dataType: 'json' });
+        if (req.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'access_token获取失败');
+        access_token = _.get(req, 'data.access_token');
+      }
       // 2,获取jsapi_token
       const jtUrl = `https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=${access_token}&type=jsapi`;
       const jtReq = await this.ctx.curl(jtUrl, { method: 'GET', dataType: 'json' });