lrf 2 年之前
父節點
當前提交
27af4b1361
共有 1 個文件被更改,包括 14 次插入0 次删除
  1. 14 0
      app/service/user/admin.js

+ 14 - 0
app/service/user/admin.js

@@ -10,6 +10,20 @@ class AdminService extends CrudService {
     super(ctx, 'admin');
     super(ctx, 'admin');
     this.model = this.ctx.model.User.Admin;
     this.model = this.ctx.model.User.Admin;
   }
   }
+
+  async beforeCreate(data) {
+    const { account } = data;
+    const num = await this.model.count({ account });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
+    return data;
+  }
+  async beforeUpdate(filter, update) {
+    const { account } = update;
+    const id = _.get(filter, '_id', _.get(filter, 'id'));
+    const num = await this.model.count({ _id: { $ne: id }, account });
+    if (num > 0) throw new BusinessError(ErrorCode.DATA_EXISTED, '该账号名已存在');
+    return { filter, update };
+  }
   /**
   /**
    * 登陆
    * 登陆
    * @param {Object} body 登陆参数
    * @param {Object} body 登陆参数