mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
146 lines
4.3 KiB
146 lines
4.3 KiB
package v1
|
|
|
|
import (
|
|
"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.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
if err := captcha.VerifyCode(req.CaptchaID, req.Captcha); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
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) LogOut(c *gin.Context) {
|
|
if err := userService.LogOut(c); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, nil)
|
|
}
|
|
|
|
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.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, 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) PageUsers(c *gin.Context) {
|
|
var req dto.SearchWithPage
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
|
|
total, list, err := userService.Page(req)
|
|
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.BatchDeleteReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
|
|
if err := userService.BatchDelete(req.Ids); 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.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
if err := global.VALID.Struct(req); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
id, err := helper.GetParamID(c)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
|
|
upMap := make(map[string]interface{})
|
|
upMap["email"] = req.Email
|
|
upMap["name"] = req.Name
|
|
if err := userService.Update(id, upMap); err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, nil)
|
|
}
|
|
|
|
func (b *BaseApi) GetUserInfo(c *gin.Context) {
|
|
id, err := helper.GetParamID(c)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
return
|
|
}
|
|
user, err := userService.Get(id)
|
|
if err != nil {
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
return
|
|
}
|
|
helper.SuccessWithData(c, user)
|
|
}
|