2024-03-06 16:24:49 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-03-07 15:11:29 +08:00
|
|
|
"gitee.ltd/lxh/logger/log"
|
2024-03-06 16:24:49 +08:00
|
|
|
"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
|
2024-06-13 11:28:02 +08:00
|
|
|
func (avatar) GenerateAvatar(isUpload bool) (path string, err error) {
|
2024-03-06 16:24:49 +08:00
|
|
|
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&"+
|
2024-03-06 16:24:49 +08:00
|
|
|
"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())
|
2024-03-06 16:24:49 +08:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2024-06-13 11:28:02 +08:00
|
|
|
if isUpload {
|
|
|
|
path, err = FileSystem().UploadFile(result.Body(), ".png")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return
|
2024-03-06 16:24:49 +08:00
|
|
|
}
|
|
|
|
|
2024-06-13 11:28:02 +08:00
|
|
|
return string(result.Body()), nil
|
2024-03-06 16:24:49 +08:00
|
|
|
}
|