Переглянути джерело

增加用户注册和微信绑定

liuyu 5 роки тому
батько
коміт
d3165d4dc2
3 змінених файлів з 32 додано та 3 видалено
  1. 7 1
      app/controller/user.js
  2. 5 0
      app/service/teacher.js
  3. 20 2
      app/service/user.js

+ 7 - 1
app/controller/user.js

@@ -18,11 +18,17 @@ class UserController extends Controller {
     this.ctx.ok({ data: res });
   }
 
-  // 学注册
+  // 学注册
   async register() {
     const res = await this.service.register(this.ctx.request.body);
     this.ctx.ok({ data: res });
   }
+
+  // 学生微信绑定
+  async bind() {
+    const res = await this.service.bind(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 }
 
 module.exports = CrudController(UserController, meta);

+ 5 - 0
app/service/teacher.js

@@ -11,6 +11,7 @@ class TeacherService extends CrudService {
   constructor(ctx) {
     super(ctx, 'teacher');
     this.model = this.ctx.model.Teacher;
+    this.umodel = this.ctx.model.User;
   }
 
   // 创建教师用户
@@ -110,6 +111,10 @@ class TeacherService extends CrudService {
       let detail = '';
       if (status === '1') {
         detail = '您的账号身份已确认,请尽快登录账号上传课件资料附件';
+        // 状态更新后创建教师用户
+        const newdata = { name: teacher.name, mobile: teacher.phone, type: '3', uid: teacher.id };
+        newdata.passwd = teacher.password;
+        await this.umodel.create(newdata);
       } else if (status === '4') {
         detail = '您已通过审核被正式录入教师库';
       }

+ 20 - 2
app/service/user.js

@@ -35,8 +35,26 @@ class UserService extends CrudService {
     return res;
   }
 
-  // 学注册
+  // 学注册
   async register(data) {
+    const { code, mobile, passwd, name } = data;
+    assert(code && mobile && name && passwd, '缺少部分信息项');
+    const user = await this.model.findOne({ mobile });
+    if (user) {
+      throw new BusinessError(ErrorCode.DATA_EXISTED);
+    }
+    const sch = await this.schModel.findOne({ code });
+    if (!sch) {
+      throw new BusinessError(ErrorCode.DATA_NOT_EXIST);
+    }
+    const newdata = { name, mobile, type: '2', uid: sch.id };
+    newdata.passwd = { secret: passwd };
+    const res = await this.model.create(newdata);
+    return res;
+  }
+
+  // 学生绑定
+  async bind(data) {
     const { openid, id_number, mobile, passwd } = data;
     assert(openid && id_number && mobile, '缺少部分信息项');
     let user = await this.model.findOne({ mobile });
@@ -55,7 +73,7 @@ class UserService extends CrudService {
       await stud.save();
       const newdata = { name: stud.name, mobile: stud.phone, type: '4', uid: stud.id, openid };
       newdata.passwd = { secret: '12345678' };
-      user = await this.umodel.create(newdata);
+      user = await this.model.create(newdata);
     }
     return user;
   }