Ver Fonte

Merge branch 'master' of http://git.cc-lotus.info/new_train/service-center

reloaded há 5 anos atrás
pai
commit
6e510707e9
4 ficheiros alterados com 47 adições e 11 exclusões
  1. 6 0
      app/controller/user.js
  2. 5 0
      app/router.js
  3. 11 11
      app/service/school.js
  4. 25 0
      app/service/user.js

+ 6 - 0
app/controller/user.js

@@ -17,6 +17,12 @@ class UserController extends Controller {
     const res = await this.service.create(this.ctx.request.body);
     this.ctx.ok({ data: res });
   }
+
+  // 学生注册
+  async register() {
+    const res = await this.service.register(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
 }
 
 module.exports = CrudController(UserController, meta);

+ 5 - 0
app/router.js

@@ -125,4 +125,9 @@ module.exports = app => {
   // 行政区划表设置路由
   router.resources('region', '/api/train/region', controller.region); // index、create、show、destroy
   router.post('region', '/api/train/region/update/:id', controller.region.update);
+
+  // 用户表设置路由
+  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);
 };

+ 11 - 11
app/service/school.js

@@ -34,11 +34,11 @@ class SchoolService extends CrudService {
     // 将数据存入数据库中
     for (const stu of studatas) {
       const res = await this.smodel.create(stu);
-      if (res) {
-        const newdata = { name: stu.name, mobile: stu.phone, type: '4', uid: res.id };
-        newdata.passwd = { secret: '12345678' };
-        await this.umodel.create(newdata);
-      }
+      // if (res) {
+      //   const newdata = { name: stu.name, mobile: stu.phone, type: '4', uid: res.id };
+      //   newdata.passwd = { secret: '12345678' };
+      //   await this.umodel.create(newdata);
+      // }
     }
     return datacheck;
   }
@@ -73,11 +73,11 @@ class SchoolService extends CrudService {
         entry_year: data[theadRule[8]],
         finish_year: data[theadRule[9]],
         school_job: data[theadRule[10]],
-        phone: data[theadRule[10]],
-        qq: data[theadRule[11]],
-        family_place: data[theadRule[12]],
-        family_is_hard: data[theadRule[13]],
-        have_grant: data[theadRule[14]],
+        phone: data[theadRule[11]],
+        qq: data[theadRule[12]],
+        family_place: data[theadRule[13]],
+        family_is_hard: data[theadRule[14]],
+        have_grant: data[theadRule[15]],
         termid,
         schid,
       });
@@ -112,7 +112,7 @@ class SchoolService extends CrudService {
         errorcode = '1';
         data.msg = data.msg + '手机号不允许为空,';
       }
-      if (/^\d{11}$/i.test(data.phone)) {
+      if (!/^\d{11}$/i.test(data.phone)) {
         errorcode = '1';
         data.msg = data.msg + '手机号不正确,';
 

+ 25 - 0
app/service/user.js

@@ -35,6 +35,31 @@ class UserService extends CrudService {
     return res;
   }
 
+  // 学生注册
+  async register(data) {
+    const { openid, id_number, mobile, passwd } = data;
+    assert(openid && id_number && mobile, '缺少部分信息项');
+    let user = await this.model.findOne({ mobile });
+    if (user) {
+      if (user.passwd.secret !== passwd) {
+        throw new BusinessError(ErrorCode.BAD_PASSWORD);
+      }
+      user.openid = openid;
+      await user.save();
+    } else {
+      const stud = await this.stuModel.findOne({ id_number });
+      if (!stud) {
+        throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+      }
+      stud.openid = openid;
+      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);
+    }
+    return user;
+  }
+
   // 通过openid查询用户信息
   async findByOpenid(openid) {
     // 通过openid查询用户信息