liuyu 4 gadi atpakaļ
vecāks
revīzija
a35bdc7fff
4 mainītis faili ar 55 papildinājumiem un 21 dzēšanām
  1. 24 10
      app/controller/weixin.js
  2. 1 1
      app/service/login.js
  3. 29 10
      app/service/weixin.js
  4. 1 0
      config/config.default.js

+ 24 - 10
app/controller/weixin.js

@@ -54,11 +54,18 @@ class WeixinController extends Controller {
     await this.app.redis.set(key, val, 'EX', 600);
 
     // 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`;
+    // console.log('url-->' + to_uri);
+    // this.ctx.redirect(to_uri);
     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 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 backUrl = encodeURI(`${this.app.config.baseUrl}${this.config.authUrl}/`);
+    const to_uri = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${wxapi.appid}&response_type=code&scope=snsapi_base&redirect_uri=${backUrl}&state=${state}&connect_redirect=1#wechat_redirect`;
     console.log('url-->' + to_uri);
     this.ctx.redirect(to_uri);
   }
@@ -137,26 +144,33 @@ class WeixinController extends Controller {
     assert(openid, 'openid不能为空');
     if (openid) {
       console.log('redirect_uri-->' + redirect_uri);
-      let user = await this.ctx.service.user.findByOpenid(openid);
-      user = JSON.parse(JSON.stringify(user));
+      const user = await this.ctx.service.user.findByOpenid(openid);
       if (type === '0') {
         // 通过openid取得用户信息
         if (user) {
-          const token = await this.ctx.service.login.createJwt(user);
-          console.log(`token:${token}\n`);
+          const user_ = JSON.parse(JSON.stringify(user));
+          user_ = checkVip(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}/mobile/error`;
+          const touri = `${this.app.config.baseUrl}/platmobile/error`;
           const to_uri = urljoin(touri, `?openid=${openid}`);
           // TODO: 重定性页面
           this.ctx.redirect(to_uri);
         }
       } else if (type === '1') {
-        const user = await this.checkVip(user);
+        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 if (type === '2') {
         const to_uri = urljoin(
           redirect_uri,
           `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`

+ 1 - 1
app/service/login.js

@@ -94,7 +94,7 @@ class LoginService extends CrudService {
     await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
 
 
-    const operationlogdata = { login_id: user.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录' };
+    const operationlogdata = { dockid: user.remark, login_id: user.id, login_name: user.name, login_role: user.role, type: '0', operation_edit: '登录' };
     const url = 'http://127.0.0.1:9004/api/market/operationlog/';
     const operationlog = await this.ctx.curl(url, {
       method: 'post',

+ 29 - 10
app/service/weixin.js

@@ -15,22 +15,41 @@ class WeixinAuthService extends AxiosService {
   // 通过认证码获得用户信息
   async fetch(code) {
     // TODO:参数检查和默认参数处理
+    // assert(code);
+    // const { wxapi } = this.app.config;
+    // let res = await this.httpGet('/api/fetch', { code });
+    // 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, '获得微信用户信息失败');
+    // }
+    // return res;
     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',
+    };
+    console.log('rrrr-' + params);
+    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

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