guhongwei 4 years ago
parent
commit
5c9082202c
2 changed files with 34 additions and 37 deletions
  1. 33 36
      app/controller/weixin.js
  2. 1 1
      app/service/weixin.js

+ 33 - 36
app/controller/weixin.js

@@ -60,47 +60,44 @@ class WeixinController extends Controller {
     this.ctx.logger.debug(`[auth-back] code - ${code}, state - ${state}`);
     this.ctx.logger.debug(`[auth-back] code - ${code}, state - ${state}`);
     assert(code, 'code不能为空');
     assert(code, 'code不能为空');
     assert(state, 'state不能为空');
     assert(state, 'state不能为空');
-    // console.log('code-->' + code);
     console.log('code-->' + code);
     console.log('code-->' + code);
     const { weixin } = this.ctx.service;
     const { weixin } = this.ctx.service;
-    const openid = await weixin.fetch(code);
-    // try {
-    //   ({ openid } = await weixin.fetch(code));
-    // } catch (err) {
-    //   await this.ctx.render('error.njk', { title: err.message, message: err.details });
-    //   return;
-    // }
-    console.log('code-->' + code);
+    let openid;
+    try {
+      ({ openid } = await weixin.fetch(code));
+    } catch (err) {
+      await this.ctx.render('error.njk', { title: err.message, message: err.details });
+      return;
+    }
     console.log('openid--->' + openid);
     console.log('openid--->' + openid);
     if (openid) {
     if (openid) {
-      console.log('openid--1>' + openid);
-      // const key = `visit:auth:state:${state}`;
-      // const val = await this.app.redis.get(key);
-      // const { redirect_uri, type, uid, qrcode } = JSON.parse(val);
-      // // console.log('redirect_uri-->' + redirect_uri);
-      // const user = await this.ctx.service.user.findByOpenid(openid);
-      // if (type === '0') {
-      //   // 通过openid取得用户信息
-      //   if (user) {
-      //     const token = await this.ctx.service.login.createJwt(user);
-      //     const to_uri = urljoin(redirect_uri, `?token=${token}`);
-      //     // TODO: 重定性页面
-      //     // console.log('to_uri000-->' + to_uri);
-      //     this.ctx.redirect(to_uri);
+      const key = `visit:auth:state:${state}`;
+      const val = await this.app.redis.get(key);
+      const { redirect_uri, type, uid, qrcode } = JSON.parse(val);
+      // console.log('redirect_uri-->' + redirect_uri);
+      const user = await this.ctx.service.user.findByOpenid(openid);
+      if (type === '0') {
+        // 通过openid取得用户信息
+        if (user) {
+          const token = await this.ctx.service.login.createJwt(user);
+          const to_uri = urljoin(redirect_uri, `?token=${token}`);
+          // TODO: 重定性页面
+          // console.log('to_uri000-->' + to_uri);
+          this.ctx.redirect(to_uri);
 
 
-      //   } else {
-      //     // console.log('rrr0000--->' + redirect_uri);
-      //     const touri = `${this.app.config.baseUrl}/platmobile/live/index`;
-      //     const to_uri = urljoin(touri, `?openid=${openid}`);
-      //     // TODO: 重定性页面
-      //     this.ctx.redirect(to_uri);
-      //   }
-      // } else if (type === '1') {
-      //   const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`);
-      //   // TODO: 重定性页面
-      //   // console.log('1111---?' + to_uri);
-      //   this.ctx.redirect(to_uri);
-      // }
+        } else {
+          // console.log('rrr0000--->' + redirect_uri);
+          const touri = `${this.app.config.baseUrl}/platmobile/live/index`;
+          const to_uri = urljoin(touri, `?openid=${openid}`);
+          // TODO: 重定性页面
+          this.ctx.redirect(to_uri);
+        }
+      } else if (type === '1') {
+        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`);
+        // TODO: 重定性页面
+        // console.log('1111---?' + to_uri);
+        this.ctx.redirect(to_uri);
+      }
     }
     }
 
 
   }
   }

+ 1 - 1
app/service/weixin.js

@@ -23,7 +23,7 @@ class WeixinAuthService extends AxiosService {
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信认证信息失败');
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信认证信息失败');
     }
     }
     const { openid } = res;
     const { openid } = res;
-
+    console.log('1res' + res);
     // TODO: 获得用户信息
     // TODO: 获得用户信息
     res = await this.httpGet('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid: wxapi.appid, openid });
     res = await this.httpGet('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid: wxapi.appid, openid });
     // console.debug('res: ', res);
     // console.debug('res: ', res);