40 lines
1.1 KiB
Go
40 lines
1.1 KiB
Go
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)
|
||
}
|