plugins/utils/helper/common.go

40 lines
1.1 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 helper
import (
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"os"
"regexp"
)
func FileExists(filePath string) bool {
_, err := os.Stat(filePath)
return err == nil || os.IsExist(err)
}
// ToChinese 处理文件名编码为简体中文
func ToChinese(s string) string {
encoder := simplifiedchinese.GBK.NewDecoder()
encodedName, _, _ := transform.String(encoder, s)
return encodedName
}
// 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)
}