:new:初始化脚本以及一些工具类
This commit is contained in:
34
utils/avatar.go
Normal file
34
utils/avatar.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go.uber.org/zap"
|
||||
"math/rand"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
)
|
||||
|
||||
type avatar struct{}
|
||||
|
||||
func Avatar() avatar {
|
||||
return avatar{}
|
||||
}
|
||||
|
||||
func (avatar) GenerateAvatar() (path string, err error) {
|
||||
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
r := client.HttpClient.R()
|
||||
result, err := r.Get(fmt.Sprintf("https://api.dicebear.com/7.x/croodles/png?seed=%d&scale=100&size=80&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 {
|
||||
zap.S().Errorf("生成头像失败")
|
||||
return "", err
|
||||
}
|
||||
|
||||
filePath, err := FileSystem().UploadFile(result.Body(), ".png")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
||||
41
utils/file_system.go
Normal file
41
utils/file_system.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/config"
|
||||
)
|
||||
|
||||
type fileSystem struct{}
|
||||
|
||||
func FileSystem() fileSystem {
|
||||
return fileSystem{}
|
||||
}
|
||||
|
||||
// UploadFile
|
||||
// @description: 上传文件
|
||||
// @receiver fileSystem
|
||||
// @param file
|
||||
// @return filePath
|
||||
// @return err
|
||||
func (fileSystem) UploadFile(file []byte, suffix string) (filePath string, err error) {
|
||||
switch config.Config.File.Type {
|
||||
case "oss":
|
||||
ossObj, err := client.OSS.Put(config.Config.File.Path, file, suffix)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return ossObj.LongPath, nil
|
||||
case "local":
|
||||
filePath = fmt.Sprintf("%v/%d-avatar%s", config.Config.File.Path, time.Now().Unix(), suffix)
|
||||
if err = os.WriteFile(filePath, file, os.FileMode(0777)); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return filePath, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
30
utils/password.go
Normal file
30
utils/password.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package utils
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
type password struct{}
|
||||
|
||||
func Password() password {
|
||||
return password{}
|
||||
}
|
||||
|
||||
// GenerateHashPassword
|
||||
// @description: 生成hash密码
|
||||
// @receiver password
|
||||
// @param pass
|
||||
// @return string
|
||||
func (password) GenerateHashPassword(pass string) string {
|
||||
bytePass := []byte(pass)
|
||||
hPass, _ := bcrypt.GenerateFromPassword(bytePass, bcrypt.DefaultCost)
|
||||
return string(hPass)
|
||||
}
|
||||
|
||||
// ComparePassword
|
||||
// @description: 密码比对
|
||||
// @receiver password
|
||||
// @param dbPass
|
||||
// @param pass
|
||||
// @return bool
|
||||
func (password) ComparePassword(dbPass, pass string) bool {
|
||||
return bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)) == nil
|
||||
}
|
||||
Reference in New Issue
Block a user