mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-18 21:38:57 +08:00
134 lines
4 KiB
Go
134 lines
4 KiB
Go
package v1
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
|
|
"github.com/1Panel-dev/1Panel/app/dto"
|
|
"github.com/1Panel-dev/1Panel/constant"
|
|
"github.com/1Panel-dev/1Panel/global"
|
|
"github.com/1Panel-dev/1Panel/utils/captcha"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type BaseApi struct{}
|
|
|
|
func (b *BaseApi) Login(c *gin.Context) {
|
|
var req dto.Login
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqBody, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamValid, err)
|
|
return
|
|
}
|
|
if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqBody, errors.New("captcha code error"))
|
|
return
|
|
}
|
|
|
|
user, err := userService.Login(c, req)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, user)
|
|
}
|
|
|
|
func (b *BaseApi) Captcha(c *gin.Context) {
|
|
captcha, err := captcha.CreateCaptcha()
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
}
|
|
helper.SuccessWithData(c, captcha)
|
|
}
|
|
|
|
func (b *BaseApi) Register(c *gin.Context) {
|
|
var req dto.UserCreate
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqBody, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamValid, err)
|
|
return
|
|
}
|
|
if err := userService.Register(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, nil)
|
|
}
|
|
|
|
func (b *BaseApi) GetUserList(c *gin.Context) {
|
|
pagenation, isOK := helper.GeneratePaginationFromReq(c)
|
|
if !isOK {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqQuery, constant.ErrPageParam)
|
|
return
|
|
}
|
|
|
|
total, list, err := userService.Page(pagenation.Page, pagenation.PageSize)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
Items: list,
|
|
Total: total,
|
|
})
|
|
}
|
|
|
|
func (b *BaseApi) DeleteUser(c *gin.Context) {
|
|
var req dto.OperationWithName
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqBody, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamValid, err)
|
|
return
|
|
}
|
|
|
|
if err := userService.Delete(req.Name); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, nil)
|
|
}
|
|
|
|
func (b *BaseApi) UpdateUser(c *gin.Context) {
|
|
var req dto.UserUpdate
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqBody, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamValid, err)
|
|
return
|
|
}
|
|
|
|
upMap := make(map[string]interface{})
|
|
upMap["email"] = req.Email
|
|
if err := userService.Update(upMap); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, nil)
|
|
}
|
|
|
|
func (b *BaseApi) GetUserInfo(c *gin.Context) {
|
|
name, ok := c.Params.Get("name")
|
|
if !ok {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeParamInReqQuery, errors.New("error name"))
|
|
return
|
|
}
|
|
|
|
user, err := userService.Get(name)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, user)
|
|
}
|