262 lines
6.6 KiB
Go
262 lines
6.6 KiB
Go
package admin
|
|
|
|
import (
|
|
"Gwen/global"
|
|
"Gwen/http/request/admin"
|
|
"Gwen/http/response"
|
|
adResp "Gwen/http/response/admin"
|
|
"Gwen/service"
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
"strconv"
|
|
)
|
|
|
|
type User struct {
|
|
}
|
|
|
|
// Detail 管理员
|
|
// @Tags 用户
|
|
// @Summary 管理员详情
|
|
// @Description 管理员详情
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "ID"
|
|
// @Success 200 {object} response.Response{data=model.User}
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/detail/{id} [get]
|
|
// @Security token
|
|
func (ct *User) Detail(c *gin.Context) {
|
|
id := c.Param("id")
|
|
iid, _ := strconv.Atoi(id)
|
|
u := service.AllService.UserService.InfoById(uint(iid))
|
|
if u.Id > 0 {
|
|
response.Success(c, u)
|
|
return
|
|
}
|
|
response.Fail(c, 101, "信息不存在")
|
|
return
|
|
}
|
|
|
|
// Create 管理员
|
|
// @Tags 用户
|
|
// @Summary 创建管理员
|
|
// @Description 创建管理员
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body admin.UserForm true "管理员信息"
|
|
// @Success 200 {object} response.Response{data=model.User}
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/create [post]
|
|
// @Security token
|
|
func (ct *User) Create(c *gin.Context) {
|
|
f := &admin.UserForm{}
|
|
if err := c.ShouldBindJSON(f); err != nil {
|
|
response.Fail(c, 101, "参数错误")
|
|
return
|
|
}
|
|
errList := global.Validator.ValidStruct(f)
|
|
if len(errList) > 0 {
|
|
response.Fail(c, 101, errList[0])
|
|
return
|
|
}
|
|
u := f.ToUser()
|
|
err := service.AllService.UserService.Create(u)
|
|
if err != nil {
|
|
response.Fail(c, 101, "创建失败")
|
|
return
|
|
}
|
|
response.Success(c, u)
|
|
}
|
|
|
|
// List 列表
|
|
// @Tags 用户
|
|
// @Summary 管理员列表
|
|
// @Description 管理员列表
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param page query int false "页码"
|
|
// @Param page_size query int false "页大小"
|
|
// @Param username query int false "账户"
|
|
// @Success 200 {object} response.Response{data=model.UserList}
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/list [get]
|
|
// @Security token
|
|
func (ct *User) List(c *gin.Context) {
|
|
query := &admin.UserQuery{}
|
|
if err := c.ShouldBindQuery(query); err != nil {
|
|
response.Fail(c, 101, "参数错误")
|
|
return
|
|
}
|
|
res := service.AllService.UserService.List(query.Page, query.PageSize, func(tx *gorm.DB) {
|
|
if query.Username != "" {
|
|
tx.Where("username like ?", "%"+query.Username+"%")
|
|
}
|
|
})
|
|
response.Success(c, res)
|
|
}
|
|
|
|
// Update 编辑
|
|
// @Tags 用户
|
|
// @Summary 管理员编辑
|
|
// @Description 管理员编辑
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body admin.UserForm true "用户信息"
|
|
// @Success 200 {object} response.Response{data=model.User}
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/update [post]
|
|
// @Security token
|
|
func (ct *User) Update(c *gin.Context) {
|
|
f := &admin.UserForm{}
|
|
if err := c.ShouldBindJSON(f); err != nil {
|
|
response.Fail(c, 101, "参数错误:"+err.Error())
|
|
return
|
|
}
|
|
if f.Id == 0 {
|
|
response.Fail(c, 101, "参数错误")
|
|
return
|
|
}
|
|
errList := global.Validator.ValidStruct(f)
|
|
if len(errList) > 0 {
|
|
response.Fail(c, 101, errList[0])
|
|
return
|
|
}
|
|
u := f.ToUser()
|
|
err := service.AllService.UserService.Update(u)
|
|
if err != nil {
|
|
response.Fail(c, 101, "更新失败")
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Delete 删除
|
|
// @Tags 用户
|
|
// @Summary 管理员删除
|
|
// @Description 管理员编删除
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body admin.UserForm true "用户信息"
|
|
// @Success 200 {object} response.Response
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/delete [post]
|
|
// @Security token
|
|
func (ct *User) Delete(c *gin.Context) {
|
|
f := &admin.UserForm{}
|
|
if err := c.ShouldBindJSON(f); err != nil {
|
|
response.Fail(c, 101, "系统错误")
|
|
return
|
|
}
|
|
id := f.Id
|
|
errList := global.Validator.ValidVar(id, "required,gt=0")
|
|
if len(errList) > 0 {
|
|
response.Fail(c, 101, errList[0])
|
|
return
|
|
}
|
|
u := service.AllService.UserService.InfoById(f.Id)
|
|
if u.Id > 0 {
|
|
err := service.AllService.UserService.Delete(u)
|
|
if err == nil {
|
|
response.Success(c, nil)
|
|
return
|
|
}
|
|
response.Fail(c, 101, err.Error())
|
|
return
|
|
}
|
|
response.Fail(c, 101, "信息不存在")
|
|
}
|
|
|
|
// UpdatePassword 修改密码
|
|
// @Tags 用户
|
|
// @Summary 修改密码
|
|
// @Description 修改密码
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body admin.UserPasswordForm true "用户信息"
|
|
// @Success 200 {object} response.Response
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/updatePassword [post]
|
|
// @Security token
|
|
func (ct *User) UpdatePassword(c *gin.Context) {
|
|
f := &admin.UserPasswordForm{}
|
|
if err := c.ShouldBindJSON(f); err != nil {
|
|
response.Fail(c, 101, "参数错误")
|
|
return
|
|
}
|
|
errList := global.Validator.ValidStruct(f)
|
|
if len(errList) > 0 {
|
|
response.Fail(c, 101, errList[0])
|
|
return
|
|
}
|
|
u := service.AllService.UserService.InfoById(f.Id)
|
|
if u.Id == 0 {
|
|
response.Fail(c, 101, "信息不存在")
|
|
return
|
|
}
|
|
err := service.AllService.UserService.UpdatePassword(u, f.Password)
|
|
if err != nil {
|
|
response.Fail(c, 101, "更新失败")
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|
|
|
|
// Current 当前用户
|
|
// @Tags 用户
|
|
// @Summary 当前用户
|
|
// @Description 当前用户
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} response.Response{data=adResp.LoginPayload}
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/current [get]
|
|
// @Security token
|
|
func (ct *User) Current(c *gin.Context) {
|
|
u := service.AllService.UserService.CurUser(c)
|
|
token, _ := c.Get("token")
|
|
t := token.(string)
|
|
response.Success(c, &adResp.LoginPayload{
|
|
Token: t,
|
|
Username: u.Username,
|
|
RouteNames: service.AllService.UserService.RouteNames(u),
|
|
Nickname: u.Nickname,
|
|
})
|
|
}
|
|
|
|
// ChangeCurPwd 修改当前用户密码
|
|
// @Tags 用户
|
|
// @Summary 修改当前用户密码
|
|
// @Description 修改当前用户密码
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body admin.ChangeCurPasswordForm true "用户信息"
|
|
// @Success 200 {object} response.Response
|
|
// @Failure 500 {object} response.Response
|
|
// @Router /admin/user/changeCurPwd [post]
|
|
// @Security token
|
|
func (ct *User) ChangeCurPwd(c *gin.Context) {
|
|
f := &admin.ChangeCurPasswordForm{}
|
|
if err := c.ShouldBindJSON(f); err != nil {
|
|
response.Fail(c, 101, "参数错误")
|
|
return
|
|
}
|
|
|
|
errList := global.Validator.ValidStruct(f)
|
|
if len(errList) > 0 {
|
|
response.Fail(c, 101, errList[0])
|
|
return
|
|
}
|
|
u := service.AllService.UserService.CurUser(c)
|
|
oldPwd := service.AllService.UserService.EncryptPassword(f.OldPassword)
|
|
if u.Password != oldPwd {
|
|
response.Fail(c, 101, "旧密码错误")
|
|
return
|
|
}
|
|
err := service.AllService.UserService.UpdatePassword(u, f.NewPassword)
|
|
if err != nil {
|
|
response.Fail(c, 101, "更新失败")
|
|
return
|
|
}
|
|
response.Success(c, nil)
|
|
}
|