lrf402788946 4 years ago
parent
commit
366d040aa6
2 changed files with 59 additions and 1 deletions
  1. 7 0
      app/controller/user.js
  2. 52 1
      app/service/user.js

+ 7 - 0
app/controller/user.js

@@ -41,6 +41,13 @@ class UserController extends Controller {
     const res = await this.service.schoolregister();
     this.ctx.ok({ data: res });
   }
+
+  // 用appopenid查找用户
+  async toFindAppUser() {
+    const { appopenid } = this.ctx.request.body;
+    const res = await this.service.findByAppOpenid(appopenid);
+    this.ctx.ok({ data: res });
+  }
 }
 
 module.exports = CrudController(UserController, meta);

+ 52 - 1
app/service/user.js

@@ -145,12 +145,63 @@ class UserService extends CrudService {
   }
   // 通过openid查询用户信息
   async findByAppOpenid(appopenid) {
+    console.log(appopenid);
     // 通过openid查询用户信息
     if (!appopenid) {
       console.error('没有appopenid');
       return;
     }
-    const user = await this.model.findOne({ appopenid });
+    let user = await this.model.findOne({ appopenid }).populate({
+      path: 'uid',
+      model: 'Student',
+      select: 'classid bedroomid',
+      populate: [
+        {
+          path: 'classid',
+          model: 'Class',
+          select: 'jslocationid',
+          populate: {
+            path: 'jslocationid',
+            model: 'Location',
+            select: 'name ibeacon',
+          },
+        },
+        {
+          path: 'bedroomid',
+          model: 'Bedroom',
+          select: 'code ibeacon',
+        },
+      ],
+    });
+
+    if (user) {
+      user = JSON.parse(JSON.stringify(user));
+      // 整理数据
+      const { uid } = user;
+      if (uid) {
+        const { _id, classid, bedroomid } = uid;
+        // 先解析classid
+        if (classid) {
+          const { jslocationid } = classid;
+          if (jslocationid) {
+            const { name, ibeacon } = jslocationid;
+            if (name && ibeacon) {
+              user.jsname = name;
+              user.jsibeacon = ibeacon;
+            }
+          }
+        }
+        // 在解析bedroomid
+        if (bedroomid) {
+          const { code, ibeacon } = bedroomid;
+          if (code && ibeacon) {
+            user.bedroomname = code;
+            user.bedroomibeacon = ibeacon;
+          }
+        }
+        user.uid = _id;
+      }
+    }
     return user;
   }