🎨用户接口处理完毕
This commit is contained in:
155
http/api/user.go
155
http/api/user.go
@@ -1,7 +1,9 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"gitee.ltd/lxh/logger/log"
|
||||
"github.com/gin-gonic/gin"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/compoment"
|
||||
"wireguard-dashboard/constant"
|
||||
"wireguard-dashboard/http/param"
|
||||
@@ -66,26 +68,159 @@ func (u user) Login(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// Logout
|
||||
// @description: 退出登陆
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) Logout(c *gin.Context) {
|
||||
data, ok := c.Get("user")
|
||||
if !ok {
|
||||
utils.GinResponse(c).FailedWithMsg("你还没有登陆")
|
||||
return
|
||||
}
|
||||
if err := compoment.JWT().Logout(data.(*entity.User).Id); err != nil {
|
||||
log.Errorf("退出登陆失败: %v", err.Error())
|
||||
utils.GinResponse(c).FailedWithMsg("退出登陆失败")
|
||||
return
|
||||
}
|
||||
utils.GinResponse(c).OK()
|
||||
}
|
||||
|
||||
// List
|
||||
// @description: 用户列表
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) List(c *gin.Context) {
|
||||
var p param.UserList
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
data, total, err := repository.User().List(p)
|
||||
if err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("获取失败")
|
||||
return
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OkWithPage(data, total, p.Current, p.Size)
|
||||
}
|
||||
|
||||
// GetUser
|
||||
// @description: 获取登陆用户信息
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) GetUser(c *gin.Context) {
|
||||
user, ok := c.Get("user")
|
||||
info, ok := c.Get("user")
|
||||
if !ok {
|
||||
utils.GinResponse(c).FailedWithMsg("获取信息失败")
|
||||
return
|
||||
}
|
||||
data := &vo.User{
|
||||
Id: user.(*entity.User).Id,
|
||||
Name: user.(*entity.User).Name,
|
||||
Avatar: user.(*entity.User).Avatar,
|
||||
Account: user.(*entity.User).Account,
|
||||
Email: user.(*entity.User).Email,
|
||||
IsAdmin: user.(*entity.User).IsAdmin,
|
||||
Status: user.(*entity.User).Status,
|
||||
CreatedAt: user.(*entity.User).CreatedAt,
|
||||
UpdatedAt: user.(*entity.User).UpdatedAt,
|
||||
Id: info.(*entity.User).Id,
|
||||
Name: info.(*entity.User).Name,
|
||||
Avatar: info.(*entity.User).Avatar,
|
||||
Account: info.(*entity.User).Account,
|
||||
Email: info.(*entity.User).Email,
|
||||
IsAdmin: info.(*entity.User).IsAdmin,
|
||||
Status: info.(*entity.User).Status,
|
||||
CreatedAt: info.(*entity.User).CreatedAt,
|
||||
UpdatedAt: info.(*entity.User).UpdatedAt,
|
||||
}
|
||||
utils.GinResponse(c).OKWithData(data)
|
||||
}
|
||||
|
||||
// Save
|
||||
// @description: 新增/更改用户信息
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) Save(c *gin.Context) {
|
||||
var p param.SaveUser
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 只有新增才会判断
|
||||
if p.ID == "" {
|
||||
// 判断用户是否已经存在
|
||||
var count int64
|
||||
if err := client.DB.Model(&entity.User{}).Where("account = ?", p.Account).Count(&count).Error; err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
utils.GinResponse(c).FailedWithMsg("用户已存在!")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
if err := repository.User().Save(&entity.User{
|
||||
Base: entity.Base{
|
||||
Id: p.ID,
|
||||
},
|
||||
Avatar: p.Avatar,
|
||||
Name: p.Name,
|
||||
Account: p.Account,
|
||||
Email: p.Email,
|
||||
Password: p.Password,
|
||||
IsAdmin: p.IsAdmin,
|
||||
Status: p.Status,
|
||||
}); err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OK()
|
||||
}
|
||||
|
||||
// ChangePassword
|
||||
// @description: 更改密码
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) ChangePassword(c *gin.Context) {
|
||||
var p param.ChangePassword
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
user, ok := c.Get("user")
|
||||
if !ok {
|
||||
utils.GinResponse(c).AuthorizationFailed()
|
||||
return
|
||||
}
|
||||
|
||||
if !utils.Password().ComparePassword(user.(*entity.User).Password, p.OriginPassword) {
|
||||
utils.GinResponse(c).FailedWithMsg("原密码错误")
|
||||
return
|
||||
}
|
||||
|
||||
// 开始变更密码
|
||||
if err := repository.User().ChangePassword(p, user.(*entity.User).Id); err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("更改密码失败")
|
||||
return
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OK()
|
||||
}
|
||||
|
||||
// ChangeUserState
|
||||
// @description: 改变用户状态
|
||||
// @receiver u
|
||||
// @param c
|
||||
func (u user) ChangeUserState(c *gin.Context) {
|
||||
var p param.ChangeUserState
|
||||
if err := c.ShouldBind(&p); err != nil {
|
||||
utils.GinResponse(c).FailedWithErr("参数错误", err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := repository.User().ChangeUserState(p); err != nil {
|
||||
utils.GinResponse(c).FailedWithMsg("操作失败")
|
||||
return
|
||||
}
|
||||
|
||||
utils.GinResponse(c).OK()
|
||||
}
|
||||
|
Reference in New Issue
Block a user