Przeglądaj źródła

增加注册方法

liuyu 4 lat temu
rodzic
commit
aa0caa6565
1 zmienionych plików z 65 dodań i 0 usunięć
  1. 65 0
      app/service/roomuser.js

+ 65 - 0
app/service/roomuser.js

@@ -11,6 +11,71 @@ class RoomuserService extends CrudService {
   constructor(ctx) {
     super(ctx, 'room_user');
     this.model = this.ctx.model.Roomuser;
+    this.umodel = this.ctx.model.User;
+  }
+
+  async create(data) {
+    const { name, password } = data;
+    assert(name, '用户名不能为空');
+    assert(password, '密码不能为空');
+    const { phone } = data;
+    const has_phone = await this.model.findOne({ phone });
+    if (has_phone) {
+      throw new BusinessError('此手机号已被注册,请更换手机号');
+    }
+    const newdata = data;
+    newdata.password = { secret: password };
+    const res = await this.model.create(newdata);
+    if (res) {
+      const newdata = {
+        name,
+        phone: data.phone,
+        passwd: password,
+        uid: res.id,
+        role: data.role,
+        deptname: data.deptname,
+        remark: data.remark,
+      };
+      await this.umodel.create(newdata);
+    }
+    return res;
+  }
+
+  async update({ id }, data) {
+    const user = await this.model.findById(id);
+    const { phone } = data;
+    const phoneList = await this.model.find({ phone });
+    const is_has = phoneList.find(f => f.id !== id);
+    if (is_has) throw new BusinessError('此手机号已被注册,请更换手机号');
+    if (data.name) {
+      user.name = data.name;
+    }
+    if (data.role) {
+      user.role = data.role;
+    }
+    if (data.deptname) {
+      user.deptname = data.deptname;
+    }
+    if (data.level) {
+      user.level = data.level;
+    }
+    if (data.title) {
+      user.title = data.title;
+    }
+    if (data.remark) {
+      user.remark = data.remark;
+    }
+    if (data.openid) {
+      user.openid = data.openid;
+    }
+    const res = await user.save();
+    if (res) {
+      const uu = await this.umodel.findOne({ uid: user.id });
+      uu.name = data.name;
+      uu.deptname = data.deptname;
+      await this.umodel.save();
+    }
+    return res;
   }
 
 }