dcfd24b624
v1.0.3 定位为 bug fix release,收口 v1.0.2 引入的破坏性清理并修复 4 个轻量 bug + 依赖复查 + 版本号治理 + 文档对齐。同时包含此前未提交 的 v1.0.2 全部工作。 v1.0.3 变更: Fixed: - generateJTI 忽略 rand.Read 错误(jwt)— 改为 (string, error) 并传播 - QueryBuilder.Page Count 受残留 Limit 截断(repository)— countDB 加 Limit(-1).Offset(-1) - OSS/本地存储文件名冲突(storage)— 新增 uniqueFilename 加 8 字节随机后缀 - 数据库重试策略对不可恢复错误无效(database)— 新增 isTransientDBError Dependencies: - go mod tidy 补全 postgres 方言传递依赖;安全补丁升级 x/crypto v0.49→v0.53、golang-jwt/jwt/v5 v5.2.1→v5.3.1、gorilla/websocket v1.5.1→v1.5.3 Removed: - Breaking: 清理 v1.0.2 兼容别名(InitMySQL* / driverName) - 死代码 database.DBResolver - 代码中 292 行"评分/理由"自夸注释(#26,23 个文件) Changed: - Breaking: 错误码体系重构 CodeSuccess 1→0、CodeFail 0→1,删除 CodeInvalidParams,加编译期防撞码 - database/mysql.go → manager.go;Logger 拆分三独立 core 修复 Tee 重复写入 - 版本号常量化:app.go 新增 const Version 作为唯一来源,CLI/脚手架模板引用之,不再散落字面量 Security: - CORS 中间件按 W3C 规范修复 Allow-Credentials / Vary: Origin / 非白名单不回显 Added: - console 包显式 level 控制(SetLevel/WithLevel/LevelSilent,atomic.Int32 并发安全) - 新增测试 jwt/repository/storage/database/router/middleware/app - 文档:新增 CHANGELOG.md、Version_v1.0.2_report.md;更新 README/GUIDE 顶部更新日志 - 文档对齐:删除对不存在的 config.DefaultManager() getter 的描述(保持 API 与文档一致) 详见 CHANGELOG.md 与 README.md 更新日志。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
123 lines
2.6 KiB
Go
123 lines
2.6 KiB
Go
package utils
|
|
|
|
import (
|
|
"regexp"
|
|
)
|
|
|
|
// IsPhone 检查是否为有效的中国大陆手机号
|
|
// 注意: 正则基于当前号段,新号段开放时需更新
|
|
func IsPhone(phone string) bool {
|
|
// 1开头,第二位为3-9,共11位
|
|
pattern := `^1[3-9]\d{9}$`
|
|
matched, _ := regexp.MatchString(pattern, phone)
|
|
return matched
|
|
}
|
|
|
|
// IsEmail 检查是否为有效的邮箱地址
|
|
func IsEmail(email string) bool {
|
|
// 简单邮箱验证:xxx@xxx.xxx
|
|
pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`
|
|
matched, _ := regexp.MatchString(pattern, email)
|
|
return matched
|
|
}
|
|
|
|
// IsIPv4 检查是否为有效的 IPv4 地址
|
|
func IsIPv4(ip string) bool {
|
|
pattern := `^(\d{1,3}\.){3}\d{1,3}$`
|
|
matched, _ := regexp.MatchString(pattern, ip)
|
|
if !matched {
|
|
return false
|
|
}
|
|
// 验证每个段在 0-255 范围内
|
|
parts := splitByDot(ip)
|
|
for _, part := range parts {
|
|
n := ToInt(part)
|
|
if n < 0 || n > 255 {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsIDCard 检查是否为有效的中国身份证号(18位)
|
|
// 注意: 仅校验格式,不校验校验位
|
|
func IsIDCard(id string) bool {
|
|
// 18位身份证:6位地区码 + 8位生日 + 3位顺序码 + 1位校验码
|
|
pattern := `^\d{6}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dXx]$`
|
|
matched, _ := regexp.MatchString(pattern, id)
|
|
return matched
|
|
}
|
|
|
|
// IsChinese 检查字符串是否全部为中文字符
|
|
func IsChinese(s string) bool {
|
|
for _, r := range s {
|
|
if r < 0x4E00 || r > 0x9FFF {
|
|
return false
|
|
}
|
|
}
|
|
return len(s) > 0
|
|
}
|
|
|
|
// HasChinese 检查字符串是否包含中文字符
|
|
func HasChinese(s string) bool {
|
|
for _, r := range s {
|
|
if r >= 0x4E00 && r <= 0x9FFF {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// IsNumeric 检查字符串是否全部为数字
|
|
func IsNumeric(s string) bool {
|
|
if len(s) == 0 {
|
|
return false
|
|
}
|
|
for _, r := range s {
|
|
if r < '0' || r > '9' {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsAlpha 检查字符串是否全部为字母
|
|
func IsAlpha(s string) bool {
|
|
if len(s) == 0 {
|
|
return false
|
|
}
|
|
for _, r := range s {
|
|
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// IsAlphanumeric 检查字符串是否全部为字母或数字
|
|
func IsAlphanumeric(s string) bool {
|
|
if len(s) == 0 {
|
|
return false
|
|
}
|
|
for _, r := range s {
|
|
if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
// 内部函数
|
|
func splitByDot(s string) []string {
|
|
var result []string
|
|
start := 0
|
|
for i := 0; i < len(s); i++ {
|
|
if s[i] == '.' {
|
|
result = append(result, s[start:i])
|
|
start = i + 1
|
|
}
|
|
}
|
|
result = append(result, s[start:])
|
|
return result
|
|
}
|