add batch delete user token

This commit is contained in:
lejianwen
2024-12-06 10:36:27 +08:00
parent 7749ff0729
commit 1c5cd3ae62
4 changed files with 18 additions and 20 deletions

View File

@@ -88,37 +88,26 @@ func (ct *UserToken) Delete(c *gin.Context) {
// @Description 登录凭证批量删除 // @Description 登录凭证批量删除
// @Accept json // @Accept json
// @Produce json // @Produce json
// @Param body body model.UserToken true "登录凭证信息" // @Param body body admin.UserTokenBatchDeleteForm true "登录凭证信息"
// @Success 200 {object} response.Response // @Success 200 {object} response.Response
// @Failure 500 {object} response.Response // @Failure 500 {object} response.Response
// @Router /admin/user_token/delete [post] // @Router /admin/user_token/delete [post]
// @Security token // @Security token
func (ct *UserToken) BatchDelete(c *gin.Context) { func (ct *UserToken) BatchDelete(c *gin.Context) {
f := &model.UserToken{} f := &admin.UserTokenBatchDeleteForm{}
if err := c.ShouldBindJSON(f); err != nil { if err := c.ShouldBindJSON(f); err != nil {
response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error()) response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
return return
} }
id := f.Id ids := f.Ids
errList := global.Validator.ValidVar(c, id, "required,gt=0") if len(ids) == 0 {
if len(errList) > 0 { response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
response.Fail(c, 101, errList[0])
return return
} }
l := service.AllService.UserService.TokenInfoById(f.Id) err := service.AllService.UserService.BatchDeleteUserToken(ids)
u := service.AllService.UserService.CurUser(c) if err == nil {
if !service.AllService.UserService.IsAdmin(u) && l.UserId != u.Id { response.Success(c, nil)
response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
return return
} }
if l.Id > 0 { response.Fail(c, 101, err.Error())
err := service.AllService.UserService.DeleteToken(l)
if err == nil {
response.Success(c, nil)
return
}
response.Fail(c, 101, err.Error())
return
}
response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
} }

View File

@@ -69,3 +69,7 @@ type RegisterForm struct {
Password string `json:"password" validate:"required,gte=4,lte=32"` Password string `json:"password" validate:"required,gte=4,lte=32"`
ConfirmPassword string `json:"confirm_password" validate:"required,gte=4,lte=32"` ConfirmPassword string `json:"confirm_password" validate:"required,gte=4,lte=32"`
} }
type UserTokenBatchDeleteForm struct {
Ids []uint `json:"ids" validate:"required"`
}

View File

@@ -195,6 +195,7 @@ func UserTokenBind(rg *gin.RouterGroup) {
cont := &admin.UserToken{} cont := &admin.UserToken{}
aR.GET("/list", cont.List) aR.GET("/list", cont.List)
aR.POST("/delete", cont.Delete) aR.POST("/delete", cont.Delete)
aR.POST("/batchDelete", cont.BatchDelete)
} }
func ConfigBind(rg *gin.RouterGroup) { func ConfigBind(rg *gin.RouterGroup) {
aR := rg.Group("/config") aR := rg.Group("/config")

View File

@@ -458,3 +458,7 @@ func (us *UserService) AutoRefreshAccessToken(ut *model.UserToken) {
us.RefreshAccessToken(ut) us.RefreshAccessToken(ut)
} }
} }
func (us *UserService) BatchDeleteUserToken(ids []uint) error {
return global.DB.Where("id in ?", ids).Delete(&model.UserToken{}).Error
}