Browse Source

增加绑定

liuyu 5 years ago
parent
commit
714d8db262
4 changed files with 45 additions and 8 deletions
  1. 6 0
      app/controller/user.js
  2. 24 6
      app/controller/weixin.js
  3. 2 2
      app/router.js
  4. 13 0
      app/service/user.js

+ 6 - 0
app/controller/user.js

@@ -30,6 +30,12 @@ class UserController extends Controller {
     this.ctx.ok({ data: res });
   }
 
+  // 其他用户微信绑定
+  async userbind() {
+    const res = await this.service.bind(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+
   // 学校用户生成
   async schoolregister() {
     const res = await this.service.schoolregister();

+ 24 - 6
app/controller/weixin.js

@@ -24,7 +24,7 @@ class WeixinController extends Controller {
   //       store - 默认,认证结果写入sessionStore,然后重定向回请求页面(要求请求页面和认证服务在同一域名下)
   //       token - url带上token参数重定向到原始地址
   async auth() {
-    const { redirect_uri, code, test, type, response_type = 'store', objid } = this.ctx.query;
+    const { redirect_uri, code, test, type, response_type = 'store', uid } = this.ctx.query;
     if (test) {
       return await this.authTest();
     }
@@ -42,7 +42,7 @@ class WeixinController extends Controller {
     // TODO: 保存原始请求地址
     const state = uuid();
     const key = `visit:auth:state:${state}`;
-    const val = JSON.stringify({ redirect_uri, type, objid });
+    const val = JSON.stringify({ redirect_uri, type, uid });
     await this.app.redis.set(key, val, 'EX', 600);
 
     // TODO: 生成回调地址
@@ -74,7 +74,7 @@ class WeixinController extends Controller {
     if (openid) {
       const key = `visit:auth:state:${state}`;
       const val = await this.app.redis.get(key);
-      const { redirect_uri, type, objid } = JSON.parse(val);
+      const { redirect_uri, type, uid } = JSON.parse(val);
       console.log('redirect_uri-->' + redirect_uri);
       // 通过openid取得用户信息
       const user = await this.ctx.service.user.findByOpenid(openid);
@@ -86,8 +86,14 @@ class WeixinController extends Controller {
         this.ctx.redirect(to_uri);
       } else {
         console.log('rrr--->' + redirect_uri);
-        const to_uri = urljoin(redirect_uri, `?openid=${openid}`);
-
+        const user = await this.ctx.service.user.fetch(uid);
+        let _type = '';
+        if (user) {
+          _type = user.type;
+        } else {
+          _type = type;
+        }
+        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}`);
         // TODO: 重定性页面
         this.ctx.redirect(to_uri);
       }
@@ -97,7 +103,7 @@ class WeixinController extends Controller {
 
   // GET 用户授权内部测试接口
   async authTest() {
-    const { redirect_uri, type, groupid, doctorid } = this.ctx.query;
+    const { redirect_uri, type, uid } = this.ctx.query;
     const openid = '12345678';
     this.ctx.logger.debug(`[auth-test] reditect_uri - ${redirect_uri}, openid - ${openid}`);
     assert(redirect_uri, '回调地址不能为空');
@@ -111,6 +117,18 @@ class WeixinController extends Controller {
       // TODO: 重定性页面
       console.log('to_uri222-->' + to_uri);
       this.ctx.redirect(to_uri);
+    } else {
+      console.log('rrr--->' + redirect_uri);
+      const user = await this.ctx.service.user.fetch(uid);
+      let _type = '';
+      if (user) {
+        _type = user.type;
+      } else {
+        _type = type;
+      }
+      const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${_type}`);
+      // TODO: 重定性页面
+      this.ctx.redirect(to_uri);
     }
   }
 }

+ 2 - 2
app/router.js

@@ -132,8 +132,8 @@ module.exports = app => {
   router.resources('user', '/api/train/user', controller.user); // index、create、show、destroy
   router.post('user', '/api/train/user/update/:id', controller.user.update);
   router.post('user', '/api/train/user/register', controller.user.register);// 学校注册
-  router.post('user', '/api/train/user/bind', controller.user.bind);// 学生或班主任微信绑定
-
+  router.post('user', '/api/train/user/bind', controller.user.bind);// 学生微信绑定
+  router.post('user', '/api/train/user/userbind', controller.user.userbind);// 其他用户微信绑定
 
   // 行政区划表设置路由
   router.resources('termquest', '/api/train/termquest', controller.termquest); // index、create、show、destroy

+ 13 - 0
app/service/user.js

@@ -101,6 +101,19 @@ class UserService extends CrudService {
     return user;
   }
 
+  // 其他用户绑定
+  async userbind(data) {
+    const { openid, uid, type } = data;
+    assert(openid && uid && type, '缺少部分信息项');
+    const user = await this.model.findById(uid);
+    if (!user) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    user.openid = openid;
+    user.save();
+    return user;
+  }
+
   // 通过openid查询用户信息
   async findByOpenid(openid) {
     // 通过openid查询用户信息