You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
2.7 KiB
137 lines
2.7 KiB
package common
|
|
|
|
import (
|
|
"bytes"
|
|
"crypto/md5"
|
|
"encoding/hex"
|
|
"github.com/go-playground/validator/v10"
|
|
"golang.org/x/text/encoding/simplifiedchinese"
|
|
"golang.org/x/text/transform"
|
|
"io"
|
|
"math/rand"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// @Title 随机数种子
|
|
func init() {
|
|
// 随机数种子
|
|
rand.Seed(time.Now().UnixNano())
|
|
}
|
|
|
|
// @Title md5加密
|
|
func MD5(str string) string {
|
|
h := md5.New()
|
|
h.Write([]byte(str))
|
|
return hex.EncodeToString(h.Sum(nil))
|
|
}
|
|
|
|
// @Title 生成随机数
|
|
func RandStr(n int, str ...string) string {
|
|
s := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
|
|
if len(str) > 0 {
|
|
s = str[0]
|
|
}
|
|
res := ""
|
|
for i := 0; i < n; i++ {
|
|
res += string(s[rand.Intn(len(s))])
|
|
}
|
|
return res
|
|
}
|
|
|
|
// @Title 处理验证错误消息
|
|
func GetVerErr(err error) string {
|
|
er, ok := err.(validator.ValidationErrors)
|
|
if ok {
|
|
field := er[0].Field()
|
|
if field == er[0].StructField() {
|
|
field = strings.ToLower(field[0:1]) + field[1:]
|
|
}
|
|
switch er[0].Tag() {
|
|
case "required":
|
|
return field + "不能为空"
|
|
case "min":
|
|
if er[0].Type().String() == "string" {
|
|
return field + "不能小于" + er[0].Param() + "位"
|
|
}
|
|
return field + "不能小于" + er[0].Param()
|
|
}
|
|
return field + "错误"
|
|
} else {
|
|
return "参数格式错误"
|
|
}
|
|
}
|
|
|
|
// @Title 是否在数组中
|
|
func InArray(need interface{}, data interface{}) bool {
|
|
if datas, ok := data.([]int); ok {
|
|
for _, item := range datas {
|
|
if item == need {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
if datas, ok := data.([]uint); ok {
|
|
for _, item := range datas {
|
|
if item == need {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
if datas, ok := data.([]string); ok {
|
|
for _, item := range datas {
|
|
if item == need {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// CreateDateDir 根据当前日期来创建文件夹
|
|
func CreateDateDir(Path string) string {
|
|
folderName := time.Now().Format("20060102")
|
|
folderPath := filepath.Join(Path, folderName)
|
|
if _, err := os.Stat(folderPath); os.IsNotExist(err) {
|
|
os.MkdirAll(folderPath, 0755)
|
|
}
|
|
return folderName
|
|
}
|
|
|
|
// @Title 下载文件
|
|
func DownloadFormUrl(src string, filename string) error {
|
|
res, err := http.Get(src)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer res.Body.Close()
|
|
// 获得get请求响应的reader对象
|
|
body, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
out, err := os.Create(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := io.Copy(out, bytes.NewReader(body)); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Title utf8转gbk
|
|
func Utf8ToGbk(source string) string {
|
|
result, _, _ := transform.String(simplifiedchinese.GBK.NewEncoder(), source)
|
|
return result
|
|
}
|
|
|
|
// @Title gbk转utf8
|
|
func GbkToUtf8(source string) string {
|
|
result, _, _ := transform.String(simplifiedchinese.GBK.NewDecoder(), source)
|
|
return result
|
|
}
|