wireguard-dashboard/utils/avatar.go

43 lines
1.1 KiB
Go
Raw Normal View History

package utils
import (
"fmt"
2024-03-07 15:11:29 +08:00
"gitee.ltd/lxh/logger/log"
"math/rand"
"time"
"wireguard-dashboard/client"
)
type avatar struct{}
func Avatar() avatar {
return avatar{}
}
2024-03-11 15:50:13 +08:00
// GenerateAvatar
// @description: 生成随机头像 - 默认头像
// @receiver avatar
// @return path
// @return err
func (avatar) GenerateAvatar(isUpload bool) (path string, err error) {
rand.New(rand.NewSource(time.Now().UnixNano()))
r := client.HttpClient.R()
2024-05-17 17:30:26 +08:00
result, err := r.Get(fmt.Sprintf("https://api.dicebear.com/7.x/croodles/png?seed=%d&scale=120&size=200&clip=true&randomizeIds=true&beard=variant01,variant02,variant03&"+
"eyes=variant01,variant02,variant03,variant04,variant05,variant06,variant07,variant08,variant09,variant10,variant11,variant12&mustache=variant01,variant02,variant03&"+
"topColor=000000,0fa958,699bf7", rand.Uint32()))
if err != nil {
2024-03-07 15:11:29 +08:00
log.Errorf("请求头像API失败: %v", err.Error())
return "", err
}
if isUpload {
path, err = FileSystem().UploadFile(result.Body(), ".png")
if err != nil {
return "", err
}
return
}
return string(result.Body()), nil
}