Browse Source

refactor: 修改管理员注册的url; 注册时,若用户存在,直接返回

fanyanpeng 2 years ago
parent
commit
0a2a358019
2 changed files with 24 additions and 16 deletions
  1. 23 15
      server/controller/user.js
  2. 1 1
      server/routes/index.js

+ 23 - 15
server/controller/user.js

@@ -77,25 +77,33 @@ class UserController {
   批量注册用户
   接收一个数组,返回一个map
    */
-  static async batchRegister(ctx){
+  static async registerWithAdministrator(ctx){
     if (!UserController.canManageUser(ctx)){
       return "Method Not Allowed."
     }
+    const userToRegister = ctx.request.body;
 
-    const usersToRegister = ctx.request.body;
-    let registrationResult = []
-
+    const {name, password, phone, email} = userToRegister
+    let msg = ""
+    let id = null
+    let result
+    try {
+      let user = await userModel.findByPhone(phone) // 若用户已经找到,直接返回
+      console.log(result)
+      if (user){
+        msg = "用户已存在"
+        id = user.id
 
-    for (const user of usersToRegister) {
-      const {name, password, phone, email} = user
-      try {
-        const result = await UserController.createUser(name, password, phone, email);
-        user.result = "创建成功"
-      }catch (e){
-        user.result = e.message
+      }else {
+        result = await UserController.createUser(name, password, phone, email);
+        msg = "新建用户"
+        id = result.data.id
       }
-      registrationResult.push({phone:phone,result:user.result})
+    }catch (e){
+      msg = e.message
     }
+    const registrationResult={phone:phone,result:msg,id:id}
+
     console.log(registrationResult)
     ctx.body = Response.success(registrationResult);
   }
@@ -129,7 +137,7 @@ class UserController {
     const result = await userModel.create(name, hashPassword, phone, email);
 
     if (result.code === 0) {
-      return result
+      return result //返回创建成功的对象
     } else if (result.code === -2) {
       throw new Error('此手机号已注册');
     } else if (result.code === -1) {
@@ -152,7 +160,7 @@ class UserController {
   // 验证注册数据是否合法
   static validateRegistrationData(name, password, phone, email) {
     const emailRegex  = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
-    const usernameRegex  = /^\w+$/; //由字母、数字、下划线组成且不包含其他字符的字符串
+    const nameRegex  = /^\w+$/; //由字母、数字、下划线组成且不包含其他字符的字符串
 
     if (!emailRegex.test(email)) {
       throw new Error('邮箱格式有误');
@@ -170,7 +178,7 @@ class UserController {
       throw new Error('手机号码长度应为11位');
     }
 
-    if (!usernameRegex.test(name)) {
+    if (!nameRegex.test(name)) {
       throw new Error('用户名只能包含字母、数字和下划线');
     }
 

+ 1 - 1
server/routes/index.js

@@ -10,7 +10,7 @@ const router = new Router({prefix: '/api'});
 module.exports = () => {
     router.post('/user/login', UserController.login); // 登录
     router.post('/user/register', UserController.register);
-    router.post('/usr/register/batch', UserController.batchRegister);
+    router.post('/user/management/register', UserController.registerWithAdministrator);
   router.post('/user/sendMessage', UserController.sendMessage);
   router.get('/user/getInfo', UserController.getInfo);
   router.post('/user/saveName', UserController.saveName);