sun-pc-linux пре 1 месец
родитељ
комит
4d8e14eeea

+ 5 - 1
pack-nrhe/mongo-migrations/1_users.down.json

@@ -11,5 +11,9 @@
              "limit" : 1
            }
         ]
-     }
+     },
+     {
+      "dropIndex": "users",
+      "key": { "loginName": 1 }
+  }
 ]

+ 6 - 1
pack-nrhe/mongo-migrations/1_users.up.json

@@ -13,5 +13,10 @@
             "updateTime": "2024-10-15T08:47:05.583Z",
             "createTime": "2024-10-15T08:47:05.583Z"
         }]
-     }
+     },
+     {
+        "createIndex": "users",
+        "key": { "loginName": 1 },
+        "options": { "unique": true }
+    }
 ]

BIN
pack-nrhe/template/用户信息模板.xlsx


+ 4 - 7
src/api/user.go

@@ -359,13 +359,11 @@ func ImportUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			user.LoginName = us[0]
 			user.Nid = us[1]
 			if len(us[0]) == 0 {
-				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "用户名不能为空"))
-				continue
-			}
-			if len(us[1]) == 0 {
-				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "编号不能为空"))
+				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "账号不能为空"))
 				continue
 			}
+			// !查询用户,确保账号名唯一,用数据库唯一索引约束:db.users.createIndex({ loginName: 1 }, { unique: true })
+
 			roles := []string{}
 			if us[2] == "管理员" {
 				roles = []string{"admin"}
@@ -383,14 +381,13 @@ func ImportUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			} else if len(us[3]) == 32 {
 				user.Password = us[3]
 			}
-			user.LoginName = fmt.Sprintf("%s_%s", user.LoginName, user.Nid)
 
 			user.CreateTime = time.Now()
 			user.UpdateTime = time.Now()
 
 			_, err = repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user)
 			if err != nil {
-				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "保存数据失败, 请检查数据格式是否正确/号是否重复"))
+				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "保存数据失败, 请检查数据格式是否正确/号是否重复"))
 				log.Error(err)
 			}