diff --git a/http/api/user.go b/http/api/user.go index 90ff182..6cc3ff7 100644 --- a/http/api/user.go +++ b/http/api/user.go @@ -1,7 +1,9 @@ package api import ( + "encoding/base64" "errors" + "fmt" "github.com/gin-gonic/gin" "wireguard-ui/global/constant" "wireguard-ui/http/param" @@ -250,3 +252,17 @@ func (UserApi) ResetPassword(c *gin.Context) { response.R(c).OK() } + +// GenerateAvatar +// @description: 生成头像 +// @receiver UserApi +// @param c +func (UserApi) GenerateAvatar(c *gin.Context) { + avatar, err := utils.Avatar().GenerateAvatar(false) + if err != nil { + response.R(c).FailedWithError(fmt.Errorf("生成头像失败: %s", err.Error())) + return + } + + response.R(c).OkWithData(fmt.Sprintf("data:image/png;base64,%s", base64.StdEncoding.EncodeToString([]byte(avatar)))) +} diff --git a/http/router/user.go b/http/router/user.go index ff41d74..bce2b3b 100644 --- a/http/router/user.go +++ b/http/router/user.go @@ -19,5 +19,6 @@ func UserApi(r *gin.RouterGroup) { userApi.PUT("/status/:id", api.User().Status) // 修改用户状态 userApi.PUT("/change-password", api.User().ChangePassword) // 修改用户密码 userApi.PUT("/reset-password/:id", api.User().ResetPassword) // 重置用户密码 + userApi.POST("/generate-avatar", api.User().GenerateAvatar) // 生成头像 } }