浏览代码

Merge branch 'master' of http://git.cc-lotus.info/live-platform/service-onlive

reloaded 4 年之前
父节点
当前提交
85e4a28bba
共有 4 个文件被更改,包括 14 次插入12 次删除
  1. 1 1
      .vscode/settings.json
  2. 1 1
      app/controller/weixin.js
  3. 11 10
      app/service/weixin.js
  4. 1 0
      config/config.default.js

+ 1 - 1
.vscode/settings.json

@@ -1,3 +1,3 @@
 {
-    "eggHelper.serverPort": 35684
+    "eggHelper.serverPort": 59973
 }

+ 1 - 1
app/controller/weixin.js

@@ -49,7 +49,7 @@ class WeixinController extends Controller {
     // TODO: 生成回调地址
     const { wxapi, authUrl = this.ctx.path } = this.app.config;
     const backUrl = encodeURI(`${this.app.config.baseUrl}${this.config.authUrl}?state=${state}`);
-    const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}&connect_redirect=1#wechat`;
+    const to_uri = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}&connect_redirect=1#wechat_redirect`;
     console.log('url-->' + to_uri);
     this.ctx.redirect(to_uri);
   }

+ 11 - 10
app/service/weixin.js

@@ -17,20 +17,21 @@ class WeixinAuthService extends AxiosService {
     // TODO:参数检查和默认参数处理
     assert(code);
     const { wxapi } = this.app.config;
-    let res = await this.httpGet('/api/fetch', { code });
+
+    const reqUrl = 'https://api.weixin.qq.com/sns/oauth2/access_token';
+    const params = {
+      appid: wxapi.appid,
+      secret: wxapi.appSecret,
+      code,
+      grant_type: 'authorization_code',
+    };
+    const res = await this.httpGet(reqUrl, params);
+
     if (res.errcode && res.errcode !== 0) {
       this.ctx.logger.error(`[WeixinAuthService] fetch open by code fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
       throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信认证信息失败');
     }
-    const { openid } = res;
-
-    // TODO: 获得用户信息
-    res = await this.httpGet('/api.weixin.qq.com/cgi-bin/user/info?lang=zh_CN', { appid: wxapi.appid, openid });
-    // console.debug('res: ', res);
-    if (res.errcode && res.errcode !== 0) {
-      this.ctx.logger.error(`[WeixinAuthService] fetch userinfo by openid fail, errcode: ${res.errcode}, errmsg: ${res.errmsg}`);
-      throw new BusinessError(ErrorCode.SERVICE_FAULT, '获得微信用户信息失败');
-    }
+    // const { openid } = res;
     return res;
   }
 

+ 1 - 0
config/config.default.js

@@ -35,6 +35,7 @@ module.exports = appInfo => {
   config.wxapi = {
     // appid: 'wxdf3ed83c095be97a', // 微信公众号APPID
     appid: 'wx39cb76688a6981ae',
+    appSecret: '',
     baseUrl: 'http://wx.cc-lotus.info', // 微信网关地址
     mchid: '1505364491', // 商户ID
     mchkey: '1qaz2wsx3edc4rfv5tgb6yhn7ujm8ik9', // 商户key