plugin/utils/utils.go

54 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package utils
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"regexp"
"strings"
)
// Load 获取插件目录中的文件信息
func Load(dir string) ([]string, error) {
entries, err := os.ReadDir(dir)
if err != nil {
return nil, fmt.Errorf("读取目录错误:%w", err)
}
files := make([]string, 0, len(entries))
for _, entry := range entries {
info, err := entry.Info()
if err != nil {
return nil, fmt.Errorf("获取文件信息错误:%w", err)
}
if !info.IsDir() {
files = append(files, fmt.Sprintf("%s/%s", dir, info.Name()))
}
}
return files, nil
}
func Md5(values []byte) string {
hash := md5.Sum(values)
return strings.ToUpper(hex.EncodeToString(hash[:]))
}
// IsPhoneNumber 检查给定的字符串是否为有效的手机号码
func IsPhoneNumber(phoneNumber string) bool {
phoneRegex := `^1[34578]\d{9}$`
return regexp.MustCompile(phoneRegex).MatchString(phoneNumber)
}
// IsEmail 检查给定的字符串是否为有效的电子邮箱地址
func IsEmail(email string) bool {
var emailRegex = regexp.MustCompile(`[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
return emailRegex.MatchString(email)
}
// IsValidQQ 检查给定的字符串是否为有效的 QQ 号
func IsValidQQ(qq string) bool {
// QQ号正则表达式5到11位数字且开头不为0的情况
re := regexp.MustCompile(`^(?!0)[0-9]{5,11}$`)
return re.MatchString(qq)
}