Browse Source

add batch delete

animeic 2 years ago
parent
commit
9e683efa0a
1 changed files with 51 additions and 27 deletions
  1. 51 27
      oilseal-train/api/user.go

+ 51 - 27
oilseal-train/api/user.go

@@ -22,7 +22,7 @@ func User(r *GinRouter) {
 	r.POSTJWT("/user/create", UserAdd)
 	r.GETJWT("/user/list", UserList)
 	r.POSTJWT("/user/update", UserEdit)
-	r.POSTJWT("/user/delete/:id", UserDelete)
+	r.POSTJWT("/user/delete/:ids", UserDelete)
 	r.GETJWT("/user/exportXls", UserExportXls)
 	r.POSTJWT("/user/importXls", UserImportXls)
 	r.GETJWT("/user/template", UserTemplate)
@@ -47,16 +47,16 @@ func Login(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	}
 	found, err := repo.RepoSeachDoc(ctx, options, user)
 	if err != nil {
-		return false, err
+		return nil, err
 	}
 	if !found {
-		return false, NewError("账号密码不对")
+		return nil, NewError("账号密码不对")
 	}
 	if user.State == -1 {
-		return false, NewError("当前用户已禁用")
+		return nil, NewError("当前用户已禁用")
 	}
 	if form.Role != user.Role {
-		return false, NewError("当前账号角色不正确")
+		return nil, NewError("当前账号角色不正确")
 	}
 
 	// 封装JWT
@@ -64,7 +64,7 @@ func Login(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 	token, _, err := apictx.Svc.JWT.JwtCreateToken(jwtU)
 	if err != nil {
-		return false, err
+		return nil, err
 	}
 
 	// 前端返回处理
@@ -124,11 +124,12 @@ func Register(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 // 添加用户
 func UserAdd(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 操作用户为admin
-	if apictx.User.Role != "admin" {
-		return nil, errors.New("当前用户不是管理员")
+	err := IsAdmin(apictx)
+	if err != nil {
+		return nil, err
 	}
 	var form model.User
-	err := c.ShouldBindJSON(&form)
+	err = c.ShouldBindJSON(&form)
 	if err != nil {
 		return nil, errors.New("参数错误")
 	}
@@ -177,14 +178,16 @@ func UserList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 // 更新用户
 func UserEdit(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 操作用户为admin
-	if apictx.User.Role != "admin" {
-		return nil, errors.New("当前用户不是管理员")
+	err := IsAdmin(apictx)
+	if err != nil {
+		return nil, err
 	}
 	var form model.User
-	err := c.ShouldBindJSON(&form)
+	err = c.ShouldBindJSON(&form)
 	if err != nil {
 		return nil, errors.New("参数错误")
 	}
+	fmt.Println(len(form.Id))
 	if len(form.Id) != 12 {
 		return nil, errors.New("id不正确")
 	}
@@ -196,18 +199,26 @@ func UserEdit(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionUser, form.Id.Hex(), &form)
 }
 
-// 删除用户
+// 批量删除用户
 func UserDelete(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 操作用户为admin
-	if apictx.User.Role != "admin" {
-		return nil, errors.New("当前用户不是管理员")
-	}
-	_id := c.Param("id")
-	_, err := primitive.ObjectIDFromHex(_id)
+	err := IsAdmin(apictx)
 	if err != nil {
-		return nil, errors.New("id错误")
+		return nil, err
 	}
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, _id)
+	_ids := c.Param("ids")
+	ids := strings.Split(_ids, ",")
+	if len(ids) > 0 {
+		for _, id := range ids {
+			fmt.Println(len(id))
+			if len(id) == 24 {
+				repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionUser, id)
+
+			}
+		}
+
+	}
+	return true, nil
 }
 
 type ExcelColumn struct {
@@ -220,17 +231,18 @@ type ExcelColumn struct {
 // 6396e841e90c9e38d70793d0,6396e841e90c9e38d70793d0
 func UserExportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 操作用户为admin
-	if apictx.User.Role != "admin" {
-		return nil, errors.New("当前用户不是管理员")
+	err := IsAdmin(apictx)
+	if err != nil {
+		return nil, err
 	}
 	selections := c.Query("selections")
 	query := repo.Map{}
-	if len(selections) >= 12 {
+	if len(selections) >= 24 {
 		selects := strings.Split(selections, ",")
 		query["_id"] = bson.M{"$in": selects}
 	}
 	users := make([]map[string]interface{}, 0)
-	err := repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+	err = repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
 		CollectName: repo.CollectionUser,
 		Query:       query,
 		Sort:        bson.M{"_id": -1},
@@ -350,8 +362,9 @@ func UserExportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
 func UserImportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// 操作用户为admin
-	if apictx.User.Role != "admin" {
-		return nil, errors.New("当前用户不是管理员")
+	err := IsAdmin(apictx)
+	if err != nil {
+		return nil, err
 	}
 	file, _, err := c.Request.FormFile("file")
 	if err != nil {
@@ -366,6 +379,8 @@ func UserImportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, err
 	}
 	errors := []string{}
+	failNum := 0
+	sucessNum := 0
 	if len(users) > 0 {
 		for index, user := range users {
 			if index == 0 {
@@ -375,18 +390,27 @@ func UserImportXls(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			addUser, err := formatUser(user, rowNum)
 			if err != nil {
 				errors = append(errors, err.Error())
+				failNum++
 				continue
 			}
 			_, err = repo.RepoAddDoc(apictx.CreateRepoCtx(), repo.CollectionUser, addUser)
 			if err != nil {
+				failNum++
 				errors = append(errors, fmt.Sprintf("第%d行错误: %s", rowNum, "保存数据失败"))
 				log.Error(err)
+			} else {
+				sucessNum++
 			}
 
 		}
 	}
+	result := map[string]interface{}{
+		"errors":    errors,
+		"failNum":   failNum,
+		"sucessNum": sucessNum,
+	}
 
-	return errors, nil
+	return result, nil
 }
 
 func formatUser(u []string, rowNum int) (*model.User, error) {