lrf402788946 před 4 roky
rodič
revize
cab0fd73a8
1 změnil soubory, kde provedl 12 přidání a 18 odebrání
  1. 12 18
      app/service/weixin.js

+ 12 - 18
app/service/weixin.js

@@ -106,26 +106,20 @@ class WeixinAuthService extends AxiosService {
     let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
     let jsapi_ticket = await this.app.redis.get(this.jsapiKey);
     const { appid, appSecret } = this.wxInfo;
     const { appid, appSecret } = this.wxInfo;
     if (!jsapi_ticket) {
     if (!jsapi_ticket) {
-      const res = await this.httpGet('/api.weixin.qq.com/cgi-bin/ticket/getticket&type=jsapi', { appid });
-      jsapi_ticket = _.get(res, 'data.ticket');
-      console.log(jsapi_ticket);
-      const expiresIn = _.get(res, 'data.expires_in', 6000);
+      // 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');
+      // 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' });
+      if (jtReq.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'jsapi_ticket获取失败');
+      jsapi_ticket = _.get(jtReq, 'data.ticket');
+      // 实际过期时间是7200s(2h),系统默认设置6000s
+      const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
       // 缓存jsapi_ticket,重复使用
       // 缓存jsapi_ticket,重复使用
       await this.app.redis.set(this.jsapiKey, jsapi_ticket, 'EX', expiresIn);
       await this.app.redis.set(this.jsapiKey, jsapi_ticket, 'EX', expiresIn);
-      // // 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');
-      // // 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' });
-      // if (jtReq.status !== 200) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'jsapi_ticket获取失败');
-      // jsapi_ticket = _.get(jtReq, 'data.ticket');
-      // 实际过期时间是7200s(2h),系统默认设置6000s
-      // const expiresIn = _.get(jtReq, 'data.expires_in', 6000);
-      // // 缓存jsapi_ticket,重复使用
-      // await this.app.redis.set(this.jsapiKey, jsapi_ticket, 'EX', expiresIn);
     }
     }
     const noncestr = random(16).toLowerCase();
     const noncestr = random(16).toLowerCase();
     const timestamp = moment().unix();
     const timestamp = moment().unix();