mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-17 21:08:25 +08:00
feat: demo 增加删除
This commit is contained in:
parent
80fb98e20c
commit
74556bcbd7
11 changed files with 64 additions and 42 deletions
|
|
@ -67,3 +67,12 @@ func SuccessWithData(ctx *gin.Context, data interface{}) {
|
||||||
ctx.JSON(http.StatusOK, res)
|
ctx.JSON(http.StatusOK, res)
|
||||||
ctx.Abort()
|
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
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,6 @@
|
||||||
package v1
|
package v1
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/pkg/errors"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
|
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
|
||||||
"github.com/1Panel-dev/1Panel/app/dto"
|
"github.com/1Panel-dev/1Panel/app/dto"
|
||||||
"github.com/1Panel-dev/1Panel/constant"
|
"github.com/1Panel-dev/1Panel/constant"
|
||||||
|
|
@ -96,7 +93,7 @@ func (b *BaseApi) PageUsers(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseApi) DeleteUser(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 {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
|
|
@ -106,7 +103,7 @@ func (b *BaseApi) DeleteUser(c *gin.Context) {
|
||||||
return
|
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)
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -123,11 +120,16 @@ func (b *BaseApi) UpdateUser(c *gin.Context) {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
id, err := helper.GetParamID(c)
|
||||||
|
if err != nil {
|
||||||
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
upMap := make(map[string]interface{})
|
upMap := make(map[string]interface{})
|
||||||
upMap["email"] = req.Email
|
upMap["email"] = req.Email
|
||||||
upMap["name"] = req.Name
|
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)
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -135,14 +137,12 @@ func (b *BaseApi) UpdateUser(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *BaseApi) GetUserInfo(c *gin.Context) {
|
func (b *BaseApi) GetUserInfo(c *gin.Context) {
|
||||||
idParam, ok := c.Params.Get("id")
|
id, err := helper.GetParamID(c)
|
||||||
if !ok {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, errors.New("error name"))
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
intNum, _ := strconv.Atoi(idParam)
|
user, err := userService.Get(id)
|
||||||
|
|
||||||
user, err := userService.Get(uint(intNum))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,10 @@ type OperationWithName struct {
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type BatchDeleteReq struct {
|
||||||
|
Ids []uint `json:"ids" validate:"required"`
|
||||||
|
}
|
||||||
|
|
||||||
type OperationWithNameAndType struct {
|
type OperationWithNameAndType struct {
|
||||||
Name string `json:"name" validate:"required"`
|
Name string `json:"name" validate:"required"`
|
||||||
Type string `json:"type" validate:"required"`
|
Type string `json:"type" validate:"required"`
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ type ICommonRepo interface {
|
||||||
WithByID(id uint) DBOption
|
WithByID(id uint) DBOption
|
||||||
WithByName(name string) DBOption
|
WithByName(name string) DBOption
|
||||||
WithOrderBy(orderStr string) DBOption
|
WithOrderBy(orderStr string) DBOption
|
||||||
|
WithLikeName(name string) DBOption
|
||||||
|
WithIdsIn(ids []uint) DBOption
|
||||||
}
|
}
|
||||||
|
|
||||||
type CommonRepo struct{}
|
type CommonRepo struct{}
|
||||||
|
|
@ -35,3 +37,9 @@ func (c *CommonRepo) WithOrderBy(orderStr string) DBOption {
|
||||||
return g.Order(orderStr)
|
return g.Order(orderStr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *CommonRepo) WithIdsIn(ids []uint) DBOption {
|
||||||
|
return func(g *gorm.DB) *gorm.DB {
|
||||||
|
return g.Where("id in (?)", ids)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ type IUserService interface {
|
||||||
Delete(name string) error
|
Delete(name string) error
|
||||||
Save(req model.User) error
|
Save(req model.User) error
|
||||||
Update(id uint, upMap map[string]interface{}) error
|
Update(id uint, upMap map[string]interface{}) error
|
||||||
|
BatchDelete(ids []uint) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewIUserService() IUserService {
|
func NewIUserService() IUserService {
|
||||||
|
|
@ -132,6 +133,10 @@ func (u *UserService) Delete(name string) error {
|
||||||
return userRepo.Delete(commonRepo.WithByName(name))
|
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 {
|
func (u *UserService) Save(req model.User) error {
|
||||||
return userRepo.Save(req)
|
return userRepo.Save(req)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup) {
|
||||||
baseApi := v1.ApiGroupApp.BaseApi
|
baseApi := v1.ApiGroupApp.BaseApi
|
||||||
{
|
{
|
||||||
withRecordRouter.POST("", baseApi.Register)
|
withRecordRouter.POST("", baseApi.Register)
|
||||||
withRecordRouter.DELETE("", baseApi.DeleteUser)
|
withRecordRouter.POST("/del", baseApi.DeleteUser)
|
||||||
userRouter.POST("/search", baseApi.PageUsers)
|
userRouter.POST("/search", baseApi.PageUsers)
|
||||||
userRouter.GET(":id", baseApi.GetUserInfo)
|
userRouter.GET(":id", baseApi.GetUserInfo)
|
||||||
userRouter.POST(":id", baseApi.UpdateUser)
|
userRouter.POST(":id", baseApi.UpdateUser)
|
||||||
|
|
|
||||||
|
|
@ -27,5 +27,5 @@ export const editUser = (params: User.User) => {
|
||||||
|
|
||||||
// * 批量删除用户
|
// * 批量删除用户
|
||||||
export const deleteUser = (params: { ids: number[] }) => {
|
export const deleteUser = (params: { ids: number[] }) => {
|
||||||
return http.post(`/users/delete`, params);
|
return http.post(`/users/del`, params);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ export const useDeleteData = <P = any, R = any>(
|
||||||
if (!res) return reject(false);
|
if (!res) return reject(false);
|
||||||
ElMessage({
|
ElMessage({
|
||||||
type: 'success',
|
type: 'success',
|
||||||
message: i18n.global.t('commons.msg.deleteSuccss'),
|
message: i18n.global.t('commons.msg.deleteSuccess'),
|
||||||
});
|
});
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ const demoRouter = {
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
path: '/demos/table/:op/:id?',
|
path: '/demos/table/:op/:id?',
|
||||||
name: 'DemoCreate',
|
name: 'DemoOperate',
|
||||||
props: true,
|
props: true,
|
||||||
hidden: true,
|
hidden: true,
|
||||||
component: () => import('@/views/demos/table/operate/index.vue'),
|
component: () => import('@/views/demos/table/operate/index.vue'),
|
||||||
|
|
|
||||||
|
|
@ -59,38 +59,34 @@ const openOperate = (row: User.User | null) => {
|
||||||
params.id = row.id;
|
params.id = row.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
router.push({ name: 'DemoCreate', params });
|
router.push({ name: 'DemoOperate', params });
|
||||||
|
};
|
||||||
|
|
||||||
|
const batchDelete = async (row: User.User | null) => {
|
||||||
|
let ids: Array<number> = [];
|
||||||
|
|
||||||
|
if (row === null) {
|
||||||
|
selects.value.forEach((item: User.User) => {
|
||||||
|
ids.push(item.id);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
ids.push(row.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
await useDeleteData(deleteUser, { ids: ids }, 'commons.msg.delete');
|
||||||
|
search();
|
||||||
};
|
};
|
||||||
const buttons = [
|
const buttons = [
|
||||||
{
|
{
|
||||||
label: i18n.global.t('commons.button.edit'),
|
label: i18n.global.t('commons.button.edit'),
|
||||||
click: openOperate,
|
click: openOperate,
|
||||||
},
|
},
|
||||||
// {
|
{
|
||||||
// label: '执行',
|
label: i18n.global.t('commons.button.delete'),
|
||||||
// click: buttonClick,
|
type: 'danger',
|
||||||
// },
|
click: batchDelete,
|
||||||
// {
|
},
|
||||||
// label: '删除',
|
|
||||||
// type: 'danger',
|
|
||||||
// click: buttonClick,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// label: '复制',
|
|
||||||
// click: buttonClick,
|
|
||||||
// },
|
|
||||||
// {
|
|
||||||
// label: '定时任务',
|
|
||||||
// click: buttonClick,
|
|
||||||
// },
|
|
||||||
];
|
];
|
||||||
const batchDelete = async () => {
|
|
||||||
let ids: Array<number> = [];
|
|
||||||
selects.value.forEach((item: User.User) => {
|
|
||||||
ids.push(item.id);
|
|
||||||
});
|
|
||||||
await useDeleteData(deleteUser, { ids: ids }, 'commons.msg.delete');
|
|
||||||
};
|
|
||||||
|
|
||||||
const search = async () => {
|
const search = async () => {
|
||||||
userSearch.page = paginationConfig.page;
|
userSearch.page = paginationConfig.page;
|
||||||
|
|
|
||||||
|
|
@ -77,10 +77,10 @@ const submitForm = async (formEl: FormInstance | undefined) => {
|
||||||
const getUser = async (id: number) => {
|
const getUser = async (id: number) => {
|
||||||
const res = await getUserById(id);
|
const res = await getUserById(id);
|
||||||
demoForm.value = res.data;
|
demoForm.value = res.data;
|
||||||
console.log(demoForm);
|
|
||||||
};
|
};
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
if (props.op == 'edit') {
|
if (props.op == 'edit') {
|
||||||
|
console.log(props);
|
||||||
getUser(props.id).catch(() => {
|
getUser(props.id).catch(() => {
|
||||||
router.back();
|
router.back();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue