lrf402788946 4 years ago
parent
commit
d8ee52dfda
1 changed files with 9 additions and 2 deletions
  1. 9 2
      app/service/weixin.js

+ 9 - 2
app/service/weixin.js

@@ -35,8 +35,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`;
-    this.ctx.redirect(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 to_uri = `${baseUrl}/api/auth?appid=${appid}&response_type=code&redirect_uri=${backUrl}#wechat`;
+    this.ctx.redirect(to_uri);
   }
 
   /**