diff --git a/backend/app/api/v1/helper/helper.go b/backend/app/api/v1/helper/helper.go index ec7fbc39a..dab2f5861 100644 --- a/backend/app/api/v1/helper/helper.go +++ b/backend/app/api/v1/helper/helper.go @@ -67,3 +67,12 @@ func SuccessWithData(ctx *gin.Context, data interface{}) { ctx.JSON(http.StatusOK, res) ctx.Abort() } + +func GetParamID(c *gin.Context) (uint, error) { + idParam, ok := c.Params.Get("id") + if !ok { + return 0, errors.New("error name") + } + intNum, _ := strconv.Atoi(idParam) + return uint(intNum), nil +} diff --git a/backend/app/api/v1/user.go b/backend/app/api/v1/user.go index f511ce2fc..58ba96939 100644 --- a/backend/app/api/v1/user.go +++ b/backend/app/api/v1/user.go @@ -1,9 +1,6 @@ package v1 import ( - "github.com/pkg/errors" - "strconv" - "github.com/1Panel-dev/1Panel/app/api/v1/helper" "github.com/1Panel-dev/1Panel/app/dto" "github.com/1Panel-dev/1Panel/constant" @@ -96,7 +93,7 @@ func (b *BaseApi) PageUsers(c *gin.Context) { } func (b *BaseApi) DeleteUser(c *gin.Context) { - var req dto.OperationWithName + var req dto.BatchDeleteReq if err := c.ShouldBindJSON(&req); err != nil { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return @@ -106,7 +103,7 @@ func (b *BaseApi) DeleteUser(c *gin.Context) { return } - if err := userService.Delete(req.Name); err != nil { + if err := userService.BatchDelete(req.Ids); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } @@ -123,11 +120,16 @@ func (b *BaseApi) UpdateUser(c *gin.Context) { helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } + id, err := helper.GetParamID(c) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return + } upMap := make(map[string]interface{}) upMap["email"] = req.Email upMap["name"] = req.Name - if err := userService.Update(req.ID, upMap); err != nil { + if err := userService.Update(id, upMap); err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return } @@ -135,14 +137,12 @@ func (b *BaseApi) UpdateUser(c *gin.Context) { } func (b *BaseApi) GetUserInfo(c *gin.Context) { - idParam, ok := c.Params.Get("id") - if !ok { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name")) + id, err := helper.GetParamID(c) + if err != nil { + helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) return } - intNum, _ := strconv.Atoi(idParam) - - user, err := userService.Get(uint(intNum)) + user, err := userService.Get(id) if err != nil { helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err) return diff --git a/backend/app/dto/common_req.go b/backend/app/dto/common_req.go index 42d3c2098..0b03c629a 100644 --- a/backend/app/dto/common_req.go +++ b/backend/app/dto/common_req.go @@ -10,6 +10,10 @@ type OperationWithName struct { Name string `json:"name" validate:"required"` } +type BatchDeleteReq struct { + Ids []uint `json:"ids" validate:"required"` +} + type OperationWithNameAndType struct { Name string `json:"name" validate:"required"` Type string `json:"type" validate:"required"` diff --git a/backend/app/repo/common.go b/backend/app/repo/common.go index df42ac6a2..c4afede8e 100644 --- a/backend/app/repo/common.go +++ b/backend/app/repo/common.go @@ -8,6 +8,8 @@ type ICommonRepo interface { WithByID(id uint) DBOption WithByName(name string) DBOption WithOrderBy(orderStr string) DBOption + WithLikeName(name string) DBOption + WithIdsIn(ids []uint) DBOption } type CommonRepo struct{} @@ -35,3 +37,9 @@ func (c *CommonRepo) WithOrderBy(orderStr string) DBOption { return g.Order(orderStr) } } + +func (c *CommonRepo) WithIdsIn(ids []uint) DBOption { + return func(g *gorm.DB) *gorm.DB { + return g.Where("id in (?)", ids) + } +} diff --git a/backend/app/service/user.go b/backend/app/service/user.go index c230c9bbd..d75b54eff 100644 --- a/backend/app/service/user.go +++ b/backend/app/service/user.go @@ -25,6 +25,7 @@ type IUserService interface { Delete(name string) error Save(req model.User) error Update(id uint, upMap map[string]interface{}) error + BatchDelete(ids []uint) error } func NewIUserService() IUserService { @@ -132,6 +133,10 @@ func (u *UserService) Delete(name string) error { return userRepo.Delete(commonRepo.WithByName(name)) } +func (u *UserService) BatchDelete(ids []uint) error { + return userRepo.Delete(commonRepo.WithIdsIn(ids)) +} + func (u *UserService) Save(req model.User) error { return userRepo.Save(req) } diff --git a/backend/router/ro_user.go b/backend/router/ro_user.go index 1c88b3741..fedd3fa88 100644 --- a/backend/router/ro_user.go +++ b/backend/router/ro_user.go @@ -16,7 +16,7 @@ func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) { baseApi := v1.ApiGroupApp.BaseApi { withRecordRouter.POST("", baseApi.Register) - withRecordRouter.DELETE("", baseApi.DeleteUser) + withRecordRouter.POST("/del", baseApi.DeleteUser) userRouter.POST("/search", baseApi.PageUsers) userRouter.GET(":id", baseApi.GetUserInfo) userRouter.POST(":id", baseApi.UpdateUser) diff --git a/frontend/src/api/modules/user.ts b/frontend/src/api/modules/user.ts index 129df2489..32bcc57e4 100644 --- a/frontend/src/api/modules/user.ts +++ b/frontend/src/api/modules/user.ts @@ -27,5 +27,5 @@ export const editUser = (params: User.User) => { // * 批量删除用户 export const deleteUser = (params: { ids: number[] }) => { - return http.post(`/users/delete`, params); + return http.post(`/users/del`, params); }; diff --git a/frontend/src/hooks/use-delete-data.ts b/frontend/src/hooks/use-delete-data.ts index a50f3dc23..3355ec208 100644 --- a/frontend/src/hooks/use-delete-data.ts +++ b/frontend/src/hooks/use-delete-data.ts @@ -27,7 +27,7 @@ export const useDeleteData =
(
if (!res) return reject(false);
ElMessage({
type: 'success',
- message: i18n.global.t('commons.msg.deleteSuccss'),
+ message: i18n.global.t('commons.msg.deleteSuccess'),
});
resolve(true);
});
diff --git a/frontend/src/routers/modules/demo.ts b/frontend/src/routers/modules/demo.ts
index 9786e5d66..3b4e84e02 100644
--- a/frontend/src/routers/modules/demo.ts
+++ b/frontend/src/routers/modules/demo.ts
@@ -21,7 +21,7 @@ const demoRouter = {
},
{
path: '/demos/table/:op/:id?',
- name: 'DemoCreate',
+ name: 'DemoOperate',
props: true,
hidden: true,
component: () => import('@/views/demos/table/operate/index.vue'),
diff --git a/frontend/src/views/demos/table/index.vue b/frontend/src/views/demos/table/index.vue
index 34dc850f8..491dc8b95 100644
--- a/frontend/src/views/demos/table/index.vue
+++ b/frontend/src/views/demos/table/index.vue
@@ -59,38 +59,34 @@ const openOperate = (row: User.User | null) => {
params.id = row.id;
}
- router.push({ name: 'DemoCreate', params });
+ router.push({ name: 'DemoOperate', params });
+};
+
+const batchDelete = async (row: User.User | null) => {
+ let ids: Array