lrf402788946 4 years ago
parent
commit
97d1fe6108
1 changed files with 11 additions and 20 deletions
  1. 11 20
      app/service/weixin.js

+ 11 - 20
app/service/weixin.js

@@ -53,19 +53,11 @@ class WeixinAuthService extends AxiosService {
   async authBack(query) {
     const { code, state } = query;
     if (!code) throw new BusinessError(ErrorCode.SERVICE_FAULT, '授权未成功');
-    const { appid, appSecret } = this.wxInfo;
-    // const url = 'https://api.weixin.qq.com/sns/oauth2/access_token';
-    // const params = {
-    //   appid,
-    //   secret: appSecret,
-    //   code,
-    //   grant_type: 'authorization_code',
-    // };
-    // const req = await this.httpGet(url, params);
-    console.log(code);
+    const { appid } = this.wxInfo;
     const req = await this.httpGet('/api/fetch', { code });
     if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
     const openid = _.get(req, 'openid');
+    console.log(req);
     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) {
@@ -73,10 +65,9 @@ class WeixinAuthService extends AxiosService {
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '未获取到openid');
     }
     // 获取微信的用户信息
-    console.log({ access_token, 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);
+    const res = await this.httpGet('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid, openid });
+    const object = _.pick(res, [ 'nickname', 'headimgurl', 'openid' ]); // 昵称,头像,openid
+    console.log(object);
     // 验证获取openid结束,接下来应该返回前端
     const key = `${this.prefix}${state}`;
     let fqueries = await this.app.redis.get(key);
@@ -87,12 +78,12 @@ class WeixinAuthService extends AxiosService {
     if (groupid) {
       if (type === 'group') {
         // TODO加用户,进组
-        // const udata = { name: object.nickname, icon: object.headimgurl, openid: object.openid, groupid };
-        // try {
-        //   await this.ctx.service.patient.create(udata);
-        // } catch (error) {
-        //   this.logger.error(error);
-        // }
+        const udata = { name: object.nickname, icon: object.headimgurl, openid: object.openid, groupid };
+        try {
+          await this.ctx.service.patient.create(udata);
+        } catch (error) {
+          this.logger.error(error);
+        }
         redirect_uri = `${this.ctx.app.config.baseUrl}/mobile`;
       } else {
         // TODO 点击进入群组聊天,不过没改也没加