sunsheng há 1 ano atrás
pai
commit
d36aefe3ff
2 ficheiros alterados com 29 adições e 8 exclusões
  1. 0 3
      src/api/test.go
  2. 29 5
      src/api/user.go

+ 0 - 3
src/api/test.go

@@ -16,9 +16,6 @@ import (
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
-// 用户唯一
-// db.collection.createIndex({ loginName: 1 }, { unique: true })
-
 // 新增试题
 // /admin/test/create/:scope
 func CreateTest(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 29 - 5
src/api/user.go

@@ -62,6 +62,8 @@ func UserLoginPassword(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	return out, nil
 }
 
+// 用户唯一
+// db.users.createIndex({ nid: 1 }, { unique: true })
 func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 验证是否为管理员
 	isAdmin, err := IsAdmin(c, apictx)
@@ -78,19 +80,30 @@ func CreateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		log.Error(err)
 		return nil, err
 	}
-
-	// 验证登录名是否存在
+	// 验证编号是否存在
 	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
 		CollectName: repo.CollectionUser,
-		Query:       repo.Map{"loginName": user.LoginName},
+		Query:       repo.Map{"nid": user.Nid},
 	}, user)
 	if err != nil {
 		return nil, err
 	}
 	if found {
-		return nil, errors.New("该号已存在")
+		return nil, errors.New("该号已存在")
 	}
 
+	// ! 编号唯一 验证了编号就不用验证登录名是否存在
+	// found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+	// 	CollectName: repo.CollectionUser,
+	// 	Query:       repo.Map{"loginName": user.LoginName},
+	// }, user)
+	// if err != nil {
+	// 	return nil, err
+	// }
+	// if found {
+	// 	return nil, errors.New("该账号已存在")
+	// }
+
 	// student,teacher,admin
 	if len(user.Roles) < 1 {
 		user.Roles = []string{"student"}
@@ -215,6 +228,17 @@ func UpdateUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if user.Id.IsZero() {
 		return nil, errors.New("id错误")
 	}
+	su := &model.User{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionUser,
+		Query:       repo.Map{"nid": user.Nid},
+	}, su)
+	if err != nil {
+		return nil, err
+	}
+	if found {
+		return nil, errors.New("该编号已存在")
+	}
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, user.Id.Hex(), user)
 }
 
@@ -360,7 +384,7 @@ func ImportUser(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 			_, 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)
 			}