package admin import ( "fmt" "net/http" "bindbox-game/internal/code" "bindbox-game/internal/pkg/core" "bindbox-game/internal/pkg/utils" "bindbox-game/internal/pkg/validation" adminsvc "bindbox-game/internal/service/admin" ) type loginRequest struct { Username string `json:"username" binding:"required"` // 用户名 Password string `json:"password" binding:"required"` // 密码 (MD5加密后的密码) } type loginResponse struct { Token string `json:"token"` // 登录成功后颁发的 Token IsSuper int32 `json:"is_super"` // 是否是超级管理员(1:是 0:否) } // Login 管理员登录 // @Summary 管理员登录 // @Description 管理员登录 // @Tags 管理端.登录 // @Accept json // @Produce json // @Param RequestBody body loginRequest true "请求参数" // @Success 200 {object} loginResponse // @Failure 400 {object} code.Failure // @Router /api/admin/login [post] func (h *handler) Login() core.HandlerFunc { return func(ctx core.Context) { req := new(loginRequest) res := new(loginResponse) if err := ctx.ShouldBindJSON(req); err != nil { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.ParamBindError, validation.Error(err)), ) return } result, err := h.svc.Login(ctx.RequestContext(), adminsvc.LoginInput{ Username: req.Username, Password: req.Password, IP: utils.GetIP(ctx.Request()), }) if err != nil { ctx.AbortWithError(core.Error( http.StatusBadRequest, code.AdminLoginError, fmt.Sprintf("%s:%s", code.Text(code.AdminLoginError), err.Error())), ) return } res.Token = result.Token res.IsSuper = result.IsSuper ctx.Payload(res) } }