e9d2e41f9e
- 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.
234 lines
5.9 KiB
Go
234 lines
5.9 KiB
Go
package dashboard
|
|
|
|
import (
|
|
"cs-agent/internal/builders"
|
|
"cs-agent/internal/pkg/constants"
|
|
"cs-agent/internal/pkg/dto/request"
|
|
"cs-agent/internal/pkg/dto/response"
|
|
"cs-agent/internal/pkg/enums"
|
|
"cs-agent/internal/pkg/httpx"
|
|
"cs-agent/internal/services"
|
|
|
|
"cs-agent/internal/pkg/httpx/params"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mlogclub/simple/web"
|
|
)
|
|
|
|
func UserAnyList(ctx *gin.Context) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
cnd := params.NewPagedSqlCnd(ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "username", Op: params.Like},
|
|
params.QueryFilter{ParamName: "nickname", Op: params.Like},
|
|
).Desc("id")
|
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
|
list, paging := services.UserService.FindPageByCnd(cnd)
|
|
results := builders.BuildUserList(list, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: false,
|
|
})
|
|
httpx.WriteJSON(ctx, &web.PageResult{Results: results, Page: paging})
|
|
return
|
|
}
|
|
|
|
func UserAnyList_all(ctx *gin.Context) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
cnd := params.NewSqlCnd(ctx,
|
|
params.QueryFilter{ParamName: "status"},
|
|
params.QueryFilter{ParamName: "username", Op: params.Like},
|
|
params.QueryFilter{ParamName: "nickname", Op: params.Like},
|
|
).Desc("id")
|
|
cnd.Where("status <> ?", enums.StatusDeleted)
|
|
|
|
list := services.UserService.Find(cnd)
|
|
results := builders.BuildUserList(list, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: false,
|
|
})
|
|
httpx.WriteJSON(ctx, results)
|
|
return
|
|
}
|
|
|
|
func UserGetBy(ctx *gin.Context, id int64) {
|
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserView); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
item := services.UserService.Get(id)
|
|
if item == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("用户不存在"))
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, builders.BuildUserResponse(item, builders.UserBuildOptions{
|
|
Roles: true,
|
|
Permissions: true,
|
|
}))
|
|
return
|
|
}
|
|
|
|
func UserPostCreate(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserCreate)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.CreateUserRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
user, generatedPassword, err := services.UserService.CreateUser(req, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, &response.CreateUserResultResponse{
|
|
User: builders.BuildUserResponse(user, builders.UserBuildOptions{Roles: true, Permissions: true}),
|
|
Password: generatedPassword,
|
|
})
|
|
return
|
|
}
|
|
|
|
func UserPostUpdate(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.UpdateUserRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.UserService.UpdateUser(req, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func UserPostDelete(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserDelete)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.DeleteUserRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.UserService.DeleteUser(req.ID, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func UserPostUpdate_status(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.UpdateUserStatusRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.UserService.UpdateStatus(req.ID, req.Status, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func UserPostReset_password(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserUpdate)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
var req struct {
|
|
UserID int64 `json:"userId"`
|
|
}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
password, err := services.UserService.ResetPassword(req.UserID, operator)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, map[string]any{
|
|
"password": password,
|
|
})
|
|
return
|
|
}
|
|
|
|
func UserPostChange_password(ctx *gin.Context) {
|
|
principal := services.AuthService.GetAuthPrincipal(ctx)
|
|
if principal == nil {
|
|
if _, err := services.AuthService.Authenticate(ctx); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
principal = services.AuthService.GetAuthPrincipal(ctx)
|
|
}
|
|
if principal == nil {
|
|
httpx.WriteJSON(ctx, web.JsonErrorMsg("未登录或登录已过期"))
|
|
return
|
|
}
|
|
|
|
req := request.ChangePasswordRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.UserService.ChangeOwnPassword(req.Password, principal); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|
|
|
|
func UserPostAssign_role(ctx *gin.Context) {
|
|
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionUserAssignRole)
|
|
if err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
|
|
req := request.AssignRoleRequest{}
|
|
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
if err := services.UserService.AssignRoles(req.UserID, req.RoleIDs, operator); err != nil {
|
|
httpx.WriteJSON(ctx, err)
|
|
return
|
|
}
|
|
httpx.WriteJSON(ctx, nil)
|
|
return
|
|
}
|