lrf402788946 4 years ago
parent
commit
0a4e64e8f1
1 changed files with 20 additions and 21 deletions
  1. 20 21
      app/service/weixin.js

+ 20 - 21
app/service/weixin.js

@@ -34,15 +34,15 @@ class WeixinAuthService extends AxiosService {
     const key = `${this.prefix}${state}`;
     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);
+    // 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);
   }
 
@@ -51,20 +51,19 @@ class WeixinAuthService extends AxiosService {
    * @param {Object} query 参数
    */
   async authBack(query) {
-    console.log('in function:back');
     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',
-    };
-    console.log(params);
-    const req = await this.httpGet(url, params);
-    console.log(req);
+    // const url = '/api.weixin.qq.com/sns/oauth2/access_token';
+    const url = '/api/fetch';
+    // const params = {
+    //   appid,
+    //   secret: appSecret,
+    //   code,
+    //   grant_type: 'authorization_code',
+    // };
+    // const req = await this.httpGet(url, params);
+    const req = await this.httpGet(url, { code });
     if (req.errcode && req.errcode !== 0) throw new BusinessError(ErrorCode.SERVICE_FAULT, 'openid获取失败');
     const openid = _.get(req, 'openid');
     if (!openid) {
@@ -72,7 +71,7 @@ class WeixinAuthService extends AxiosService {
       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('/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结束,接下来应该返回前端