package api import ( "encoding/base64" "fmt" "gitee.ltd/lxh/logger/log" "github.com/gin-gonic/gin" "strings" "wireguard-dashboard/client" "wireguard-dashboard/component" "wireguard-dashboard/constant" "wireguard-dashboard/http/param" "wireguard-dashboard/model/entity" "wireguard-dashboard/model/vo" "wireguard-dashboard/repository" "wireguard-dashboard/utils" ) type user struct{} func UserApi() user { return user{} } // Login // @description: 登陆 // @receiver u // @param c func (user) Login(c *gin.Context) { var p param.Login if err := c.ShouldBind(&p); err != nil { utils.GinResponse(c).FailedWithErr("参数错误", err) return } // 校验验证码 pass := component.CaptchaStore{}.Verify(p.CaptchaId, p.CaptchaAnswer, true) if !pass { utils.GinResponse(c).FailedWithMsg("验证码错误") return } // 校验用户是否存在 user, err := repository.User().GetUserByAccount(p.Account) if err != nil { utils.GinResponse(c).FailedWithMsg("账户不存在") return } if user.Status != constant.Normal { utils.GinResponse(c).FailedWithMsg("账户状态异常") return } // 校验密码 if !utils.Password().ComparePassword(user.Password, p.Password) { utils.GinResponse(c).FailedWithMsg("密码错误") return } // 生成token token, expireTime, err := component.JWT().GenerateToken(user.Id) if err != nil { utils.GinResponse(c).FailedWithMsg("登陆失败") return } utils.GinResponse(c).OKWithData(map[string]any{ "token": token, "type": "Bearer", "expireAt": expireTime.Unix(), }) } // Logout // @description: 退出登陆 // @receiver u // @param c func (user) Logout(c *gin.Context) { data, ok := c.Get("user") if !ok { utils.GinResponse(c).FailedWithMsg("你还没有登陆") return } if err := component.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 (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 (user) GetUser(c *gin.Context) { info, ok := c.Get("user") if !ok { utils.GinResponse(c).FailedWithMsg("获取信息失败") return } data := &vo.User{ 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 (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 p.Avatar != "" && p.ID != "" { // 判断头像是base64开头的就需要重新上传更新 if strings.HasPrefix(p.Avatar, "data:image/png;base64,") { avatar := strings.Replace(p.Avatar, "data:image/png;base64,", "", -1) avatarByte, err := base64.StdEncoding.DecodeString(avatar) if err != nil { log.Errorf("反解析头像失败: %v", err.Error()) utils.GinResponse(c).FailedWithMsg("上传头像失败") return } file, err := utils.FileSystem().UploadFile(avatarByte, ".png") if err != nil { log.Errorf("上传头像失败: %v", err.Error()) utils.GinResponse(c).FailedWithMsg("上传头像失败") return } p.Avatar = file } } 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 (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 (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() } // DeleteUser // @description: 删除用户 // @receiver user // @param c func (user) DeleteUser(c *gin.Context) { var id = c.Param("id") if id == "" || id == "undefined" { utils.GinResponse(c).FailedWithMsg("参数错误") return } loginUser, ok := c.Get("user") if !ok { utils.GinResponse(c).FailedWithMsg("获取信息失败") return } if err := repository.User().DeleteUser(loginUser.(*entity.User), id); err != nil { utils.GinResponse(c).FailedWithErr("操作失败", err) return } utils.GinResponse(c).OK() } // ChangeAvatar // @description: 切换头像 // @receiver user // @param c func (user) ChangeAvatar(c *gin.Context) { avatar, err := utils.Avatar().GenerateAvatar(false) if err != nil { utils.GinResponse(c).FailedWithErr("生成头像失败", err) return } utils.GinResponse(c).OKWithData(fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString([]byte(avatar)))) }