Files
ai_site/platform/internal/userstore/phone.go
2026-07-31 10:31:17 +08:00

35 lines
823 B
Go

package userstore
import (
"fmt"
"regexp"
"strings"
)
var chinaMobileRe = regexp.MustCompile(`^1[3-9]\d{9}$`)
// NormalizePhone 规范化中国大陆手机号(去空格、去 +86/86 前缀)。
func NormalizePhone(raw string) (string, error) {
s := strings.TrimSpace(raw)
s = strings.ReplaceAll(s, " ", "")
s = strings.ReplaceAll(s, "-", "")
if strings.HasPrefix(s, "+86") {
s = s[3:]
} else if strings.HasPrefix(s, "86") && len(s) == 13 {
s = s[2:]
}
if s == "" {
return "", fmt.Errorf("请填写手机号")
}
if !chinaMobileRe.MatchString(s) {
return "", fmt.Errorf("手机号格式不正确(需 11 位大陆号)")
}
return s, nil
}
// LooksLikePhone 判断登录账号是否按手机号解析。
func LooksLikePhone(raw string) bool {
_, err := NormalizePhone(raw)
return err == nil
}