瀏覽代碼

更改微信登录逻辑

liuyu 5 年之前
父節點
當前提交
e6a8616448
共有 4 個文件被更改,包括 36 次插入5 次删除
  1. 2 3
      app/controller/login.js
  2. 2 2
      app/controller/weixin.js
  3. 1 0
      app/router.js
  4. 31 0
      app/service/login.js

+ 2 - 3
app/controller/login.js

@@ -28,9 +28,8 @@ class LoginController extends Controller {
   }
 
   // POST 检查二维码
-  async check() {
-    const { qrcode } = this.ctx.requestparam;
-    const res = await this.service.checkQrcode(qrcode);
+  async wxcheck() {
+    const res = await this.service.wxcheck(this.ctx.request.body);
     this.ctx.ok(res);
   }
 

+ 2 - 2
app/controller/weixin.js

@@ -100,7 +100,7 @@ class WeixinController extends Controller {
         } else {
           _type = type;
         }
-        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}`);
+        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}&qrcode=${qrcode}`);
         // TODO: 重定性页面
         this.ctx.redirect(to_uri);
       } else if (type === '2') {
@@ -143,7 +143,7 @@ class WeixinController extends Controller {
       } else {
         _type = type;
       }
-      const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}`);
+      const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}&qrcode=${qrcode}`);
       // TODO: 重定性页面
       this.ctx.redirect(to_uri);
     } else if (type === '2') {

+ 1 - 0
app/router.js

@@ -149,6 +149,7 @@ module.exports = app => {
   router.post('/api/train/login', controller.login.login);// 登录
   // 微信端登录
   router.get('/api/train/qrcode', controller.login.qrcode);// 登录
+  router.post('/api/train/wxcheck', controller.login.wxcheck);// 微信检查登录
   router.post('/api/train/wxlogin', controller.login.wxlogin);// 登录
 
   // 评分表设置路由

+ 31 - 0
app/service/login.js

@@ -66,7 +66,38 @@ class LoginService extends CrudService {
     return token;
   }
 
+  // 微信校验mq接口
+  async wxcheck(data) {
+    const { qrcode, openid } = data;
+    assert(qrcode, 'qrcode不能为空');
+    assert(openid, 'openid不能为空');
+    // TODO: 发布扫码成功消息
+    const { mq } = this.ctx;
+    const ex = 'qrcode.login';
+    const parm = {
+      durable: true,
+      headers: {
+        openid,
+      } };
+    if (mq) {
+      await mq.topic(ex, qrcode, 'scaned', parm);
+    }
+  }
+
+  // 微信登录接口
   async wxlogin(data) {
+    const { qrcode, openid } = data;
+    assert(qrcode, 'qrcode不能为空');
+    assert(openid, 'openid不能为空');
+    const res = await this.uModel.findOne({ openid });
+    if (!res) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    const result = await this.createJwt(res);
+    return result;
+  }
+
+  async userbind(data) {
     const { qrcode, openid } = data;
     assert(qrcode, 'qrcode不能为空');
     assert(openid, 'openid不能为空');