:tada:初步完成用户

This commit is contained in:
coward
2024-10-18 17:19:19 +08:00
commit f375118c88
45 changed files with 3302 additions and 0 deletions

42
utils/avatar.go Normal file
View File

@@ -0,0 +1,42 @@
package utils
import (
"fmt"
"gitee.ltd/lxh/logger/log"
"math/rand"
"time"
"website-nav/global/client"
)
type avatar struct{}
func Avatar() avatar {
return avatar{}
}
// 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()
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 {
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
}

50
utils/file_system.go Normal file
View File

@@ -0,0 +1,50 @@
package utils
import (
"fmt"
"gitee.ltd/lxh/logger/log"
"github.com/google/uuid"
"os"
"strings"
"time"
"website-nav/config"
"website-nav/global/client"
)
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.GlobalConfig.File.Type {
case "oss":
ossObj, err := client.OSS.Put(config.GlobalConfig.File.Path, file, suffix)
if err != nil {
return "", err
}
filePath = ossObj.LongPath
case "local":
basePath := fmt.Sprintf("%s/%d/avatar", config.GlobalConfig.File.Path, time.Now().Unix())
filePath = fmt.Sprintf("%s/%s%s", basePath, strings.ReplaceAll(uuid.NewString(), "-", ""), suffix)
// 创建目录
if err = os.MkdirAll(basePath, os.FileMode(0777)); err != nil {
log.Errorf("本地存储目录创建失败: %v", err)
return "", err
}
if err = os.WriteFile(filePath, file, os.FileMode(0777)); err != nil {
return "", err
}
subPath := strings.ReplaceAll(filePath, fmt.Sprintf("%s/", config.GlobalConfig.File.Path), "")
filePath = fmt.Sprintf("http://%s/assets/%s", config.GlobalConfig.Http.Endpoint, subPath)
}
return filePath, err
}

44
utils/hash.go Normal file
View File

@@ -0,0 +1,44 @@
package utils
import (
"crypto/md5"
"crypto/sha256"
"crypto/sha512"
"encoding/hex"
)
type hash struct{}
func Hash() hash {
return hash{}
}
// MD5
// @description: MD5摘要
// @param str
// @return string
func (hash) MD5(str string) string {
hs := md5.New()
hs.Write([]byte(str))
return hex.EncodeToString(hs.Sum(nil))
}
// SHA256
// @description: SHA256
// @param str
// @return string
func (hash) SHA256(str string) string {
hasher := sha256.New()
hasher.Write([]byte(str))
return hex.EncodeToString(hasher.Sum(nil))
}
// SHA512
// @description: SHA512
// @param str
// @return string
func (hash) SHA512(str string) string {
hasher := sha512.New()
hasher.Write([]byte(str))
return hex.EncodeToString(hasher.Sum(nil))
}

25
utils/paginate.go Normal file
View File

@@ -0,0 +1,25 @@
package utils
type paginate struct{}
func Paginate() paginate {
return paginate{}
}
// Generate
// @description: 生成页码
// @receiver paginate
// @param count
// @param size
// @return int
func (paginate) Generate(count int64, size int) int {
totalPage := 0
if count > 0 {
upPage := 0
if int(count)%size > 0 {
upPage = 1
}
totalPage = (int(count) / size) + upPage
}
return totalPage
}

38
utils/password.go Normal file
View File

@@ -0,0 +1,38 @@
package utils
import (
"gitee.ltd/lxh/logger/log"
"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 {
if err := bcrypt.CompareHashAndPassword([]byte(dbPass), []byte(pass)); err != nil {
log.Errorf("密码错误: %v", err.Error())
return false
}
return true
}

23
utils/website.go Normal file
View File

@@ -0,0 +1,23 @@
package utils
import "net/url"
type website struct{}
func WebSite() website {
return website{}
}
// GetHost
// @description: 获取主机host
// @receiver website
// @param addr
// @return string
func (website) GetHost(addr string) string {
uu, err := url.Parse(addr)
if err != nil {
return ""
}
return uu.Host
}