Refactor controllers to use httpx for JSON responses and eliminate context dependency
- Updated TagController methods to use httpx.WriteJSON for consistent JSON response handling. - Refactored TicketController to replace context-dependent methods with standalone functions using httpx. - Modified UserController to utilize httpx for error handling and response formatting. - Adjusted WechatController to remove context dependency and standardize response handling.
This commit is contained in:
@@ -4,25 +4,24 @@ import (
|
||||
"cs-agent/internal/pkg/constants"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/dto/response"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/pkg/utils"
|
||||
"cs-agent/internal/services"
|
||||
"time"
|
||||
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
type SessionController struct {
|
||||
Ctx *gin.Context
|
||||
}
|
||||
|
||||
func (c *SessionController) AnyList() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func SessionAnyList(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
cnd := params.NewPagedSqlCnd(c.Ctx,
|
||||
cnd := params.NewPagedSqlCnd(ctx,
|
||||
params.QueryFilter{ParamName: "userId"},
|
||||
params.QueryFilter{ParamName: "clientType"},
|
||||
).Desc("id")
|
||||
@@ -45,37 +44,46 @@ func (c *SessionController) AnyList() *web.JsonResult {
|
||||
LastSeenAt: utils.FormatTimePtr(item.LastSeenAt),
|
||||
})
|
||||
}
|
||||
return web.JsonData(&web.PageResult{Results: results, Page: paging})
|
||||
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SessionController) PostRevoke() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke)
|
||||
func SessionPostRevoke(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionRevoke)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.RevokeSessionRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.LoginSessionService.Revoke(req.ID, user.UserID, user.Username); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *SessionController) PostRevokeByUser() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionSessionRevoke)
|
||||
func SessionPostRevokeByUser(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionSessionRevoke)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
|
||||
req := request.RevokeUserSessionsRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.LoginSessionService.RevokeByUser(req.UserID, user.UserID, user.Username); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user